-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchain_test.go
More file actions
538 lines (429 loc) · 11.5 KB
/
chain_test.go
File metadata and controls
538 lines (429 loc) · 11.5 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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
package gormx
import (
"testing"
"time"
"gorm.io/gorm"
)
// TestChainProduct is a test model for chain queries
type TestChainProduct struct {
ID uint `gorm:"primarykey"`
Name string `gorm:"column:name"`
Category string `gorm:"column:category"`
Price float64 `gorm:"column:price"`
Quantity int `gorm:"column:quantity"`
InStock bool `gorm:"column:in_stock"`
CreatedAt time.Time `gorm:"column:created_at"`
UpdatedAt time.Time `gorm:"column:updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index"`
}
func (TestChainProduct) TableName() string {
return "test_chain_products"
}
func setupChainTestData(t *testing.T) {
// Skip if no database connection (catch panic from GetDB)
defer func() {
if r := recover(); r != nil {
t.Skip("No database connection available")
}
}()
db := GetDB()
if db == nil {
t.Skip("No database connection available")
}
// Create test table
err := GetDB().AutoMigrate(&TestChainProduct{})
if err != nil {
t.Fatalf("Failed to migrate test table: %v", err)
}
// Clean up test data
GetDB().Unscoped().Where("1 = 1").Delete(&TestChainProduct{})
// Insert test data
testProducts := []TestChainProduct{
{Name: "Laptop", Category: "Electronics", Price: 1000.0, Quantity: 10, InStock: true},
{Name: "Phone", Category: "Electronics", Price: 500.0, Quantity: 20, InStock: true},
{Name: "Book", Category: "Books", Price: 20.0, Quantity: 50, InStock: true},
{Name: "Pen", Category: "Stationery", Price: 2.0, Quantity: 100, InStock: true},
{Name: "Notebook", Category: "Stationery", Price: 5.0, Quantity: 80, InStock: true},
{Name: "Monitor", Category: "Electronics", Price: 300.0, Quantity: 0, InStock: false},
}
for _, product := range testProducts {
GetDB().Create(&product)
}
}
func cleanupChainTestData(t *testing.T) {
// Skip cleanup if no database connection (catch panic from GetDB)
defer func() {
if r := recover(); r != nil {
// DB not available, nothing to clean up
return
}
}()
db := GetDB()
if db != nil {
db.Unscoped().Where("1 = 1").Delete(&TestChainProduct{})
}
}
func TestQueryBuilder_Where(t *testing.T) {
setupChainTestData(t)
defer cleanupChainTestData(t)
t.Run("Simple Where", func(t *testing.T) {
results, err := NewQuery[TestChainProduct]().
Where("category", "Electronics").
Find()
if err != nil {
t.Fatalf("Where query failed: %v", err)
}
if len(results) != 3 {
t.Errorf("Expected 3 results, got %d", len(results))
}
})
t.Run("WhereEqual", func(t *testing.T) {
results, err := NewQuery[TestChainProduct]().
WhereEqual("category", "Books").
Find()
if err != nil {
t.Fatalf("WhereEqual query failed: %v", err)
}
if len(results) != 1 {
t.Errorf("Expected 1 result, got %d", len(results))
}
})
t.Run("WhereIn", func(t *testing.T) {
results, err := NewQuery[TestChainProduct]().
WhereIn("category", []string{"Electronics", "Books"}).
Find()
if err != nil {
t.Fatalf("WhereIn query failed: %v", err)
}
if len(results) != 4 {
t.Errorf("Expected 4 results, got %d", len(results))
}
})
t.Run("Multiple Where Conditions", func(t *testing.T) {
results, err := NewQuery[TestChainProduct]().
Where("category", "Electronics").
Where("in_stock", true).
Find()
if err != nil {
t.Fatalf("Multiple where query failed: %v", err)
}
if len(results) != 2 {
t.Errorf("Expected 2 results, got %d", len(results))
}
})
}
func TestQueryBuilder_OrderBy(t *testing.T) {
setupChainTestData(t)
defer cleanupChainTestData(t)
t.Run("OrderBy Ascending", func(t *testing.T) {
results, err := NewQuery[TestChainProduct]().
OrderByAsc("price").
Find()
if err != nil {
t.Fatalf("OrderBy query failed: %v", err)
}
if len(results) < 2 {
t.Fatal("Not enough results")
}
if results[0].Price > results[1].Price {
t.Error("Results not ordered correctly")
}
})
t.Run("OrderBy Descending", func(t *testing.T) {
results, err := NewQuery[TestChainProduct]().
OrderByDesc("price").
Find()
if err != nil {
t.Fatalf("OrderBy query failed: %v", err)
}
if len(results) < 2 {
t.Fatal("Not enough results")
}
if results[0].Price < results[1].Price {
t.Error("Results not ordered correctly")
}
})
}
func TestQueryBuilder_Limit(t *testing.T) {
setupChainTestData(t)
defer cleanupChainTestData(t)
t.Run("Limit", func(t *testing.T) {
results, err := NewQuery[TestChainProduct]().
Limit(3).
Find()
if err != nil {
t.Fatalf("Limit query failed: %v", err)
}
if len(results) != 3 {
t.Errorf("Expected 3 results, got %d", len(results))
}
})
t.Run("Limit and Offset", func(t *testing.T) {
results, err := NewQuery[TestChainProduct]().
OrderByAsc("id").
Limit(2).
Offset(2).
Find()
if err != nil {
t.Fatalf("Limit and Offset query failed: %v", err)
}
if len(results) != 2 {
t.Errorf("Expected 2 results, got %d", len(results))
}
})
t.Run("Page", func(t *testing.T) {
results, err := NewQuery[TestChainProduct]().
Page(2, 2).
Find()
if err != nil {
t.Fatalf("Page query failed: %v", err)
}
if len(results) != 2 {
t.Errorf("Expected 2 results, got %d", len(results))
}
})
}
func TestQueryBuilder_Select(t *testing.T) {
setupChainTestData(t)
defer cleanupChainTestData(t)
t.Run("Select Specific Columns", func(t *testing.T) {
results, err := NewQuery[TestChainProduct]().
Select("name", "price").
Find()
if err != nil {
t.Fatalf("Select query failed: %v", err)
}
if len(results) == 0 {
t.Error("Expected results")
}
})
}
func TestQueryBuilder_Count(t *testing.T) {
setupChainTestData(t)
defer cleanupChainTestData(t)
t.Run("Count All", func(t *testing.T) {
count, err := NewQuery[TestChainProduct]().Count()
if err != nil {
t.Fatalf("Count query failed: %v", err)
}
if count != 6 {
t.Errorf("Expected count 6, got %d", count)
}
})
t.Run("Count with Where", func(t *testing.T) {
count, err := NewQuery[TestChainProduct]().
Where("category", "Electronics").
Count()
if err != nil {
t.Fatalf("Count with where query failed: %v", err)
}
if count != 3 {
t.Errorf("Expected count 3, got %d", count)
}
})
}
func TestQueryBuilder_First(t *testing.T) {
setupChainTestData(t)
defer cleanupChainTestData(t)
t.Run("First", func(t *testing.T) {
result, err := NewQuery[TestChainProduct]().
Where("category", "Books").
First()
if err != nil {
t.Fatalf("First query failed: %v", err)
}
if result == nil {
t.Error("Expected result")
}
if result.Category != "Books" {
t.Errorf("Expected category Books, got %s", result.Category)
}
})
}
func TestQueryBuilder_Exists(t *testing.T) {
setupChainTestData(t)
defer cleanupChainTestData(t)
t.Run("Exists - True", func(t *testing.T) {
exists, err := NewQuery[TestChainProduct]().
Where("category", "Electronics").
Exists()
if err != nil {
t.Fatalf("Exists query failed: %v", err)
}
if !exists {
t.Error("Expected exists to be true")
}
})
t.Run("Exists - False", func(t *testing.T) {
exists, err := NewQuery[TestChainProduct]().
Where("category", "NonExistent").
Exists()
if err != nil {
t.Fatalf("Exists query failed: %v", err)
}
if exists {
t.Error("Expected exists to be false")
}
})
}
func TestQueryBuilder_Aggregate(t *testing.T) {
setupChainTestData(t)
defer cleanupChainTestData(t)
t.Run("Sum", func(t *testing.T) {
sum, err := NewQuery[TestChainProduct]().
Where("category", "Electronics").
Sum("price")
if err != nil {
t.Fatalf("Sum query failed: %v", err)
}
expected := 1800.0 // 1000 + 500 + 300
if sum != expected {
t.Errorf("Expected sum %f, got %f", expected, sum)
}
})
t.Run("Avg", func(t *testing.T) {
avg, err := NewQuery[TestChainProduct]().
Where("category", "Stationery").
Avg("price")
if err != nil {
t.Fatalf("Avg query failed: %v", err)
}
expected := 3.5 // (2 + 5) / 2
if avg != expected {
t.Errorf("Expected avg %f, got %f", expected, avg)
}
})
t.Run("Min", func(t *testing.T) {
min, err := NewQuery[TestChainProduct]().
Min("price")
if err != nil {
t.Fatalf("Min query failed: %v", err)
}
if min == nil {
t.Error("Expected min value")
}
})
t.Run("Max", func(t *testing.T) {
max, err := NewQuery[TestChainProduct]().
Max("price")
if err != nil {
t.Fatalf("Max query failed: %v", err)
}
if max == nil {
t.Error("Expected max value")
}
})
}
func TestQueryBuilder_Paginate(t *testing.T) {
setupChainTestData(t)
defer cleanupChainTestData(t)
t.Run("Paginate", func(t *testing.T) {
results, total, err := NewQuery[TestChainProduct]().
OrderByAsc("id").
Paginate(1, 2)
if err != nil {
t.Fatalf("Paginate query failed: %v", err)
}
if len(results) != 2 {
t.Errorf("Expected 2 results, got %d", len(results))
}
if total != 6 {
t.Errorf("Expected total 6, got %d", total)
}
})
}
func TestQueryBuilder_Chunk(t *testing.T) {
setupChainTestData(t)
defer cleanupChainTestData(t)
t.Run("Chunk", func(t *testing.T) {
var processedCount int
err := NewQuery[TestChainProduct]().
OrderByAsc("id").
Chunk(2, func(products []*TestChainProduct) error {
processedCount += len(products)
return nil
})
if err != nil {
t.Fatalf("Chunk query failed: %v", err)
}
if processedCount != 6 {
t.Errorf("Expected to process 6 records, processed %d", processedCount)
}
})
}
func TestQueryBuilder_GroupBy(t *testing.T) {
setupChainTestData(t)
defer cleanupChainTestData(t)
t.Run("GroupBy", func(t *testing.T) {
var results []struct {
Category string
Count int64
}
err := NewQuery[TestChainProduct]().
Select("category", "COUNT(*) as count").
GroupBy("category").
Scan(&results)
if err != nil {
t.Fatalf("GroupBy query failed: %v", err)
}
if len(results) != 3 {
t.Errorf("Expected 3 groups, got %d", len(results))
}
})
}
func TestQueryBuilder_Distinct(t *testing.T) {
setupChainTestData(t)
defer cleanupChainTestData(t)
t.Run("Distinct", func(t *testing.T) {
var categories []string
err := NewQuery[TestChainProduct]().
Distinct().
Pluck("category", &categories)
if err != nil {
t.Fatalf("Distinct query failed: %v", err)
}
if len(categories) != 3 {
t.Errorf("Expected 3 distinct categories, got %d", len(categories))
}
})
}
func TestQueryBuilder_Clone(t *testing.T) {
setupChainTestData(t)
defer cleanupChainTestData(t)
t.Run("Clone", func(t *testing.T) {
originalQuery := NewQuery[TestChainProduct]().
Where("category", "Electronics").
OrderByAsc("price")
clonedQuery := originalQuery.Clone()
// Modify cloned query
clonedQuery.Where("in_stock", true)
// Original should not be affected
originalCount, _ := originalQuery.Count()
clonedCount, _ := clonedQuery.Count()
if originalCount == clonedCount {
t.Log("Clone might not be fully independent (expected, this is a simple clone)")
}
})
}
func TestQueryBuilder_ChainedOperations(t *testing.T) {
setupChainTestData(t)
defer cleanupChainTestData(t)
t.Run("Complex Chained Query", func(t *testing.T) {
results, err := NewQuery[TestChainProduct]().
Where("category", "Electronics").
Where("in_stock", true).
OrderByDesc("price").
Limit(2).
Find()
if err != nil {
t.Fatalf("Chained query failed: %v", err)
}
if len(results) != 2 {
t.Errorf("Expected 2 results, got %d", len(results))
}
// Should be ordered by price descending
if len(results) == 2 && results[0].Price < results[1].Price {
t.Error("Results not ordered correctly")
}
})
}