Skip to content

Commit ebd2441

Browse files
committed
Adding further tests to verify behaviour when attribute or summed attribute(s) are null or unknown for int64 atLeastSumOf (#20)
1 parent a56e62f commit ebd2441

File tree

1 file changed

+66
-11
lines changed

1 file changed

+66
-11
lines changed

int64validator/at_least_sum_of_test.go

+66-11
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestAtLeastSumOfValidator(t *testing.T) {
1616
type testCase struct {
1717
val attr.Value
1818
attributesToSumPaths []*tftypes.AttributePath
19-
requestConfigRaw map[string]tftypes.Value
19+
requestConfigRaw map[string]attr.Value
2020
expectError bool
2121
}
2222
tests := map[string]testCase{
@@ -36,9 +36,9 @@ func TestAtLeastSumOfValidator(t *testing.T) {
3636
tftypes.NewAttributePath().WithAttributeName("one"),
3737
tftypes.NewAttributePath().WithAttributeName("two"),
3838
},
39-
requestConfigRaw: map[string]tftypes.Value{
40-
"one": tftypes.NewValue(tftypes.Number, 15),
41-
"two": tftypes.NewValue(tftypes.Number, 15),
39+
requestConfigRaw: map[string]attr.Value{
40+
"one": types.Int64{Value: 15},
41+
"two": types.Int64{Value: 15},
4242
},
4343
expectError: true,
4444
},
@@ -48,9 +48,9 @@ func TestAtLeastSumOfValidator(t *testing.T) {
4848
tftypes.NewAttributePath().WithAttributeName("one"),
4949
tftypes.NewAttributePath().WithAttributeName("two"),
5050
},
51-
requestConfigRaw: map[string]tftypes.Value{
52-
"one": tftypes.NewValue(tftypes.Number, 5),
53-
"two": tftypes.NewValue(tftypes.Number, 5),
51+
requestConfigRaw: map[string]attr.Value{
52+
"one": types.Int64{Value: 5},
53+
"two": types.Int64{Value: 5},
5454
},
5555
},
5656
"valid integer as Int64 greater than sum of attributes": {
@@ -59,21 +59,76 @@ func TestAtLeastSumOfValidator(t *testing.T) {
5959
tftypes.NewAttributePath().WithAttributeName("one"),
6060
tftypes.NewAttributePath().WithAttributeName("two"),
6161
},
62-
requestConfigRaw: map[string]tftypes.Value{
63-
"one": tftypes.NewValue(tftypes.Number, 4),
64-
"two": tftypes.NewValue(tftypes.Number, 4),
62+
requestConfigRaw: map[string]attr.Value{
63+
"one": types.Int64{Value: 4},
64+
"two": types.Int64{Value: 4},
65+
},
66+
},
67+
"valid integer as Int64 greater than sum of attributes, when one summed attribute is null": {
68+
val: types.Int64{Value: 10},
69+
attributesToSumPaths: []*tftypes.AttributePath{
70+
tftypes.NewAttributePath().WithAttributeName("one"),
71+
tftypes.NewAttributePath().WithAttributeName("two"),
72+
},
73+
requestConfigRaw: map[string]attr.Value{
74+
"one": types.Int64{Null: true},
75+
"two": types.Int64{Value: 9},
76+
},
77+
},
78+
"valid integer as Int64 does not return error when all attributes are null": {
79+
val: types.Int64{Null: true},
80+
attributesToSumPaths: []*tftypes.AttributePath{
81+
tftypes.NewAttributePath().WithAttributeName("one"),
82+
tftypes.NewAttributePath().WithAttributeName("two"),
83+
},
84+
requestConfigRaw: map[string]attr.Value{
85+
"one": types.Int64{Null: true},
86+
"two": types.Int64{Null: true},
87+
},
88+
},
89+
"valid integer as Int64 greater than sum of attributes, when one summed attribute is unknown": {
90+
val: types.Int64{Value: 10},
91+
attributesToSumPaths: []*tftypes.AttributePath{
92+
tftypes.NewAttributePath().WithAttributeName("one"),
93+
tftypes.NewAttributePath().WithAttributeName("two"),
94+
},
95+
requestConfigRaw: map[string]attr.Value{
96+
"one": types.Int64{Unknown: true},
97+
"two": types.Int64{Value: 9},
98+
},
99+
},
100+
"valid integer as Int64 does not return error when all attributes are unknown": {
101+
val: types.Int64{Unknown: true},
102+
attributesToSumPaths: []*tftypes.AttributePath{
103+
tftypes.NewAttributePath().WithAttributeName("one"),
104+
tftypes.NewAttributePath().WithAttributeName("two"),
105+
},
106+
requestConfigRaw: map[string]attr.Value{
107+
"one": types.Int64{Unknown: true},
108+
"two": types.Int64{Unknown: true},
65109
},
66110
},
67111
}
68112

69113
for name, test := range tests {
70114
name, test := name, test
71115
t.Run(name, func(t *testing.T) {
116+
reqConf := make(map[string]tftypes.Value, len(test.requestConfigRaw))
117+
118+
for k, v := range test.requestConfigRaw {
119+
val, err := v.ToTerraformValue(context.Background())
120+
if err != nil {
121+
t.Fatalf("could not attr.Value at key:%s to tftypes.Value", k)
122+
}
123+
124+
reqConf[k] = val
125+
}
126+
72127
request := tfsdk.ValidateAttributeRequest{
73128
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
74129
AttributeConfig: test.val,
75130
Config: tfsdk.Config{
76-
Raw: tftypes.NewValue(tftypes.Object{}, test.requestConfigRaw),
131+
Raw: tftypes.NewValue(tftypes.Object{}, reqConf),
77132
Schema: tfsdk.Schema{
78133
Attributes: map[string]tfsdk.Attribute{
79134
"test": {Type: types.Int64Type},

0 commit comments

Comments
 (0)