-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfield_test.go
More file actions
389 lines (344 loc) · 9.89 KB
/
field_test.go
File metadata and controls
389 lines (344 loc) · 9.89 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package fmt
import (
"testing"
)
func TestFieldTypeString(t *testing.T) {
tests := []struct {
ft FieldType
want string
}{
{FieldText, "text"},
{FieldInt, "int"},
{FieldFloat, "float"},
{FieldBool, "bool"},
{FieldBlob, "blob"},
{FieldStruct, "struct"},
{FieldType(-1), "unknown"},
{FieldType(6), "unknown"},
}
for _, tt := range tests {
t.Run(tt.want, func(t *testing.T) {
if got := tt.ft.String(); got != tt.want {
t.Errorf("FieldType(%d).String() = %v, want %v", tt.ft, got, tt.want)
}
})
}
}
func TestFieldZeroValue(t *testing.T) {
var f Field
if f.Name != "" {
t.Errorf("expected empty Name, got %v", f.Name)
}
if f.Type != FieldText {
t.Errorf("expected FieldText type, got %v", f.Type)
}
if f.PK || f.Unique || f.NotNull || f.AutoInc {
t.Errorf("expected all bools false, got PK=%v, Unique=%v, NotNull=%v, AutoInc=%v", f.PK, f.Unique, f.NotNull, f.AutoInc)
}
}
func TestFieldOmitEmpty(t *testing.T) {
f := Field{OmitEmpty: true}
if !f.OmitEmpty {
t.Error("expected OmitEmpty true")
}
}
func TestFieldConstraints(t *testing.T) {
f := Field{
Name: "id",
Type: FieldInt,
PK: true,
Unique: true,
NotNull: true,
AutoInc: true,
}
if f.Name != "id" {
t.Errorf("expected Name 'id', got %v", f.Name)
}
if f.Type != FieldInt {
t.Errorf("expected FieldInt type, got %v", f.Type)
}
if !f.PK {
t.Error("expected PK true")
}
if !f.Unique {
t.Error("expected Unique true")
}
if !f.NotNull {
t.Error("expected NotNull true")
}
if !f.AutoInc {
t.Error("expected AutoInc true")
}
}
func TestFieldValidate(t *testing.T) {
tests := []struct {
name string
field Field
value string
wantErr bool
}{
{"NotNull empty", Field{NotNull: true}, "", true},
{"NotNull not empty", Field{NotNull: true}, "foo", false},
{"Nullable empty", Field{NotNull: false}, "", false},
{"With rules pass", Field{Permitted: Permitted{Numbers: true}}, "123", false},
{"With rules fail", Field{Permitted: Permitted{Numbers: true}}, "abc", true},
{"No rules pass", Field{Name: "any"}, "any value", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.field.Validate(tt.value); (err != nil) != tt.wantErr {
t.Errorf("Field.Validate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
type fullMock struct {
Text string
Int int
Int32 int32
Int64 int64
Uint uint
Uint32 uint32
Uint64 uint64
Float float64
Float32 float32
Bool bool
Blob []byte
Nested *mockUser
}
func (m *fullMock) Schema() []Field {
return []Field{
{Name: "text", Type: FieldText, NotNull: true},
{Name: "int", Type: FieldInt, NotNull: true},
{Name: "int32", Type: FieldInt},
{Name: "int64", Type: FieldInt},
{Name: "uint", Type: FieldInt},
{Name: "uint32", Type: FieldInt},
{Name: "uint64", Type: FieldInt},
{Name: "float", Type: FieldFloat, NotNull: true},
{Name: "float32", Type: FieldFloat},
{Name: "bool", Type: FieldBool, NotNull: true},
{Name: "blob", Type: FieldBlob, NotNull: true},
{Name: "nested", Type: FieldStruct, NotNull: true},
}
}
func (m *fullMock) Pointers() []any {
return []any{&m.Text, &m.Int, &m.Int32, &m.Int64, &m.Uint, &m.Uint32, &m.Uint64, &m.Float, &m.Float32, &m.Bool, &m.Blob, m.Nested}
}
type mockUser struct {
id string
name string
}
func (m *mockUser) Schema() []Field {
return []Field{
{Name: "id", Type: FieldText, PK: true},
{Name: "name", Type: FieldText, NotNull: true},
}
}
func (m *mockUser) Pointers() []any { return []any{&m.id, &m.name} }
func (m *mockUser) Validate(action byte) error { return ValidateFields(action, m) }
func TestValidateFieldsRecursive(t *testing.T) {
m := &fullMock{
Text: "hello",
Int: 1,
Float: 1.1,
Bool: true,
Blob: []byte{1},
Nested: &mockUser{id: "u1", name: "Alice"},
}
if err := ValidateFields('u', m); err != nil {
t.Errorf("expected success, got %v", err)
}
// Fail nested
m.Nested.name = ""
if err := ValidateFields('u', m); err == nil {
t.Error("expected failure in nested struct")
}
// Fail other types
m.Nested.name = "Alice"
m.Int = 0 // NotNull
if err := ValidateFields('u', m); err == nil {
t.Error("expected failure for int zero")
}
}
func TestReadValuesAllTypes(t *testing.T) {
m := &fullMock{
Text: "text",
Int: 10,
Int32: 32,
Int64: 64,
Uint: 100,
Uint32: 320,
Uint64: 640,
Float: 1.1,
Float32: 2.2,
Bool: true,
Blob: []byte{0x01},
Nested: &mockUser{id: "u1", name: "Alice"},
}
schema := m.Schema()
ptrs := m.Pointers()
vals := ReadValues(schema, ptrs)
expected := []any{"text", 10, int32(32), int64(64), uint(100), uint32(320), uint64(640), 1.1, float32(2.2), true, []byte{0x01}, m.Nested}
for i, v := range expected {
if i == 10 { // Blob check
b1 := v.([]byte)
b2 := vals[i].([]byte)
if len(b1) != len(b2) || b1[0] != b2[0] {
t.Errorf("mismatch at index %d: got %v, want %v", i, vals[i], v)
}
continue
}
if vals[i] != v {
t.Errorf("mismatch at index %d: got %v, want %v", i, vals[i], v)
}
}
// Test nil pointers
ptrsCopy := make([]any, len(ptrs))
copy(ptrsCopy, ptrs)
ptrsCopy[0] = nil
valsNil := ReadValues(schema, ptrsCopy)
if valsNil[0] != nil {
t.Error("expected nil for nil pointer")
}
}
func TestIsZeroPtrAllTypes(t *testing.T) {
var i int = 0
if !isZeroPtr(&i, FieldInt) { t.Error("int 0 should be zero") }
i = 1
if isZeroPtr(&i, FieldInt) { t.Error("int 1 should not be zero") }
var i32 int32 = 0
if !isZeroPtr(&i32, FieldInt) { t.Error("int32 0 should be zero") }
var i64 int64 = 0
if !isZeroPtr(&i64, FieldInt) { t.Error("int64 0 should be zero") }
var u uint = 0
if !isZeroPtr(&u, FieldInt) { t.Error("uint 0 should be zero") }
var u32 uint32 = 0
if !isZeroPtr(&u32, FieldInt) { t.Error("uint32 0 should be zero") }
var u64 uint64 = 0
if !isZeroPtr(&u64, FieldInt) { t.Error("uint64 0 should be zero") }
var f64 float64 = 0
if !isZeroPtr(&f64, FieldFloat) { t.Error("float64 0 should be zero") }
f64 = 0.1
if isZeroPtr(&f64, FieldFloat) { t.Error("float64 0.1 should not be zero") }
var f32 float32 = 0
if !isZeroPtr(&f32, FieldFloat) { t.Error("float32 0 should be zero") }
var b bool = false
if !isZeroPtr(&b, FieldBool) { t.Error("bool false should be zero") }
b = true
if isZeroPtr(&b, FieldBool) { t.Error("bool true should not be zero") }
var bl []byte
if !isZeroPtr(&bl, FieldBlob) { t.Error("nil blob should be zero") }
bl = []byte{}
if !isZeroPtr(&bl, FieldBlob) { t.Error("empty blob should be zero") }
bl = []byte{1}
if isZeroPtr(&bl, FieldBlob) { t.Error("non-empty blob should not be zero") }
}
func TestReadStringPtr(t *testing.T) {
s := "hello"
val, ok := ReadStringPtr(&s)
if !ok || val != "hello" {
t.Errorf("ReadStringPtr failed: got (%q, %v), want (\"hello\", true)", val, ok)
}
val, ok = ReadStringPtr("not a pointer")
if ok {
t.Error("ReadStringPtr should have failed for non-pointer")
}
var ns *string
val, ok = ReadStringPtr(ns)
if ok {
t.Error("ReadStringPtr should have failed for nil pointer")
}
}
type fielderOnlyMock struct {
id string
}
func (m *fielderOnlyMock) Schema() []Field { return []Field{{Name: "id", Type: FieldText}} }
func (m *fielderOnlyMock) Pointers() []any { return []any{&m.id} }
func TestValidateFieldsWithOnlyFielder(t *testing.T) {
// Nested struct that only implements Fielder, not Validator.
sub := &fielderOnlyMock{id: "ok"}
schema := []Field{{Name: "sub", Type: FieldStruct}}
ptrs := []any{sub}
// Validate it through the manualFielder helper
if err := ValidateFields('u', &manualFielder{schema, ptrs}); err != nil {
t.Errorf("expected success, got %v", err)
}
}
func TestValidateFieldsActions(t *testing.T) {
type userActionMock struct {
ID int
Name string
Email string
Version int
}
schema := []Field{
{Name: "id", Type: FieldInt, PK: true, AutoInc: true},
{Name: "name", Type: FieldText, NotNull: true},
{Name: "email", Type: FieldText, Permitted: Permitted{Letters: true, Extra: []rune{'@', '.'}}},
{Name: "version", Type: FieldInt, NotNull: true},
}
// Helper to create manual Fielder
getFielder := func(m *userActionMock) Fielder {
return &manualFielder{
schema: schema,
ptrs: []any{&m.ID, &m.Name, &m.Email, &m.Version},
}
}
t.Run("Create 'c'", func(t *testing.T) {
m := &userActionMock{Name: "Alice", Email: "a@b.com", Version: 1}
f := getFielder(m)
// PK+AutoInc should be skipped in 'c'
if err := ValidateFields('c', f); err != nil {
t.Errorf("expected success in 'c' with zero ID, got %v", err)
}
// NotNull still applies
m.Name = ""
if err := ValidateFields('c', f); err == nil {
t.Error("expected failure in 'c' with empty Name")
}
})
t.Run("Update 'u'", func(t *testing.T) {
m := &userActionMock{ID: 1, Name: "Alice", Email: "a@b.com", Version: 1}
f := getFielder(m)
if err := ValidateFields('u', f); err != nil {
t.Errorf("expected success in 'u', got %v", err)
}
// PK is required in 'u'
m.ID = 0
if err := ValidateFields('u', f); err == nil {
t.Error("expected failure in 'u' with zero ID")
}
})
t.Run("Delete 'd'", func(t *testing.T) {
m := &userActionMock{ID: 1, Name: "", Email: "invalid!!", Version: 0}
f := getFielder(m)
// Only PK matters in 'd'
if err := ValidateFields('d', f); err != nil {
t.Errorf("expected success in 'd' with only PK, got %v", err)
}
// PK missing
m.ID = 0
if err := ValidateFields('d', f); err == nil {
t.Error("expected failure in 'd' with missing PK")
}
})
t.Run("Unknown 'x'", func(t *testing.T) {
m := &userActionMock{ID: 1, Name: "Alice", Email: "a@b.com", Version: 1}
f := getFielder(m)
// Should behave like 'u'
if err := ValidateFields('x', f); err != nil {
t.Errorf("expected success in 'x', got %v", err)
}
m.ID = 0
if err := ValidateFields('x', f); err == nil {
t.Error("expected failure in 'x' with missing PK")
}
})
}
type manualFielder struct {
schema []Field
ptrs []any
}
func (f *manualFielder) Schema() []Field { return f.schema }
func (f *manualFielder) Pointers() []any { return f.ptrs }