test: coverage for tabletype added

This commit is contained in:
Saeid Saeidee 2023-08-02 22:35:00 +02:00
parent a7f01bd1b2
commit b0b7a57b9f

View File

@ -1598,3 +1598,48 @@ func TestMigrateExistingBoolColumnPG(t *testing.T) {
}
}
}
func TestTableType(t *testing.T) {
// currently it is only supported for mysql driver
if DB.Dialector.Name() != "mysql" {
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)
}
}