From 5baf19be81f20332dbdab44bf0ff4c0093128e4b Mon Sep 17 00:00:00 2001 From: Matt White <16320656+matt-FFFFFF@users.noreply.github.com> Date: Fri, 12 May 2023 14:10:34 +0100 Subject: [PATCH] feat: contains (#21) * feat: contains * test: add additional tests * test: remove interface --- check/key.go | 29 +++++++++++++++++ check/key_test.go | 79 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) diff --git a/check/key.go b/check/key.go index e08a7a2..cf276f3 100644 --- a/check/key.go +++ b/check/key.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "reflect" + "strings" "github.com/Azure/terratest-terraform-fluent/testerror" "github.com/gruntwork-io/terratest/modules/terraform" @@ -50,6 +51,34 @@ func (twk ThatTypeWithKey) HasValue(expected any) *testerror.Error { return nil } +// ContainsString returns a *testerror.Error if the resource does not exist in the plan or if the value of the key does not contain the +// expected string +func (twk ThatTypeWithKey) ContainsString(expected string) *testerror.Error { + if err := twk.Exists(); err != nil { + return err + } + + resource := twk.Plan.ResourcePlannedValuesMap[twk.ResourceName] + actual := resource.AttributeValues[twk.Key] + + actualString, ok := actual.(string) + if !ok { + return testerror.Newf("Cannot convert value to string: %s.%s", twk.ResourceName, twk.Key) + } + + if !strings.Contains(actualString, expected) { + return testerror.Newf( + "%s: attribute %s, planned value %s does not contain assertion %s", + twk.ResourceName, + twk.Key, + actual, + expected, + ) + } + + return nil +} + // ContainsJsonValue returns a *testerror.Error which asserts upon a given JSON string set into // the State by deserializing it and then asserting on it via the JsonAssertionFunc. func (twk ThatTypeWithKey) ContainsJsonValue(assertion JsonAssertionFunc) *testerror.Error { diff --git a/check/key_test.go b/check/key_test.go index ceda779..f8edf4b 100644 --- a/check/key_test.go +++ b/check/key_test.go @@ -9,6 +9,8 @@ import ( "github.com/Azure/terratest-terraform-fluent/setuptest" "github.com/Azure/terratest-terraform-fluent/to" + "github.com/gruntwork-io/terratest/modules/terraform" + tfjson "github.com/hashicorp/terraform-json" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -252,3 +254,80 @@ func TestIsFunctionNot(t *testing.T) { var s *string assert.False(t, isFunction(s)) } + +func TestContainsString(t *testing.T) { + t.Parallel() + + // Create a mock ThatTypeWithKey object + twk := ThatTypeWithKey{ + Plan: &terraform.PlanStruct{}, + ResourceName: "test_resource", + Key: "test_key", + } + + // Set the planned value for the key + twk.Plan.ResourcePlannedValuesMap = map[string]*tfjson.StateResource{ + "test_resource": { + AttributeValues: map[string]interface{}{ + "test_key": "this is a test string", + }, + }, + } + + // Test that the function returns nil when the expected string is contained in the actual string + err := twk.ContainsString("test") + assert.Nil(t, err.AsError()) + + // Test that the function returns an error when the expected string is not contained in the actual string + err = twk.ContainsString("not in string") + assert.NotNil(t, err.AsError()) + assert.Contains(t, err.Error(), "does not contain assertion") +} + +func TestContainsStringNotAString(t *testing.T) { + t.Parallel() + + // Create a mock ThatTypeWithKey object + twk := ThatTypeWithKey{ + Plan: &terraform.PlanStruct{}, + ResourceName: "test_resource", + Key: "test_key", + } + + // Set the planned value for the key + twk.Plan.ResourcePlannedValuesMap = map[string]*tfjson.StateResource{ + "test_resource": { + AttributeValues: map[string]interface{}{ + "test_key": interface{}(nil), + }, + }, + } + + // Test that the function returns expected error if string conversion is not possible + err := twk.ContainsString("test") + assert.ErrorContains(t, err, "Cannot convert value to string") +} + +func TestContainsStringKeyNotExists(t *testing.T) { + t.Parallel() + + // Create a mock ThatTypeWithKey object + twk := ThatTypeWithKey{ + Plan: &terraform.PlanStruct{}, + ResourceName: "test_resource", + Key: "not_exists", + } + + // Set the planned value for the key + twk.Plan.ResourcePlannedValuesMap = map[string]*tfjson.StateResource{ + "test_resource": { + AttributeValues: map[string]interface{}{ + "test_key": "this is a test string", + }, + }, + } + + // Test that the function returns expected error if string conversion is not possible + err := twk.ContainsString("test") + assert.ErrorContains(t, err, "key not_exists not found in resource") +}