forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
finders_test.go
707 lines (556 loc) · 16 KB
/
finders_test.go
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
package pop_test
import (
"testing"
"github.com/gobuffalo/pop"
"github.com/gobuffalo/pop/nulls"
"github.com/gobuffalo/uuid"
"github.com/stretchr/testify/require"
)
func Test_Find(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)
user := User{Name: nulls.NewString("Mark")}
err := tx.Create(&user)
r.NoError(err)
u := User{}
err = tx.Find(&u, user.ID)
r.NoError(err)
r.NotEqual(u.ID, 0)
r.Equal(u.Name.String, "Mark")
})
}
func Test_Find_UTF8(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)
user := User{Name: nulls.NewString("💩")}
err := tx.Create(&user)
r.NoError(err)
u := User{}
err = tx.Find(&u, user.ID)
r.NoError(err)
r.NotEqual(u.ID, 0)
r.Equal(u.Name.String, "💩")
})
}
func Test_Select(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)
user := User{Name: nulls.NewString("Mark"), Email: "[email protected]"}
err := tx.Create(&user)
r.NoError(err)
q := tx.Select("name", "email", "\n", "\t\n", "")
sm := &pop.Model{Value: &User{}}
sql, _ := q.ToSQL(sm)
r.Equal(tx.Dialect.TranslateSQL("SELECT email, name FROM users AS users"), sql)
u := User{}
err = q.Find(&u, user.ID)
r.NoError(err)
r.Equal(u.Email, "[email protected]")
r.Equal(u.Name.String, "Mark")
r.Zero(u.ID)
})
}
func Test_Find_Eager_Has_Many(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)
user := User{Name: nulls.NewString("Mark")}
err := tx.Create(&user)
r.NoError(err)
book := Book{Title: "Pop Book", Isbn: "PB1", UserID: nulls.NewInt(user.ID)}
err = tx.Create(&book)
r.NoError(err)
u := User{}
err = tx.Eager("Books").Find(&u, user.ID)
r.NoError(err)
r.NotEqual(u.ID, 0)
r.Equal(u.Name.String, "Mark")
books := u.Books
r.NotEqual(len(books), 0)
r.Equal(books[0].Title, book.Title)
})
}
func Test_Find_Eager_Has_Many_Order_By(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)
user := User{Name: nulls.NewString("Mark")}
err := tx.Create(&user)
r.NoError(err)
book1 := Book{Title: "Pop Book", Isbn: "PB1", UserID: nulls.NewInt(user.ID)}
err = tx.Create(&book1)
r.NoError(err)
book2 := Book{Title: "New Pop Book", Isbn: "PB2", UserID: nulls.NewInt(user.ID)}
err = tx.Create(&book2)
r.NoError(err)
u := User{}
err = tx.Eager().Find(&u, user.ID)
r.NoError(err)
r.Equal(len(u.Books), 2)
r.Equal(book2.Title, u.Books[0].Title)
})
}
func Test_Find_Eager_Belongs_To(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)
user := User{Name: nulls.NewString("Mark")}
err := tx.Create(&user)
r.NoError(err)
book := Book{Title: "Pop Book", Isbn: "PB1", UserID: nulls.NewInt(user.ID)}
err = tx.Create(&book)
r.NoError(err)
b := Book{}
err = tx.Eager().Find(&b, book.ID)
r.NoError(err)
r.NotEqual(b.ID, 0)
r.NotEqual(b.User.ID, 0)
r.Equal(b.User.ID, user.ID)
})
}
func Test_Find_Eager_Belongs_To_Nulls(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)
user := User{Name: nulls.NewString("Mark")}
err := tx.Create(&user)
r.NoError(err)
book := Book{Title: "Pop Book", Isbn: "PB1"}
err = tx.Create(&book)
r.NoError(err)
b := Book{}
err = tx.Eager().Find(&b, book.ID)
r.NoError(err)
})
}
func Test_Find_Eager_Has_One(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)
user := User{Name: nulls.NewString("Mark")}
err := tx.Create(&user)
r.NoError(err)
coolSong := Song{Title: "Hook - Blues Traveler", UserID: user.ID}
err = tx.Create(&coolSong)
r.NoError(err)
u := User{}
err = tx.Eager().Find(&u, user.ID)
r.NoError(err)
r.NotEqual(u.ID, 0)
r.Equal(u.Name.String, "Mark")
r.Equal(u.FavoriteSong.ID, coolSong.ID)
//eager should work with rawquery
uid := u.ID
u = User{}
err = tx.RawQuery("select * from users where id=?", uid).First(&u)
r.NoError(err)
r.Equal(u.FavoriteSong.ID, uuid.Nil)
err = tx.RawQuery("select * from users where id=?", uid).Eager("FavoriteSong").First(&u)
r.NoError(err)
r.Equal(u.FavoriteSong.ID, coolSong.ID)
})
}
func Test_Find_Eager_Has_One_With_Inner_Associations_Struct(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)
user := User{Name: nulls.NewString("Mark")}
err := tx.Create(&user)
r.NoError(err)
composer := Composer{Name: "Blues Traveler"}
err = tx.Create(&composer)
r.NoError(err)
coolSong := Song{Title: "Hook", UserID: user.ID, ComposedByID: composer.ID}
err = tx.Create(&coolSong)
r.NoError(err)
u := User{}
err = tx.Eager("FavoriteSong.ComposedBy").Find(&u, user.ID)
r.NoError(err)
r.NotEqual(u.ID, 0)
r.Equal(u.Name.String, "Mark")
r.Equal(u.FavoriteSong.ID, coolSong.ID)
r.Equal(u.FavoriteSong.ComposedBy.Name, composer.Name)
})
}
func Test_Find_Eager_Has_One_With_Inner_Associations_Slice(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)
user := User{Name: nulls.NewString("Mark")}
err := tx.Create(&user)
r.NoError(err)
book := Book{Title: "Pop Book", Isbn: "PB1", UserID: nulls.NewInt(user.ID)}
err = tx.Create(&book)
r.NoError(err)
writer := Writer{Name: "Mark Bates", BookID: book.ID}
err = tx.Create(&writer)
r.NoError(err)
u := User{}
err = tx.Eager("Books.Writers").Find(&u, user.ID)
r.NoError(err)
r.NotEqual(u.ID, 0)
r.Equal(u.Name.String, "Mark")
r.Equal(len(u.Books), 1)
r.Equal(len(u.Books[0].Writers), 1)
r.Equal(u.Books[0].Title, book.Title)
r.Equal(u.Books[0].Writers[0].Name, writer.Name)
r.Zero(u.Books[0].Writers[0].Book.ID)
})
}
func Test_Eager_Bad_Format(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)
user := User{Name: nulls.NewString("Mark")}
err := tx.Create(&user)
r.NoError(err)
u := User{}
err = tx.Eager("Books.").First(&u)
r.Error(err)
err = tx.Eager("Books.*").First(&u)
r.Error(err)
err = tx.Eager(".*").First(&u)
r.Error(err)
err = tx.Eager(".").First(&u)
r.Error(err)
})
}
func Test_Find_Eager_Many_To_Many(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)
user := User{Name: nulls.NewString("Mark")}
err := tx.Create(&user)
r.NoError(err)
address := Address{Street: "Pop Avenue", HouseNumber: 1}
err = tx.Create(&address)
r.NoError(err)
ownerProperty := UsersAddress{UserID: user.ID, AddressID: address.ID}
err = tx.Create(&ownerProperty)
r.NoError(err)
u := User{}
err = tx.Eager("Houses").Find(&u, user.ID)
r.NoError(err)
r.NotEqual(u.ID, 0)
r.Equal(u.Name.String, "Mark")
r.Equal(len(u.Houses), 1)
r.Equal(u.Houses[0].Street, address.Street)
address2 := Address{Street: "Pop Avenue 2", HouseNumber: 1}
err = tx.Create(&address2)
r.NoError(err)
user2 := User{Name: nulls.NewString("Mark 2")}
err = tx.Create(&user2)
r.NoError(err)
ownerProperty2 := UsersAddress{UserID: user2.ID, AddressID: address2.ID}
err = tx.Create(&ownerProperty2)
r.NoError(err)
//eager should work with rawquery
uid := u.ID
u = User{}
err = tx.RawQuery("select * from users where id=?", uid).Eager("Houses").First(&u)
r.NoError(err)
r.Equal(1, len(u.Houses))
//eager ALL
var users []User
err = tx.RawQuery("select * from users order by created_at asc").Eager("Houses").All(&users)
r.NoError(err)
r.Equal(2, len(users))
u = users[0]
r.Equal(u.Name.String, "Mark")
r.Equal(1, len(u.Houses))
r.Equal(u.Houses[0].Street, "Pop Avenue")
u = users[1]
r.Equal(u.Name.String, "Mark 2")
r.Equal(1, len(u.Houses))
r.Equal(u.Houses[0].Street, "Pop Avenue 2")
})
}
func Test_Load_Associations_Loaded_Model(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)
user := User{Name: nulls.NewString("Mark")}
err := tx.Create(&user)
r.NoError(err)
book := Book{Title: "Pop Book", Isbn: "PB1", UserID: nulls.NewInt(user.ID)}
err = tx.Create(&book)
r.NoError(err)
u := User{}
err = tx.Find(&u, user.ID)
r.NoError(err)
r.Zero(len(u.Books))
err = tx.Load(&u)
r.NoError(err)
r.Equal(len(u.Books), 1)
r.Equal(u.Books[0].Title, book.Title)
})
}
func Test_First(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)
first := User{Name: nulls.NewString("Mark")}
err := tx.Create(&first)
r.NoError(err)
last := User{Name: nulls.NewString("Mark")}
err = tx.Create(&last)
r.NoError(err)
u := User{}
err = tx.Where("name = 'Mark'").First(&u)
r.NoError(err)
r.Equal(first.ID, u.ID)
})
}
func Test_Last(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)
first := User{Name: nulls.NewString("Mark")}
err := tx.Create(&first)
r.NoError(err)
last := User{Name: nulls.NewString("Mark")}
err = tx.Create(&last)
r.NoError(err)
u := User{}
err = tx.Where("name = 'Mark'").Last(&u)
r.NoError(err)
r.Equal(last.ID, u.ID)
})
}
func Test_All(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)
for _, name := range []string{"Mark", "Joe", "Jane"} {
user := User{Name: nulls.NewString(name)}
err := tx.Create(&user)
r.NoError(err)
}
u := Users{}
err := tx.All(&u)
r.NoError(err)
r.Equal(len(u), 3)
u = Users{}
err = tx.Where("name = 'Mark'").All(&u)
r.NoError(err)
r.Equal(len(u), 1)
})
}
func Test_All_Eager_Slice_With_All(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)
for _, name := range []string{"Mark", "Joe", "Jane"} {
user := User{Name: nulls.NewString(name)}
err := tx.Create(&user)
r.NoError(err)
book := Book{Title: "Book of " + user.Name.String, UserID: nulls.NewInt(user.ID)}
err = tx.Create(&book)
r.NoError(err)
}
u := Users{}
err := tx.Eager("Books.User").All(&u)
r.NoError(err)
r.Equal(len(u), 3)
r.Equal(u[0].ID, u[0].Books[0].User.ID)
r.Equal(u[1].ID, u[1].Books[0].User.ID)
r.Equal(u[2].ID, u[2].Books[0].User.ID)
})
}
func Test_All_Eager(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)
for _, name := range []string{"Mark", "Joe", "Jane"} {
user := User{Name: nulls.NewString(name)}
err := tx.Create(&user)
r.NoError(err)
if name == "Mark" {
book := Book{Title: "Pop Book", Isbn: "PB1", UserID: nulls.NewInt(user.ID)}
err = tx.Create(&book)
r.NoError(err)
}
}
u := Users{}
err := tx.Eager(" Books ", " ").Where("name = 'Mark'").All(&u)
r.NoError(err)
r.Equal(len(u), 1)
r.Equal(len(u[0].Books), 1)
})
}
func Test_All_Eager_For_Query(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)
user := User{Name: nulls.NewString("Mark")}
err := tx.Create(&user)
r.NoError(err)
book := Book{Title: "Pop Book", Isbn: "PB1", UserID: nulls.NewInt(user.ID)}
err = tx.Create(&book)
r.NoError(err)
u := Users{}
q := tx.Q()
err = q.Eager("Books").Where("name = 'Mark'").All(&u)
r.NoError(err)
r.Equal(len(u), 1)
r.Equal(len(u[0].Books), 1)
})
}
func Test_All_Eager_Field_Not_Found_Error(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)
user := User{Name: nulls.NewString("Mark")}
err := tx.Create(&user)
r.NoError(err)
u := Users{}
err = tx.Eager("FieldNotFound").Where("name = 'Mark'").All(&u)
r.Error(err)
r.Equal("field FieldNotFound does not exist in model User", err.Error())
})
}
func Test_All_Eager_Allow_Chain_Call(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)
user := User{Name: nulls.NewString("Mark")}
err := tx.Create(&user)
r.NoError(err)
coolSong := Song{Title: "Hook - Blues Traveler", UserID: user.ID}
err = tx.Create(&coolSong)
r.NoError(err)
book := Book{Title: "Pop Book", Isbn: "PB1", UserID: nulls.NewInt(user.ID)}
err = tx.Create(&book)
r.NoError(err)
u := Users{}
err = tx.Eager("Books").Eager("FavoriteSong").Where("name = 'Mark'").All(&u)
r.NoError(err)
r.Equal(len(u), 1)
r.Equal(len(u[0].Books), 1)
r.Equal(u[0].FavoriteSong.Title, coolSong.Title)
})
}
func Test_Count(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)
user := User{Name: nulls.NewString("Mark")}
err := tx.Create(&user)
r.NoError(err)
c, err := tx.Count(&user)
r.NoError(err)
r.Equal(c, 1)
c, err = tx.Where("1=1").CountByField(&user, "distinct id")
r.NoError(err)
r.Equal(c, 1)
// should ignore order in count
c, err = tx.Order("id desc").Count(&user)
r.NoError(err)
r.Equal(c, 1)
})
}
func Test_Count_Disregards_Pagination(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)
names := []string{
"Jack",
"Hurley",
"Charlie",
"Desmond",
"Juliet",
"Locke",
"Sawyer",
"Kate",
"Benjamin Linus",
}
for _, name := range names {
user := User{Name: nulls.NewString(name)}
err := tx.Create(&user)
r.NoError(err)
}
first_users := Users{}
second_users := Users{}
q := tx.Paginate(1, 3)
q.All(&first_users)
r.Equal(len(names), q.Paginator.TotalEntriesSize) //ensure paginator populates count
r.Equal(3, len(first_users))
first_users = Users{}
q = tx.RawQuery("select * from users").Paginate(1, 3)
q.All(&first_users)
r.Equal(1, q.Paginator.Page)
r.Equal(3, q.Paginator.PerPage)
r.Equal(len(names), q.Paginator.TotalEntriesSize) //ensure paginator populates count
r.Equal(3, len(first_users))
totalFirstPage := q.Paginator.TotalPages
q = tx.Paginate(2, 3)
q.All(&second_users)
r.Equal(3, len(second_users))
totalSecondPage := q.Paginator.TotalPages
r.NotEqual(0, totalFirstPage)
r.NotEqual(0, totalSecondPage)
r.Equal(totalFirstPage, totalSecondPage)
first_users = Users{}
q = tx.RawQuery("select * from users limit 2").Paginate(1, 5)
err := q.All(&first_users)
r.NoError(err)
r.Equal(2, len(first_users)) //raw query limit applies
first_users = Users{}
q = tx.RawQuery("select * from users limit 2 offset 1").Paginate(1, 5)
err = q.All(&first_users)
r.NoError(err)
r.Equal(2, len(first_users))
first_users = Users{}
q = tx.RawQuery("select * from users limit 2 offset\t1").Paginate(1, 5)
err = q.All(&first_users)
r.NoError(err)
r.Equal(2, len(first_users))
first_users = Users{}
q = tx.RawQuery(`select * from users limit 2 offset
1`).Paginate(1, 5)
err = q.All(&first_users)
r.NoError(err)
r.Equal(2, len(first_users))
first_users = Users{}
q = tx.RawQuery(`select * from users limit 2 offset
1
`).Paginate(1, 5) //ending space and tab
err = q.All(&first_users)
r.NoError(err)
r.Equal(2, len(first_users))
if tx.Dialect.Name() == "sqlite" {
first_users = Users{}
q = tx.RawQuery("select * from users limit 2,1").Paginate(1, 5)
err = q.All(&first_users)
r.NoError(err)
r.Equal(2, len(first_users))
first_users = Users{}
q = tx.RawQuery("select * from users limit 2 , 1").Paginate(1, 5)
err = q.All(&first_users)
r.NoError(err)
r.Equal(2, len(first_users))
}
if tx.Dialect.Name() == "postgresql" {
first_users = Users{}
q = tx.RawQuery("select * from users FETCH FIRST 3 rows only").Paginate(1, 5)
err = q.All(&first_users)
r.NoError(err)
r.Equal(3, len(first_users)) //should fetch only 3
}
})
}
func Test_Count_RawQuery(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)
user := User{Name: nulls.NewString("Mark")}
err := tx.Create(&user)
r.NoError(err)
c, err := tx.RawQuery("select * from users as users").Count(nil)
r.NoError(err)
r.Equal(c, 1)
c, err = tx.RawQuery("select * from users as users where id = -1").Count(nil)
r.NoError(err)
r.Equal(c, 0)
c, err = tx.RawQuery("select name, max(created_at) from users as users group by name").Count(nil)
r.NoError(err)
r.Equal(c, 1)
c, err = tx.RawQuery("select name from users order by name asc limit 5").Count(nil)
r.NoError(err)
r.Equal(c, 1)
c, err = tx.RawQuery("select name from users order by name asc limit 5 offset 0").Count(nil)
r.NoError(err)
r.Equal(c, 1)
})
}
func Test_Exists(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)
t, _ := tx.Where("id = ?", 0).Exists("users")
r.False(t)
user := User{Name: nulls.NewString("Mark")}
err := tx.Create(&user)
r.NoError(err)
t, _ = tx.Where("id = ?", user.ID).Exists("users")
r.True(t)
})
}