-
Notifications
You must be signed in to change notification settings - Fork 54
/
schema_loader_test.go
116 lines (93 loc) · 2.73 KB
/
schema_loader_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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package jsonschema_test
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"os"
"strings"
"testing"
"github.com/qri-io/jsonschema"
)
func createTestServer() *httptest.Server {
validSchema := `{
"type": "string"
}`
invalidSchema := "invalid_schema"
return httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/valid_schema.json":
fmt.Fprintln(w, validSchema)
default:
fmt.Fprintln(w, invalidSchema)
}
}))
}
func TestFetchSchema(t *testing.T) {
ts := createTestServer()
defer ts.Close()
wd, err := os.Getwd()
if err != nil {
t.Errorf("failed to get working dir: %q", err)
}
cases := []struct {
uri string
expectsError bool
message string
}{
{fmt.Sprintf("%s/valid_schema.json", ts.URL), false, ""},
{fmt.Sprintf("%s/invalid_schema.json", ts.URL), true, "invalid character"},
{fmt.Sprintf("file://%s/testdata/draft-07_schema.json", wd), false, ""},
{fmt.Sprintf("file://%s/testdata/missing_file.json", wd), true, "no such file or directory"},
{"unknownscheme://resource.json#definitions/property", true, "unknownscheme is not supported for uri"},
}
for i, c := range cases {
rs := &jsonschema.Schema{}
err := jsonschema.FetchSchema(context.Background(), c.uri, rs)
if !c.expectsError && err == nil {
continue
}
if c.expectsError {
if err == nil {
t.Errorf("case %d expected an error", i)
continue
}
if !strings.Contains(err.Error(), c.message) {
t.Errorf("case %d expected error to include %q actual: %q", i, c.message, err.Error())
continue
}
} else if err != nil {
t.Errorf("case %d unexpected error: %s", i, err)
continue
}
}
}
func TestCustomSchemaLoader(t *testing.T) {
lr := jsonschema.GetSchemaLoaderRegistry()
lr.Register("special", func(ctx context.Context, uri *url.URL, schema *jsonschema.Schema) error {
path := uri.Host + uri.Path
body := fmt.Sprintf(`{ "type": "string", "description": "example description for '%s'"}`, path)
if schema == nil {
schema = &jsonschema.Schema{}
}
return json.Unmarshal([]byte(body), schema)
})
resourceURI := "special://schema_name"
rs := &jsonschema.Schema{}
err := jsonschema.FetchSchema(context.Background(), resourceURI, rs)
if err != nil {
t.Errorf("failed to load schema: %s", err)
return
}
if rs.TopLevelType() != "string" {
t.Errorf("expected schema top level type to be %q, actual: %q", "string", rs.TopLevelType())
}
expectedDesc := "example description for 'schema_name'"
actualDesc := string(*rs.JSONProp("description").(*jsonschema.Description))
if actualDesc != expectedDesc {
t.Errorf("expected 'description' to be %q, actual: %v", expectedDesc, actualDesc)
}
}