Skip to content

Commit

Permalink
test: coverage for tabletype added (#6496)
Browse files Browse the repository at this point in the history
* test: coverage for tabletype added

* test: tidb exclueded

---------

Co-authored-by: Saeid Saeidee <[email protected]>
  • Loading branch information
saeidee and Saeid Saeidee authored Aug 4, 2023
1 parent a7f01bd commit 1fb26ac
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
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)
}
}

0 comments on commit 1fb26ac

Please sign in to comment.