|
| 1 | +package int64validator |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/tfsdk" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 11 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 12 | +) |
| 13 | + |
| 14 | +func TestAtLeastSumOfValidator(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + |
| 17 | + type testCase struct { |
| 18 | + val attr.Value |
| 19 | + attributesToSumExpressions path.Expressions |
| 20 | + requestConfigRaw map[string]tftypes.Value |
| 21 | + expectError bool |
| 22 | + } |
| 23 | + tests := map[string]testCase{ |
| 24 | + "not an Int64": { |
| 25 | + val: types.Bool{Value: true}, |
| 26 | + expectError: true, |
| 27 | + }, |
| 28 | + "unknown Int64": { |
| 29 | + val: types.Int64{Unknown: true}, |
| 30 | + }, |
| 31 | + "null Int64": { |
| 32 | + val: types.Int64{Null: true}, |
| 33 | + }, |
| 34 | + "valid integer as Int64 less than sum of attributes": { |
| 35 | + val: types.Int64{Value: 10}, |
| 36 | + attributesToSumExpressions: path.Expressions{ |
| 37 | + path.MatchRoot("one"), |
| 38 | + path.MatchRoot("two"), |
| 39 | + }, |
| 40 | + requestConfigRaw: map[string]tftypes.Value{ |
| 41 | + "one": tftypes.NewValue(tftypes.Number, 15), |
| 42 | + "two": tftypes.NewValue(tftypes.Number, 15), |
| 43 | + }, |
| 44 | + expectError: true, |
| 45 | + }, |
| 46 | + "valid integer as Int64 equal to sum of attributes": { |
| 47 | + val: types.Int64{Value: 10}, |
| 48 | + attributesToSumExpressions: path.Expressions{ |
| 49 | + path.MatchRoot("one"), |
| 50 | + path.MatchRoot("two"), |
| 51 | + }, |
| 52 | + requestConfigRaw: map[string]tftypes.Value{ |
| 53 | + "one": tftypes.NewValue(tftypes.Number, 5), |
| 54 | + "two": tftypes.NewValue(tftypes.Number, 5), |
| 55 | + }, |
| 56 | + }, |
| 57 | + "valid integer as Int64 greater than sum of attributes": { |
| 58 | + val: types.Int64{Value: 10}, |
| 59 | + attributesToSumExpressions: path.Expressions{ |
| 60 | + path.MatchRoot("one"), |
| 61 | + path.MatchRoot("two"), |
| 62 | + }, |
| 63 | + requestConfigRaw: map[string]tftypes.Value{ |
| 64 | + "one": tftypes.NewValue(tftypes.Number, 4), |
| 65 | + "two": tftypes.NewValue(tftypes.Number, 4), |
| 66 | + }, |
| 67 | + }, |
| 68 | + "valid integer as Int64 greater than sum of attributes, when one summed attribute is null": { |
| 69 | + val: types.Int64{Value: 10}, |
| 70 | + attributesToSumExpressions: path.Expressions{ |
| 71 | + path.MatchRoot("one"), |
| 72 | + path.MatchRoot("two"), |
| 73 | + }, |
| 74 | + requestConfigRaw: map[string]tftypes.Value{ |
| 75 | + "one": tftypes.NewValue(tftypes.Number, nil), |
| 76 | + "two": tftypes.NewValue(tftypes.Number, 9), |
| 77 | + }, |
| 78 | + }, |
| 79 | + "valid integer as Int64 does not return error when all attributes are null": { |
| 80 | + val: types.Int64{Null: true}, |
| 81 | + attributesToSumExpressions: path.Expressions{ |
| 82 | + path.MatchRoot("one"), |
| 83 | + path.MatchRoot("two"), |
| 84 | + }, |
| 85 | + requestConfigRaw: map[string]tftypes.Value{ |
| 86 | + "one": tftypes.NewValue(tftypes.Number, nil), |
| 87 | + "two": tftypes.NewValue(tftypes.Number, nil), |
| 88 | + }, |
| 89 | + }, |
| 90 | + "valid integer as Int64 returns error when all attributes to sum are null": { |
| 91 | + val: types.Int64{Value: -1}, |
| 92 | + attributesToSumExpressions: path.Expressions{ |
| 93 | + path.MatchRoot("one"), |
| 94 | + path.MatchRoot("two"), |
| 95 | + }, |
| 96 | + requestConfigRaw: map[string]tftypes.Value{ |
| 97 | + "one": tftypes.NewValue(tftypes.Number, nil), |
| 98 | + "two": tftypes.NewValue(tftypes.Number, nil), |
| 99 | + }, |
| 100 | + expectError: true, |
| 101 | + }, |
| 102 | + "valid integer as Int64 greater than sum of attributes, when one summed attribute is unknown": { |
| 103 | + val: types.Int64{Value: 10}, |
| 104 | + attributesToSumExpressions: path.Expressions{ |
| 105 | + path.MatchRoot("one"), |
| 106 | + path.MatchRoot("two"), |
| 107 | + }, |
| 108 | + requestConfigRaw: map[string]tftypes.Value{ |
| 109 | + "one": tftypes.NewValue(tftypes.Number, tftypes.UnknownValue), |
| 110 | + "two": tftypes.NewValue(tftypes.Number, 9), |
| 111 | + }, |
| 112 | + }, |
| 113 | + "valid integer as Int64 does not return error when all attributes are unknown": { |
| 114 | + val: types.Int64{Unknown: true}, |
| 115 | + attributesToSumExpressions: path.Expressions{ |
| 116 | + path.MatchRoot("one"), |
| 117 | + path.MatchRoot("two"), |
| 118 | + }, |
| 119 | + requestConfigRaw: map[string]tftypes.Value{ |
| 120 | + "one": tftypes.NewValue(tftypes.Number, tftypes.UnknownValue), |
| 121 | + "two": tftypes.NewValue(tftypes.Number, tftypes.UnknownValue), |
| 122 | + }, |
| 123 | + }, |
| 124 | + "valid integer as Int64 does not return error when all attributes to sum are unknown": { |
| 125 | + val: types.Int64{Value: -1}, |
| 126 | + attributesToSumExpressions: path.Expressions{ |
| 127 | + path.MatchRoot("one"), |
| 128 | + path.MatchRoot("two"), |
| 129 | + }, |
| 130 | + requestConfigRaw: map[string]tftypes.Value{ |
| 131 | + "one": tftypes.NewValue(tftypes.Number, tftypes.UnknownValue), |
| 132 | + "two": tftypes.NewValue(tftypes.Number, tftypes.UnknownValue), |
| 133 | + }, |
| 134 | + }, |
| 135 | + "error when attribute to sum is not Number": { |
| 136 | + val: types.Int64{Value: 9}, |
| 137 | + attributesToSumExpressions: path.Expressions{ |
| 138 | + path.MatchRoot("one"), |
| 139 | + path.MatchRoot("two"), |
| 140 | + }, |
| 141 | + requestConfigRaw: map[string]tftypes.Value{ |
| 142 | + "one": tftypes.NewValue(tftypes.Bool, true), |
| 143 | + "two": tftypes.NewValue(tftypes.Number, 9), |
| 144 | + }, |
| 145 | + expectError: true, |
| 146 | + }, |
| 147 | + } |
| 148 | + |
| 149 | + for name, test := range tests { |
| 150 | + name, test := name, test |
| 151 | + t.Run(name, func(t *testing.T) { |
| 152 | + request := tfsdk.ValidateAttributeRequest{ |
| 153 | + AttributePath: path.Root("test"), |
| 154 | + AttributePathExpression: path.MatchRoot("test"), |
| 155 | + AttributeConfig: test.val, |
| 156 | + Config: tfsdk.Config{ |
| 157 | + Raw: tftypes.NewValue(tftypes.Object{}, test.requestConfigRaw), |
| 158 | + Schema: tfsdk.Schema{ |
| 159 | + Attributes: map[string]tfsdk.Attribute{ |
| 160 | + "test": {Type: types.Int64Type}, |
| 161 | + "one": {Type: types.Int64Type}, |
| 162 | + "two": {Type: types.Int64Type}, |
| 163 | + }, |
| 164 | + }, |
| 165 | + }, |
| 166 | + } |
| 167 | + |
| 168 | + response := tfsdk.ValidateAttributeResponse{} |
| 169 | + |
| 170 | + AtLeastSumOf(test.attributesToSumExpressions...).Validate(context.Background(), request, &response) |
| 171 | + |
| 172 | + if !response.Diagnostics.HasError() && test.expectError { |
| 173 | + t.Fatal("expected error, got no error") |
| 174 | + } |
| 175 | + |
| 176 | + if response.Diagnostics.HasError() && !test.expectError { |
| 177 | + t.Fatalf("got unexpected error: %s", response.Diagnostics) |
| 178 | + } |
| 179 | + }) |
| 180 | + } |
| 181 | +} |
0 commit comments