Skip to content

Commit 87b38ab

Browse files
committed
Add minor fixes and tests
1 parent e6988a1 commit 87b38ab

File tree

4 files changed

+87
-3
lines changed

4 files changed

+87
-3
lines changed

oracle/oracle.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ import (
6161
_ "github.com/godror/godror"
6262
)
6363

64+
const DefaultDriverName string = "godror"
65+
6466
type Config struct {
6567
DriverName string
6668
DataSourceName string
@@ -80,7 +82,7 @@ func (d Dialector) Name() string {
8082

8183
// Open creates a new godror Dialector with the given DSN
8284
func Open(dsn string) gorm.Dialector {
83-
return &Dialector{Config: &Config{DriverName: "godror", DataSourceName: dsn}}
85+
return &Dialector{Config: &Config{DataSourceName: dsn}}
8486
}
8587

8688
// New creates a new Dialector with the given config
@@ -90,6 +92,10 @@ func New(config Config) gorm.Dialector {
9092

9193
// Initializes the database connection
9294
func (d Dialector) Initialize(db *gorm.DB) (err error) {
95+
if d.DriverName == "" {
96+
d.DriverName = DefaultDriverName
97+
}
98+
9399
d.DefaultStringSize = 4000
94100

95101
config := &callbacks.Config{

tests/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/config_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package tests
2+
3+
import (
4+
"regexp"
5+
"testing"
6+
7+
"gorm.io/gorm"
8+
)
9+
10+
type Student struct {
11+
ID uint
12+
Name string
13+
}
14+
15+
func (s Student) TableName() string {
16+
return "STUDENTS"
17+
}
18+
19+
func TestSkipQuoteIdentifiers(t *testing.T) {
20+
db, err := openTestDBWithOptions(true, &gorm.Config{
21+
Logger: newLogger,
22+
})
23+
if err != nil {
24+
t.Fatalf("failed to connect database, got error %v", err)
25+
}
26+
27+
db.Migrator().DropTable(&Student{})
28+
db.Migrator().CreateTable(&Student{})
29+
30+
if !db.Migrator().HasTable(&Student{}) {
31+
t.Errorf("Failed to get table: student")
32+
}
33+
34+
if !db.Migrator().HasColumn(&Student{}, "ID") {
35+
t.Errorf("Failed to get column: id")
36+
}
37+
38+
if !db.Migrator().HasColumn(&Student{}, "NAME") {
39+
t.Errorf("Failed to get column: name")
40+
}
41+
42+
dryrunDB := db.Session(&gorm.Session{DryRun: true})
43+
44+
result := dryrunDB.Model(&Student{}).Create(&Student{ID: 1, Name: "John"})
45+
if !regexp.MustCompile(`^INSERT INTO STUDENTS \(name,id\) VALUES \(:1,:2\)$`).MatchString(result.Statement.SQL.String()) {
46+
t.Errorf("invalid insert SQL, got %v", result.Statement.SQL.String())
47+
}
48+
49+
result = dryrunDB.First(&Student{})
50+
if !regexp.MustCompile(`^SELECT \* FROM STUDENTS ORDER BY STUDENTS\.id FETCH NEXT 1 ROW ONLY$`).MatchString(result.Statement.SQL.String()) {
51+
t.Fatalf("SQL should include selected names, but got %v", result.Statement.SQL.String())
52+
}
53+
54+
result = dryrunDB.Find(&Student{ID: 1, Name: "John"})
55+
if !regexp.MustCompile(`^SELECT \* FROM STUDENTS WHERE STUDENTS\.id = :1$`).MatchString(result.Statement.SQL.String()) {
56+
t.Fatalf("SQL should include selected names, but got %v", result.Statement.SQL.String())
57+
}
58+
59+
result = dryrunDB.Save(&Student{ID: 2, Name: "Mary"})
60+
if !regexp.MustCompile(`^UPDATE STUDENTS SET name=:1 WHERE id = :2$`).MatchString(result.Statement.SQL.String()) {
61+
t.Fatalf("SQL should include selected names, but got %v", result.Statement.SQL.String())
62+
}
63+
64+
// Update with conditions
65+
result = dryrunDB.Model(&Student{}).Where("id = ?", 1).Update("name", "hello")
66+
if !regexp.MustCompile(`^UPDATE STUDENTS SET name=:1 WHERE id = :2$`).MatchString(result.Statement.SQL.String()) {
67+
t.Fatalf("SQL should include selected names, but got %v", result.Statement.SQL.String())
68+
}
69+
}

tests/tests_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,22 @@ func init() {
118118
}
119119

120120
func OpenTestConnection(cfg *gorm.Config) (db *gorm.DB, err error) {
121+
return openTestDBWithOptions(false, cfg)
122+
}
123+
124+
func openTestDBWithOptions(skipQuoteIdentifiers bool, cfg *gorm.Config) (db *gorm.DB, err error) {
121125
dbDSN := os.Getenv("GORM_DSN")
122126
log.Println("testing oracle...")
123127
if dbDSN == "" {
124128
dbDSN = oracleDSN
125129
}
126-
db, err = gorm.Open(oracle.Open(dbDSN), cfg)
130+
db, err = gorm.Open(
131+
oracle.New(oracle.Config{
132+
SkipQuoteIdentifiers: skipQuoteIdentifiers,
133+
DataSourceName: dbDSN,
134+
}),
135+
cfg,
136+
)
127137

128138
if err != nil {
129139
return

0 commit comments

Comments
 (0)