Skip to content

Commit 3b714db

Browse files
committed
chore: auto generate consensus tests readme
test readme is not generated when tests ran locally updatate consensus readme to show output in json format
1 parent a0967af commit 3b714db

File tree

8 files changed

+2097
-2041
lines changed

8 files changed

+2097
-2041
lines changed

test/README.md

Lines changed: 184 additions & 185 deletions
Large diffs are not rendered by default.

test/array_test.go

Lines changed: 295 additions & 297 deletions
Large diffs are not rendered by default.

test/bracket_test.go

Lines changed: 341 additions & 343 deletions
Large diffs are not rendered by default.

test/dot_test.go

Lines changed: 366 additions & 367 deletions
Large diffs are not rendered by default.

test/filter_test.go

Lines changed: 656 additions & 658 deletions
Large diffs are not rendered by default.

test/helper_test.go

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
package test
44

55
import (
6+
"bufio"
7+
"encoding/json"
68
"fmt"
9+
"io"
10+
"os"
711
"reflect"
812
"testing"
913

@@ -44,30 +48,94 @@ func batchTest(t *testing.T, tests []testData) {
4448
}
4549
}
4650

47-
func printConsensusMatrix(tests []testData) {
48-
fmt.Println("|query|data|consensus|actual|match|")
49-
fmt.Println("|---|---|---|---|---|")
51+
func Test_generateReadme(t *testing.T) {
52+
53+
header := `# cburgmer tests
54+
55+
This test package has tests detailed by https://cburgmer.github.io/json-path-comparison/ comparison matrix which details the community consensus on the expected response from multiple JSONPath queries
56+
57+
This implementation would be closer to the 'Scalar consensus' as it does not always return an array, but instead can return a single item when that is expected.
58+
`
59+
60+
type section struct {
61+
title string
62+
testData []testData
63+
}
64+
65+
sections := []section{
66+
{
67+
title: "Array Test",
68+
testData: arrayTests,
69+
},
70+
{
71+
title: "Bracket Test",
72+
testData: bracketTests,
73+
},
74+
{
75+
title: "Dot Test",
76+
testData: dotTests,
77+
},
78+
{
79+
title: "Filter Test",
80+
testData: filterTests,
81+
},
82+
{
83+
title: "Misc Test",
84+
testData: miscTests,
85+
},
86+
{
87+
title: "Union Test",
88+
testData: unionTests,
89+
},
90+
}
91+
92+
file, err := os.OpenFile("README.md", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
93+
if err != nil {
94+
t.FailNow()
95+
}
96+
defer file.Close()
97+
98+
writer := bufio.NewWriter(file)
99+
defer writer.Flush()
100+
101+
fmt.Fprintf(writer, "%s\n", header)
102+
103+
for _, section := range sections {
104+
fmt.Fprintf(writer, "\n## %s\n\n", section.title)
105+
printConsensusMatrix(writer, section.testData)
106+
}
107+
}
108+
109+
func printConsensusMatrix(writer io.Writer, tests []testData) {
110+
fmt.Fprintf(writer, "|query|data|consensus|actual|match|\n")
111+
fmt.Fprintf(writer, "|---|---|---|---|---|\n")
50112
for _, test := range tests {
51113
expected := test.expected
52114
if expected == nil {
53-
expected = "nil"
115+
expected = "null"
116+
} else {
117+
bytes, _ := json.Marshal(expected)
118+
expected = string(bytes)
54119
}
55120

56121
if test.consensus == consensusNone {
57-
fmt.Printf("|`%s`|`%v`|%s|`%v`|%s|\n", test.query, test.data, "none", expected, ":question:")
122+
fmt.Fprintf(writer, "|`%s`|`%v`|%s|`%v`|%s|\n", test.query, test.data, "none", expected, ":question:")
58123
continue
59124
}
60125

61126
consensus := test.consensus
62127
if consensus == nil {
63128
consensus = "nil"
129+
} else if consensus != consensusNone {
130+
bytes, _ := json.Marshal(consensus)
131+
consensus = string(bytes)
64132
}
65133

66134
symbol := ":no_entry:"
67135
if isEqual := reflect.DeepEqual(test.consensus, test.expected); isEqual {
68136
symbol = ":white_check_mark:"
69137
}
70138

71-
fmt.Printf("|`%s`|`%v`|`%v`|`%v`|%s|\n", test.query, test.data, consensus, expected, symbol)
139+
fmt.Fprintf(writer, "|`%s`|`%v`|`%v`|`%v`|%s|\n", test.query, test.data, consensus, expected, symbol)
72140
}
73141
}

test/misc_test.go

Lines changed: 73 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,80 +3,78 @@ package test
33
import "testing"
44

55
func Test_Misc(t *testing.T) {
6+
batchTest(t, miscTests)
7+
}
68

7-
tests := []testData{
8-
{
9-
query: "", // empty
10-
data: `{"a": 42, "": 21}`,
11-
expected: nil,
12-
consensus: nil,
13-
expectedError: "invalid JSONPath query '' unexpected token '' at index 0",
14-
},
15-
{
16-
query: `$.data.sum()`,
17-
data: `{"data": [1,2,3,4]}`,
18-
expected: nil,
19-
consensus: consensusNone,
20-
expectedError: "key: invalid token target. expected [map] got [slice]",
21-
},
22-
{
23-
query: `$(key,more)`,
24-
data: `{"key": 1, "some": 2, "more": 3}`,
25-
expected: nil,
26-
consensus: nil,
27-
expectedError: "invalid JSONPath query '$(key,more)' unexpected token '(' at index 1",
28-
},
29-
{
30-
query: `$..`,
31-
data: `[{"a": {"b": "c"}}, [0, 1]]`,
32-
expected: []interface{}{[]interface{}{map[string]interface{}{"a": map[string]interface{}{"b": "c"}}, []interface{}{float64(0), float64(1)}}, map[string]interface{}{"a": map[string]interface{}{"b": "c"}}, map[string]interface{}{"b": "c"}, "c", []interface{}{float64(0), float64(1)}, float64(0), float64(1)},
33-
consensus: consensusNone,
34-
expectedError: "",
35-
},
36-
{
37-
query: `$.key..`,
38-
data: `{"some key": "value", "key": {"complex": "string", "primitives": [0, 1]}}`,
39-
expected: []interface{}{map[string]interface{}{"complex": "string", "primitives": []interface{}{float64(0), float64(1)}}, []interface{}{float64(0), float64(1)}, float64(0), float64(1), "string"},
40-
consensus: consensusNone,
41-
expectedError: "",
42-
},
43-
{
44-
query: `$`,
45-
data: `{ "key": "value", "another key": { "complex": [ "a", 1 ] } }`,
46-
expected: map[string]interface{}{"key": "value", "another key": map[string]interface{}{"complex": []interface{}{"a", float64(1)}}},
47-
consensus: map[string]interface{}{"key": "value", "another key": map[string]interface{}{"complex": []interface{}{"a", float64(1)}}},
48-
expectedError: "",
49-
},
50-
{
51-
query: `$`,
52-
data: `42`,
53-
expected: int64(42),
54-
consensus: int64(42),
55-
expectedError: "",
56-
},
57-
{
58-
query: `$`,
59-
data: `false`,
60-
expected: false,
61-
consensus: false,
62-
expectedError: "",
63-
},
64-
{
65-
query: `$`,
66-
data: `true`,
67-
expected: true,
68-
consensus: true,
69-
expectedError: "",
70-
},
71-
{
72-
query: `$[(@.length-1)]`,
73-
data: `["first", "second", "third", "forth", "fifth"]`,
74-
expected: "fifth",
75-
consensus: nil,
76-
expectedError: "",
77-
},
78-
}
79-
80-
batchTest(t, tests)
81-
// printConsensusMatrix(tests)
9+
var miscTests []testData = []testData{
10+
{
11+
query: "", // empty
12+
data: `{"a": 42, "": 21}`,
13+
expected: nil,
14+
consensus: nil,
15+
expectedError: "invalid JSONPath query '' unexpected token '' at index 0",
16+
},
17+
{
18+
query: `$.data.sum()`,
19+
data: `{"data": [1,2,3,4]}`,
20+
expected: nil,
21+
consensus: consensusNone,
22+
expectedError: "key: invalid token target. expected [map] got [slice]",
23+
},
24+
{
25+
query: `$(key,more)`,
26+
data: `{"key": 1, "some": 2, "more": 3}`,
27+
expected: nil,
28+
consensus: nil,
29+
expectedError: "invalid JSONPath query '$(key,more)' unexpected token '(' at index 1",
30+
},
31+
{
32+
query: `$..`,
33+
data: `[{"a": {"b": "c"}}, [0, 1]]`,
34+
expected: []interface{}{[]interface{}{map[string]interface{}{"a": map[string]interface{}{"b": "c"}}, []interface{}{float64(0), float64(1)}}, map[string]interface{}{"a": map[string]interface{}{"b": "c"}}, map[string]interface{}{"b": "c"}, "c", []interface{}{float64(0), float64(1)}, float64(0), float64(1)},
35+
consensus: consensusNone,
36+
expectedError: "",
37+
},
38+
{
39+
query: `$.key..`,
40+
data: `{"some key": "value", "key": {"complex": "string", "primitives": [0, 1]}}`,
41+
expected: []interface{}{map[string]interface{}{"complex": "string", "primitives": []interface{}{float64(0), float64(1)}}, []interface{}{float64(0), float64(1)}, float64(0), float64(1), "string"},
42+
consensus: consensusNone,
43+
expectedError: "",
44+
},
45+
{
46+
query: `$`,
47+
data: `{ "key": "value", "another key": { "complex": [ "a", 1 ] } }`,
48+
expected: map[string]interface{}{"key": "value", "another key": map[string]interface{}{"complex": []interface{}{"a", float64(1)}}},
49+
consensus: map[string]interface{}{"key": "value", "another key": map[string]interface{}{"complex": []interface{}{"a", float64(1)}}},
50+
expectedError: "",
51+
},
52+
{
53+
query: `$`,
54+
data: `42`,
55+
expected: int64(42),
56+
consensus: int64(42),
57+
expectedError: "",
58+
},
59+
{
60+
query: `$`,
61+
data: `false`,
62+
expected: false,
63+
consensus: false,
64+
expectedError: "",
65+
},
66+
{
67+
query: `$`,
68+
data: `true`,
69+
expected: true,
70+
consensus: true,
71+
expectedError: "",
72+
},
73+
{
74+
query: `$[(@.length-1)]`,
75+
data: `["first", "second", "third", "forth", "fifth"]`,
76+
expected: "fifth",
77+
consensus: nil,
78+
expectedError: "",
79+
},
8280
}

0 commit comments

Comments
 (0)