-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathschema_test.go
67 lines (58 loc) · 1.73 KB
/
schema_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package jsonschema
import (
"testing"
"github.com/test-go/testify/assert"
)
func TestGetRootSchema(t *testing.T) {
compiler := NewCompiler()
root := &Schema{ID: "root"}
child := &Schema{ID: "child"}
grandChild := &Schema{ID: "grandChild"}
child.initializeSchema(compiler, root)
grandChild.initializeSchema(compiler, child)
if grandChild.getRootSchema().ID != "root" {
t.Errorf("Expected root schema ID to be 'root', got '%s'", grandChild.getRootSchema().ID)
}
}
func TestSchemaInitialization(t *testing.T) {
compiler := NewCompiler().SetDefaultBaseURI("http://default.com/")
tests := []struct {
name string
id string
expectedID string
expectedURI string
expectedBaseURI string
}{
{
name: "Schema with absolute $id",
id: "http://example.com/schema",
expectedID: "http://example.com/schema",
expectedURI: "http://example.com/schema",
expectedBaseURI: "http://example.com/",
},
{
name: "Schema with relative $id",
id: "schema",
expectedID: "schema",
expectedURI: "http://default.com/schema",
expectedBaseURI: "http://default.com/",
},
{
name: "Schema without $id",
id: "",
expectedID: "",
expectedURI: "",
expectedBaseURI: "http://default.com/",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
schemaJSON := createTestSchemaJSON(tt.id, map[string]string{"name": "string"}, []string{"name"})
schema, err := compiler.Compile([]byte(schemaJSON))
assert.NoError(t, err)
assert.Equal(t, tt.expectedID, schema.ID)
assert.Equal(t, tt.expectedURI, schema.uri)
assert.Equal(t, tt.expectedBaseURI, schema.baseURI)
})
}
}