-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8383e2d
Showing
7 changed files
with
281 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
Copyright (c) 2022, Cristian Maglie. | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions | ||
are met: | ||
|
||
1. Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
|
||
2. Redistributions in binary form must reproduce the above copyright | ||
notice, this list of conditions and the following disclaimer in | ||
the documentation and/or other materials provided with the | ||
distribution. | ||
|
||
3. Neither the name of the copyright holder nor the names of its | ||
contributors may be used to endorse or promote products derived | ||
from this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
POSSIBILITY OF SUCH DAMAGE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# `go.bug.st/testifyjson/requirejson` - unit-test JSON output in golang. | ||
|
||
Package testifyjson is a collection of utilities and helper function for unit testing | ||
JSON output in golang. | ||
|
||
It is based on the excellent libraries `github.com/itchyny/gojq` and `github.com/stretchr/testify`. It provides an interface similar to `testify` but with the powerful methods available in `gojq`. | ||
|
||
## Examples | ||
|
||
```go | ||
import ( | ||
"testing" | ||
"go.bug.st/testifyjson/requirejson" | ||
) | ||
|
||
func TestJSONQuery(t *testing.T) { | ||
in := []byte(` | ||
{ | ||
"id" : 1, | ||
"list" : [ | ||
10, 20, 30 | ||
], | ||
"emptylist" : [] | ||
} | ||
`) | ||
requirejson.Query(t, in, ".list", "[10, 20, 30]") | ||
requirejson.Query(t, in, ".list.[1]", "20") | ||
|
||
requirejson.Contains(t, in, `{ "list": [ 30 ] }`) | ||
requirejson.NotContains(t, in, `{ "list": [ 50 ] }`) | ||
|
||
in2 := []byte(`[ ]`) | ||
requirejson.Empty(t, in2) | ||
requirejson.Len(t, in2, 0) | ||
|
||
in3 := []byte(`[ 10, 20, 30 ]`) | ||
requirejson.NotEmpty(t, in3) | ||
requirejson.Len(t, in3, 3) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// | ||
// Copyright 2022 Cristian Maglie. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
// | ||
|
||
package testifyjson |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module go.bug.st/testifyjson | ||
|
||
go 1.19 | ||
|
||
require ( | ||
github.com/itchyny/gojq v0.12.8 | ||
github.com/stretchr/testify v1.8.0 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/itchyny/timefmt-go v0.1.3 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= | ||
github.com/itchyny/gojq v0.12.8 h1:Zxcwq8w4IeR8JJYEtoG2MWJZUv0RGY6QqJcO1cqV8+A= | ||
github.com/itchyny/gojq v0.12.8/go.mod h1:gE2kZ9fVRU0+JAksaTzjIlgnCa2akU+a1V0WXgJQN5c= | ||
github.com/itchyny/timefmt-go v0.1.3 h1:7M3LGVDsqcd0VZH2U+x393obrzZisp7C0uEe921iRkU= | ||
github.com/itchyny/timefmt-go v0.1.3/go.mod h1:0osSSCQSASBJMsIZnhAaF1C2fCBTJZXrnj37mG8/c+A= | ||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= | ||
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= | ||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= | ||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= | ||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// | ||
// Copyright 2022 Cristian Maglie. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
// | ||
|
||
package requirejson | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/itchyny/gojq" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// Query performs a test on a given json output. A jq-like query is performed | ||
// on the given jsonData and the result is compared with the expected output. | ||
// If the output doesn't match the test fails. If msgAndArgs are provided they | ||
// will be used to explain the error. | ||
func Query(t *testing.T, jsonData []byte, jqQuery string, jsonExpected string, msgAndArgs ...interface{}) { | ||
var data interface{} | ||
require.NoError(t, json.Unmarshal(jsonData, &data)) | ||
var expected interface{} | ||
require.NoError(t, json.Unmarshal([]byte(jsonExpected), &expected)) | ||
q, err := gojq.Parse(jqQuery) | ||
require.NoError(t, err) | ||
i := q.Run(data) | ||
v, ok := i.Next() | ||
require.True(t, ok) | ||
require.IsType(t, expected, v) | ||
require.Equal(t, expected, v, msgAndArgs...) | ||
} | ||
|
||
// Contains check if the json object is a subset of the jsonData. | ||
// If the output doesn't match the test fails. If msgAndArgs are provided they | ||
// will be used to explain the error. | ||
func Contains(t *testing.T, jsonData []byte, jsonObject string, msgAndArgs ...interface{}) { | ||
var data interface{} | ||
require.NoError(t, json.Unmarshal(jsonData, &data)) | ||
q, err := gojq.Parse("contains(" + jsonObject + ")") | ||
require.NoError(t, err) | ||
i := q.Run(data) | ||
v, ok := i.Next() | ||
require.True(t, ok) | ||
require.IsType(t, true, v) | ||
if !v.(bool) { | ||
msg := fmt.Sprintf("json data does not contain: %s", jsonObject) | ||
require.FailNow(t, msg, msgAndArgs...) | ||
} | ||
} | ||
|
||
// NotContains check if the json object is NOT a subset of the jsonData. | ||
// If the output match the test fails. If msgAndArgs are provided they | ||
// will be used to explain the error. | ||
func NotContains(t *testing.T, jsonData []byte, jsonObject string, msgAndArgs ...interface{}) { | ||
var data interface{} | ||
require.NoError(t, json.Unmarshal(jsonData, &data)) | ||
q, err := gojq.Parse("contains(" + jsonObject + ")") | ||
require.NoError(t, err) | ||
i := q.Run(data) | ||
v, ok := i.Next() | ||
require.True(t, ok) | ||
require.IsType(t, true, v) | ||
if v.(bool) { | ||
msg := fmt.Sprintf("json data contains: %s", jsonObject) | ||
require.FailNow(t, msg, msgAndArgs...) | ||
} | ||
} | ||
|
||
// Len check if the size of the json object match the given value. | ||
// If the lenght doesn't match the test fails. If msgAndArgs are provided they | ||
// will be used to explain the error. | ||
func Len(t *testing.T, jsonData []byte, expectedLen int, msgAndArgs ...interface{}) { | ||
var data interface{} | ||
require.NoError(t, json.Unmarshal(jsonData, &data)) | ||
q, err := gojq.Parse("length") | ||
require.NoError(t, err) | ||
i := q.Run(data) | ||
v, ok := i.Next() | ||
require.True(t, ok) | ||
require.IsType(t, expectedLen, v) | ||
if v.(int) != expectedLen { | ||
msg := fmt.Sprintf("json data length does not match: expected=%d, actual=%d", expectedLen, v.(int)) | ||
require.FailNow(t, msg, msgAndArgs...) | ||
} | ||
} | ||
|
||
// Empty check if the size of the json object is zero. | ||
// If the lenght is not zero the test fails. If msgAndArgs are provided they | ||
// will be used to explain the error. | ||
func Empty(t *testing.T, jsonData []byte, msgAndArgs ...interface{}) { | ||
var data interface{} | ||
require.NoError(t, json.Unmarshal(jsonData, &data)) | ||
q, err := gojq.Parse("length") | ||
require.NoError(t, err) | ||
i := q.Run(data) | ||
v, ok := i.Next() | ||
require.True(t, ok) | ||
require.IsType(t, 0, v) | ||
if v.(int) != 0 { | ||
require.FailNow(t, "json data is not empty", msgAndArgs...) | ||
} | ||
} | ||
|
||
// NotEmpty check if the size of the json object is greater than zero. | ||
// If the lenght is not greater than zero the test fails. If msgAndArgs are provided they | ||
// will be used to explain the error. | ||
func NotEmpty(t *testing.T, jsonData []byte, msgAndArgs ...interface{}) { | ||
var data interface{} | ||
require.NoError(t, json.Unmarshal(jsonData, &data)) | ||
q, err := gojq.Parse("length") | ||
require.NoError(t, err) | ||
i := q.Run(data) | ||
v, ok := i.Next() | ||
require.True(t, ok) | ||
require.IsType(t, 0, v) | ||
if v.(int) == 0 { | ||
require.FailNow(t, "json data is empty", msgAndArgs...) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// Copyright 2022 Cristian Maglie. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
// | ||
|
||
package requirejson_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"go.bug.st/testifyjson/requirejson" | ||
) | ||
|
||
func TestJSONQuery(t *testing.T) { | ||
in := []byte(` | ||
{ | ||
"id" : 1, | ||
"list" : [ | ||
10, 20, 30 | ||
], | ||
"emptylist" : [] | ||
} | ||
`) | ||
requirejson.Query(t, in, ".list", "[10, 20, 30]") | ||
requirejson.Query(t, in, ".list.[1]", "20") | ||
|
||
requirejson.Contains(t, in, `{ "list": [ 30 ] }`) | ||
requirejson.NotContains(t, in, `{ "list": [ 50 ] }`) | ||
|
||
in2 := []byte(`[ ]`) | ||
requirejson.Empty(t, in2) | ||
requirejson.Len(t, in2, 0) | ||
|
||
in3 := []byte(`[ 10, 20, 30 ]`) | ||
requirejson.NotEmpty(t, in3) | ||
requirejson.Len(t, in3, 3) | ||
} |