-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathvalue_from_test.go
220 lines (203 loc) · 5.11 KB
/
value_from_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfsdk
import (
"context"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
)
type person struct {
Name types.String `tfsdk:"name"`
Age types.Int64 `tfsdk:"age"`
OptedIn types.Bool `tfsdk:"opted_in"`
Address types.List `tfsdk:"address"`
FullName types.Map `tfsdk:"full_name"`
}
func TestValueFrom(t *testing.T) {
t.Parallel()
personAttrTypes := map[string]attr.Type{
"name": types.StringType,
"age": types.Int64Type,
"opted_in": types.BoolType,
"address": types.ListType{
ElemType: types.StringType,
},
"full_name": types.MapType{
ElemType: types.StringType,
},
}
mrX := person{
Name: types.StringValue("x"),
Age: types.Int64Value(30),
OptedIn: types.BoolValue(true),
Address: types.ListValueMust(
types.StringType,
[]attr.Value{
types.StringValue("1"),
types.StringValue("Beckford Close"),
types.StringValue("Gotham"),
},
),
FullName: types.MapValueMust(
types.StringType,
map[string]attr.Value{
"first": types.StringValue("x"),
"middle": types.StringValue("b"),
"last": types.StringValue("c"),
},
),
}
mrsY := person{
Name: types.StringValue("y"),
Age: types.Int64Value(23),
OptedIn: types.BoolValue(false),
Address: types.ListValueMust(
types.StringType,
[]attr.Value{
types.StringValue("2"),
types.StringValue("Windmill Close"),
types.StringValue("Smallville"),
},
),
FullName: types.MapValueMust(
types.StringType,
map[string]attr.Value{
"first": types.StringValue("y"),
"middle": types.StringValue("e"),
"last": types.StringValue("f"),
},
),
}
expectedMrXObj := types.ObjectValueMust(
personAttrTypes,
map[string]attr.Value{
"name": types.StringValue("x"),
"age": types.Int64Value(30),
"opted_in": types.BoolValue(true),
"address": types.ListValueMust(
types.StringType,
[]attr.Value{
types.StringValue("1"),
types.StringValue("Beckford Close"),
types.StringValue("Gotham"),
},
),
"full_name": types.MapValueMust(
types.StringType,
map[string]attr.Value{
"first": types.StringValue("x"),
"middle": types.StringValue("b"),
"last": types.StringValue("c"),
},
),
},
)
expectedMrsYObj := types.ObjectValueMust(
personAttrTypes,
map[string]attr.Value{
"name": types.StringValue("y"),
"age": types.Int64Value(23),
"opted_in": types.BoolValue(false),
"address": types.ListValueMust(
types.StringType,
[]attr.Value{
types.StringValue("2"),
types.StringValue("Windmill Close"),
types.StringValue("Smallville"),
},
),
"full_name": types.MapValueMust(
types.StringType,
map[string]attr.Value{
"first": types.StringValue("y"),
"middle": types.StringValue("e"),
"last": types.StringValue("f"),
},
),
},
)
type testCase struct {
val interface{}
target attr.Value
expected attr.Value
expectedDiags diag.Diagnostics
}
tests := map[string]testCase{
"primitive": {
val: "hello",
target: types.String{},
expected: types.StringValue("hello"),
},
"struct": {
val: mrX,
target: types.ObjectNull(personAttrTypes),
expected: expectedMrXObj,
},
"list": {
val: []person{mrX, mrsY},
target: types.ListNull(
types.ObjectType{
AttrTypes: personAttrTypes,
},
),
expected: types.ListValueMust(
types.ObjectType{
AttrTypes: personAttrTypes,
},
[]attr.Value{expectedMrXObj, expectedMrsYObj},
),
},
"map": {
val: map[string]person{
"x": mrX,
"y": mrsY,
},
target: types.MapNull(
types.ObjectType{
AttrTypes: personAttrTypes,
},
),
expected: types.MapValueMust(
types.ObjectType{
AttrTypes: personAttrTypes,
},
map[string]attr.Value{
"x": expectedMrXObj,
"y": expectedMrsYObj,
},
),
},
"incompatible-type": {
val: 0,
target: types.String{},
expectedDiags: diag.Diagnostics{
diag.WithPath(
path.Empty(),
diag.NewErrorDiagnostic(
"Value Conversion Error",
"An unexpected error was encountered trying to convert the Terraform value. This is always an error in the provider. Please report the following to the provider developer:\n\ncan't unmarshal tftypes.Number into *string, expected string",
),
),
},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
diags := ValueFrom(context.Background(), tc.val, tc.target.Type(context.Background()), &tc.target)
if diff := cmp.Diff(tc.expectedDiags, diags); diff != "" {
t.Fatalf("Unexpected diff in diagnostics (-wanted, +got): %s", diff)
}
if diags.HasError() {
return
}
if diff := cmp.Diff(tc.expected, tc.target); diff != "" {
t.Fatalf("Unexpected diff in results (-wanted, +got): %s", diff)
}
})
}
}