-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_test.go
More file actions
164 lines (142 loc) · 4.29 KB
/
validate_test.go
File metadata and controls
164 lines (142 loc) · 4.29 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
package augur
import (
"testing"
)
func TestExtractAndParseEnvelope_Direct(t *testing.T) {
input := `{"data":{"name":"Tom"},"meta":{},"notes":""}`
env, err := extractAndParseEnvelope(input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if env.Data["name"] != "Tom" {
t.Errorf("unexpected data: %v", env.Data)
}
}
func TestExtractAndParseEnvelope_MarkdownFence(t *testing.T) {
input := "Here is the result:\n```json\n{\"data\":{\"x\":1},\"meta\":{},\"notes\":\"\"}\n```"
env, err := extractAndParseEnvelope(input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if env.Data["x"] != float64(1) {
t.Errorf("unexpected data: %v", env.Data)
}
}
func TestExtractAndParseEnvelope_BraceScan(t *testing.T) {
input := `Here is the JSON: {"data":{"y":2},"meta":{},"notes":""} and that's it.`
env, err := extractAndParseEnvelope(input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if env.Data["y"] != float64(2) {
t.Errorf("unexpected data: %v", env.Data)
}
}
func TestExtractAndParseEnvelope_Failure(t *testing.T) {
_, err := extractAndParseEnvelope("this is not json at all")
if err == nil {
t.Fatal("expected error")
}
}
func TestProcessResponse_FullSuccess(t *testing.T) {
schema, _ := SchemaFromJSON(`{
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"}
},
"required": ["name", "age"]
}`)
raw := `{"data":{"name":"Tom Hanks","age":67},"meta":{"name":{"confidence":0.99,"sources":[]},"age":{"confidence":0.9,"sources":[]}},"notes":""}`
result, err := processResponse(raw, schema, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result.errors) != 0 {
t.Errorf("expected no errors, got %v", result.errors)
}
if result.data["name"] != "Tom Hanks" {
t.Errorf("name: got %v", result.data["name"])
}
}
func TestProcessResponse_CoercesStringToInt(t *testing.T) {
schema, _ := SchemaFromJSON(`{
"type": "object",
"properties": {
"net_worth": {"type": "integer"}
},
"required": ["net_worth"]
}`)
raw := `{"data":{"net_worth":"$400,000,000"},"meta":{},"notes":""}`
result, err := processResponse(raw, schema, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result.errors) != 0 {
t.Errorf("expected no errors after coercion, got %v", result.errors)
}
if result.data["net_worth"] != float64(400000000) {
t.Errorf("net_worth: got %v, want 400000000", result.data["net_worth"])
}
}
func TestProcessResponse_AppliesDefault(t *testing.T) {
schema, _ := SchemaFromType[withDefault]()
raw := `{"data":{"amount":100},"meta":{},"notes":""}`
result, err := processResponse(raw, schema, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result.data["currency"] != "USD" {
t.Errorf("currency default not applied, got %v", result.data["currency"])
}
}
func TestProcessResponse_MissingRequired(t *testing.T) {
schema, _ := SchemaFromJSON(`{
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"}
},
"required": ["name", "age"]
}`)
raw := `{"data":{"name":"Tom Hanks"},"meta":{},"notes":""}`
result, err := processResponse(raw, schema, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
failed := requiredFailures(result, schema)
if len(failed) != 1 || failed[0].Field != "age" {
t.Errorf("expected age to be a required failure, got %v", failed)
}
}
func TestProcessResponse_MalformedJSON(t *testing.T) {
schema, _ := SchemaFromJSON(`{"type":"object","properties":{"x":{"type":"string"}}}`)
_, err := processResponse("not json", schema, nil)
if err == nil {
t.Fatal("expected error")
}
}
func TestMergeRetryResult(t *testing.T) {
base := &processResult{
data: map[string]any{"name": "Tom"},
errors: []FieldError{
{Field: "age", Reason: "missing"},
},
meta: map[string]map[string]any{},
}
retry := &processResult{
data: map[string]any{"age": float64(67)},
errors: []FieldError{},
meta: map[string]map[string]any{"age": {"confidence": float64(0.9)}},
}
mergeRetryResult(base, retry)
if base.data["name"] != "Tom" {
t.Error("original field should be preserved")
}
if base.data["age"] != float64(67) {
t.Errorf("retry field not merged, got %v", base.data["age"])
}
if len(base.errors) != 0 {
t.Errorf("resolved error should be removed, got %v", base.errors)
}
}