From c88fd109e749a76aa75a702f9d6f43cb6346e930 Mon Sep 17 00:00:00 2001 From: Bruno Schaatsbergen Date: Fri, 3 Jan 2025 16:00:14 +0100 Subject: [PATCH] test: improve compound validation --- internal/provider/not_null_function_test.go | 26 ++++++++------------- internal/provider/null_function_test.go | 23 +++++++----------- 2 files changed, 19 insertions(+), 30 deletions(-) diff --git a/internal/provider/not_null_function_test.go b/internal/provider/not_null_function_test.go index 2c7aabf..36437ef 100644 --- a/internal/provider/not_null_function_test.go +++ b/internal/provider/not_null_function_test.go @@ -225,39 +225,33 @@ func TestNotNullFunction_compoundValidation(t *testing.T) { t.Parallel() resource.UnitTest(t, resource.TestCase{ TerraformVersionChecks: []tfversion.TerraformVersionCheck{ - tfversion.SkipBelow(version.Must(version.NewVersion(MinimalRequiredTerraformVersion))), + tfversion.SkipBelow(version.Must(version.NewVersion("1.2.0"))), }, ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, Steps: []resource.TestStep{ { Config: ` -variable "ipv4_ipam_pool_id" { +variable "example_value_a" { default = null - description = "ID of the IPv4 IPAM pool to use for the VPC." + description = "Example input A for validation." type = string } - -variable "cidr_block" { +variable "example_value_b" { default = null - description = "CIDR block for the VPC." + description = "Example input B for validation." type = string - - validation { - condition = provider::assert::cidr(var.cidr_block) - error_message = "CIDR block must be a valid CIDR range." - } - validation { condition = anytrue([ - provider::assert::not_null(var.cidr_block), - provider::assert::not_null(var.ipv4_ipam_pool_id) + provider::assert::not_null(var.example_value_a), + provider::assert::not_null(var.example_value_b) + ]) - error_message = "Exactly one of cidr_block or ipv4_ipam_pool_id must be provided." + error_message = "At least one of example_value_a or example_value_b must be provided." } } `, ConfigVariables: config.Variables{ - "cidr_block": config.StringVariable("10.0.42.0/24"), + "example_value_b": config.StringVariable("example-format-value"), }, Check: resource.ComposeAggregateTestCheckFunc(), }, diff --git a/internal/provider/null_function_test.go b/internal/provider/null_function_test.go index 1a1347e..bc35576 100644 --- a/internal/provider/null_function_test.go +++ b/internal/provider/null_function_test.go @@ -76,39 +76,34 @@ func TestNullFunction_compoundValidation(t *testing.T) { t.Parallel() resource.UnitTest(t, resource.TestCase{ TerraformVersionChecks: []tfversion.TerraformVersionCheck{ - tfversion.SkipBelow(version.Must(version.NewVersion(MinimalRequiredTerraformVersion))), + tfversion.SkipBelow(version.Must(version.NewVersion("1.2.0"))), }, ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, Steps: []resource.TestStep{ { Config: ` -variable "ipv4_ipam_pool_id" { +variable "example_value_a" { default = null - description = "ID of the IPv4 IPAM pool to use for the VPC." + description = "Example option A for testing null validation." type = string } -variable "cidr_block" { +variable "example_value_b" { default = null - description = "CIDR block for the VPC." + description = "Example option B for testing null validation." type = string - validation { - condition = provider::assert::cidr(var.cidr_block) - error_message = "CIDR block must be a valid CIDR range." - } - validation { condition = anytrue([ - !provider::assert::null(var.cidr_block), - !provider::assert::null(var.ipv4_ipam_pool_id) + !provider::assert::null(var.example_value_a), + !provider::assert::null(var.example_value_b) ]) - error_message = "Exactly one of cidr_block or ipv4_ipam_pool_id must be provided." + error_message = "Exactly one of example_value_a or example_value_b must be provided." } } `, ConfigVariables: config.Variables{ - "cidr_block": config.StringVariable("10.0.42.0/24"), + "example_value_b": config.StringVariable("example-value"), }, Check: resource.ComposeAggregateTestCheckFunc(), },