Skip to content

Commit 5e7c2fb

Browse files
authored
Merge pull request #127 from dolmen/fix-test-NullInt
testsuite: fixes in NullInt/NullTime
2 parents b9ca56c + 5c0fec0 commit 5e7c2fb

File tree

1 file changed

+47
-28
lines changed

1 file changed

+47
-28
lines changed

stubs_test.go

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sqlmock
22

33
import (
44
"database/sql/driver"
5+
"errors"
56
"fmt"
67
"strconv"
78
"time"
@@ -18,53 +19,71 @@ type NullInt struct {
1819
}
1920

2021
// Satisfy sql.Scanner interface
21-
func (ni *NullInt) Scan(value interface{}) (err error) {
22-
if value == nil {
22+
func (ni *NullInt) Scan(value interface{}) error {
23+
switch v := value.(type) {
24+
case nil:
2325
ni.Integer, ni.Valid = 0, false
24-
return
25-
}
2626

27-
switch v := value.(type) {
28-
case int, int8, int16, int32, int64:
29-
ni.Integer, ni.Valid = v.(int), true
30-
return
27+
// FIXME int, int8, int16, int32 types are handled here but that should not
28+
// be necessary: only int64 is a driver.Value
29+
// Unfortunately, the sqlmock testsuite currently relies on that because
30+
// sqlmock doesn't properly limits itself internally to pure driver.Value.
31+
case int:
32+
ni.Integer, ni.Valid = v, true
33+
case int8:
34+
ni.Integer, ni.Valid = int(v), true
35+
case int16:
36+
ni.Integer, ni.Valid = int(v), true
37+
case int32:
38+
ni.Integer, ni.Valid = int(v), true
39+
40+
case int64:
41+
const maxUint = ^uint(0)
42+
const minUint = 0
43+
const maxInt = int(maxUint >> 1)
44+
const minInt = -maxInt - 1
45+
46+
if v > int64(maxInt) || v < int64(minInt) {
47+
return errors.New("value out of int range")
48+
}
49+
ni.Integer, ni.Valid = int(v), true
3150
case []byte:
32-
ni.Integer, err = strconv.Atoi(string(v))
33-
ni.Valid = (err == nil)
34-
return
51+
n, err := strconv.Atoi(string(v))
52+
if err != nil {
53+
return err
54+
}
55+
ni.Integer, ni.Valid = n, true
3556
case string:
36-
ni.Integer, err = strconv.Atoi(v)
37-
ni.Valid = (err == nil)
38-
return
57+
n, err := strconv.Atoi(v)
58+
if err != nil {
59+
return err
60+
}
61+
ni.Integer, ni.Valid = n, true
62+
default:
63+
return fmt.Errorf("can't convert %T to integer", value)
3964
}
40-
41-
ni.Valid = false
42-
return fmt.Errorf("Can't convert %T to integer", value)
65+
return nil
4366
}
4467

4568
// Satisfy sql.Valuer interface.
4669
func (ni NullInt) Value() (driver.Value, error) {
4770
if !ni.Valid {
4871
return nil, nil
4972
}
50-
return ni.Integer, nil
73+
return int64(ni.Integer), nil
5174
}
5275

5376
// Satisfy sql.Scanner interface
54-
func (nt *NullTime) Scan(value interface{}) (err error) {
55-
if value == nil {
56-
nt.Time, nt.Valid = time.Time{}, false
57-
return
58-
}
59-
77+
func (nt *NullTime) Scan(value interface{}) error {
6078
switch v := value.(type) {
79+
case nil:
80+
nt.Time, nt.Valid = time.Time{}, false
6181
case time.Time:
6282
nt.Time, nt.Valid = v, true
63-
return
83+
default:
84+
return fmt.Errorf("can't convert %T to time.Time", value)
6485
}
65-
66-
nt.Valid = false
67-
return fmt.Errorf("Can't convert %T to time.Time", value)
86+
return nil
6887
}
6988

7089
// Satisfy sql.Valuer interface.

0 commit comments

Comments
 (0)