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

Function TestGenericsFindInBatches

tests/generics_test.go:294–324  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

292}
293
294func TestGenericsFindInBatches(t *testing.T) {
295 ctx := context.Background()
296
297 users := []User{
298 {Name: "GenericsFindBatchA"},
299 {Name: "GenericsFindBatchB"},
300 {Name: "GenericsFindBatchC"},
301 {Name: "GenericsFindBatchD"},
302 {Name: "GenericsFindBatchE"},
303 }
304 if err := gorm.G[User](DB).CreateInBatches(ctx, &users, len(users)); err != nil {
305 t.Fatalf("CreateInBatches failed: %v", err)
306 }
307
308 total := 0
309 err := gorm.G[User](DB).Where("name like ?", "GenericsFindBatch%").FindInBatches(ctx, 2, func(chunk []User, batch int) error {
310 if len(chunk) > 2 {
311 t.Errorf("batch size exceed 2: got %d", len(chunk))
312 }
313
314 total += len(chunk)
315 return nil
316 })
317 if err != nil {
318 t.Fatalf("FindInBatches failed: %v", err)
319 }
320
321 if total != len(users) {
322 t.Errorf("expected total %d, got %d", len(users), total)
323 }
324}
325
326func TestGenericsScopes(t *testing.T) {
327 ctx := context.Background()

Callers

nothing calls this directly

Calls 3

CreateInBatchesMethod · 0.65
FindInBatchesMethod · 0.65
WhereMethod · 0.65

Tested by

no test coverage detected