|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package listvalidator_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-framework-validators/listvalidator" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/function" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 17 | +) |
| 18 | + |
| 19 | +func TestNoNullValuesValidator(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + |
| 22 | + type testCase struct { |
| 23 | + val types.List |
| 24 | + expectError bool |
| 25 | + } |
| 26 | + tests := map[string]testCase{ |
| 27 | + "List unknown": { |
| 28 | + val: types.ListUnknown( |
| 29 | + types.StringType, |
| 30 | + ), |
| 31 | + expectError: false, |
| 32 | + }, |
| 33 | + "List null": { |
| 34 | + val: types.ListNull( |
| 35 | + types.StringType, |
| 36 | + ), |
| 37 | + expectError: false, |
| 38 | + }, |
| 39 | + "No null values": { |
| 40 | + val: types.ListValueMust( |
| 41 | + types.StringType, |
| 42 | + []attr.Value{ |
| 43 | + types.StringValue("first"), |
| 44 | + types.StringValue("second"), |
| 45 | + }, |
| 46 | + ), |
| 47 | + expectError: false, |
| 48 | + }, |
| 49 | + "Unknown value": { |
| 50 | + val: types.ListValueMust( |
| 51 | + types.StringType, |
| 52 | + []attr.Value{ |
| 53 | + types.StringValue("first"), |
| 54 | + types.StringUnknown(), |
| 55 | + }, |
| 56 | + ), |
| 57 | + expectError: false, |
| 58 | + }, |
| 59 | + "Null value": { |
| 60 | + val: types.ListValueMust( |
| 61 | + types.StringType, |
| 62 | + []attr.Value{ |
| 63 | + types.StringValue("first"), |
| 64 | + types.StringNull(), |
| 65 | + }, |
| 66 | + ), |
| 67 | + expectError: true, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + for name, test := range tests { |
| 72 | + t.Run(fmt.Sprintf("ValidateList - %s", name), func(t *testing.T) { |
| 73 | + t.Parallel() |
| 74 | + request := validator.ListRequest{ |
| 75 | + Path: path.Root("test"), |
| 76 | + PathExpression: path.MatchRoot("test"), |
| 77 | + ConfigValue: test.val, |
| 78 | + } |
| 79 | + response := validator.ListResponse{} |
| 80 | + listvalidator.NoNullValues().ValidateList(context.TODO(), request, &response) |
| 81 | + |
| 82 | + if !response.Diagnostics.HasError() && test.expectError { |
| 83 | + t.Fatal("expected error, got no error") |
| 84 | + } |
| 85 | + |
| 86 | + if response.Diagnostics.HasError() && !test.expectError { |
| 87 | + t.Fatalf("got unexpected error: %s", response.Diagnostics) |
| 88 | + } |
| 89 | + }) |
| 90 | + |
| 91 | + t.Run(fmt.Sprintf("ValidateParameterList - %s", name), func(t *testing.T) { |
| 92 | + t.Parallel() |
| 93 | + request := function.ListParameterValidatorRequest{ |
| 94 | + ArgumentPosition: 0, |
| 95 | + Value: test.val, |
| 96 | + } |
| 97 | + response := function.ListParameterValidatorResponse{} |
| 98 | + listvalidator.NoNullValues().ValidateParameterList(context.TODO(), request, &response) |
| 99 | + |
| 100 | + if response.Error == nil && test.expectError { |
| 101 | + t.Fatal("expected error, got no error") |
| 102 | + } |
| 103 | + |
| 104 | + if response.Error != nil && !test.expectError { |
| 105 | + t.Fatalf("got unexpected error: %s", response.Error) |
| 106 | + } |
| 107 | + }) |
| 108 | + } |
| 109 | +} |
0 commit comments