test: coverage for tabletype added (#6496)

* test: coverage for tabletype added

* test: tidb exclueded

---------

Co-authored-by: Saeid Saeidee <s.saeidee@sensysgatso.com>
This commit is contained in:
Saeid 2023-08-04 04:30:07 +02:00 committed by GitHub
parent a7f01bd1b2
commit 1fb26ac90e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 0 deletions

View File

@ -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()

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 !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)
}
}