forked from gocraft/dbr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types_test.go
102 lines (94 loc) · 2.17 KB
/
types_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
91
92
93
94
95
96
97
98
99
100
101
102
package dbr
import (
"encoding/json"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var (
filledRecord = nullTypedRecord{
StringVal: NewNullString("wow"),
Int64Val: NewNullInt64(42),
Float64Val: NewNullFloat64(1.618),
TimeVal: NewNullTime(time.Date(2009, 1, 3, 18, 15, 5, 0, time.UTC)),
BoolVal: NewNullBool(true),
}
)
func TestNullTypesScanning(t *testing.T) {
for _, test := range []struct {
in nullTypedRecord
}{
{},
{
in: filledRecord,
},
} {
for _, sess := range testSession {
test.in.Id = nextID()
_, err := sess.InsertInto("null_types").Columns("id", "string_val", "int64_val", "float64_val", "time_val", "bool_val").Record(test.in).Exec()
assert.NoError(t, err)
var record nullTypedRecord
err = sess.Select("*").From("null_types").Where(Eq("id", test.in.Id)).LoadStruct(&record)
assert.NoError(t, err)
if sess == postgresSession {
// TODO: https://github.com/lib/pq/issues/329
if !record.TimeVal.Time.IsZero() {
record.TimeVal.Time = record.TimeVal.Time.UTC()
}
}
assert.Equal(t, test.in, record)
}
}
}
func TestNullTypesJSON(t *testing.T) {
for _, test := range []struct {
in interface{}
in2 interface{}
out interface{}
want string
}{
{
in: &filledRecord.BoolVal,
in2: filledRecord.BoolVal,
out: new(NullBool),
want: "true",
},
{
in: &filledRecord.Float64Val,
in2: filledRecord.Float64Val,
out: new(NullFloat64),
want: "1.618",
},
{
in: &filledRecord.Int64Val,
in2: filledRecord.Int64Val,
out: new(NullInt64),
want: "42",
},
{
in: &filledRecord.StringVal,
in2: filledRecord.StringVal,
out: new(NullString),
want: `"wow"`,
},
{
in: &filledRecord.TimeVal,
in2: filledRecord.TimeVal,
out: new(NullTime),
want: `"2009-01-03T18:15:05Z"`,
},
} {
// marshal ptr
b, err := json.Marshal(test.in)
assert.NoError(t, err)
assert.Equal(t, test.want, string(b))
// marshal value
b, err = json.Marshal(test.in2)
assert.NoError(t, err)
assert.Equal(t, test.want, string(b))
// unmarshal
err = json.Unmarshal(b, test.out)
assert.NoError(t, err)
assert.Equal(t, test.in, test.out)
}
}