Skip to content

Commit 223c0e9

Browse files
Merge pull request #30 from oracle-samples/tinglwan-fix-miscellaneous-tests
Fix miscellaneous tests
2 parents fd83ff2 + 17e41a2 commit 223c0e9

File tree

6 files changed

+28
-20
lines changed

6 files changed

+28
-20
lines changed

.github/workflows/run-tests.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ jobs:
4747
cd tests
4848
go get -t github.com/oracle-samples/gorm-oracle/tests
4949
go get .
50-
go test -failfast
50+
go test -failfast

tests/create_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ func TestCreateFromMap(t *testing.T) {
181181
}
182182

183183
func TestCreateWithAssociations(t *testing.T) {
184-
t.Skip()
185184
user := *GetUser("create_with_associations", Config{
186185
Account: true,
187186
Pets: 2,
@@ -200,7 +199,7 @@ func TestCreateWithAssociations(t *testing.T) {
200199
CheckUser(t, user, user)
201200

202201
var user2 User
203-
DB.Preload("Account").Preload("Pets").Preload("Toys").Preload("Company").Preload("Manager").Preload("Team").Preload("Languages").Preload("Friends").Find(&user2, "id = ?", user.ID)
202+
DB.Preload("Account").Preload("Pets").Preload("Toys").Preload("Company").Preload("Manager").Preload("Team").Preload("Languages").Preload("Friends").Find(&user2, "\"id\" = ?", user.ID)
204203
CheckUser(t, user2, user)
205204
}
206205

@@ -265,7 +264,6 @@ func TestBulkCreatePtrDataWithAssociations(t *testing.T) {
265264
}
266265

267266
func TestPolymorphicHasOne(t *testing.T) {
268-
t.Skip()
269267
t.Run("Struct", func(t *testing.T) {
270268
pet := Pet{
271269
Name: "PolymorphicHasOne",
@@ -279,7 +277,7 @@ func TestPolymorphicHasOne(t *testing.T) {
279277
CheckPet(t, pet, pet)
280278

281279
var pet2 Pet
282-
DB.Preload("Toy").Find(&pet2, "id = ?", pet.ID)
280+
DB.Preload("Toy").Find(&pet2, "\"id\" = ?", pet.ID)
283281
CheckPet(t, pet2, pet)
284282
})
285283

tests/hooks_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,14 @@ func (s Product2) BeforeCreate(tx *gorm.DB) (err error) {
274274
}
275275

276276
func (s *Product2) BeforeUpdate(tx *gorm.DB) (err error) {
277-
tx.Statement.Where("\"owner\" != ?", "admin")
277+
// In Oracle DB, when owner is NULL, the condition "owner" != 'admin' will
278+
// always returns false.
279+
// Hence, we use NVL("owner", 'none') to substitute 'none' when owner is NULL.
280+
tx.Statement.Where("NVL(\"owner\", 'none') != ?", "admin")
278281
return
279282
}
280283

281284
func TestUseDBInHooks(t *testing.T) {
282-
t.Skip()
283285
DB.Migrator().DropTable(&Product2{})
284286
DB.AutoMigrate(&Product2{})
285287

@@ -296,12 +298,12 @@ func TestUseDBInHooks(t *testing.T) {
296298
}
297299

298300
var result Product2
299-
if err := DB.First(&result, "name = ?", "Nice").Error; err != nil {
301+
if err := DB.First(&result, "\"name\" = ?", "Nice").Error; err != nil {
300302
t.Fatalf("Failed to query product, got error: %v", err)
301303
}
302304

303305
var resultClone Product2
304-
if err := DB.First(&resultClone, "name = ?", "Nice_clone").Error; err != nil {
306+
if err := DB.First(&resultClone, "\"name\" = ?", "Nice_clone").Error; err != nil {
305307
t.Fatalf("Failed to find cloned product, got error: %v", err)
306308
}
307309

@@ -311,7 +313,7 @@ func TestUseDBInHooks(t *testing.T) {
311313

312314
DB.Model(&result).Update("Price", 500)
313315
var result2 Product2
314-
DB.First(&result2, "name = ?", "Nice")
316+
DB.First(&result2, "\"name\" = ?", "Nice")
315317

316318
if result2.Price != 500 {
317319
t.Errorf("Failed to update product's price, expects: %v, got %v", 500, result2.Price)
@@ -323,13 +325,13 @@ func TestUseDBInHooks(t *testing.T) {
323325
}
324326

325327
var result3 Product2
326-
if err := DB.First(&result3, "name = ?", "Nice2").Error; err != nil {
328+
if err := DB.First(&result3, "\"name\" = ?", "Nice2").Error; err != nil {
327329
t.Fatalf("Failed to query product, got error: %v", err)
328330
}
329331

330332
DB.Model(&result3).Update("Price", 800)
331333
var result4 Product2
332-
DB.First(&result4, "name = ?", "Nice2")
334+
DB.First(&result4, "\"name\" = ?", "Nice2")
333335

334336
if result4.Price != 600 {
335337
t.Errorf("Admin product's price should not be changed, expects: %v, got %v", 600, result4.Price)

tests/main_test.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@
3939
package tests
4040

4141
import (
42-
"fmt"
42+
"strings"
4343
"testing"
4444
"time"
45-
"strings"
4645

4746
. "github.com/oracle-samples/gorm-oracle/tests/utils"
4847
)
@@ -54,9 +53,6 @@ func TestExceptionsWithInvalidSql(t *testing.T) {
5453
t.Errorf("Should got error with invalid SQL")
5554
}
5655

57-
tx := DB.Model(&User{}).Where("name = ?", "sd;;;aa").Pluck("name", &columns)
58-
fmt.Println(tx.Error)
59-
6056
if DB.Model(&User{}).Where("sdsd.zaaa = ?", "sd;;;aa").Pluck("aaa", &columns).Error == nil {
6157
t.Errorf("Should got error with invalid SQL")
6258
}
@@ -65,13 +61,20 @@ func TestExceptionsWithInvalidSql(t *testing.T) {
6561
t.Errorf("Should got error with invalid SQL")
6662
}
6763

64+
// Create a user
65+
user := *GetUser("user-for-test-exceptions-with-invalid-sql", Config{})
66+
67+
if results := DB.Create(&user); results.Error != nil {
68+
t.Errorf("errors happened when create: %v", results.Error)
69+
}
70+
6871
var count1, count2 int64
6972
DB.Model(&User{}).Count(&count1)
7073
if count1 <= 0 {
7174
t.Errorf("Should find some users")
7275
}
7376

74-
if DB.Where("name = ?", "jinzhu; delete * from users").First(&User{}).Error == nil {
77+
if DB.Where("\"name\" = ?", "jinzhu; delete * from users").First(&User{}).Error == nil {
7578
t.Errorf("Should got error with invalid SQL")
7679
}
7780

tests/scan_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,13 @@ func TestScanRowsNullValuesScanToFieldDefault(t *testing.T) {
303303
}
304304

305305
func TestScanToEmbedded(t *testing.T) {
306-
t.Skip()
306+
// TODO: Fix the issue that PersonAddress is skipped when dropping the tables together with Person and Address
307+
DB.Migrator().DropTable(&PersonAddress{})
308+
DB.Migrator().DropTable(&Person{}, &Address{}, &PersonAddress{})
309+
if err := DB.AutoMigrate(&Person{}, &Address{}, &PersonAddress{}); err != nil {
310+
t.Fatalf("Failed to migrate, got %v", err)
311+
}
312+
307313
person1 := Person{Name: "person 1"}
308314
person2 := Person{Name: "person 2"}
309315
DB.Save(&person1).Save(&person2)

tests/update_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,6 @@ func TestBatchUpdateSlice(t *testing.T) {
10291029
}
10301030
}
10311031
func TestMixedSaveBatch(t *testing.T) {
1032-
t.Skip()
10331032
users := []*User{
10341033
GetUser("existing1", Config{}),
10351034
GetUser("existing2", Config{}),

0 commit comments

Comments
 (0)