Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions tests/connection_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"testing"
"time"

"github.com/oracle-samples/gorm-oracle/oracle"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -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)
}
}
4 changes: 3 additions & 1 deletion tests/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}
57 changes: 43 additions & 14 deletions tests/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -536,47 +549,52 @@ 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)
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") {
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 {
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 {
if DB.Table("migrate_add_drop_columns").Migrator().HasColumn(&MigrateAddDropColumns{}, "NewName") {
t.Fatalf("Found renamed column")
}

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("column_structs").Migrator().HasColumn(&NewColumnStruct{}, "new_new_name") {
if DB.Table("migrate_add_drop_columns").Migrator().HasColumn(&MigrateAddDropColumns{}, "new_new_name") {
t.Fatalf("Found deleted column")
}
}
Expand Down Expand Up @@ -2073,6 +2091,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) {
Expand Down
29 changes: 29 additions & 0 deletions tests/sql_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down
Loading