-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathconversions_test.go
90 lines (74 loc) · 2.25 KB
/
conversions_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package pq_types
import (
"database/sql"
"fmt"
"time"
"gopkg.in/check.v1"
)
func insertQuery(col string) string {
return fmt.Sprintf("INSERT INTO pq_types (%s) VALUES($1)", col)
}
func selectQuery(col string) string {
return fmt.Sprintf("SELECT %s FROM pq_types LIMIT 1;", col)
}
// For each type there need to be a pair of tests: with empty -> nil value and with non-empty value -> value.
func (s *TypesSuite) TestConversionNullString(c *check.C) {
cases := []sql.NullString{
{String: ""},
{String: "truly random string", Valid: true},
}
for _, expected := range cases {
val := NullString(expected.String)
_, err := s.db.Exec(insertQuery("null_str"), val)
c.Assert(err, check.IsNil)
var actual sql.NullString
err = s.db.QueryRow(selectQuery("null_str")).Scan(actual)
c.Check(err, check.IsNil)
c.Check(actual, check.DeepEquals, expected)
}
}
func (s *TypesSuite) TestConversionNullInt32(c *check.C) {
cases := []sql.NullInt32{
{Int32: 0},
{Int32: 0xabc, Valid: true},
}
for _, expected := range cases {
val := NullInt32(expected.Int32)
_, err := s.db.Exec(insertQuery("null_int32"), val)
c.Assert(err, check.IsNil)
var actual sql.NullInt32
err = s.db.QueryRow(selectQuery("null_int32")).Scan(actual)
c.Check(err, check.IsNil)
c.Check(actual, check.DeepEquals, expected)
}
}
func (s *TypesSuite) TestConversionNullInt64(c *check.C) {
cases := []sql.NullInt64{
{Int64: 0},
{Int64: 0xabcdef, Valid: true},
}
for _, expected := range cases {
val := NullInt64(expected.Int64)
_, err := s.db.Exec(insertQuery("null_int64"), val)
c.Assert(err, check.IsNil)
var actual sql.NullInt64
err = s.db.QueryRow(selectQuery("null_int64")).Scan(actual)
c.Check(err, check.IsNil)
c.Check(actual, check.DeepEquals, expected)
}
}
func (s *TypesSuite) TestConversionNullTimestamp(c *check.C) {
// here we use another approach, as input is a pointer
now := time.Now()
cases := []*time.Time{nil, &now}
for _, expected := range cases {
s.SetUpTest(c)
val := NullTimestampP(expected)
_, err := s.db.Exec(insertQuery("null_timestamp"), val)
c.Assert(err, check.IsNil)
var actual *time.Time
err = s.db.QueryRow(selectQuery("null_timestamp")).Scan(&actual)
c.Check(err, check.IsNil)
c.Check(actual, check.Equals, expected)
}
}