Skip to content

Commit

Permalink
[Internal] make ApplyAndExpectData work with nested set
Browse files Browse the repository at this point in the history
  • Loading branch information
nkvuong committed Nov 16, 2024
1 parent 27ff289 commit 470fdfa
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
8 changes: 5 additions & 3 deletions qa/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"reflect"
"regexp"
"sort"
"strings"
Expand Down Expand Up @@ -390,9 +391,10 @@ func (f ResourceFixture) ApplyAndExpectData(t *testing.T, data map[string]any) {
if k == "id" {
assert.Equal(t, expected, d.Id())
} else if that, ok := d.Get(k).(*schema.Set); ok {
this := expected.([]string)
assert.Equal(t, len(this), that.Len(), "set has different length")
for _, item := range this {
this := reflect.ValueOf(expected)
assert.Equal(t, this.Len(), that.Len(), "set has different length")
for i := 0; i < this.Len(); i++ {
item := this.Index(i).Interface()
assert.True(t, that.Contains(item), "set does not contain %s", item)
}
} else {
Expand Down
47 changes: 47 additions & 0 deletions qa/testing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,22 @@ var noopContextResource = common.Resource{
Type: schema.TypeBool,
Required: true,
},
"nested": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"tags": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
},
Read: noopContext,
Create: noopContext,
Expand Down Expand Up @@ -218,6 +234,37 @@ func TestResourceFixture_ApplyAndExpectData(t *testing.T) {
}.ApplyAndExpectData(t, map[string]any{"id": "x", "dummy": true, "trigger": "now"})
}

func TestResourceFixture_ApplyAndExpectDataSet(t *testing.T) {
ResourceFixture{
CommandMock: func(commandStr string) common.CommandResults {
return common.CommandResults{
ResultType: "text",
Data: "yes",
}
},
Azure: true,
Resource: noopContextResource,
ID: "x",
Delete: true,
HCL: `
dummy = true
trigger = "now"
nested {
tags {
env = "prod"
}
}
`,
}.ApplyAndExpectData(t,
map[string]any{
"id": "x",
"dummy": true,
"trigger": "now",
"nested": []any{map[string]any{"tags": map[string]any{"env": "prod"}}},
},
)
}

func TestResourceFixture_InstanceState(t *testing.T) {
ResourceFixture{
Resource: noopContextResource,
Expand Down

0 comments on commit 470fdfa

Please sign in to comment.