-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathdefault_funcs_test.go
More file actions
346 lines (300 loc) · 8.09 KB
/
Copy pathdefault_funcs_test.go
File metadata and controls
346 lines (300 loc) · 8.09 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package jsonschema
import (
"fmt"
"testing"
"time"
)
func TestDefaultFunc_DefaultNowFunc(t *testing.T) {
tests := []struct {
name string
args []any
wantType string
}{
{
name: "default RFC3339",
args: []any{},
wantType: "string",
},
{
name: "custom format",
args: []any{"2006-01-02"},
wantType: "string",
},
{
name: "another custom format",
args: []any{"15:04:05"},
wantType: "string",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := DefaultNowFunc(tt.args...)
if err != nil {
t.Errorf("DefaultNowFunc() error = %v", err)
return
}
if _, ok := result.(string); !ok {
t.Errorf("DefaultNowFunc() result type = %T, want string", result)
}
})
}
}
func TestParseFunctionCall(t *testing.T) {
tests := []struct {
name string
input string
want *FunctionCall
}{
{
name: "simple function no args",
input: "now()",
want: &FunctionCall{Name: "now", Args: []any{}},
},
{
name: "function with string arg",
input: "now(unix)",
want: &FunctionCall{Name: "now", Args: []any{"unix"}},
},
{
name: "function with multiple args",
input: "func(arg1, 42, 3.14)",
want: &FunctionCall{Name: "func", Args: []any{"arg1", int64(42), float64(3.14)}},
},
{
name: "function skips empty args",
input: "func(arg1, , 42,)",
want: &FunctionCall{Name: "func", Args: []any{"arg1", int64(42)}},
},
{
name: "not a function call",
input: "just a string",
want: nil,
},
{
name: "empty string",
input: "",
want: nil,
},
{
name: "invalid format",
input: "func(",
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := parseFunctionCall(tt.input)
if tt.want == nil {
if got != nil {
t.Errorf("parseFunctionCall() = %v, want nil", got)
}
return
}
if got == nil {
t.Errorf("parseFunctionCall() = nil, want %v", tt.want)
return
}
if got.Name != tt.want.Name {
t.Errorf("parseFunctionCall() Name = %v, want %v", got.Name, tt.want.Name)
}
if len(got.Args) != len(tt.want.Args) {
t.Errorf("parseFunctionCall() Args length = %v, want %v", len(got.Args), len(tt.want.Args))
return
}
for i, arg := range got.Args {
if arg != tt.want.Args[i] {
t.Errorf("parseFunctionCall() Args[%d] = %v, want %v", i, arg, tt.want.Args[i])
}
}
})
}
}
func TestCompiler_RegisterDefaultFunc(t *testing.T) {
compiler := NewCompiler()
// Test registration
testFunc := func(_ ...any) (any, error) {
return "test_result", nil
}
compiler.RegisterDefaultFunc("test", testFunc)
// Test retrieval
fn, exists := compiler.defaultFunc("test")
if !exists {
t.Error("Expected function to be registered")
}
result, err := fn()
if err != nil {
t.Errorf("Function call failed: %v", err)
}
if result != "test_result" {
t.Errorf("Function result = %v, want test_result", result)
}
}
// customIDFunc generates a simple ID for testing using timestamp and counter
func customIDFunc(_ ...any) (any, error) {
// Use timestamp and a simple counter instead of random numbers for testing
staticCounter := 42 // Static value for deterministic testing
return fmt.Sprintf("id_%d_%d", time.Now().Unix(), staticCounter), nil
}
func TestDynamicDefaultValues_Integration(t *testing.T) {
compiler := NewCompiler()
// Register functions
compiler.RegisterDefaultFunc("now", DefaultNowFunc)
compiler.RegisterDefaultFunc("randomId", customIDFunc)
// Create schema with dynamic defaults
schemaJSON := `{
"type": "object",
"properties": {
"id": {
"type": "string",
"default": "randomId()"
},
"createdAt": {
"type": "string",
"default": "now()"
},
"updatedAt": {
"type": "string",
"default": "now(2006-01-02 15:04:05)"
},
"status": {
"type": "string",
"default": "active"
},
"version": {
"type": "string",
"default": "unregistered_func()"
}
}
}`
schema, err := compiler.Compile([]byte(schemaJSON))
if err != nil {
t.Fatalf("Failed to compile schema: %v", err)
}
// Test with partial data
inputData := map[string]any{
"status": "pending",
}
var result map[string]any
err = schema.Unmarshal(&result, inputData)
if err != nil {
t.Fatalf("Failed to unmarshal: %v", err)
}
// Verify results
if result["status"] != "pending" {
t.Errorf("status = %v, want pending", result["status"])
}
// Check that random ID was generated
if id, ok := result["id"].(string); !ok || len(id) == 0 {
t.Errorf("id should be a non-empty string, got %v", result["id"])
} else if id[:3] != "id_" {
t.Errorf("id should start with 'id_', got %v", id)
}
// Check that timestamp was generated
if createdAt, ok := result["createdAt"].(string); !ok || len(createdAt) == 0 {
t.Errorf("createdAt should be a non-empty string, got %v", result["createdAt"])
}
// Check that updated timestamp was generated
if updatedAt, ok := result["updatedAt"].(string); !ok || len(updatedAt) == 0 {
t.Errorf("updatedAt should be a non-empty string, got %v", result["updatedAt"])
}
// Check that unregistered function falls back to literal
if version, ok := result["version"].(string); !ok || version != "unregistered_func()" {
t.Errorf("version should be literal string 'unregistered_func()', got %v", result["version"])
}
t.Logf("Result: %+v", result)
}
func TestSchemaSetCompiler(t *testing.T) {
compiler := NewCompiler()
compiler.RegisterDefaultFunc("test", func(_ ...any) (any, error) {
return "test_value", nil
})
schema := Object(
Prop("field", String(Default("test()"))),
).SetCompiler(compiler)
data := map[string]any{}
var result map[string]any
err := schema.Unmarshal(&result, data)
if err != nil {
t.Errorf("Unmarshal() error = %v", err)
return
}
if result["field"] != "test_value" {
t.Errorf("result['field'] = %v, want test_value", result["field"])
}
}
func TestSchemaCompilerIsolation(t *testing.T) {
compiler1 := NewCompiler()
compiler1.RegisterDefaultFunc("func1", func(_ ...any) (any, error) {
return "value1", nil
})
compiler2 := NewCompiler()
compiler2.RegisterDefaultFunc("func2", func(_ ...any) (any, error) {
return "value2", nil
})
schema1 := Object(
Prop("field1", String(Default("func1()"))),
).SetCompiler(compiler1)
schema2 := Object(
Prop("field2", String(Default("func2()"))),
).SetCompiler(compiler2)
// Verify isolation
data1 := map[string]any{}
data2 := map[string]any{}
var result1, result2 map[string]any
err := schema1.Unmarshal(&result1, data1)
if err != nil {
t.Errorf("schema1.Unmarshal() error = %v", err)
return
}
err = schema2.Unmarshal(&result2, data2)
if err != nil {
t.Errorf("schema2.Unmarshal() error = %v", err)
return
}
if result1["field1"] != "value1" {
t.Errorf("result1['field1'] = %v, want value1", result1["field1"])
}
if result2["field2"] != "value2" {
t.Errorf("result2['field2'] = %v, want value2", result2["field2"])
}
}
func TestSchemaSetCompilerChaining(t *testing.T) {
compiler := NewCompiler()
compiler.RegisterDefaultFunc("test", func(_ ...any) (any, error) {
return "chained", nil
})
// Test method chaining
schema := Object(
Prop("field", String(Default("test()"))),
).SetCompiler(compiler)
data := map[string]any{}
var result map[string]any
err := schema.Unmarshal(&result, data)
if err != nil {
t.Errorf("Unmarshal() error = %v", err)
return
}
if result["field"] != "chained" {
t.Errorf("result['field'] = %v, want chained", result["field"])
}
}
func TestSchemaCompilerInheritance(t *testing.T) {
compiler := NewCompiler()
compiler.RegisterDefaultFunc("test", func(_ ...any) (any, error) {
return "inherited", nil
})
// Parent Schema sets Compiler, child Schema should inherit
schema := Object(
Prop("child", String(Default("test()"))), // Child Schema doesn't set compiler
).SetCompiler(compiler) // Only set on parent Schema
data := map[string]any{}
var result map[string]any
err := schema.Unmarshal(&result, data)
if err != nil {
t.Errorf("Unmarshal() error = %v", err)
return
}
if result["child"] != "inherited" {
t.Errorf("result['child'] = %v, want inherited", result["child"])
}
}