From 2f08e50da5d37805af51ba05c6faefb39012ed09 Mon Sep 17 00:00:00 2001 From: shelly-li-sl Date: Tue, 30 Sep 2025 21:13:32 +0000 Subject: [PATCH 1/2] add more code coverage tests --- tests/connection_pool_test.go | 34 ++++++++++++++++ tests/info.go | 4 +- tests/migrate_test.go | 75 +++++++++++++++++++++++++---------- tests/sql_builder_test.go | 29 ++++++++++++++ 4 files changed, 119 insertions(+), 23 deletions(-) diff --git a/tests/connection_pool_test.go b/tests/connection_pool_test.go index fd62ca2..5ac394e 100644 --- a/tests/connection_pool_test.go +++ b/tests/connection_pool_test.go @@ -46,6 +46,7 @@ import ( "testing" "time" + "github.com/oracle-samples/gorm-oracle/oracle" "gorm.io/gorm" ) @@ -589,3 +590,36 @@ func TestConnectionPoolStats(t *testing.T) { t.Error("Idle connections should not be negative") } } + +func TestGormOpenWithExistingConn(t *testing.T) { + sqlDB, err := DB.DB() + if err != nil { + t.Fatalf("Failed to get underlying DB: %v", err) + } + + sqlDB.SetMaxOpenConns(10) + sqlDB.SetMaxIdleConns(5) + sqlDB.SetConnMaxLifetime(30 * time.Minute) + sqlDB.SetConnMaxIdleTime(5 * time.Minute) + + config := oracle.Config{ + Conn: sqlDB, + } + + dialector := oracle.New(config) + + newDB, err := gorm.Open(dialector, &gorm.Config{}) + if err != nil { + panic(err) + } + + underlyingDB, err := newDB.DB() + if err != nil { + panic(err) + } + + stats := underlyingDB.Stats() + if stats.MaxOpenConnections != 10 { + t.Errorf("Expected MaxOpenConnections: 10, got: %d", stats.MaxOpenConnections) + } +} diff --git a/tests/info.go b/tests/info.go index 57ca350..da13d8a 100644 --- a/tests/info.go +++ b/tests/info.go @@ -83,7 +83,7 @@ func GormInfo() { // Get ORACLE DATABASE VERSION var banner, bannerFull, bannerLegacy string var conID int - err = sqlDB.QueryRow(`SELECT BANNER, BANNER_FULL, BANNER_LEGACY, CON_ID FROM v$version WHERE banner LIKE 'Oracle Database%'`).Scan(&banner, &bannerFull, &bannerLegacy, &conID) + err = sqlDB.QueryRow(`SELECT BANNER, BANNER_FULL, BANNER_LEGACY, CON_ID FROM v$version WHERE banner LIKE 'Oracle % Database%'`).Scan(&banner, &bannerFull, &bannerLegacy, &conID) if err != nil { fmt.Printf("Failed to get Oracle DB version: %v\n", err) } else { @@ -115,5 +115,7 @@ func GormInfo() { fmt.Printf("Oracle Client library version : %s\n", clientVersion2) } + fmt.Printf("CurrentDatabase : %s\n", db.Migrator().CurrentDatabase()) + fmt.Printf("==========================================\n\n") } diff --git a/tests/migrate_test.go b/tests/migrate_test.go index cf819c9..d481b10 100644 --- a/tests/migrate_test.go +++ b/tests/migrate_test.go @@ -359,6 +359,19 @@ func TestMigrateWithUniqueIndex(t *testing.T) { if !DB.Migrator().HasIndex(&UserWithUniqueIndex{}, "idx_user_with_unique_indices_u_name") { t.Errorf("Failed to find created index") } + + type IndexWithOption struct { + ID int + Name string `gorm:"size:20;index:idx_name_opt,unique,option:TABLESPACE SYSAUX NOPARALLEL"` + } + DB.Migrator().DropTable(&IndexWithOption{}) + if err := DB.AutoMigrate(&IndexWithOption{}); err != nil { + t.Fatalf("failed to migrate, got %v", err) + } + + if !DB.Migrator().HasIndex(&IndexWithOption{}, "idx_name_opt") { + t.Errorf("Failed to find created index") + } } func TestMigrateTable(t *testing.T) { @@ -536,49 +549,56 @@ func TestMigrateColumns(t *testing.T) { } } } +} - type NewColumnStruct struct { +func TestMigrateAddDropColumns(t *testing.T) { + type MigrateAddDropColumns struct { gorm.Model Name string NewName string } - if err := DB.Table("column_structs").Migrator().AddColumn(&NewColumnStruct{}, "NewName"); err != nil { - t.Fatalf("Failed to add column, got %v", err) - } + DB.Migrator().DropTable(&MigrateAddDropColumns{}) + DB.AutoMigrate(&MigrateAddDropColumns{}) - if !DB.Table("column_structs").Migrator().HasColumn(&NewColumnStruct{}, "NewName") { - t.Fatalf("Failed to find added column") + if err := DB.Table("migrate_add_drop_columns").Migrator().AddColumn(&MigrateAddDropColumns{}, "NewName"); err == nil { + t.Fatalf("Should fail to add column with existing name") } - if err := DB.Table("column_structs").Migrator().DropColumn(&NewColumnStruct{}, "NewName"); err != nil { - t.Fatalf("Failed to add column, got %v", err) - } + // https://github.com/oracle-samples/gorm-oracle/issues/80 + // if err := DB.Table("migrate_add_drop_columns").Migrator().DropColumn(&MigrateAddDropColumns{}, "NewName"); err != nil { + // t.Fatalf("Failed to drop column, got %v", err) + // } - if DB.Table("column_structs").Migrator().HasColumn(&NewColumnStruct{}, "NewName") { - t.Fatalf("Found deleted column") - } + // if DB.Table("migrate_add_drop_columns").Migrator().HasColumn(&MigrateAddDropColumns{}, "NewName") { + // t.Fatalf("Found deleted column") + // } - if err := DB.Table("column_structs").Migrator().AddColumn(&NewColumnStruct{}, "NewName"); err != nil { - t.Fatalf("Failed to add column, got %v", err) - } + // if err := DB.Table("migrate_add_drop_columns").Migrator().AddColumn(&MigrateAddDropColumns{}, "NewName"); err != nil { + // t.Fatalf("Failed to add column, got %v", err) + // } - if err := DB.Table("column_structs").Migrator().RenameColumn(&NewColumnStruct{}, "NewName", + if err := DB.Table("migrate_add_drop_columns").Migrator().RenameColumn(&MigrateAddDropColumns{}, "NewName", "new_new_name"); err != nil { t.Fatalf("Failed to add column, got %v", err) } - if !DB.Table("column_structs").Migrator().HasColumn(&NewColumnStruct{}, "new_new_name") { + if !DB.Table("migrate_add_drop_columns").Migrator().HasColumn(&MigrateAddDropColumns{}, "new_new_name") { t.Fatalf("Failed to found renamed column") } - if err := DB.Table("column_structs").Migrator().DropColumn(&NewColumnStruct{}, "new_new_name"); err != nil { - t.Fatalf("Failed to add column, got %v", err) + if DB.Table("migrate_add_drop_columns").Migrator().HasColumn(&MigrateAddDropColumns{}, "NewName") { + t.Fatalf("Found renamed column") } - if DB.Table("column_structs").Migrator().HasColumn(&NewColumnStruct{}, "new_new_name") { - t.Fatalf("Found deleted column") - } + // https://github.com/oracle-samples/gorm-oracle/issues/80 + // if err := DB.Table("migrate_add_drop_columns").Migrator().DropColumn(&MigrateAddDropColumns{}, "new_new_name"); err != nil { + // t.Fatalf("Failed to add column, got %v", err) + // } + + // if DB.Table("migrate_add_drop_columns").Migrator().HasColumn(&MigrateAddDropColumns{}, "new_new_name") { + // t.Fatalf("Found deleted column") + // } } func TestMigrateConstraint(t *testing.T) { @@ -2073,6 +2093,17 @@ func TestOracleErrorHandling(t *testing.T) { if err := DB.AutoMigrate(&TestModel{}); err != nil { t.Fatalf("Duplicate table creation should not error: %v", err) } + + type NotAModel struct{} + if err := DB.Migrator().CreateTable(NotAModel{}); err == nil { + t.Fatalf("table creation with empty struct shoud report error: %v", err) + } + + err := DB.Migrator().CreateTable("not_a_model_name") + if err != nil && err.Error() != "failed to get schema" { + t.Fatalf("Expect 'failed to get schema', but got : %v", err) + } + } func TestMigrateOnUpdateConstraint(t *testing.T) { diff --git a/tests/sql_builder_test.go b/tests/sql_builder_test.go index a1aa92f..b3559ef 100644 --- a/tests/sql_builder_test.go +++ b/tests/sql_builder_test.go @@ -829,6 +829,19 @@ func TestToSQL(t *testing.T) { }) assertEqualSQL(t, `INSERT INTO "users" ("created_at","updated_at","deleted_at","name","age","birthday","company_id","manager_id","active") VALUES ('2021-10-18 00:00:00','2021-10-18 00:00:00',NULL,'foo',20,NULL,NULL,NULL,false) RETURNING "id" INTO .*`, sql) + // insert with explicit Table via clause.Insert + user = &User{Name: "bar", Age: 42} + user.CreatedAt = date + user.UpdatedAt = date + sql = DB.ToSQL(func(tx *gorm.DB) *gorm.DB { + return tx.Clauses(clause.Insert{Table: clause.Table{Name: "custom_table"}}).Create(user) + }) + assertEqualSQL(t, `INSERT INTO "custom_table" ("created_at","updated_at","deleted_at","name","age","birthday","company_id","manager_id","active") VALUES ('2021-10-18 00:00:00','2021-10-18 00:00:00',NULL,'bar',42,NULL,NULL,NULL,false) RETURNING "id" INTO .*`, sql) + sql = DB.ToSQL(func(tx *gorm.DB) *gorm.DB { + return tx.Clauses(clause.Insert{Table: clause.Table{Name: "custom_table"}}).Unscoped().Create(user) + }) + assertEqualSQL(t, `INSERT INTO "custom_table" ("created_at","updated_at","deleted_at","name","age","birthday","company_id","manager_id","active") VALUES ('2021-10-18 00:00:00','2021-10-18 00:00:00',NULL,'bar',42,NULL,NULL,NULL,false) RETURNING "id" INTO .*`, sql) + // updates user = &User{Name: "bar", Age: 22} user.CreatedAt = date @@ -838,6 +851,22 @@ func TestToSQL(t *testing.T) { }) assertEqualSQL(t, `UPDATE "users" SET "created_at"='2021-10-18 00:00:00',"updated_at"='2021-10-18 19:50:09.438',"name"='bar',"age"=22 WHERE id = 100 AND "users"."deleted_at" IS NULL`, sql) + // UPDATE with explicit Table via clause.Update + sql = DB.ToSQL(func(tx *gorm.DB) *gorm.DB { + return tx.Clauses(clause.Update{Table: clause.Table{Name: "custom_update_table"}}).Unscoped(). + Where("id = ?", 200). + Updates(&User{Name: "patched", Age: 99}) + }) + assertEqualSQL(t, `UPDATE "custom_update_table" SET "updated_at"=?,"name"='patched',"age"=99 WHERE id = 200`, sql) + + // https://github.com/oracle-samples/gorm-oracle/issues/81 + // sql = DB.ToSQL(func(tx *gorm.DB) *gorm.DB { + // return tx.Clauses(clause.Update{Table: clause.Table{Name: "custom_update_table"}}). + // Where("id = ?", 200). + // Updates(&User{Name: "patched", Age: 99}) + // }) + // assertEqualSQL(t, `UPDATE "custom_update_table" SET "updated_at"=?,"name"='patched',"age"=99 WHERE id = 200 AND "custom_update_table"."deleted_at" IS NULL`, sql) + // update sql = DB.ToSQL(func(tx *gorm.DB) *gorm.DB { return tx.Model(&User{}).Where("id = ?", 100).Update("name", "Foo bar") From cab9702cf3ad3148251229759fba42b6ee3c4c6d Mon Sep 17 00:00:00 2001 From: shelly-li-sl Date: Tue, 30 Sep 2025 21:20:19 +0000 Subject: [PATCH 2/2] uncomment tests related to issue #80 --- tests/migrate_test.go | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/tests/migrate_test.go b/tests/migrate_test.go index d481b10..2da7589 100644 --- a/tests/migrate_test.go +++ b/tests/migrate_test.go @@ -565,18 +565,17 @@ func TestMigrateAddDropColumns(t *testing.T) { t.Fatalf("Should fail to add column with existing name") } - // https://github.com/oracle-samples/gorm-oracle/issues/80 - // if err := DB.Table("migrate_add_drop_columns").Migrator().DropColumn(&MigrateAddDropColumns{}, "NewName"); err != nil { - // t.Fatalf("Failed to drop column, got %v", err) - // } + if err := DB.Table("migrate_add_drop_columns").Migrator().DropColumn(&MigrateAddDropColumns{}, "NewName"); err != nil { + t.Fatalf("Failed to drop column, got %v", err) + } - // if DB.Table("migrate_add_drop_columns").Migrator().HasColumn(&MigrateAddDropColumns{}, "NewName") { - // t.Fatalf("Found deleted column") - // } + if DB.Table("migrate_add_drop_columns").Migrator().HasColumn(&MigrateAddDropColumns{}, "NewName") { + t.Fatalf("Found deleted column") + } - // if err := DB.Table("migrate_add_drop_columns").Migrator().AddColumn(&MigrateAddDropColumns{}, "NewName"); err != nil { - // t.Fatalf("Failed to add column, got %v", err) - // } + if err := DB.Table("migrate_add_drop_columns").Migrator().AddColumn(&MigrateAddDropColumns{}, "NewName"); err != nil { + t.Fatalf("Failed to add column, got %v", err) + } if err := DB.Table("migrate_add_drop_columns").Migrator().RenameColumn(&MigrateAddDropColumns{}, "NewName", "new_new_name"); err != nil { @@ -591,14 +590,13 @@ func TestMigrateAddDropColumns(t *testing.T) { t.Fatalf("Found renamed column") } - // https://github.com/oracle-samples/gorm-oracle/issues/80 - // if err := DB.Table("migrate_add_drop_columns").Migrator().DropColumn(&MigrateAddDropColumns{}, "new_new_name"); err != nil { - // t.Fatalf("Failed to add column, got %v", err) - // } + if err := DB.Table("migrate_add_drop_columns").Migrator().DropColumn(&MigrateAddDropColumns{}, "new_new_name"); err != nil { + t.Fatalf("Failed to add column, got %v", err) + } - // if DB.Table("migrate_add_drop_columns").Migrator().HasColumn(&MigrateAddDropColumns{}, "new_new_name") { - // t.Fatalf("Found deleted column") - // } + if DB.Table("migrate_add_drop_columns").Migrator().HasColumn(&MigrateAddDropColumns{}, "new_new_name") { + t.Fatalf("Found deleted column") + } } func TestMigrateConstraint(t *testing.T) {