|
| 1 | +package listvalidator_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/google/go-cmp/cmp" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework-validators/listvalidator" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 14 | +) |
| 15 | + |
| 16 | +func TestUniqueValues(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + testCases := map[string]struct { |
| 20 | + list types.List |
| 21 | + expectedDiagnostics diag.Diagnostics |
| 22 | + }{ |
| 23 | + "null-list": { |
| 24 | + list: types.ListNull(types.StringType), |
| 25 | + expectedDiagnostics: nil, |
| 26 | + }, |
| 27 | + "unknown-list": { |
| 28 | + list: types.ListUnknown(types.StringType), |
| 29 | + expectedDiagnostics: nil, |
| 30 | + }, |
| 31 | + "null-value": { |
| 32 | + list: types.ListValueMust( |
| 33 | + types.StringType, |
| 34 | + []attr.Value{types.StringNull()}, |
| 35 | + ), |
| 36 | + expectedDiagnostics: nil, |
| 37 | + }, |
| 38 | + "null-values-duplicate": { |
| 39 | + list: types.ListValueMust( |
| 40 | + types.StringType, |
| 41 | + []attr.Value{types.StringNull(), types.StringNull()}, |
| 42 | + ), |
| 43 | + expectedDiagnostics: diag.Diagnostics{ |
| 44 | + diag.NewAttributeErrorDiagnostic( |
| 45 | + path.Root("test"), |
| 46 | + "Duplicate List Value", |
| 47 | + "This attribute contains duplicate values of: <null>", |
| 48 | + ), |
| 49 | + }, |
| 50 | + }, |
| 51 | + "null-values-valid": { |
| 52 | + list: types.ListValueMust( |
| 53 | + types.StringType, |
| 54 | + []attr.Value{types.StringNull(), types.StringValue("test")}, |
| 55 | + ), |
| 56 | + expectedDiagnostics: nil, |
| 57 | + }, |
| 58 | + "unknown-value": { |
| 59 | + list: types.ListValueMust( |
| 60 | + types.StringType, |
| 61 | + []attr.Value{types.StringUnknown()}, |
| 62 | + ), |
| 63 | + expectedDiagnostics: nil, |
| 64 | + }, |
| 65 | + "unknown-values-duplicate": { |
| 66 | + list: types.ListValueMust( |
| 67 | + types.StringType, |
| 68 | + []attr.Value{types.StringUnknown(), types.StringUnknown()}, |
| 69 | + ), |
| 70 | + expectedDiagnostics: nil, |
| 71 | + }, |
| 72 | + "unknown-values-valid": { |
| 73 | + list: types.ListValueMust( |
| 74 | + types.StringType, |
| 75 | + []attr.Value{types.StringUnknown(), types.StringValue("test")}, |
| 76 | + ), |
| 77 | + expectedDiagnostics: nil, |
| 78 | + }, |
| 79 | + "known-value": { |
| 80 | + list: types.ListValueMust( |
| 81 | + types.StringType, |
| 82 | + []attr.Value{types.StringValue("test")}, |
| 83 | + ), |
| 84 | + expectedDiagnostics: nil, |
| 85 | + }, |
| 86 | + "known-values-duplicate": { |
| 87 | + list: types.ListValueMust( |
| 88 | + types.StringType, |
| 89 | + []attr.Value{types.StringValue("test"), types.StringValue("test")}, |
| 90 | + ), |
| 91 | + expectedDiagnostics: diag.Diagnostics{ |
| 92 | + diag.NewAttributeErrorDiagnostic( |
| 93 | + path.Root("test"), |
| 94 | + "Duplicate List Value", |
| 95 | + "This attribute contains duplicate values of: \"test\"", |
| 96 | + ), |
| 97 | + }, |
| 98 | + }, |
| 99 | + "known-values-valid": { |
| 100 | + list: types.ListValueMust( |
| 101 | + types.StringType, |
| 102 | + []attr.Value{types.StringValue("test1"), types.StringValue("test2")}, |
| 103 | + ), |
| 104 | + expectedDiagnostics: nil, |
| 105 | + }, |
| 106 | + } |
| 107 | + |
| 108 | + for name, testCase := range testCases { |
| 109 | + name, testCase := name, testCase |
| 110 | + |
| 111 | + t.Run(name, func(t *testing.T) { |
| 112 | + t.Parallel() |
| 113 | + |
| 114 | + request := validator.ListRequest{ |
| 115 | + Path: path.Root("test"), |
| 116 | + PathExpression: path.MatchRoot("test"), |
| 117 | + ConfigValue: testCase.list, |
| 118 | + } |
| 119 | + response := validator.ListResponse{} |
| 120 | + listvalidator.UniqueValues().ValidateList(context.Background(), request, &response) |
| 121 | + |
| 122 | + if diff := cmp.Diff(response.Diagnostics, testCase.expectedDiagnostics); diff != "" { |
| 123 | + t.Errorf("unexpected diagnostics difference: %s", diff) |
| 124 | + } |
| 125 | + }) |
| 126 | + } |
| 127 | +} |
0 commit comments