From b0b7a57b9f28e68ed97bff201b23b40b207b1021 Mon Sep 17 00:00:00 2001 From: Saeid Saeidee Date: Wed, 2 Aug 2023 22:35:00 +0200 Subject: [PATCH 1/2] test: coverage for tabletype added --- tests/migrate_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tests/migrate_test.go b/tests/migrate_test.go index 69f86412b..933ea5e0c 100644 --- a/tests/migrate_test.go +++ b/tests/migrate_test.go @@ -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) + } +} From 71e3342b6d1f7317af922e5230c574460d402b8c Mon Sep 17 00:00:00 2001 From: Saeid Saeidee Date: Wed, 2 Aug 2023 23:34:35 +0200 Subject: [PATCH 2/2] test: tidb exclueded --- tests/helper_test.go | 4 ++++ tests/migrate_test.go | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/helper_test.go b/tests/helper_test.go index c34e357cf..1a4874eeb 100644 --- a/tests/helper_test.go +++ b/tests/helper_test.go @@ -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() diff --git a/tests/migrate_test.go b/tests/migrate_test.go index 933ea5e0c..849e2b7bd 100644 --- a/tests/migrate_test.go +++ b/tests/migrate_test.go @@ -1601,7 +1601,7 @@ func TestMigrateExistingBoolColumnPG(t *testing.T) { func TestTableType(t *testing.T) { // currently it is only supported for mysql driver - if DB.Dialector.Name() != "mysql" { + if !isMysql() { return }