Skip to content

Commit

Permalink
feat: contains (#21)
Browse files Browse the repository at this point in the history
* feat: contains

* test: add additional tests

* test: remove interface
  • Loading branch information
matt-FFFFFF authored May 12, 2023
1 parent 37691db commit 5baf19b
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
29 changes: 29 additions & 0 deletions check/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"reflect"
"strings"

"github.com/Azure/terratest-terraform-fluent/testerror"
"github.com/gruntwork-io/terratest/modules/terraform"
Expand Down Expand Up @@ -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 {
Expand Down
79 changes: 79 additions & 0 deletions check/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
}

0 comments on commit 5baf19b

Please sign in to comment.