Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: coverage for tabletype added #6496

Merged
merged 2 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions tests/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ func isTiDB() bool {
return os.Getenv("GORM_DIALECT") == "tidb"
}

func isMysql() bool {
return os.Getenv("GORM_DIALECT") == "mysql"
}

func db(unscoped bool) *gorm.DB {
if unscoped {
return DB.Unscoped()
Expand Down
45 changes: 45 additions & 0 deletions tests/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1598,3 +1598,48 @@ func TestMigrateExistingBoolColumnPG(t *testing.T) {
}
}
}

func TestTableType(t *testing.T) {
// currently it is only supported for mysql driver
if !isMysql() {
return
}

const tblName = "cities"
const tblSchema = "gorm"
const tblType = "BASE TABLE"
const tblComment = "foobar comment"

type City struct {
gorm.Model
Name string `gorm:"unique"`
}

DB.Migrator().DropTable(&City{})

if err := DB.Set("gorm:table_options", fmt.Sprintf("ENGINE InnoDB COMMENT '%s'", tblComment)).AutoMigrate(&City{}); err != nil {
t.Fatalf("failed to migrate cities tables, got error: %v", err)
}

tableType, err := DB.Table("cities").Migrator().TableType(&City{})
if err != nil {
t.Fatalf("failed to get table type, got error %v", err)
}

if tableType.Schema() != tblSchema {
t.Fatalf("expected tblSchema to be %s but got %s", tblSchema, tableType.Schema())
}

if tableType.Name() != tblName {
t.Fatalf("expected table name to be %s but got %s", tblName, tableType.Name())
}

if tableType.Type() != tblType {
t.Fatalf("expected table type to be %s but got %s", tblType, tableType.Type())
}

comment, ok := tableType.Comment()
if !ok || comment != tblComment {
t.Fatalf("expected comment %s got %s", tblComment, comment)
}
}