/
state_test.go
83 lines (77 loc) · 2.09 KB
/
state_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package tfprotov6_test
import (
"math/big"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
func TestRawStateUnmarshalWithOpts(t *testing.T) {
t.Parallel()
type testCase struct {
rawState tfprotov6.RawState
value tftypes.Value
typ tftypes.Type
opts tfprotov6.UnmarshalOpts
}
tests := map[string]testCase{
"object-of-bool-number": {
rawState: tfprotov6.RawState{
JSON: []byte(`{"bool":true,"number":0}`),
},
value: tftypes.NewValue(tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"bool": tftypes.Bool,
"number": tftypes.Number,
},
}, map[string]tftypes.Value{
"bool": tftypes.NewValue(tftypes.Bool, true),
"number": tftypes.NewValue(tftypes.Number, big.NewFloat(0)),
}),
typ: tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"bool": tftypes.Bool,
"number": tftypes.Number,
},
},
},
"object-with-missing-attribute": {
rawState: tfprotov6.RawState{
JSON: []byte(`{"bool":true,"number":0,"unknown":"whatever"}`),
},
value: tftypes.NewValue(tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"bool": tftypes.Bool,
"number": tftypes.Number,
},
}, map[string]tftypes.Value{
"bool": tftypes.NewValue(tftypes.Bool, true),
"number": tftypes.NewValue(tftypes.Number, big.NewFloat(0)),
}),
typ: tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"bool": tftypes.Bool,
"number": tftypes.Number,
},
},
opts: tfprotov6.UnmarshalOpts{
ValueFromJSONOpts: tftypes.ValueFromJSONOpts{
IgnoreUndefinedAttributes: true,
},
},
},
}
for name, test := range tests {
name, test := name, test
t.Run(name, func(t *testing.T) {
t.Parallel()
val, err := test.rawState.UnmarshalWithOpts(test.typ, test.opts)
if err != nil {
t.Fatalf("unexpected error unmarshaling: %s", err)
}
if diff := cmp.Diff(test.value, val); diff != "" {
t.Errorf("Unexpected results (-wanted +got): %s", diff)
}
})
}
}