Skip to content

Commit 246ca3a

Browse files
authored
Merge pull request #20 from knocknote/feature/add-types-test
add NullTime and NullInt32 test
2 parents 4c31c6c + 40761dd commit 246ca3a

File tree

1 file changed

+53
-33
lines changed

1 file changed

+53
-33
lines changed

database/sql/sql_test.go

+53-33
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ type TestRows struct {
128128

129129
func (t *TestRows) Columns() []string {
130130
if t.firstTime {
131-
return []string{"name", "age", "is_god", "point"}
131+
return []string{"name", "age", "is_god", "point", "power", "created_at"}
132132
}
133133
return []string{}
134134
}
@@ -143,6 +143,8 @@ func (t *TestRows) Next(dest []driver.Value) error {
143143
dest[1] = 10
144144
dest[2] = true
145145
dest[3] = 3.14
146+
dest[4] = 100
147+
dest[5] = time.Date(2020, 01, 01, 12, 0, 0, 0, time.Local)
146148
t.firstTime = false
147149
} else {
148150
return io.EOF
@@ -221,7 +223,7 @@ func testColumnType(t *testing.T, rows *Rows) {
221223
t.Run("validate column type", func(t *testing.T) {
222224
types, err := rows.ColumnTypes()
223225
checkErr(t, err)
224-
if len(types) != 4 {
226+
if len(types) != 6 {
225227
t.Fatal("cannot get columnTypes")
226228
}
227229
columnType := types[0]
@@ -250,12 +252,14 @@ func testRows(t *testing.T, rows *Rows) {
250252
for {
251253
for rows.Next() {
252254
var (
253-
name string
254-
age int
255-
isGod bool
256-
point float32
255+
name string
256+
age int
257+
isGod bool
258+
point float32
259+
power int32
260+
createdAt time.Time
257261
)
258-
checkErr(t, rows.Scan(&name, &age, &isGod, &point))
262+
checkErr(t, rows.Scan(&name, &age, &isGod, &point, &power, &createdAt))
259263
if name != "alice" {
260264
t.Fatal("cannot scan")
261265
}
@@ -268,6 +272,12 @@ func testRows(t *testing.T, rows *Rows) {
268272
if int(point) != 3 {
269273
t.Fatal("cannot scan")
270274
}
275+
if power != 100 {
276+
t.Fatal("cannot scan")
277+
}
278+
if !createdAt.Equal(time.Date(2020, 01, 01, 12, 00, 00, 00, time.Local)) {
279+
t.Fatal("cannot scan")
280+
}
271281
}
272282
if !rows.NextResultSet() {
273283
break
@@ -286,7 +296,7 @@ func testPrepareWithNotShardingTable(ctx context.Context, t *testing.T, db *DB)
286296
t.Run("validate columns", func(t *testing.T) {
287297
columns, err := rows.Columns()
288298
checkErr(t, err)
289-
if len(columns) != 4 {
299+
if len(columns) != 6 {
290300
t.Fatal("cannot get columns")
291301
}
292302
testColumnType(t, rows)
@@ -300,12 +310,14 @@ func testPrepareWithNotShardingTable(ctx context.Context, t *testing.T, db *DB)
300310
defer rows.Close()
301311
for rows.Next() {
302312
var (
303-
name string
304-
age int
305-
isGod bool
306-
point float32
313+
name string
314+
age int
315+
isGod bool
316+
point float32
317+
power int32
318+
createdAt time.Time
307319
)
308-
checkErr(t, rows.Scan(&name, &age, &isGod, &point))
320+
checkErr(t, rows.Scan(&name, &age, &isGod, &point, &power, &createdAt))
309321
if name != "alice" {
310322
t.Fatal("cannot scan")
311323
}
@@ -321,24 +333,28 @@ func testPrepareContextWithNotShardingTable(ctx context.Context, t *testing.T, d
321333
defer stmt.Close()
322334
t.Run("query row without context", func(t *testing.T) {
323335
var (
324-
name string
325-
age int
326-
isGod bool
327-
point float32
336+
name string
337+
age int
338+
isGod bool
339+
point float32
340+
power int32
341+
createdAt time.Time
328342
)
329-
stmt.QueryRow(1).Scan(&name, &age, &isGod, &point)
343+
stmt.QueryRow(1).Scan(&name, &age, &isGod, &point, &power, &createdAt)
330344
if name != "alice" {
331345
t.Fatal("cannot scan")
332346
}
333347
})
334348
t.Run("query row with context", func(t *testing.T) {
335349
var (
336-
name string
337-
age int
338-
isGod bool
339-
point float32
350+
name string
351+
age int
352+
isGod bool
353+
point float32
354+
power int32
355+
createdAt time.Time
340356
)
341-
stmt.QueryRowContext(ctx, 1).Scan(&name, &age, &isGod, &point)
357+
stmt.QueryRowContext(ctx, 1).Scan(&name, &age, &isGod, &point, &power, &createdAt)
342358
if name != "alice" {
343359
t.Fatal("cannot scan")
344360
}
@@ -454,12 +470,14 @@ func testTransactionStmtError(t *testing.T, tx *Tx, stmt *Stmt) {
454470
func testTransactionQueryRowWithoutContext(t *testing.T, stmt *Stmt) {
455471
t.Run("query row without context", func(t *testing.T) {
456472
var (
457-
name NullString
458-
age NullInt64
459-
isGod NullBool
460-
point NullFloat64
473+
name NullString
474+
age NullInt64
475+
isGod NullBool
476+
point NullFloat64
477+
power NullInt32
478+
createdAt NullTime
461479
)
462-
checkErr(t, stmt.QueryRow(1).Scan(&name, &age, &isGod, &point))
480+
checkErr(t, stmt.QueryRow(1).Scan(&name, &age, &isGod, &point, &power, &createdAt))
463481
nameValue, err := name.Value()
464482
checkErr(t, err)
465483
if nameValue.(string) != "alice" {
@@ -490,13 +508,15 @@ func testTransactionQueryRowWithoutContext(t *testing.T, stmt *Stmt) {
490508

491509
func testTransactionQueryWithContext(ctx context.Context, t *testing.T, stmt *Stmt) {
492510
var (
493-
name NullString
494-
age NullInt64
495-
isGod NullBool
496-
point NullFloat64
511+
name NullString
512+
age NullInt64
513+
isGod NullBool
514+
point NullFloat64
515+
power NullInt32
516+
createdAt NullTime
497517
)
498518
t.Run("query row with context", func(t *testing.T) {
499-
stmt.QueryRowContext(ctx, 1).Scan(&name, &age, &isGod, &point)
519+
stmt.QueryRowContext(ctx, 1).Scan(&name, &age, &isGod, &point, &power, &createdAt)
500520
nameValue, err := name.Value()
501521
checkErr(t, err)
502522
if nameValue.(string) != "alice" {

0 commit comments

Comments
 (0)