Skip to content

Commit 3f9d8ba

Browse files
mindengflycash
authored andcommitted
eorm: 补充 NULL 语义和基本类型之间转化的测试用例
1 parent d7954c4 commit 3f9d8ba

File tree

4 files changed

+88
-48
lines changed

4 files changed

+88
-48
lines changed

.CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
- [eorm: 分库分表: 结果集处理--聚合函数(含GroupBy子句)](https://github.com/ecodeclub/eorm/pull/193)
2727
- [eorm: 分库分表: NOT 支持](https://github.com/ecodeclub/eorm/pull/191)
2828
- [eorm: 分库分表: Merger NullAble类型数据的支持(sortMerger)](https://github.com/ecodeclub/eorm/pull/195)
29+
- [eorm: 补充 NULL 语义和基本类型之间转化的测试用例](https://github.com/ecodeclub/eorm/pull/198)
2930

3031
## v0.0.1:
3132
- [Init Project](https://github.com/ecodeclub/eorm/pull/1)

.github/workflows/golangci-lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ jobs:
4545
# working-directory: somedir
4646

4747
# Optional: golangci-lint command line arguments.
48-
# args: --issues-exit-code=0
48+
args: --timeout=10m # --issues-exit-code=0
4949

5050
# Optional: show only new issues if it's a pull request. The default value is `false`.
5151
only-new-issues: true

internal/integration/select_test.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ func (s *SelectTestSuite) SetupSuite() {
4141
res := eorm.NewInserter[test.SimpleStruct](s.orm).Values(s.data).Exec(context.Background())
4242
if res.Err() != nil {
4343
s.T().Fatal(res.Err())
44-
s.T()
4544
}
4645
}
4746

@@ -89,7 +88,7 @@ func (s *SelectTestSuite) TestSelectorGetBaseType() {
8988
testCases := []struct {
9089
name string
9190
queryRes func() (any, error)
92-
wantErr error
91+
wantErr string
9392
wantRes any
9493
}{
9594
{
@@ -99,7 +98,7 @@ func (s *SelectTestSuite) TestSelectorGetBaseType() {
9998
Where(eorm.C("Id").EQ(9))
10099
return queryer.Get(context.Background())
101100
},
102-
wantErr: eorm.ErrNoRows,
101+
wantErr: eorm.ErrNoRows.Error(),
103102
},
104103
{
105104
name: "res int",
@@ -205,13 +204,35 @@ func (s *SelectTestSuite) TestSelectorGetBaseType() {
205204
return &res
206205
}(),
207206
},
207+
{
208+
name: "res *int accept NULL",
209+
queryRes: func() (any, error) {
210+
queryer := eorm.NewSelector[*int](s.orm).Select(eorm.C("Int32Ptr")).
211+
From(eorm.TableOf(&test.SimpleStruct{}, "t1")).
212+
Where(eorm.C("Id").EQ(1))
213+
return queryer.Get(context.Background())
214+
},
215+
wantRes: func() **int {
216+
return new(*int)
217+
}(),
218+
},
219+
{
220+
name: "res int accept NULL",
221+
queryRes: func() (any, error) {
222+
queryer := eorm.NewSelector[*int](s.orm).Select(eorm.C("Int32Ptr")).
223+
From(eorm.TableOf(&test.SimpleStruct{}, "t1")).
224+
Where(eorm.C("Id").EQ(1))
225+
return queryer.Get(context.Background())
226+
},
227+
wantErr: "abc",
228+
},
208229
}
209230

210231
for _, tc := range testCases {
211232
s.T().Run(tc.name, func(t *testing.T) {
212233
res, err := tc.queryRes()
213-
assert.Equal(t, tc.wantErr, err)
214234
if err != nil {
235+
assert.EqualError(t, err, tc.wantErr)
215236
return
216237
}
217238
assert.Equal(t, tc.wantRes, res)

select_test.go

Lines changed: 61 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -290,20 +290,18 @@ func TestSelector_Get_baseType(t *testing.T) {
290290

291291
testCases := []struct {
292292
name string
293-
queryRes func(t *testing.T) any
293+
queryRes func(t *testing.T) (any, error)
294294
mockErr error
295295
mockOrder func(mock sqlmock.Sqlmock)
296-
wantErr error
296+
wantErr string
297297
wantVal any
298298
}{
299299
{
300300
name: "res int",
301-
queryRes: func(t *testing.T) any {
301+
queryRes: func(t *testing.T) (any, error) {
302302
tm := TableOf(&TestModel{}, "t1")
303303
queryer := NewSelector[int](db).Select(C("Age")).From(tm)
304-
result, err := queryer.Get(context.Background())
305-
require.NoError(t, err)
306-
return result
304+
return queryer.Get(context.Background())
307305
},
308306
mockOrder: func(mock sqlmock.Sqlmock) {
309307
rows := mock.NewRows([]string{"age"}).AddRow(10)
@@ -318,12 +316,10 @@ func TestSelector_Get_baseType(t *testing.T) {
318316
},
319317
{
320318
name: "res int32",
321-
queryRes: func(t *testing.T) any {
319+
queryRes: func(t *testing.T) (any, error) {
322320
tm := TableOf(&TestModel{}, "t1")
323321
queryer := NewSelector[int32](db).Select(C("Age")).From(tm)
324-
result, err := queryer.Get(context.Background())
325-
require.NoError(t, err)
326-
return result
322+
return queryer.Get(context.Background())
327323
},
328324
mockOrder: func(mock sqlmock.Sqlmock) {
329325
rows := mock.NewRows([]string{"age"}).AddRow(10)
@@ -338,12 +334,10 @@ func TestSelector_Get_baseType(t *testing.T) {
338334
},
339335
{
340336
name: "res int64",
341-
queryRes: func(t *testing.T) any {
337+
queryRes: func(t *testing.T) (any, error) {
342338
tm := TableOf(&TestModel{}, "t1")
343339
queryer := NewSelector[int64](db).Select(C("Age")).From(tm)
344-
result, err := queryer.Get(context.Background())
345-
require.NoError(t, err)
346-
return result
340+
return queryer.Get(context.Background())
347341
},
348342
mockOrder: func(mock sqlmock.Sqlmock) {
349343
rows := mock.NewRows([]string{"age"}).AddRow(10)
@@ -358,12 +352,10 @@ func TestSelector_Get_baseType(t *testing.T) {
358352
},
359353
{
360354
name: "avg res float32",
361-
queryRes: func(t *testing.T) any {
355+
queryRes: func(t *testing.T) (any, error) {
362356
tm := TableOf(&TestModel{}, "t1")
363357
queryer := NewSelector[float32](db).Select(C("Age")).From(tm)
364-
result, err := queryer.Get(context.Background())
365-
require.NoError(t, err)
366-
return result
358+
return queryer.Get(context.Background())
367359
},
368360
mockOrder: func(mock sqlmock.Sqlmock) {
369361
rows := mock.NewRows([]string{"age"}).AddRow(10.2)
@@ -378,12 +370,10 @@ func TestSelector_Get_baseType(t *testing.T) {
378370
},
379371
{
380372
name: "avg res float64",
381-
queryRes: func(t *testing.T) any {
373+
queryRes: func(t *testing.T) (any, error) {
382374
tm := TableOf(&TestModel{}, "t1")
383375
queryer := NewSelector[float64](db).Select(C("Age")).From(tm)
384-
result, err := queryer.Get(context.Background())
385-
require.NoError(t, err)
386-
return result
376+
return queryer.Get(context.Background())
387377
},
388378
mockOrder: func(mock sqlmock.Sqlmock) {
389379
rows := mock.NewRows([]string{"age"}).AddRow(10.02)
@@ -398,13 +388,11 @@ func TestSelector_Get_baseType(t *testing.T) {
398388
},
399389
{
400390
name: "res byte",
401-
queryRes: func(t *testing.T) any {
391+
queryRes: func(t *testing.T) (any, error) {
402392
tm := TableOf(&TestModel{}, "t1")
403393
queryer := NewSelector[byte](db).Select(C("FirstName")).
404394
From(tm).Where(C("Id").EQ(1))
405-
result, err := queryer.Get(context.Background())
406-
require.NoError(t, err)
407-
return result
395+
return queryer.Get(context.Background())
408396
},
409397
mockOrder: func(mock sqlmock.Sqlmock) {
410398
rows := mock.NewRows([]string{"first_name"}).AddRow('D')
@@ -419,13 +407,11 @@ func TestSelector_Get_baseType(t *testing.T) {
419407
},
420408
{
421409
name: "res bytes",
422-
queryRes: func(t *testing.T) any {
410+
queryRes: func(t *testing.T) (any, error) {
423411
tm := TableOf(&TestModel{}, "t1")
424412
queryer := NewSelector[[]byte](db).Select(C("FirstName")).
425413
From(tm).Where(C("Id").EQ(1))
426-
result, err := queryer.Get(context.Background())
427-
require.NoError(t, err)
428-
return result
414+
return queryer.Get(context.Background())
429415
},
430416
mockOrder: func(mock sqlmock.Sqlmock) {
431417
rows := mock.NewRows([]string{"first_name"}).AddRow([]byte("Li"))
@@ -440,13 +426,11 @@ func TestSelector_Get_baseType(t *testing.T) {
440426
},
441427
{
442428
name: "res string",
443-
queryRes: func(t *testing.T) any {
429+
queryRes: func(t *testing.T) (any, error) {
444430
tm := TableOf(&TestModel{}, "t1")
445431
queryer := NewSelector[string](db).Select(C("FirstName")).
446432
From(tm).Where(C("Id").EQ(1))
447-
result, err := queryer.Get(context.Background())
448-
require.NoError(t, err)
449-
return result
433+
return queryer.Get(context.Background())
450434
},
451435
mockOrder: func(mock sqlmock.Sqlmock) {
452436
rows := mock.NewRows([]string{"first_name"}).AddRow("Da")
@@ -461,12 +445,10 @@ func TestSelector_Get_baseType(t *testing.T) {
461445
},
462446
{
463447
name: "res struct ptr",
464-
queryRes: func(t *testing.T) any {
448+
queryRes: func(t *testing.T) (any, error) {
465449
queryer := NewSelector[TestModel](db).Select(C("FirstName"), C("Age")).
466450
Where(C("Id").EQ(1))
467-
result, err := queryer.Get(context.Background())
468-
require.NoError(t, err)
469-
return result
451+
return queryer.Get(context.Background())
470452
},
471453
mockOrder: func(mock sqlmock.Sqlmock) {
472454
rows := mock.NewRows([]string{"first_name", "age"}).AddRow("Da", 18)
@@ -483,13 +465,11 @@ func TestSelector_Get_baseType(t *testing.T) {
483465
},
484466
{
485467
name: "res sql.NullString",
486-
queryRes: func(t *testing.T) any {
468+
queryRes: func(t *testing.T) (any, error) {
487469
tm := TableOf(&TestModel{}, "t1")
488470
queryer := NewSelector[sql.NullString](db).Select(C("LastName")).
489471
From(tm).Where(C("Id").EQ(1))
490-
result, err := queryer.Get(context.Background())
491-
require.NoError(t, err)
492-
return result
472+
return queryer.Get(context.Background())
493473
},
494474
mockOrder: func(mock sqlmock.Sqlmock) {
495475
rows := mock.NewRows([]string{"last_name"}).AddRow([]byte("ming"))
@@ -501,12 +481,50 @@ func TestSelector_Get_baseType(t *testing.T) {
501481
return &sql.NullString{String: "ming", Valid: true}
502482
}(),
503483
},
484+
{
485+
name: "res *int accept NULL",
486+
queryRes: func(t *testing.T) (any, error) {
487+
tm := TableOf(&TestModel{}, "t1")
488+
queryer := NewSelector[*int](db).Select(C("Age")).
489+
From(tm).Where(C("Id").EQ(1))
490+
return queryer.Get(context.Background())
491+
},
492+
mockOrder: func(mock sqlmock.Sqlmock) {
493+
rows := mock.NewRows([]string{"age"}).AddRow(nil)
494+
mock.ExpectQuery("SELECT `age` FROM `test_model` AS `t1` WHERE `id`=? LIMIT ?;").
495+
WithArgs(1, 1).
496+
WillReturnRows(rows)
497+
},
498+
wantVal: func() **int {
499+
return new(*int)
500+
}(),
501+
},
502+
{
503+
name: "res int accept NULL",
504+
queryRes: func(t *testing.T) (any, error) {
505+
tm := TableOf(&TestModel{}, "t1")
506+
queryer := NewSelector[int](db).Select(C("Age")).
507+
From(tm).Where(C("Id").EQ(1))
508+
return queryer.Get(context.Background())
509+
},
510+
mockOrder: func(mock sqlmock.Sqlmock) {
511+
rows := mock.NewRows([]string{"age"}).AddRow(nil)
512+
mock.ExpectQuery("SELECT `age` FROM `test_model` AS `t1` WHERE `id`=? LIMIT ?;").
513+
WithArgs(1, 1).
514+
WillReturnRows(rows)
515+
},
516+
wantErr: "sql: Scan error on column index 0, name \"age\": converting NULL to int is unsupported",
517+
},
504518
}
505519

506520
for _, tc := range testCases {
507521
t.Run(tc.name, func(t *testing.T) {
508522
tc.mockOrder(mock)
509-
res := tc.queryRes(t)
523+
res, err := tc.queryRes(t)
524+
if err != nil {
525+
assert.EqualError(t, err, tc.wantErr)
526+
return
527+
}
510528
assert.Equal(t, tc.wantVal, res)
511529
})
512530
}

0 commit comments

Comments
 (0)