Skip to content

Commit c86c817

Browse files
authored
Merge pull request #11 from oracle-samples/query_tests_fix
Query tests fix -- quotes identifiers
2 parents 6c0137a + cb41758 commit c86c817

File tree

2 files changed

+33
-40
lines changed

2 files changed

+33
-40
lines changed

tests/passed-tests.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,9 @@ TestPreparedStmtClose
241241
TestPreparedStmtConcurrentClose
242242
#TestFind
243243
TestQueryWithAssociation
244-
#TestFindInBatches
245-
#TestFindInBatchesWithOffsetLimit
246-
#TestFindInBatchesWithError
244+
TestFindInBatches
245+
TestFindInBatchesWithOffsetLimit
246+
TestFindInBatchesWithError
247247
TestFillSmallerStruct
248248
TestFillSmallerStructWithAllFields
249249
TestNot
@@ -252,9 +252,9 @@ TestOr
252252
TestOrWithAllFields
253253
#TestPluck
254254
TestSelect
255-
#TestOmit
256-
#TestOmitWithAllFields
257-
#TestMapColumns
255+
TestOmit
256+
TestOmitWithAllFields
257+
TestMapColumns
258258
TestPluckWithSelect
259259
#TestSelectWithVariables
260260
TestSelectWithArrayInput
@@ -270,7 +270,7 @@ TestSearchWithStruct
270270
TestSubQuery
271271
TestSubQueryWithRaw
272272
TestSubQueryWithHaving
273-
#TestScanNullValue
273+
TestScanNullValue
274274
TestQueryWithTableAndConditions
275275
TestQueryWithTableAndConditionsAndAllFields
276276
#TestQueryScannerWithSingleColumn

