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

Function TestGenericsWithTransaction

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

Source from the content-addressed store, hash-verified

944}
945
946func TestGenericsWithTransaction(t *testing.T) {
947 ctx := context.Background()
948 tx := DB.Begin()
949 if tx.Error != nil {
950 t.Fatalf("failed to begin transaction: %v", tx.Error)
951 }
952
953 users := []User{{Name: "TestGenericsTransaction", Age: 18}, {Name: "TestGenericsTransaction2", Age: 18}}
954 err := gorm.G[User](tx).CreateInBatches(ctx, &users, 2)
955
956 count, err := gorm.G[User](tx).Where("name like ?", "TestGenericsTransaction%").Count(ctx, "*")
957 if err != nil {
958 t.Fatalf("Count failed: %v", err)
959 }
960 if count != 2 {
961 t.Errorf("expected 2 records, got %d", count)
962 }
963
964 if err := tx.Rollback().Error; err != nil {
965 t.Fatalf("failed to rollback transaction: %v", err)
966 }
967
968 count2, err := gorm.G[User](DB).Where("name like ?", "TestGenericsTransaction%").Count(ctx, "*")
969 if err != nil {
970 t.Fatalf("Count failed: %v", err)
971 }
972 if count2 != 0 {
973 t.Errorf("expected 0 records after rollback, got %d", count2)
974 }
975}
976
977func TestGenericsToSQL(t *testing.T) {
978 ctx := context.Background()

Callers

nothing calls this directly

Calls 5

BeginMethod · 0.80
CreateInBatchesMethod · 0.65
CountMethod · 0.65
WhereMethod · 0.65
RollbackMethod · 0.65

Tested by

no test coverage detected