Skip to content

Commit cba3640

Browse files
finish coverage for int, bool and float
1 parent 6e3eaa0 commit cba3640

File tree

2 files changed

+162
-56
lines changed

2 files changed

+162
-56
lines changed

pkg/tests/diff_test/detailed_diff_primitive_test.go

Lines changed: 146 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,35 @@ import (
1313
crosstests "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/tests/cross-tests"
1414
)
1515

16-
func TestSDKv2DetailedDiffString(t *testing.T) {
17-
t.Parallel()
16+
func generatePrimitiveSchemaValueMakerPairs[T any](
17+
typ schema.ValueType, ctyMaker func(v T) cty.Value, val1, val2, computedVal, defaultVal, nilVal T,
18+
) ([]diffSchemaValueMakerPair[T], []diffScenario[T]) {
19+
valueOne := ref(val1)
20+
valueTwo := ref(val2)
21+
noValue := ref(nilVal)
22+
23+
ctyVal := func(v *T) map[string]cty.Value {
24+
if v == nil {
25+
return map[string]cty.Value{}
26+
}
27+
return map[string]cty.Value{
28+
"prop": ctyMaker(*v),
29+
}
30+
}
1831

1932
optionalSchema := schema.Resource{
2033
Schema: map[string]*schema.Schema{
21-
"string_prop": {
22-
Type: schema.TypeString,
34+
"prop": {
35+
Type: typ,
2336
Optional: true,
2437
},
2538
},
2639
}
2740

2841
optionalForceNewSchema := schema.Resource{
2942
Schema: map[string]*schema.Schema{
30-
"string_prop": {
31-
Type: schema.TypeString,
43+
"prop": {
44+
Type: typ,
3245
Optional: true,
3346
ForceNew: true,
3447
},
@@ -37,26 +50,26 @@ func TestSDKv2DetailedDiffString(t *testing.T) {
3750

3851
requiredSchema := schema.Resource{
3952
Schema: map[string]*schema.Schema{
40-
"string_prop": {
41-
Type: schema.TypeString,
53+
"prop": {
54+
Type: typ,
4255
Required: true,
4356
},
4457
},
4558
}
4659

4760
requiredForceNewSchema := schema.Resource{
4861
Schema: map[string]*schema.Schema{
49-
"string_prop": {
50-
Type: schema.TypeString,
62+
"prop": {
63+
Type: typ,
5164
ForceNew: true,
5265
Required: true,
5366
},
5467
},
5568
}
5669

5770
setComputedFunc := func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
58-
if _, ok := d.GetOk("string_prop"); !ok {
59-
err := d.Set("string_prop", "computed")
71+
if _, ok := d.GetOk("prop"); !ok {
72+
err := d.Set("prop", computedVal)
6073
if err != nil {
6174
return diag.FromErr(err)
6275
}
@@ -66,8 +79,8 @@ func TestSDKv2DetailedDiffString(t *testing.T) {
6679

6780
optionalComputedSchema := schema.Resource{
6881
Schema: map[string]*schema.Schema{
69-
"string_prop": {
70-
Type: schema.TypeString,
82+
"prop": {
83+
Type: typ,
7184
Optional: true,
7285
Computed: true,
7386
},
@@ -81,8 +94,8 @@ func TestSDKv2DetailedDiffString(t *testing.T) {
8194

8295
optionalComputedForceNewSchema := schema.Resource{
8396
Schema: map[string]*schema.Schema{
84-
"string_prop": {
85-
Type: schema.TypeString,
97+
"prop": {
98+
Type: typ,
8699
Optional: true,
87100
Computed: true,
88101
ForceNew: true,
@@ -97,64 +110,142 @@ func TestSDKv2DetailedDiffString(t *testing.T) {
97110

98111
optionalDefaultSchema := schema.Resource{
99112
Schema: map[string]*schema.Schema{
100-
"string_prop": {
101-
Type: schema.TypeString,
113+
"prop": {
114+
Type: typ,
102115
Optional: true,
103-
Default: "default",
116+
Default: defaultVal,
104117
},
105118
},
106119
}
107120

108121
optionalDefaultForceNewSchema := schema.Resource{
109122
Schema: map[string]*schema.Schema{
110-
"string_prop": {
111-
Type: schema.TypeString,
123+
"prop": {
124+
Type: typ,
112125
Optional: true,
113-
Default: "default",
126+
Default: defaultVal,
114127
ForceNew: true,
115128
},
116129
},
117130
}
118131

119-
valueOne := ref("val1")
120-
valueTwo := ref("val2")
121-
var noValue *string
122-
123-
ctyVal := func(v *string) map[string]cty.Value {
124-
if v == nil {
125-
return map[string]cty.Value{}
126-
}
127-
return map[string]cty.Value{
128-
"string_prop": cty.StringVal(*v),
132+
return []diffSchemaValueMakerPair[T]{
133+
{"optional", optionalSchema, ctyVal},
134+
{"optionalForceNew", optionalForceNewSchema, ctyVal},
135+
{"required", requiredSchema, ctyVal},
136+
{"requiredForceNew", requiredForceNewSchema, ctyVal},
137+
{"optionalComputed", optionalComputedSchema, ctyVal},
138+
{"optionalComputedForceNew", optionalComputedForceNewSchema, ctyVal},
139+
{"optionalDefault", optionalDefaultSchema, ctyVal},
140+
{"optionalDefaultForceNew", optionalDefaultForceNewSchema, ctyVal},
141+
}, []diffScenario[T]{
142+
{"unchanged empty", noValue, noValue},
143+
{"unchanged non-empty", valueOne, valueOne},
144+
{"added", noValue, valueOne},
145+
{"removed", valueOne, noValue},
146+
{"changed", valueOne, valueTwo},
129147
}
148+
}
149+
150+
func TestSDKv2DetailedDiffString(t *testing.T) {
151+
t.Parallel()
152+
153+
var nilVal string
154+
schemaValueMakerPairs, scenarios := generatePrimitiveSchemaValueMakerPairs(
155+
schema.TypeString, cty.StringVal, "val1", "val2", "computed", "default", nilVal)
156+
157+
for _, schemaValueMakerPair := range schemaValueMakerPairs {
158+
t.Run(schemaValueMakerPair.name, func(t *testing.T) {
159+
t.Parallel()
160+
for _, scenario := range scenarios {
161+
t.Run(scenario.name, func(t *testing.T) {
162+
if strings.Contains(schemaValueMakerPair.name, "required") &&
163+
(scenario.initialValue == nil || scenario.changeValue == nil) {
164+
t.Skip("Required fields cannot be unset")
165+
}
166+
t.Parallel()
167+
diff := crosstests.Diff(t, &schemaValueMakerPair.schema, schemaValueMakerPair.valueMaker(scenario.initialValue), schemaValueMakerPair.valueMaker(scenario.changeValue))
168+
autogold.ExpectFile(t, testOutput{
169+
initialValue: scenario.initialValue,
170+
changeValue: scenario.changeValue,
171+
tfOut: diff.TFOut,
172+
pulumiOut: diff.PulumiOut,
173+
detailedDiff: diff.PulumiDiff.DetailedDiff,
174+
})
175+
})
176+
}
177+
})
130178
}
179+
}
131180

132-
schemaValueMakerPairs := []struct {
133-
name string
134-
schema schema.Resource
135-
valueMaker func(v *string) map[string]cty.Value
136-
}{
137-
{"optional", optionalSchema, ctyVal},
138-
{"optionalForceNew", optionalForceNewSchema, ctyVal},
139-
{"required", requiredSchema, ctyVal},
140-
{"requiredForceNew", requiredForceNewSchema, ctyVal},
141-
{"optionalComputed", optionalComputedSchema, ctyVal},
142-
{"optionalComputedForceNew", optionalComputedForceNewSchema, ctyVal},
143-
{"optionalDefault", optionalDefaultSchema, ctyVal},
144-
{"optionalDefaultForceNew", optionalDefaultForceNewSchema, ctyVal},
181+
func TestSDKv2DetailedDiffBool(t *testing.T) {
182+
t.Parallel()
183+
184+
var nilVal bool
185+
schemaValueMakerPairs, scenarios := generatePrimitiveSchemaValueMakerPairs(
186+
schema.TypeBool, cty.BoolVal, true, false, true, false, nilVal)
187+
188+
for _, schemaValueMakerPair := range schemaValueMakerPairs {
189+
t.Run(schemaValueMakerPair.name, func(t *testing.T) {
190+
t.Parallel()
191+
for _, scenario := range scenarios {
192+
t.Run(scenario.name, func(t *testing.T) {
193+
if strings.Contains(schemaValueMakerPair.name, "required") &&
194+
(scenario.initialValue == nil || scenario.changeValue == nil) {
195+
t.Skip("Required fields cannot be unset")
196+
}
197+
t.Parallel()
198+
diff := crosstests.Diff(t, &schemaValueMakerPair.schema, schemaValueMakerPair.valueMaker(scenario.initialValue), schemaValueMakerPair.valueMaker(scenario.changeValue))
199+
autogold.ExpectFile(t, testOutput{
200+
initialValue: scenario.initialValue,
201+
changeValue: scenario.changeValue,
202+
tfOut: diff.TFOut,
203+
pulumiOut: diff.PulumiOut,
204+
detailedDiff: diff.PulumiDiff.DetailedDiff,
205+
})
206+
})
207+
}
208+
})
145209
}
210+
}
211+
212+
func TestSDKv2DetailedDiffInt(t *testing.T) {
213+
t.Parallel()
146214

147-
scenarios := []struct {
148-
name string
149-
initialValue *string
150-
changeValue *string
151-
}{
152-
{"unchanged empty", noValue, noValue},
153-
{"unchanged non-empty", valueOne, valueOne},
154-
{"added", noValue, valueOne},
155-
{"removed", valueOne, noValue},
156-
{"changed", valueOne, valueTwo},
215+
var nilVal int64
216+
schemaValueMakerPairs, scenarios := generatePrimitiveSchemaValueMakerPairs(
217+
schema.TypeInt, cty.NumberIntVal, 1, 2, 3, 4, nilVal)
218+
219+
for _, schemaValueMakerPair := range schemaValueMakerPairs {
220+
t.Run(schemaValueMakerPair.name, func(t *testing.T) {
221+
t.Parallel()
222+
for _, scenario := range scenarios {
223+
t.Run(scenario.name, func(t *testing.T) {
224+
if strings.Contains(schemaValueMakerPair.name, "required") &&
225+
(scenario.initialValue == nil || scenario.changeValue == nil) {
226+
t.Skip("Required fields cannot be unset")
227+
}
228+
t.Parallel()
229+
diff := crosstests.Diff(t, &schemaValueMakerPair.schema, schemaValueMakerPair.valueMaker(scenario.initialValue), schemaValueMakerPair.valueMaker(scenario.changeValue))
230+
autogold.ExpectFile(t, testOutput{
231+
initialValue: scenario.initialValue,
232+
changeValue: scenario.changeValue,
233+
tfOut: diff.TFOut,
234+
pulumiOut: diff.PulumiOut,
235+
detailedDiff: diff.PulumiDiff.DetailedDiff,
236+
})
237+
})
238+
}
239+
})
157240
}
241+
}
242+
243+
func TestSDKv2DetailedDiffFloat(t *testing.T) {
244+
t.Parallel()
245+
246+
var nilVal float64
247+
schemaValueMakerPairs, scenarios := generatePrimitiveSchemaValueMakerPairs(
248+
schema.TypeFloat, cty.NumberFloatVal, 1.0, 2.0, 3.0, 4.0, nilVal)
158249

159250
for _, schemaValueMakerPair := range schemaValueMakerPairs {
160251
t.Run(schemaValueMakerPair.name, func(t *testing.T) {

pkg/tests/diff_test/value_makers.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
package tests
22

3-
import "github.com/zclconf/go-cty/cty"
3+
import (
4+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
5+
"github.com/zclconf/go-cty/cty"
6+
)
47

58
func ref[T any](v T) *T {
69
return &v
710
}
811

12+
type diffSchemaValueMakerPair[T any] struct {
13+
name string
14+
schema schema.Resource
15+
valueMaker func(v *T) map[string]cty.Value
16+
}
17+
18+
type diffScenario[T any] struct {
19+
name string
20+
initialValue *T
21+
changeValue *T
22+
}
23+
924
func listValueMaker(arr *[]string) cty.Value {
1025
if arr == nil {
1126
return cty.NullVal(cty.DynamicPseudoType)

0 commit comments

Comments
 (0)