|
| 1 | +package datasourcevalidator_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/google/go-cmp/cmp" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework-validators/datasourcevalidator" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/tfsdk" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 14 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 15 | +) |
| 16 | + |
| 17 | +func TestConflicting(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + |
| 20 | + testCases := map[string]struct { |
| 21 | + pathExpressions path.Expressions |
| 22 | + req datasource.ValidateConfigRequest |
| 23 | + expected *datasource.ValidateConfigResponse |
| 24 | + }{ |
| 25 | + "no-diagnostics": { |
| 26 | + pathExpressions: path.Expressions{ |
| 27 | + path.MatchRoot("test"), |
| 28 | + }, |
| 29 | + req: datasource.ValidateConfigRequest{ |
| 30 | + Config: tfsdk.Config{ |
| 31 | + Schema: tfsdk.Schema{ |
| 32 | + Attributes: map[string]tfsdk.Attribute{ |
| 33 | + "test": { |
| 34 | + Optional: true, |
| 35 | + Type: types.StringType, |
| 36 | + }, |
| 37 | + "other": { |
| 38 | + Optional: true, |
| 39 | + Type: types.StringType, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + Raw: tftypes.NewValue( |
| 44 | + tftypes.Object{ |
| 45 | + AttributeTypes: map[string]tftypes.Type{ |
| 46 | + "test": tftypes.String, |
| 47 | + "other": tftypes.String, |
| 48 | + }, |
| 49 | + }, |
| 50 | + map[string]tftypes.Value{ |
| 51 | + "test": tftypes.NewValue(tftypes.String, "test-value"), |
| 52 | + "other": tftypes.NewValue(tftypes.String, "test-value"), |
| 53 | + }, |
| 54 | + ), |
| 55 | + }, |
| 56 | + }, |
| 57 | + expected: &datasource.ValidateConfigResponse{}, |
| 58 | + }, |
| 59 | + "diagnostics": { |
| 60 | + pathExpressions: path.Expressions{ |
| 61 | + path.MatchRoot("test1"), |
| 62 | + path.MatchRoot("test2"), |
| 63 | + }, |
| 64 | + req: datasource.ValidateConfigRequest{ |
| 65 | + Config: tfsdk.Config{ |
| 66 | + Schema: tfsdk.Schema{ |
| 67 | + Attributes: map[string]tfsdk.Attribute{ |
| 68 | + "test1": { |
| 69 | + Optional: true, |
| 70 | + Type: types.StringType, |
| 71 | + }, |
| 72 | + "test2": { |
| 73 | + Optional: true, |
| 74 | + Type: types.StringType, |
| 75 | + }, |
| 76 | + "other": { |
| 77 | + Optional: true, |
| 78 | + Type: types.StringType, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + Raw: tftypes.NewValue( |
| 83 | + tftypes.Object{ |
| 84 | + AttributeTypes: map[string]tftypes.Type{ |
| 85 | + "test1": tftypes.String, |
| 86 | + "test2": tftypes.String, |
| 87 | + "other": tftypes.String, |
| 88 | + }, |
| 89 | + }, |
| 90 | + map[string]tftypes.Value{ |
| 91 | + "test1": tftypes.NewValue(tftypes.String, "test-value"), |
| 92 | + "test2": tftypes.NewValue(tftypes.String, "test-value"), |
| 93 | + "other": tftypes.NewValue(tftypes.String, "test-value"), |
| 94 | + }, |
| 95 | + ), |
| 96 | + }, |
| 97 | + }, |
| 98 | + expected: &datasource.ValidateConfigResponse{ |
| 99 | + Diagnostics: diag.Diagnostics{ |
| 100 | + diag.NewAttributeErrorDiagnostic( |
| 101 | + path.Root("test1"), |
| 102 | + "Invalid Attribute Combination", |
| 103 | + "These attributes cannot be configured together: [test1,test2]", |
| 104 | + ), |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + } |
| 109 | + |
| 110 | + for name, testCase := range testCases { |
| 111 | + name, testCase := name, testCase |
| 112 | + |
| 113 | + t.Run(name, func(t *testing.T) { |
| 114 | + t.Parallel() |
| 115 | + |
| 116 | + validator := datasourcevalidator.Conflicting(testCase.pathExpressions...) |
| 117 | + got := &datasource.ValidateConfigResponse{} |
| 118 | + |
| 119 | + validator.ValidateDataSource(context.Background(), testCase.req, got) |
| 120 | + |
| 121 | + if diff := cmp.Diff(got, testCase.expected); diff != "" { |
| 122 | + t.Errorf("unexpected difference: %s", diff) |
| 123 | + } |
| 124 | + }) |
| 125 | + } |
| 126 | +} |
0 commit comments