| 102 | } |
| 103 | |
| 104 | func TestGenericsCreateInBatches(t *testing.T) { |
| 105 | batch := []User{ |
| 106 | {Name: "GenericsCreateInBatches1"}, |
| 107 | {Name: "GenericsCreateInBatches2"}, |
| 108 | {Name: "GenericsCreateInBatches3"}, |
| 109 | } |
| 110 | ctx := context.Background() |
| 111 | |
| 112 | if err := gorm.G[User](DB).CreateInBatches(ctx, &batch, 2); err != nil { |
| 113 | t.Fatalf("CreateInBatches failed: %v", err) |
| 114 | } |
| 115 | |
| 116 | for _, u := range batch { |
| 117 | if u.ID == 0 { |
| 118 | t.Fatalf("no primary key found for %v", u) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | count, err := gorm.G[User](DB).Where("name like ?", "GenericsCreateInBatches%").Count(ctx, "*") |
| 123 | if err != nil { |
| 124 | t.Fatalf("Count failed: %v", err) |
| 125 | } |
| 126 | if count != 3 { |
| 127 | t.Errorf("expected 3 records, got %d", count) |
| 128 | } |
| 129 | |
| 130 | found, err := gorm.G[User](DB).Raw("SELECT * FROM users WHERE name LIKE ?", "GenericsCreateInBatches%").Find(ctx) |
| 131 | if len(found) != len(batch) { |
| 132 | t.Errorf("expected %d from Raw Find, got %d", len(batch), len(found)) |
| 133 | } |
| 134 | |
| 135 | found, err = gorm.G[User](DB).Where("name like ?", "GenericsCreateInBatches%").Limit(2).Find(ctx) |
| 136 | if len(found) != 2 { |
| 137 | t.Errorf("expected %d from Raw Find, got %d", 2, len(found)) |
| 138 | } |
| 139 | |
| 140 | found, err = gorm.G[User](DB).Where("name like ?", "GenericsCreateInBatches%").Offset(2).Limit(2).Find(ctx) |
| 141 | if len(found) != 1 { |
| 142 | t.Errorf("expected %d from Raw Find, got %d", 1, len(found)) |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | func TestGenericsExecAndUpdate(t *testing.T) { |
| 147 | ctx := context.Background() |