7
7
"context"
8
8
"testing"
9
9
10
+ "github.com/google/go-cmp/cmp"
11
+ "github.com/hashicorp/terraform-plugin-framework/diag"
10
12
"github.com/hashicorp/terraform-plugin-framework/path"
11
13
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
12
14
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
@@ -20,9 +22,9 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
20
22
t .Parallel ()
21
23
22
24
type testCase struct {
23
- req schemavalidator.AtLeastOneOfValidatorRequest
24
- in path.Expressions
25
- expErrors int
25
+ req schemavalidator.AtLeastOneOfValidatorRequest
26
+ in path.Expressions
27
+ expected * schemavalidator. AtLeastOneOfValidatorResponse
26
28
}
27
29
28
30
testCases := map [string ]testCase {
@@ -52,6 +54,7 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
52
54
in : path.Expressions {
53
55
path .MatchRoot ("foo" ),
54
56
},
57
+ expected : & schemavalidator.AtLeastOneOfValidatorResponse {},
55
58
},
56
59
"self-is-null" : {
57
60
req : schemavalidator.AtLeastOneOfValidatorRequest {
@@ -79,6 +82,7 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
79
82
in : path.Expressions {
80
83
path .MatchRoot ("foo" ),
81
84
},
85
+ expected : & schemavalidator.AtLeastOneOfValidatorResponse {},
82
86
},
83
87
"error_none-set" : {
84
88
req : schemavalidator.AtLeastOneOfValidatorRequest {
@@ -110,7 +114,17 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
110
114
path .MatchRoot ("foo" ),
111
115
path .MatchRoot ("baz" ),
112
116
},
113
- expErrors : 1 ,
117
+ expected : & schemavalidator.AtLeastOneOfValidatorResponse {
118
+ Diagnostics : diag.Diagnostics {
119
+ diag .WithPath (
120
+ path .Root ("bar" ),
121
+ diag .NewErrorDiagnostic (
122
+ "Invalid Attribute Combination" ,
123
+ "At least one attribute out of [foo,baz,bar] must be specified" ,
124
+ ),
125
+ ),
126
+ },
127
+ },
114
128
},
115
129
"multiple-set" : {
116
130
req : schemavalidator.AtLeastOneOfValidatorRequest {
@@ -142,6 +156,7 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
142
156
path .MatchRoot ("foo" ),
143
157
path .MatchRoot ("baz" ),
144
158
},
159
+ expected : & schemavalidator.AtLeastOneOfValidatorResponse {},
145
160
},
146
161
"allow-duplicate-input" : {
147
162
req : schemavalidator.AtLeastOneOfValidatorRequest {
@@ -174,6 +189,7 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
174
189
path .MatchRoot ("bar" ),
175
190
path .MatchRoot ("baz" ),
176
191
},
192
+ expected : & schemavalidator.AtLeastOneOfValidatorResponse {},
177
193
},
178
194
"unknowns" : {
179
195
req : schemavalidator.AtLeastOneOfValidatorRequest {
@@ -205,6 +221,7 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
205
221
path .MatchRoot ("foo" ),
206
222
path .MatchRoot ("baz" ),
207
223
},
224
+ expected : & schemavalidator.AtLeastOneOfValidatorResponse {},
208
225
},
209
226
"matches-no-attribute-in-schema" : {
210
227
req : schemavalidator.AtLeastOneOfValidatorRequest {
@@ -232,7 +249,23 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
232
249
in : path.Expressions {
233
250
path .MatchRoot ("fooz" ),
234
251
},
235
- expErrors : 2 ,
252
+ expected : & schemavalidator.AtLeastOneOfValidatorResponse {
253
+ Diagnostics : diag.Diagnostics {
254
+ diag .NewErrorDiagnostic (
255
+ "Invalid Path Expression for Schema" ,
256
+ "The Terraform Provider unexpectedly provided a path expression that does not match the current schema. " +
257
+ "This can happen if the path expression does not correctly follow the schema in structure or types. " +
258
+ "Please report this to the provider developers.\n \n Path Expression: fooz" ,
259
+ ),
260
+ diag .WithPath (
261
+ path .Root ("bar" ),
262
+ diag .NewErrorDiagnostic (
263
+ "Invalid Attribute Combination" ,
264
+ "At least one attribute out of [fooz,bar] must be specified" ,
265
+ ),
266
+ ),
267
+ },
268
+ },
236
269
},
237
270
}
238
271
@@ -246,16 +279,8 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
246
279
PathExpressions : test .in ,
247
280
}.Validate (context .TODO (), test .req , res )
248
281
249
- if test .expErrors > 0 && ! res .Diagnostics .HasError () {
250
- t .Fatal ("expected error(s), got none" )
251
- }
252
-
253
- if test .expErrors > 0 && test .expErrors != res .Diagnostics .ErrorsCount () {
254
- t .Fatalf ("expected %d error(s), got %d: %v" , test .expErrors , res .Diagnostics .ErrorsCount (), res .Diagnostics )
255
- }
256
-
257
- if test .expErrors == 0 && res .Diagnostics .HasError () {
258
- t .Fatalf ("expected no error(s), got %d: %v" , res .Diagnostics .ErrorsCount (), res .Diagnostics )
282
+ if diff := cmp .Diff (test .expected , res ); diff != "" {
283
+ t .Errorf ("unexpected diff: %s" , diff )
259
284
}
260
285
})
261
286
}
0 commit comments