Skip to content

Commit 8789663

Browse files
move and rename test generation function
1 parent 2ffc182 commit 8789663

File tree

2 files changed

+141
-140
lines changed

2 files changed

+141
-140
lines changed

pkg/tests/diff_test/detailed_diff_primitive_test.go

Lines changed: 4 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,157 +1,21 @@
11
package tests
22

33
import (
4-
"context"
54
"strings"
65
"testing"
76

8-
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
97
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
108
"github.com/hexops/autogold/v2"
119
"github.com/zclconf/go-cty/cty"
1210

1311
crosstests "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/tests/cross-tests"
1412
)
1513

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-
}
31-
32-
optionalSchema := schema.Resource{
33-
Schema: map[string]*schema.Schema{
34-
"prop": {
35-
Type: typ,
36-
Optional: true,
37-
},
38-
},
39-
}
40-
41-
optionalForceNewSchema := schema.Resource{
42-
Schema: map[string]*schema.Schema{
43-
"prop": {
44-
Type: typ,
45-
Optional: true,
46-
ForceNew: true,
47-
},
48-
},
49-
}
50-
51-
requiredSchema := schema.Resource{
52-
Schema: map[string]*schema.Schema{
53-
"prop": {
54-
Type: typ,
55-
Required: true,
56-
},
57-
},
58-
}
59-
60-
requiredForceNewSchema := schema.Resource{
61-
Schema: map[string]*schema.Schema{
62-
"prop": {
63-
Type: typ,
64-
ForceNew: true,
65-
Required: true,
66-
},
67-
},
68-
}
69-
70-
setComputedFunc := func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
71-
if _, ok := d.GetOk("prop"); !ok {
72-
err := d.Set("prop", computedVal)
73-
if err != nil {
74-
return diag.FromErr(err)
75-
}
76-
}
77-
return nil
78-
}
79-
80-
optionalComputedSchema := schema.Resource{
81-
Schema: map[string]*schema.Schema{
82-
"prop": {
83-
Type: typ,
84-
Optional: true,
85-
Computed: true,
86-
},
87-
},
88-
CreateContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
89-
rd.SetId("id")
90-
return setComputedFunc(ctx, rd, i)
91-
},
92-
UpdateContext: setComputedFunc,
93-
}
94-
95-
optionalComputedForceNewSchema := schema.Resource{
96-
Schema: map[string]*schema.Schema{
97-
"prop": {
98-
Type: typ,
99-
Optional: true,
100-
Computed: true,
101-
ForceNew: true,
102-
},
103-
},
104-
CreateContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
105-
rd.SetId("id")
106-
return setComputedFunc(ctx, rd, i)
107-
},
108-
UpdateContext: setComputedFunc,
109-
}
110-
111-
optionalDefaultSchema := schema.Resource{
112-
Schema: map[string]*schema.Schema{
113-
"prop": {
114-
Type: typ,
115-
Optional: true,
116-
Default: defaultVal,
117-
},
118-
},
119-
}
120-
121-
optionalDefaultForceNewSchema := schema.Resource{
122-
Schema: map[string]*schema.Schema{
123-
"prop": {
124-
Type: typ,
125-
Optional: true,
126-
Default: defaultVal,
127-
ForceNew: true,
128-
},
129-
},
130-
}
131-
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},
147-
}
148-
}
149-
15014
func TestSDKv2DetailedDiffString(t *testing.T) {
15115
t.Parallel()
15216

15317
var nilVal string
154-
schemaValueMakerPairs, scenarios := generatePrimitiveSchemaValueMakerPairs(
18+
schemaValueMakerPairs, scenarios := generateBaseTests(
15519
schema.TypeString, cty.StringVal, "val1", "val2", "computed", "default", nilVal)
15620

15721
for _, schemaValueMakerPair := range schemaValueMakerPairs {
@@ -182,7 +46,7 @@ func TestSDKv2DetailedDiffBool(t *testing.T) {
18246
t.Parallel()
18347

18448
var nilVal bool
185-
schemaValueMakerPairs, scenarios := generatePrimitiveSchemaValueMakerPairs(
49+
schemaValueMakerPairs, scenarios := generateBaseTests(
18650
schema.TypeBool, cty.BoolVal, true, false, true, false, nilVal)
18751

18852
for _, schemaValueMakerPair := range schemaValueMakerPairs {
@@ -213,7 +77,7 @@ func TestSDKv2DetailedDiffInt(t *testing.T) {
21377
t.Parallel()
21478

21579
var nilVal int64
216-
schemaValueMakerPairs, scenarios := generatePrimitiveSchemaValueMakerPairs(
80+
schemaValueMakerPairs, scenarios := generateBaseTests(
21781
schema.TypeInt, cty.NumberIntVal, 1, 2, 3, 4, nilVal)
21882

21983
for _, schemaValueMakerPair := range schemaValueMakerPairs {
@@ -244,7 +108,7 @@ func TestSDKv2DetailedDiffFloat(t *testing.T) {
244108
t.Parallel()
245109

246110
var nilVal float64
247-
schemaValueMakerPairs, scenarios := generatePrimitiveSchemaValueMakerPairs(
111+
schemaValueMakerPairs, scenarios := generateBaseTests(
248112
schema.TypeFloat, cty.NumberFloatVal, 1.0, 2.0, 3.0, 4.0, nilVal)
249113

250114
for _, schemaValueMakerPair := range schemaValueMakerPairs {

pkg/tests/diff_test/value_makers.go

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package tests
22

33
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
47
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
58
"github.com/zclconf/go-cty/cty"
69
)
@@ -21,6 +24,140 @@ type diffScenario[T any] struct {
2124
changeValue *T
2225
}
2326

27+
func generateBaseTests[T any](
28+
typ schema.ValueType, ctyMaker func(v T) cty.Value, val1, val2, computedVal, defaultVal, nilVal T,
29+
) ([]diffSchemaValueMakerPair[T], []diffScenario[T]) {
30+
valueOne := ref(val1)
31+
valueTwo := ref(val2)
32+
noValue := ref(nilVal)
33+
34+
ctyVal := func(v *T) map[string]cty.Value {
35+
if v == nil {
36+
return map[string]cty.Value{}
37+
}
38+
return map[string]cty.Value{
39+
"prop": ctyMaker(*v),
40+
}
41+
}
42+
43+
optionalSchema := schema.Resource{
44+
Schema: map[string]*schema.Schema{
45+
"prop": {
46+
Type: typ,
47+
Optional: true,
48+
},
49+
},
50+
}
51+
52+
optionalForceNewSchema := schema.Resource{
53+
Schema: map[string]*schema.Schema{
54+
"prop": {
55+
Type: typ,
56+
Optional: true,
57+
ForceNew: true,
58+
},
59+
},
60+
}
61+
62+
requiredSchema := schema.Resource{
63+
Schema: map[string]*schema.Schema{
64+
"prop": {
65+
Type: typ,
66+
Required: true,
67+
},
68+
},
69+
}
70+
71+
requiredForceNewSchema := schema.Resource{
72+
Schema: map[string]*schema.Schema{
73+
"prop": {
74+
Type: typ,
75+
ForceNew: true,
76+
Required: true,
77+
},
78+
},
79+
}
80+
81+
setComputedFunc := func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
82+
if _, ok := d.GetOk("prop"); !ok {
83+
err := d.Set("prop", computedVal)
84+
if err != nil {
85+
return diag.FromErr(err)
86+
}
87+
}
88+
return nil
89+
}
90+
91+
optionalComputedSchema := schema.Resource{
92+
Schema: map[string]*schema.Schema{
93+
"prop": {
94+
Type: typ,
95+
Optional: true,
96+
Computed: true,
97+
},
98+
},
99+
CreateContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
100+
rd.SetId("id")
101+
return setComputedFunc(ctx, rd, i)
102+
},
103+
UpdateContext: setComputedFunc,
104+
}
105+
106+
optionalComputedForceNewSchema := schema.Resource{
107+
Schema: map[string]*schema.Schema{
108+
"prop": {
109+
Type: typ,
110+
Optional: true,
111+
Computed: true,
112+
ForceNew: true,
113+
},
114+
},
115+
CreateContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
116+
rd.SetId("id")
117+
return setComputedFunc(ctx, rd, i)
118+
},
119+
UpdateContext: setComputedFunc,
120+
}
121+
122+
optionalDefaultSchema := schema.Resource{
123+
Schema: map[string]*schema.Schema{
124+
"prop": {
125+
Type: typ,
126+
Optional: true,
127+
Default: defaultVal,
128+
},
129+
},
130+
}
131+
132+
optionalDefaultForceNewSchema := schema.Resource{
133+
Schema: map[string]*schema.Schema{
134+
"prop": {
135+
Type: typ,
136+
Optional: true,
137+
Default: defaultVal,
138+
ForceNew: true,
139+
},
140+
},
141+
}
142+
143+
return []diffSchemaValueMakerPair[T]{
144+
{"optional", optionalSchema, ctyVal},
145+
{"optionalForceNew", optionalForceNewSchema, ctyVal},
146+
{"required", requiredSchema, ctyVal},
147+
{"requiredForceNew", requiredForceNewSchema, ctyVal},
148+
{"optionalComputed", optionalComputedSchema, ctyVal},
149+
{"optionalComputedForceNew", optionalComputedForceNewSchema, ctyVal},
150+
{"optionalDefault", optionalDefaultSchema, ctyVal},
151+
{"optionalDefaultForceNew", optionalDefaultForceNewSchema, ctyVal},
152+
}, []diffScenario[T]{
153+
{"unchanged empty", noValue, noValue},
154+
{"unchanged non-empty", valueOne, valueOne},
155+
{"added", noValue, valueOne},
156+
{"removed", valueOne, noValue},
157+
{"changed", valueOne, valueTwo},
158+
}
159+
}
160+
24161
func listValueMaker(arr *[]string) cty.Value {
25162
if arr == nil {
26163
return cty.NullVal(cty.DynamicPseudoType)

0 commit comments

Comments
 (0)