Skip to content

Commit

Permalink
feat(blueprint-test): add JSONPathEqs() (#2706)
Browse files Browse the repository at this point in the history
  • Loading branch information
apeabody authored Nov 21, 2024
1 parent 53eeec7 commit 3065ac7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
14 changes: 14 additions & 0 deletions infra/blueprint-test/pkg/golden/golden.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os"
"path"
"strings"
"sync"

"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/gcloud"
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils"
Expand Down Expand Up @@ -156,3 +157,16 @@ func (g *GoldenFile) JSONEq(a *assert.Assertions, got gjson.Result, jsonPath str
gfData := gf.Get(jsonPath).String()
a.Equal(gfData, gotData, fmt.Sprintf("expected %s to match fixture %s", jsonPath, gfData))
}

// JSONPathEqs asserts that json content in jsonPaths for got and goldenfile are the same
func (g *GoldenFile) JSONPathEqs(a *assert.Assertions, got gjson.Result, jsonPaths []string) {
var wg sync.WaitGroup
wg.Add(len(jsonPaths))
for _, path := range jsonPaths {
go func(a *assert.Assertions, got gjson.Result, path string) {
defer wg.Done()
g.JSONEq(a, got, path)
}(a, got, path)
}
wg.Wait()
}
42 changes: 42 additions & 0 deletions infra/blueprint-test/pkg/golden/golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/gcloud"
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils"
"github.com/stretchr/testify/assert"

gotest "github.com/mitchellh/go-testing-interface"
)

const testProjectID = "foo"
Expand Down Expand Up @@ -133,3 +135,43 @@ func TestJSONEq(t *testing.T) {
})
}
}

func TestJSONEqs(t *testing.T) {
tests := []struct {
name string
data string
eqPaths []string
opts []goldenFileOption
want string
hasError bool
}{
{
name: "simple",
data: "{\"foo\":\"bar\",\"baz\":{\"qux\":\"quz\"},\"fizz\":\"pop\"}",
eqPaths: []string{"foo","baz"},
want: "{\"foo\":\"bar\",\"baz\":{\"qux\":\"quz\"}}",
hasError: false,
},
{
name: "false",
data: "{\"foo\":\"bar\",\"baz\":{\"qux\":\"quz\"},\"fizz\":\"pop\"}",
eqPaths: []string{"foo","baz"},
want: "{\"foo\":\"bar\",\"baz\":{\"qux\":\"quz1\"}}",
hasError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
innerT := &gotest.RuntimeT{}
innerAssert := assert.New(innerT)
os.Setenv(gfUpdateEnvVar, "true")
defer os.Unsetenv(gfUpdateEnvVar)
got := NewOrUpdate(t, tt.data, tt.opts...)
defer os.Remove(got.GetName())
got.JSONPathEqs(innerAssert, utils.ParseJSONResult(t, tt.want), tt.eqPaths)

assert := assert.New(t)
assert.True(innerT.Failed() == tt.hasError)
})
}
}

0 comments on commit 3065ac7

Please sign in to comment.