MCPcopy
hub / github.com/go-gorm/gorm / TestFindInBatchesWithOffsetLimit

Function TestFindInBatchesWithOffsetLimit

tests/query_test.go:320–380  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

318}
319
320func TestFindInBatchesWithOffsetLimit(t *testing.T) {
321 users := []User{
322 *GetUser("find_in_batches_with_offset_limit", Config{}),
323 *GetUser("find_in_batches_with_offset_limit", Config{}),
324 *GetUser("find_in_batches_with_offset_limit", Config{}),
325 *GetUser("find_in_batches_with_offset_limit", Config{}),
326 *GetUser("find_in_batches_with_offset_limit", Config{}),
327 *GetUser("find_in_batches_with_offset_limit", Config{}),
328 *GetUser("find_in_batches_with_offset_limit", Config{}),
329 *GetUser("find_in_batches_with_offset_limit", Config{}),
330 *GetUser("find_in_batches_with_offset_limit", Config{}),
331 *GetUser("find_in_batches_with_offset_limit", Config{}),
332 }
333
334 DB.Create(&users)
335
336 var (
337 sub, results []User
338 lastBatch int
339 )
340
341 // offset limit
342 if result := DB.Offset(3).Limit(5).Where("name = ?", users[0].Name).FindInBatches(&sub, 2, func(tx *gorm.DB, batch int) error {
343 results = append(results, sub...)
344 lastBatch = batch
345 return nil
346 }); result.Error != nil || result.RowsAffected != 5 {
347 t.Errorf("Failed to batch find, got error %v, rows affected: %v", result.Error, result.RowsAffected)
348 }
349 if lastBatch != 3 {
350 t.Fatalf("incorrect last batch, expected: %v, got: %v", 3, lastBatch)
351 }
352
353 targetUsers := users[3:8]
354 for i := 0; i < len(targetUsers); i++ {
355 AssertEqual(t, results[i], targetUsers[i])
356 }
357
358 var sub1 []User
359 // limit < batchSize
360 if result := DB.Limit(5).Where("name = ?", users[0].Name).FindInBatches(&sub1, 10, func(tx *gorm.DB, batch int) error {
361 return nil
362 }); result.Error != nil || result.RowsAffected != 5 {
363 t.Errorf("Failed to batch find, got error %v, rows affected: %v", result.Error, result.RowsAffected)
364 }
365
366 var sub2 []User
367 // only offset
368 if result := DB.Offset(3).Where("name = ?", users[0].Name).FindInBatches(&sub2, 2, func(tx *gorm.DB, batch int) error {
369 return nil
370 }); result.Error != nil || result.RowsAffected != 7 {
371 t.Errorf("Failed to batch find, got error %v, rows affected: %v", result.Error, result.RowsAffected)
372 }
373
374 var sub3 []User
375 if result := DB.Limit(4).Where("name = ?", users[0].Name).FindInBatches(&sub3, 2, func(tx *gorm.DB, batch int) error {
376 return nil
377 }); result.Error != nil || result.RowsAffected != 4 {

Callers

nothing calls this directly

Calls 7

GetUserFunction · 0.85
CreateMethod · 0.65
FindInBatchesMethod · 0.65
WhereMethod · 0.65
LimitMethod · 0.65
OffsetMethod · 0.65
AssertEqualFunction · 0.50

Tested by

no test coverage detected