Skip to content

Commit 52617eb

Browse files
committed
Add tests for schema package
1 parent 48e82ca commit 52617eb

8 files changed

+252
-1
lines changed

Diff for: .prettierignore

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
.vscode/
44
.vs/
55
.ionide/
6+
7+
# Test files
8+
/check/checkdata/schema/testdata/invalid-schema.json

Diff for: check/checkdata/schema/schema_test.go

+180-1
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,197 @@
11
package schema
22

33
import (
4+
"fmt"
5+
"os"
6+
"regexp"
47
"runtime"
58
"testing"
69

710
"github.com/arduino/go-paths-helper"
11+
"github.com/arduino/go-properties-orderedmap"
12+
"github.com/ory/jsonschema/v3"
813
"github.com/stretchr/testify/require"
914
)
1015

11-
func TestPathURI(t *testing.T) {
16+
var schemasPath *paths.Path
17+
18+
var validMap = map[string]string{
19+
"property1": "foo",
20+
"property2": "bar",
21+
}
22+
23+
var validPropertiesMap = properties.NewFromHashmap(validMap)
24+
25+
var validSchemaWithReferences *jsonschema.Schema
26+
27+
func init() {
28+
workingPath, _ := os.Getwd()
29+
schemasPath = paths.New(workingPath).Join("testdata")
30+
31+
validSchemaWithReferences = Compile(
32+
"valid-schema-with-references.json",
33+
[]string{
34+
"referenced-schema-1.json",
35+
"referenced-schema-2.json",
36+
},
37+
schemasPath,
38+
)
39+
}
40+
41+
func TestCompile(t *testing.T) {
42+
require.Panics(t, func() {
43+
Compile("valid-schema-with-references.json", []string{"nonexistent.json"}, schemasPath)
44+
})
45+
46+
require.Panics(t, func() {
47+
Compile("valid-schema-with-references.json", []string{"schema-without-id.json"}, schemasPath)
48+
})
49+
50+
require.Panics(t, func() {
51+
Compile("invalid-schema.json", []string{}, schemasPath)
52+
})
53+
54+
require.Panics(t, func() {
55+
Compile("valid-schema-with-references.json", []string{}, schemasPath)
56+
})
57+
58+
require.NotPanics(t, func() {
59+
Compile("valid-schema.json", []string{}, schemasPath)
60+
})
61+
62+
require.NotPanics(t, func() {
63+
Compile(
64+
"valid-schema-with-references.json",
65+
[]string{
66+
"referenced-schema-1.json",
67+
"referenced-schema-2.json",
68+
},
69+
schemasPath,
70+
)
71+
})
72+
}
73+
74+
func TestValidate(t *testing.T) {
75+
schemaObject := Compile("valid-schema.json", []string{}, schemasPath)
76+
propertiesMap := properties.NewFromHashmap(validMap)
77+
validationResult := Validate(propertiesMap, schemaObject, schemasPath)
78+
require.Nil(t, validationResult)
79+
80+
validationResult = Validate(propertiesMap, validSchemaWithReferences, schemasPath)
81+
require.Nil(t, validationResult)
82+
83+
propertiesMap.Set("property1", "a")
84+
validationResult = Validate(propertiesMap, schemaObject, schemasPath)
85+
require.Equal(t, "#/property1", validationResult.InstancePtr)
86+
require.Equal(t, "#/properties/property1/minLength", validationResult.SchemaPtr)
87+
}
88+
89+
func TestRequiredPropertyMissing(t *testing.T) {
90+
propertiesMap := properties.NewFromHashmap(validMap)
91+
validationResult := Validate(propertiesMap, validSchemaWithReferences, schemasPath)
92+
require.False(t, RequiredPropertyMissing("property1", validationResult, schemasPath))
93+
94+
propertiesMap.Remove("property1")
95+
validationResult = Validate(propertiesMap, validSchemaWithReferences, schemasPath)
96+
require.True(t, RequiredPropertyMissing("property1", validationResult, schemasPath))
97+
}
98+
99+
func TestPropertyPatternMismatch(t *testing.T) {
100+
propertiesMap := properties.NewFromHashmap(validMap)
101+
validationResult := Validate(propertiesMap, validSchemaWithReferences, schemasPath)
102+
require.False(t, PropertyPatternMismatch("property2", validationResult, schemasPath))
103+
104+
propertiesMap.Set("property2", "fOo")
105+
validationResult = Validate(propertiesMap, validSchemaWithReferences, schemasPath)
106+
require.True(t, PropertyPatternMismatch("property2", validationResult, schemasPath))
107+
108+
require.False(t, PropertyPatternMismatch("property1", validationResult, schemasPath))
109+
}
110+
111+
func TestValidationErrorMatch(t *testing.T) {
112+
propertiesMap := properties.NewFromHashmap(validMap)
113+
validationResult := Validate(propertiesMap, validSchemaWithReferences, schemasPath)
114+
require.False(t, ValidationErrorMatch("", "", "", "", validationResult, schemasPath))
115+
116+
propertiesMap.Set("property2", "fOo")
117+
validationResult = Validate(propertiesMap, validSchemaWithReferences, schemasPath)
118+
require.False(t, ValidationErrorMatch("nomatch", "nomatch", "nomatch", "nomatch", validationResult, schemasPath))
119+
require.False(t, ValidationErrorMatch("^#/property2$", "nomatch", "nomatch", "nomatch", validationResult, schemasPath))
120+
require.False(t, ValidationErrorMatch("^#/property2$", "/pattern$", "nomatch", "nomatch", validationResult, schemasPath))
121+
require.False(t, ValidationErrorMatch("^#/property2$", "/pattern$", `^\^\[a-z\]\+\$$`, "nomatch", validationResult, schemasPath))
122+
require.True(t, ValidationErrorMatch("^#/property2$", "/pattern$", `^"\^\[a-z\]\+\$"$`, "", validationResult, schemasPath))
123+
require.True(t, ValidationErrorMatch("", "", "", "", validationResult, schemasPath))
124+
125+
propertiesMap = properties.NewFromHashmap(validMap)
126+
propertiesMap.Remove("property1")
127+
validationResult = Validate(propertiesMap, validSchemaWithReferences, schemasPath)
128+
require.False(t, ValidationErrorMatch("nomatch", "nomatch", "nomatch", "nomatch", validationResult, schemasPath))
129+
require.True(t, ValidationErrorMatch("", "", "", "^#/property1$", validationResult, schemasPath))
130+
}
131+
132+
func Test_loadReferencedSchema(t *testing.T) {
133+
compiler := jsonschema.NewCompiler()
134+
135+
require.Error(t, loadReferencedSchema(compiler, "nonexistent.json", schemasPath))
136+
require.Error(t, loadReferencedSchema(compiler, "schema-without-id.json", schemasPath))
137+
require.Nil(t, loadReferencedSchema(compiler, "referenced-schema-2.json", schemasPath))
138+
}
139+
140+
func Test_schemaID(t *testing.T) {
141+
_, err := schemaID("schema-without-id.json", schemasPath)
142+
require.NotNil(t, err)
143+
144+
id, err := schemaID("valid-schema.json", schemasPath)
145+
require.Equal(t, "https://raw.githubusercontent.com/arduino/arduino-check/main/check/checkdata/schema/testdata/schema-with-references.json", id)
146+
require.Nil(t, err)
147+
}
148+
149+
func Test_compile(t *testing.T) {
150+
compiler := jsonschema.NewCompiler()
151+
_, err := compile(compiler, "invalid-schema.json", schemasPath)
152+
require.Error(t, err)
153+
154+
_, err = compile(compiler, "valid-schema.json", schemasPath)
155+
require.Nil(t, err)
156+
}
157+
158+
func Test_pathURI(t *testing.T) {
12159
switch runtime.GOOS {
13160
case "windows":
14161
require.Equal(t, "file:///c:/foo%20bar", pathURI(paths.New("c:/foo bar")))
15162
default:
16163
require.Equal(t, "file:///foo%20bar", pathURI(paths.New("/foo bar")))
17164
}
18165
}
166+
167+
func Test_schemaPointerValue(t *testing.T) {
168+
validationError := jsonschema.ValidationError{
169+
SchemaURL: "https://raw.githubusercontent.com/arduino/arduino-check/main/check/checkdata/schema/testdata/referenced-schema-1.json",
170+
SchemaPtr: "#/definitions/patternObject/pattern",
171+
}
172+
173+
schemaPointerValueInterface := schemaPointerValue(&validationError, schemasPath)
174+
fmt.Printf("%T", schemaPointerValueInterface)
175+
schemaPointerValue, ok := schemaPointerValueInterface.(string)
176+
require.True(t, ok)
177+
require.Equal(t, "^[a-z]+$", schemaPointerValue)
178+
}
179+
180+
func Test_validationErrorContextMatch(t *testing.T) {
181+
validationError := jsonschema.ValidationError{
182+
Context: nil,
183+
}
184+
185+
require.True(t, validationErrorContextMatch(regexp.MustCompile(".*"), &validationError))
186+
require.False(t, validationErrorContextMatch(regexp.MustCompile("foo"), &validationError))
187+
188+
validationError.Context = &jsonschema.ValidationErrorContextRequired{
189+
Missing: []string{
190+
"#/foo",
191+
"#/bar",
192+
},
193+
}
194+
195+
require.True(t, validationErrorContextMatch(regexp.MustCompile("^#/bar$"), &validationError))
196+
require.False(t, validationErrorContextMatch(regexp.MustCompile("nomatch"), &validationError))
197+
}

Diff for: check/checkdata/schema/testdata/invalid-schema.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"foo": "bar"
3+
"baz": "bat"
4+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"$id": "https://raw.githubusercontent.com/arduino/arduino-check/main/check/checkdata/schema/testdata/referenced-schema-1.json",
4+
"title": "Schema for use in unit tests",
5+
"definitions": {
6+
"patternObject": {
7+
"pattern": "^[a-z]+$"
8+
},
9+
"requiredObject": {
10+
"required": ["property1"]
11+
}
12+
}
13+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"$id": "https://raw.githubusercontent.com/arduino/arduino-check/main/check/checkdata/schema/testdata/referenced-schema-2.json",
4+
"title": "Schema for use in unit tests",
5+
"definitions": {
6+
"minLengthObject": {
7+
"minLength": 2
8+
}
9+
}
10+
}
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"title": "Schema without $id keyword for use in unit tests"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"$id": "https://raw.githubusercontent.com/arduino/arduino-check/main/check/checkdata/schema/testdata/schema-with-references.json",
4+
"title": "Schema for use in unit tests",
5+
"type": "object",
6+
"properties": {
7+
"property1": {
8+
"allOf": [
9+
{
10+
"$ref": "referenced-schema-2.json#/definitions/minLengthObject"
11+
}
12+
]
13+
},
14+
"property2": {
15+
"allOf": [
16+
{
17+
"$ref": "referenced-schema-1.json#/definitions/patternObject"
18+
}
19+
]
20+
}
21+
},
22+
"allOf": [
23+
{
24+
"$ref": "referenced-schema-1.json#/definitions/requiredObject"
25+
}
26+
]
27+
}

Diff for: check/checkdata/schema/testdata/valid-schema.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"$id": "https://raw.githubusercontent.com/arduino/arduino-check/main/check/checkdata/schema/testdata/schema-with-references.json",
4+
"title": "Schema for use in unit tests",
5+
"type": "object",
6+
"properties": {
7+
"property1": {
8+
"minLength": 2
9+
}
10+
}
11+
}

0 commit comments

Comments
 (0)