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

Function TestCreateWithAutoIncrementCompositeKey

tests/create_test.go:569–599  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

567}
568
569func TestCreateWithAutoIncrementCompositeKey(t *testing.T) {
570 type CompositeKeyProduct struct {
571 ProductID int `gorm:"primaryKey;autoIncrement:true;"` // primary key
572 LanguageCode int `gorm:"primaryKey;"` // primary key
573 Code string
574 Name string
575 }
576
577 if err := DB.Migrator().DropTable(&CompositeKeyProduct{}); err != nil {
578 t.Fatalf("failed to migrate, got error %v", err)
579 }
580 if err := DB.AutoMigrate(&CompositeKeyProduct{}); err != nil {
581 t.Fatalf("failed to migrate, got error %v", err)
582 }
583
584 prod := &CompositeKeyProduct{
585 LanguageCode: 56,
586 Code: "Code56",
587 Name: "ProductName56",
588 }
589 if err := DB.Create(&prod).Error; err != nil {
590 t.Fatalf("failed to create, got error %v", err)
591 }
592
593 newProd := &CompositeKeyProduct{}
594 if err := DB.First(&newProd).Error; err != nil {
595 t.Fatalf("errors happened when query: %v", err)
596 } else {
597 AssertObjEqual(t, newProd, prod, "ProductID", "LanguageCode", "Code", "Name")
598 }
599}
600
601func TestCreateOnConflictWithDefaultNull(t *testing.T) {
602 type OnConflictUser struct {

Callers

nothing calls this directly

Calls 6

AssertObjEqualFunction · 0.85
DropTableMethod · 0.65
MigratorMethod · 0.65
AutoMigrateMethod · 0.65
CreateMethod · 0.65
FirstMethod · 0.65

Tested by

no test coverage detected