-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema_test.go
More file actions
189 lines (163 loc) · 4.46 KB
/
schema_test.go
File metadata and controls
189 lines (163 loc) · 4.46 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package augur
import (
"testing"
)
type simpleStruct struct {
Name string `json:"name" augur:"required,desc:Full name"`
Age int `json:"age" augur:"required,desc:Age in years"`
Score float64 `json:"score" augur:"desc:Score between 0 and 1"`
Active bool `json:"active"`
}
type nestedStruct struct {
Title string `json:"title" augur:"required"`
Tags []string `json:"tags" augur:"desc:List of tags"`
Metadata simpleStruct `json:"metadata"`
}
type withDefault struct {
Currency string `json:"currency" augur:"required,default:USD"`
Amount int `json:"amount" augur:"required"`
}
func TestSchemaFromType_SimpleStruct(t *testing.T) {
s, err := SchemaFromType[simpleStruct]()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
props := s.properties
if len(props) != 4 {
t.Errorf("expected 4 properties, got %d", len(props))
}
nameFS := props["name"]
if nameFS == nil {
t.Fatal("missing 'name' property")
}
if nameFS.Type != "string" {
t.Errorf("name type: got %q, want %q", nameFS.Type, "string")
}
if !nameFS.Required {
t.Error("name should be required")
}
if nameFS.Description != "Full name" {
t.Errorf("name description: got %q, want %q", nameFS.Description, "Full name")
}
ageFS := props["age"]
if ageFS == nil || ageFS.Type != "integer" {
t.Errorf("age type: got %v", ageFS)
}
scoreFS := props["score"]
if scoreFS == nil || scoreFS.Type != "number" {
t.Errorf("score type: got %v", scoreFS)
}
if scoreFS.Required {
t.Error("score should not be required")
}
activeFS := props["active"]
if activeFS == nil || activeFS.Type != "boolean" {
t.Errorf("active type: got %v", activeFS)
}
}
func TestSchemaFromType_RequiredFields(t *testing.T) {
s, err := SchemaFromType[simpleStruct]()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
required := s.RequiredFields()
requiredSet := make(map[string]bool)
for _, r := range required {
requiredSet[r] = true
}
if !requiredSet["name"] {
t.Error("expected 'name' in required fields")
}
if !requiredSet["age"] {
t.Error("expected 'age' in required fields")
}
if requiredSet["score"] {
t.Error("'score' should not be in required fields")
}
}
func TestSchemaFromType_NestedStruct(t *testing.T) {
s, err := SchemaFromType[nestedStruct]()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
props := s.properties
tagsFS := props["tags"]
if tagsFS == nil || tagsFS.Type != "array" {
t.Fatalf("tags type: got %v", tagsFS)
}
if tagsFS.Items == nil || tagsFS.Items.Type != "string" {
t.Errorf("tags items type: got %v", tagsFS.Items)
}
metaFS := props["metadata"]
if metaFS == nil || metaFS.Type != "object" {
t.Fatalf("metadata type: got %v", metaFS)
}
if metaFS.Properties == nil {
t.Fatal("metadata should have nested properties")
}
if metaFS.Properties["name"] == nil {
t.Error("metadata.name should exist")
}
}
func TestSchemaFromType_Default(t *testing.T) {
s, err := SchemaFromType[withDefault]()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
currencyFS := s.properties["currency"]
if currencyFS == nil {
t.Fatal("missing 'currency' property")
}
if currencyFS.Default != "USD" {
t.Errorf("currency default: got %v, want %q", currencyFS.Default, "USD")
}
}
func TestSchemaFromJSON(t *testing.T) {
const jsonSchema = `{
"type": "object",
"properties": {
"net_worth": {"type": "integer", "description": "Estimated net worth in USD"},
"currency": {"type": "string", "default": "USD"}
},
"required": ["net_worth"]
}`
s, err := SchemaFromJSON(jsonSchema)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
props := s.properties
nwFS := props["net_worth"]
if nwFS == nil || nwFS.Type != "integer" {
t.Errorf("net_worth type: got %v", nwFS)
}
if !nwFS.Required {
t.Error("net_worth should be required")
}
curFS := props["currency"]
if curFS == nil || curFS.Default != "USD" {
t.Errorf("currency default: got %v", curFS)
}
req := s.RequiredFields()
if len(req) != 1 || req[0] != "net_worth" {
t.Errorf("required fields: got %v", req)
}
}
func TestSchemaFromJSON_Invalid(t *testing.T) {
_, err := SchemaFromJSON(`not json`)
if err == nil {
t.Fatal("expected error for invalid JSON")
}
}
func TestSchemaToJSON(t *testing.T) {
s, err := SchemaFromType[simpleStruct]()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
out, err := s.ToJSON()
if err != nil {
t.Fatalf("ToJSON error: %v", err)
}
if out == "" {
t.Error("ToJSON returned empty string")
}
}