tests/query_test.go

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TestFind(t *testing.T) {
7272

7373
t.Run("First", func(t *testing.T) {
7474
var first User
75-
if err := DB.Where("name = ?", "find").First(&first).Error; err != nil {
75+
if err := DB.Where("\"name\" = ?", "find").First(&first).Error; err != nil {
7676
t.Errorf("errors happened when query first: %v", err)
7777
} else {
7878
CheckUser(t, first, users[0])
@@ -81,15 +81,15 @@ func TestFind(t *testing.T) {
8181

8282
t.Run("Last", func(t *testing.T) {
8383
var last User
84-
if err := DB.Where("name = ?", "find").Last(&last).Error; err != nil {
84+
if err := DB.Where("\"name\" = ?", "find").Last(&last).Error; err != nil {
8585
t.Errorf("errors happened when query last: %v", err)
8686
} else {
8787
CheckUser(t, last, users[2])
8888
}
8989
})
9090

9191
var all []User
92-
if err := DB.Where("name = ?", "find").Find(&all).Error; err != nil || len(all) != 3 {
92+
if err := DB.Where("\"name\" = ?", "find").Find(&all).Error; err != nil || len(all) != 3 {
9393
t.Errorf("errors happened when query find: %v, length: %v", err, len(all))
9494
} else {
9595
for idx, user := range users {
@@ -101,7 +101,7 @@ func TestFind(t *testing.T) {
101101

102102
t.Run("FirstMap", func(t *testing.T) {
103103
first := map[string]interface{}{}
104-
if err := DB.Model(&User{}).Where("name = ?", "find").First(first).Error; err != nil {
104+
if err := DB.Model(&User{}).Where("\"name\" = ?", "find").First(first).Error; err != nil {
105105
t.Errorf("errors happened when query first: %v", err)
106106
} else {
107107
for _, name := range []string{"Name", "Age", "Birthday"} {
@@ -132,7 +132,7 @@ func TestFind(t *testing.T) {
132132

133133
t.Run("FirstMapWithTable", func(t *testing.T) {
134134
first := map[string]interface{}{}
135-
if err := DB.Table("users").Where("name = ?", "find").Find(first).Error; err != nil {
135+
if err := DB.Table("users").Where("\"name\" = ?", "find").Find(first).Error; err != nil {
136136
t.Errorf("errors happened when query first: %v", err)
137137
} else {
138138
for _, name := range []string{"Name", "Age", "Birthday"} {
@@ -164,7 +164,7 @@ func TestFind(t *testing.T) {
164164

165165
t.Run("FirstPtrMap", func(t *testing.T) {
166166
first := map[string]interface{}{}
167-
if err := DB.Model(&User{}).Where("name = ?", "find").First(&first).Error; err != nil {
167+
if err := DB.Model(&User{}).Where("\"name\" = ?", "find").First(&first).Error; err != nil {
168168
t.Errorf("errors happened when query first: %v", err)
169169
} else {
170170
for _, name := range []string{"Name", "Age", "Birthday"} {
@@ -179,7 +179,7 @@ func TestFind(t *testing.T) {
179179

180180
t.Run("FirstSliceOfMap", func(t *testing.T) {
181181
allMap := []map[string]interface{}{}
182-
if err := DB.Model(&User{}).Where("name = ?", "find").Find(&allMap).Error; err != nil {
182+
if err := DB.Model(&User{}).Where("\"name\" = ?", "find").Find(&allMap).Error; err != nil {
183183
t.Errorf("errors happened when query find: %v", err)
184184
} else {
185185
for idx, user := range users {
@@ -214,7 +214,7 @@ func TestFind(t *testing.T) {
214214

215215
t.Run("FindSliceOfMapWithTable", func(t *testing.T) {
216216
allMap := []map[string]interface{}{}
217-
if err := DB.Table("users").Where("name = ?", "find").Find(&allMap).Error; err != nil {
217+
if err := DB.Table("users").Where("\"name\" = ?", "find").Find(&allMap).Error; err != nil {
218218
t.Errorf("errors happened when query find: %v", err)
219219
} else {
220220
for idx, user := range users {
@@ -249,7 +249,7 @@ func TestFind(t *testing.T) {
249249
})
250250

251251
var models []User
252-
if err := DB.Where("name in (?)", []string{"find"}).Find(&models).Error; err != nil || len(models) != 3 {
252+
if err := DB.Where("\"name\" in (?)", []string{"find"}).Find(&models).Error; err != nil || len(models) != 3 {
253253
t.Errorf("errors happened when query find with in clause: %v, length: %v", err, len(models))
254254
} else {
255255
for idx, user := range users {
@@ -261,7 +261,7 @@ func TestFind(t *testing.T) {
261261

262262
// test array
263263
var models2 [3]User
264-
if err := DB.Where("name in (?)", []string{"find"}).Find(&models2).Error; err != nil {
264+
if err := DB.Where("\"name\" in (?)", []string{"find"}).Find(&models2).Error; err != nil {
265265
t.Errorf("errors happened when query find with in clause: %v, length: %v", err, len(models2))
266266
} else {
267267
for idx, user := range users {
@@ -273,7 +273,7 @@ func TestFind(t *testing.T) {
273273

274274
// test smaller array
275275
var models3 [2]User
276-
if err := DB.Where("name in (?)", []string{"find"}).Find(&models3).Error; err != nil {
276+
if err := DB.Where("\"name\" in (?)", []string{"find"}).Find(&models3).Error; err != nil {
277277
t.Errorf("errors happened when query find with in clause: %v, length: %v", err, len(models3))
278278
} else {
279279
for idx, user := range users[:2] {
@@ -284,7 +284,7 @@ func TestFind(t *testing.T) {
284284
}
285285

286286
var none []User
287-
if err := DB.Where("name in (?)", []string{}).Find(&none).Error; err != nil || len(none) != 0 {
287+
if err := DB.Where("\"name\" in (?)", []string{}).Find(&none).Error; err != nil || len(none) != 0 {
288288
t.Errorf("errors happened when query find with in clause and zero length parameter: %v, length: %v", err, len(none))
289289
}
290290
}
@@ -308,7 +308,6 @@ func TestQueryWithAssociation(t *testing.T) {
308308
}
309309

310310
func TestFindInBatches(t *testing.T) {
311-
t.Skip()
312311
users := []User{
313312
*GetUser("find_in_batches", Config{}),
314313
*GetUser("find_in_batches", Config{}),
@@ -325,7 +324,7 @@ func TestFindInBatches(t *testing.T) {
325324
totalBatch int
326325
)
327326

328-
if result := DB.Table("\"users\" u").Where("\"name\" = ?", users[0].Name).FindInBatches(&results, 2, func(tx *gorm.DB, batch int) error {
327+
if result := DB.Table("users").Where("\"name\" = ?", users[0].Name).FindInBatches(&results, 2, func(tx *gorm.DB, batch int) error {
329328
totalBatch += batch
330329

331330
if tx.RowsAffected != 2 {
@@ -361,7 +360,6 @@ func TestFindInBatches(t *testing.T) {
361360
}
362361

363362
func TestFindInBatchesWithOffsetLimit(t *testing.T) {
364-
t.Skip()
365363
users := []User{
366364
*GetUser("find_in_batches_with_offset_limit", Config{}),
367365
*GetUser("find_in_batches_with_offset_limit", Config{}),
@@ -383,7 +381,7 @@ func TestFindInBatchesWithOffsetLimit(t *testing.T) {
383381
)
384382

385383
// offset limit
386-
if result := DB.Offset(3).Limit(5).Where("name = ?", users[0].Name).FindInBatches(&sub, 2, func(tx *gorm.DB, batch int) error {
384+
if result := DB.Offset(3).Limit(5).Where("\"name\" = ?", users[0].Name).FindInBatches(&sub, 2, func(tx *gorm.DB, batch int) error {
387385
results = append(results, sub...)
388386
lastBatch = batch
389387
return nil
@@ -401,30 +399,29 @@ func TestFindInBatchesWithOffsetLimit(t *testing.T) {
401399

402400
var sub1 []User
403401
// limit < batchSize
404-
if result := DB.Limit(5).Where("name = ?", users[0].Name).FindInBatches(&sub1, 10, func(tx *gorm.DB, batch int) error {
402+
if result := DB.Limit(5).Where("\"name\" = ?", users[0].Name).FindInBatches(&sub1, 10, func(tx *gorm.DB, batch int) error {
405403
return nil
406404
}); result.Error != nil || result.RowsAffected != 5 {
407405
t.Errorf("Failed to batch find, got error %v, rows affected: %v", result.Error, result.RowsAffected)
408406
}
409407

410408
var sub2 []User
411409
// only offset
412-
if result := DB.Offset(3).Where("name = ?", users[0].Name).FindInBatches(&sub2, 2, func(tx *gorm.DB, batch int) error {
410+
if result := DB.Offset(3).Where("\"name\" = ?", users[0].Name).FindInBatches(&sub2, 2, func(tx *gorm.DB, batch int) error {
413411
return nil
414412
}); result.Error != nil || result.RowsAffected != 7 {
415413
t.Errorf("Failed to batch find, got error %v, rows affected: %v", result.Error, result.RowsAffected)
416414
}
417415

418416
var sub3 []User
419-
if result := DB.Limit(4).Where("name = ?", users[0].Name).FindInBatches(&sub3, 2, func(tx *gorm.DB, batch int) error {
417+
if result := DB.Limit(4).Where("\"name\" = ?", users[0].Name).FindInBatches(&sub3, 2, func(tx *gorm.DB, batch int) error {
420418
return nil
421419
}); result.Error != nil || result.RowsAffected != 4 {
422420
t.Errorf("Failed to batch find, got error %v, rows affected: %v", result.Error, result.RowsAffected)
423421
}
424422
}
425423

426424
func TestFindInBatchesWithError(t *testing.T) {
427-
t.Skip()
428425
users := []User{
429426
*GetUser("find_in_batches_with_error", Config{}),
430427
*GetUser("find_in_batches_with_error", Config{}),
@@ -441,7 +438,7 @@ func TestFindInBatchesWithError(t *testing.T) {
441438
totalBatch int
442439
)
443440

444-
if result := DB.Table("wrong_table").Where("name = ?", users[0].Name).FindInBatches(&results, 2, func(tx *gorm.DB, batch int) error {
441+
if result := DB.Table("wrong_table").Where("\"name\" = ?", users[0].Name).FindInBatches(&results, 2, func(tx *gorm.DB, batch int) error {
445442
totalBatch += batch
446443
return nil
447444
}); result.Error == nil || result.RowsAffected > 0 {
@@ -451,7 +448,7 @@ func TestFindInBatchesWithError(t *testing.T) {
451448
t.Fatalf("incorrect total batch, expected: %v, got: %v", 0, totalBatch)
452449
}
453450

454-
if result := DB.Omit("id").Where("name = ?", users[0].Name).FindInBatches(&results, 2, func(tx *gorm.DB, batch int) error {
451+
if result := DB.Omit("id").Where("\"name\" = ?", users[0].Name).FindInBatches(&results, 2, func(tx *gorm.DB, batch int) error {
455452
totalBatch += batch
456453
return nil
457454
}); result.Error != gorm.ErrPrimaryKeyRequired {
@@ -864,12 +861,11 @@ func TestSelect(t *testing.T) {
864861
}
865862

866863
func TestOmit(t *testing.T) {
867-
t.Skip()
868864
user := User{Name: "OmitUser1", Age: 20}
869865
DB.Save(&user)
870866

871867
var result User
872-
DB.Where("name = ?", user.Name).Omit("name").Find(&result)
868+
DB.Where("\"name\"= ?", user.Name).Omit("name").Find(&result)
873869
if result.ID == 0 {
874870
t.Errorf("Should not have ID because only selected name, %+v", result.ID)
875871
}
@@ -880,12 +876,11 @@ func TestOmit(t *testing.T) {
880876
}
881877

882878
func TestOmitWithAllFields(t *testing.T) {
883-
t.Skip()
884879
user := User{Name: "OmitUser1", Age: 20}
885880
DB.Save(&user)
886881

887882
var userResult User
888-
DB.Session(&gorm.Session{QueryFields: true}).Where("users.name = ?", user.Name).Omit("name").Find(&userResult)
883+
DB.Session(&gorm.Session{QueryFields: true}).Where("\"users\".\"name\" = ?", user.Name).Omit("name").Find(&userResult)
889884
if userResult.ID == 0 {
890885
t.Errorf("Should not have ID because only selected name, %+v", userResult.ID)
891886
}
@@ -905,7 +900,6 @@ func TestOmitWithAllFields(t *testing.T) {
905900
}
906901

907902
func TestMapColumns(t *testing.T) {
908-
t.Skip()
909903
user := User{Name: "MapColumnsUser", Age: 12}
910904
DB.Save(&user)
911905

@@ -915,7 +909,7 @@ func TestMapColumns(t *testing.T) {
915909
Age uint
916910
}
917911
var res result
918-
DB.Table("users").Where("name = ?", user.Name).MapColumns(map[string]string{"name": "nickname"}).Scan(&res)
912+
DB.Table("users").Where("\"name\" = ?", user.Name).MapColumns(map[string]string{"name": "nickname"}).Scan(&res)
919913
if res.Nickname != user.Name {
920914
t.Errorf("Expected res.Nickname to be %s, but got %s", user.Name, res.Nickname)
921915
}
@@ -1305,7 +1299,6 @@ func TestSubQueryWithHaving(t *testing.T) {
13051299
}
13061300

13071301
func TestScanNullValue(t *testing.T) {
1308-
t.Skip()
13091302
user := GetUser("scan_null_value", Config{})
13101303
DB.Create(&user)
13111304

@@ -1314,7 +1307,7 @@ func TestScanNullValue(t *testing.T) {
13141307
}
13151308

13161309
var result User
1317-
if err := DB.First(&result, "id = ?", user.ID).Error; err != nil {
1310+
if err := DB.First(&result, "\"id\" = ?", user.ID).Error; err != nil {
13181311
t.Fatalf("failed to query struct data with null age, got error %v", err)
13191312
}
13201313

@@ -1332,7 +1325,7 @@ func TestScanNullValue(t *testing.T) {
13321325
}
13331326

13341327
var results []User
1335-
if err := DB.Find(&results, "name like ?", "scan_null_value_for_slice%").Error; err != nil {
1328+
if err := DB.Find(&results, "\"name\" like ?", "scan_null_value_for_slice%").Error; err != nil {
13361329
t.Fatalf("failed to query slice data with null age, got error %v", err)
13371330
}
13381331
}
@@ -1376,15 +1369,15 @@ func TestQueryScannerWithSingleColumn(t *testing.T) {
13761369
DB.Create(&user)
13771370

13781371
var result1 DoubleInt64
1379-
if err := DB.Model(&User{}).Where("name LIKE ?", "scanner_raw_%").Limit(1).Pluck(
1372+
if err := DB.Model(&User{}).Where("\"name\" LIKE ?", "scanner_raw_%").Limit(1).Pluck(
13801373
"age", &result1).Error; err != nil {
13811374
t.Errorf("Failed, got error: %v", err)
13821375
}
13831376

13841377
tests.AssertEqual(t, result1.data, 20)
13851378

13861379
var result2 DoubleInt64
1387-
if err := DB.Model(&User{}).Where("name LIKE ?", "scanner_raw_%").Limit(1).Select(
1380+
if err := DB.Model(&User{}).Where("\"name\" LIKE ?", "scanner_raw_%").Limit(1).Select(
13881381
"age").Scan(&result2).Error; err != nil {
13891382
t.Errorf("Failed, got error: %v", err)
13901383
}

0 commit comments

Comments
 (0)