Skip to content

Commit 2fc01f4

Browse files
author
Ivan De Marino
committed
Adding more tests for OneOf() and NoneOf() for all packages
1 parent 5f7ea16 commit 2fc01f4

16 files changed

+542
-20
lines changed

float64validator/at_least_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package float64validator
1+
package float64validator_test
22

33
import (
44
"context"
55
"testing"
66

7+
"github.com/hashicorp/terraform-plugin-framework-validators/float64validator"
78
"github.com/hashicorp/terraform-plugin-framework/attr"
89
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
910
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -58,7 +59,7 @@ func TestAtLeastValidator(t *testing.T) {
5859
AttributeConfig: test.val,
5960
}
6061
response := tfsdk.ValidateAttributeResponse{}
61-
AtLeast(test.min).Validate(context.TODO(), request, &response)
62+
float64validator.AtLeast(test.min).Validate(context.TODO(), request, &response)
6263

6364
if !response.Diagnostics.HasError() && test.expectError {
6465
t.Fatal("expected error, got no error")

float64validator/at_most_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package float64validator
1+
package float64validator_test
22

33
import (
44
"context"
55
"testing"
66

7+
"github.com/hashicorp/terraform-plugin-framework-validators/float64validator"
78
"github.com/hashicorp/terraform-plugin-framework/attr"
89
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
910
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -58,7 +59,7 @@ func TestAtMostValidator(t *testing.T) {
5859
AttributeConfig: test.val,
5960
}
6061
response := tfsdk.ValidateAttributeResponse{}
61-
AtMost(test.max).Validate(context.TODO(), request, &response)
62+
float64validator.AtMost(test.max).Validate(context.TODO(), request, &response)
6263

6364
if !response.Diagnostics.HasError() && test.expectError {
6465
t.Fatal("expected error, got no error")

float64validator/between_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package float64validator
1+
package float64validator_test
22

33
import (
44
"context"
55
"testing"
66

7+
"github.com/hashicorp/terraform-plugin-framework-validators/float64validator"
78
"github.com/hashicorp/terraform-plugin-framework/attr"
89
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
910
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -76,7 +77,7 @@ func TestBetweenValidator(t *testing.T) {
7677
AttributeConfig: test.val,
7778
}
7879
response := tfsdk.ValidateAttributeResponse{}
79-
Between(test.min, test.max).Validate(context.TODO(), request, &response)
80+
float64validator.Between(test.min, test.max).Validate(context.TODO(), request, &response)
8081

8182
if !response.Diagnostics.HasError() && test.expectError {
8283
t.Fatal("expected error, got no error")

float64validator/none_of_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package float64validator_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-framework-validators/float64validator"
8+
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
9+
"github.com/hashicorp/terraform-plugin-framework/attr"
10+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
11+
"github.com/hashicorp/terraform-plugin-framework/types"
12+
)
13+
14+
func TestNoneOfValidator(t *testing.T) {
15+
t.Parallel()
16+
17+
type testCase struct {
18+
in attr.Value
19+
validator tfsdk.AttributeValidator
20+
expErrors int
21+
}
22+
23+
testCases := map[string]testCase{
24+
"simple-match": {
25+
in: types.Float64{Value: 123.456},
26+
validator: float64validator.NoneOf(
27+
123.456,
28+
234.567,
29+
8910.11,
30+
1213.1415,
31+
),
32+
expErrors: 1,
33+
},
34+
"simple-mismatch": {
35+
in: types.Float64{Value: 123.456},
36+
validator: float64validator.NoneOf(
37+
234.567,
38+
8910.11,
39+
1213.1415,
40+
),
41+
expErrors: 0,
42+
},
43+
"skip-validation-on-null": {
44+
in: types.Float64{Null: true},
45+
validator: float64validator.NoneOf(
46+
234.567,
47+
8910.11,
48+
1213.1415,
49+
),
50+
expErrors: 0,
51+
},
52+
"skip-validation-on-unknown": {
53+
in: types.Float64{Unknown: true},
54+
validator: float64validator.NoneOf(
55+
234.567,
56+
8910.11,
57+
1213.1415,
58+
),
59+
expErrors: 0,
60+
},
61+
}
62+
63+
for name, test := range testCases {
64+
name, test := name, test
65+
t.Run(name, func(t *testing.T) {
66+
req := tfsdk.ValidateAttributeRequest{
67+
AttributeConfig: test.in,
68+
}
69+
res := tfsdk.ValidateAttributeResponse{}
70+
test.validator.Validate(context.TODO(), req, &res)
71+
72+
if test.expErrors > 0 && !res.Diagnostics.HasError() {
73+
t.Fatalf("expected %d error(s), got none", test.expErrors)
74+
}
75+
76+
if test.expErrors > 0 && test.expErrors != validatordiag.ErrorsCount(res.Diagnostics) {
77+
t.Fatalf("expected %d error(s), got %d: %v", test.expErrors, validatordiag.ErrorsCount(res.Diagnostics), res.Diagnostics)
78+
}
79+
80+
if test.expErrors == 0 && res.Diagnostics.HasError() {
81+
t.Fatalf("expected no error(s), got %d: %v", validatordiag.ErrorsCount(res.Diagnostics), res.Diagnostics)
82+
}
83+
})
84+
}
85+
}

float64validator/one_of_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package float64validator_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-framework-validators/float64validator"
8+
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
9+
"github.com/hashicorp/terraform-plugin-framework/attr"
10+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
11+
"github.com/hashicorp/terraform-plugin-framework/types"
12+
)
13+
14+
func TestOneOfValidator(t *testing.T) {
15+
t.Parallel()
16+
17+
type testCase struct {
18+
in attr.Value
19+
validator tfsdk.AttributeValidator
20+
expErrors int
21+
}
22+
23+
testCases := map[string]testCase{
24+
"simple-match": {
25+
in: types.Float64{Value: 123.456},
26+
validator: float64validator.OneOf(
27+
123.456,
28+
234.567,
29+
8910.11,
30+
1213.1415,
31+
),
32+
expErrors: 0,
33+
},
34+
"simple-mismatch": {
35+
in: types.Float64{Value: 123.456},
36+
validator: float64validator.OneOf(
37+
234.567,
38+
8910.11,
39+
1213.1415,
40+
),
41+
expErrors: 1,
42+
},
43+
"skip-validation-on-null": {
44+
in: types.Float64{Null: true},
45+
validator: float64validator.OneOf(
46+
234.567,
47+
8910.11,
48+
1213.1415,
49+
),
50+
expErrors: 0,
51+
},
52+
"skip-validation-on-unknown": {
53+
in: types.Float64{Unknown: true},
54+
validator: float64validator.OneOf(
55+
234.567,
56+
8910.11,
57+
1213.1415,
58+
),
59+
expErrors: 0,
60+
},
61+
}
62+
63+
for name, test := range testCases {
64+
name, test := name, test
65+
t.Run(name, func(t *testing.T) {
66+
req := tfsdk.ValidateAttributeRequest{
67+
AttributeConfig: test.in,
68+
}
69+
res := tfsdk.ValidateAttributeResponse{}
70+
test.validator.Validate(context.TODO(), req, &res)
71+
72+
if test.expErrors > 0 && !res.Diagnostics.HasError() {
73+
t.Fatalf("expected %d error(s), got none", test.expErrors)
74+
}
75+
76+
if test.expErrors > 0 && test.expErrors != validatordiag.ErrorsCount(res.Diagnostics) {
77+
t.Fatalf("expected %d error(s), got %d: %v", test.expErrors, validatordiag.ErrorsCount(res.Diagnostics), res.Diagnostics)
78+
}
79+
80+
if test.expErrors == 0 && res.Diagnostics.HasError() {
81+
t.Fatalf("expected no error(s), got %d: %v", validatordiag.ErrorsCount(res.Diagnostics), res.Diagnostics)
82+
}
83+
})
84+
}
85+
}

int64validator/at_least_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package int64validator
1+
package int64validator_test
22

33
import (
44
"context"
55
"testing"
66

7+
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
78
"github.com/hashicorp/terraform-plugin-framework/attr"
89
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
910
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -54,7 +55,7 @@ func TestAtLeastValidator(t *testing.T) {
5455
AttributeConfig: test.val,
5556
}
5657
response := tfsdk.ValidateAttributeResponse{}
57-
AtLeast(test.min).Validate(context.TODO(), request, &response)
58+
int64validator.AtLeast(test.min).Validate(context.TODO(), request, &response)
5859

5960
if !response.Diagnostics.HasError() && test.expectError {
6061
t.Fatal("expected error, got no error")

int64validator/at_most_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package int64validator
1+
package int64validator_test
22

33
import (
44
"context"
55
"testing"
66

7+
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
78
"github.com/hashicorp/terraform-plugin-framework/attr"
89
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
910
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -54,7 +55,7 @@ func TestAtMostValidator(t *testing.T) {
5455
AttributeConfig: test.val,
5556
}
5657
response := tfsdk.ValidateAttributeResponse{}
57-
AtMost(test.max).Validate(context.TODO(), request, &response)
58+
int64validator.AtMost(test.max).Validate(context.TODO(), request, &response)
5859

5960
if !response.Diagnostics.HasError() && test.expectError {
6061
t.Fatal("expected error, got no error")

int64validator/between_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package int64validator
1+
package int64validator_test
22

33
import (
44
"context"
55
"testing"
66

7+
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
78
"github.com/hashicorp/terraform-plugin-framework/attr"
89
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
910
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -71,7 +72,7 @@ func TestBetweenValidator(t *testing.T) {
7172
AttributeConfig: test.val,
7273
}
7374
response := tfsdk.ValidateAttributeResponse{}
74-
Between(test.min, test.max).Validate(context.TODO(), request, &response)
75+
int64validator.Between(test.min, test.max).Validate(context.TODO(), request, &response)
7576

7677
if !response.Diagnostics.HasError() && test.expectError {
7778
t.Fatal("expected error, got no error")

int64validator/none_of_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package int64validator_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
8+
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
9+
"github.com/hashicorp/terraform-plugin-framework/attr"
10+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
11+
"github.com/hashicorp/terraform-plugin-framework/types"
12+
)
13+
14+
func TestNoneOfValidator(t *testing.T) {
15+
t.Parallel()
16+
17+
type testCase struct {
18+
in attr.Value
19+
validator tfsdk.AttributeValidator
20+
expErrors int
21+
}
22+
23+
testCases := map[string]testCase{
24+
"simple-match": {
25+
in: types.Int64{Value: 123},
26+
validator: int64validator.NoneOf(
27+
123,
28+
234,
29+
8910,
30+
1213,
31+
),
32+
expErrors: 1,
33+
},
34+
"simple-mismatch": {
35+
in: types.Int64{Value: 123},
36+
validator: int64validator.NoneOf(
37+
234,
38+
8910,
39+
1213,
40+
),
41+
expErrors: 0,
42+
},
43+
"skip-validation-on-null": {
44+
in: types.Int64{Null: true},
45+
validator: int64validator.NoneOf(
46+
234,
47+
8910,
48+
1213,
49+
),
50+
expErrors: 0,
51+
},
52+
"skip-validation-on-unknown": {
53+
in: types.Int64{Unknown: true},
54+
validator: int64validator.NoneOf(
55+
234,
56+
8910,
57+
1213,
58+
),
59+
expErrors: 0,
60+
},
61+
}
62+
63+
for name, test := range testCases {
64+
name, test := name, test
65+
t.Run(name, func(t *testing.T) {
66+
req := tfsdk.ValidateAttributeRequest{
67+
AttributeConfig: test.in,
68+
}
69+
res := tfsdk.ValidateAttributeResponse{}
70+
test.validator.Validate(context.TODO(), req, &res)
71+
72+
if test.expErrors > 0 && !res.Diagnostics.HasError() {
73+
t.Fatalf("expected %d error(s), got none", test.expErrors)
74+
}
75+
76+
if test.expErrors > 0 && test.expErrors != validatordiag.ErrorsCount(res.Diagnostics) {
77+
t.Fatalf("expected %d error(s), got %d: %v", test.expErrors, validatordiag.ErrorsCount(res.Diagnostics), res.Diagnostics)
78+
}
79+
80+
if test.expErrors == 0 && res.Diagnostics.HasError() {
81+
t.Fatalf("expected no error(s), got %d: %v", validatordiag.ErrorsCount(res.Diagnostics), res.Diagnostics)
82+
}
83+
})
84+
}
85+
}

0 commit comments

Comments
 (0)