forked from jostyee/dbr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types_test.go
96 lines (82 loc) · 2.8 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
package dbr
import (
"database/sql"
"encoding/json"
"testing"
"time"
"github.com/meican-dev/mysql"
"github.com/stretchr/testify/assert"
)
func TestNullTypeScanning(t *testing.T) {
s := createRealSessionWithFixtures()
type nullTypeScanningTest struct {
record *nullTypedRecord
valid bool
}
tests := []nullTypeScanningTest{
nullTypeScanningTest{
record: &nullTypedRecord{},
valid: false,
},
nullTypeScanningTest{
record: newNullTypedRecordWithData(),
valid: true,
},
}
for _, test := range tests {
// Create the record in the db
res, err := s.InsertInto("null_types").Columns("string_val", "int64_val", "float64_val", "time_val", "bool_val").Record(test.record).Exec()
assert.NoError(t, err)
id, err := res.LastInsertId()
assert.NoError(t, err)
// Scan it back and check that all fields are of the correct validity and are
// equal to the reference record
nullTypeSet := &nullTypedRecord{}
err = s.Select("*").From("null_types").Where("id = ?", id).LoadStruct(nullTypeSet)
assert.NoError(t, err)
assert.Equal(t, test.record, nullTypeSet)
assert.Equal(t, test.valid, nullTypeSet.StringVal.Valid)
assert.Equal(t, test.valid, nullTypeSet.Int64Val.Valid)
assert.Equal(t, test.valid, nullTypeSet.Float64Val.Valid)
assert.Equal(t, test.valid, nullTypeSet.TimeVal.Valid)
assert.Equal(t, test.valid, nullTypeSet.BoolVal.Valid)
nullTypeSet.StringVal.String = "newStringVal"
assert.NotEqual(t, test.record, nullTypeSet)
}
}
func TestNullTypeJSONMarshal(t *testing.T) {
type nullTypeJSONTest struct {
record *nullTypedRecord
expectedJSON []byte
}
tests := []nullTypeJSONTest{
nullTypeJSONTest{
record: &nullTypedRecord{},
expectedJSON: []byte(`{"Id":0,"StringVal":null,"Int64Val":null,"Float64Val":null,"TimeVal":null,"BoolVal":null}`),
},
nullTypeJSONTest{
record: newNullTypedRecordWithData(),
expectedJSON: []byte(`{"Id":0,"StringVal":"wow","Int64Val":42,"Float64Val":1.618,"TimeVal":"2009-01-03T18:15:05Z","BoolVal":true}`),
},
}
for _, test := range tests {
// Marshal the record
rawJSON, err := json.Marshal(test.record)
assert.NoError(t, err)
assert.Equal(t, test.expectedJSON, rawJSON)
// Unmarshal it back
newRecord := &nullTypedRecord{}
err = json.Unmarshal([]byte(rawJSON), newRecord)
assert.NoError(t, err)
assert.Equal(t, test.record, newRecord)
}
}
func newNullTypedRecordWithData() *nullTypedRecord {
return &nullTypedRecord{
StringVal: NullString{sql.NullString{String: "wow", Valid: true}},
Int64Val: NullInt64{sql.NullInt64{Int64: 42, Valid: true}},
Float64Val: NullFloat64{sql.NullFloat64{Float64: 1.618, Valid: true}},
TimeVal: NullTime{mysql.NullTime{Time: time.Date(2009, 1, 3, 18, 15, 5, 0, time.Local), Valid: true}},
BoolVal: NullBool{sql.NullBool{Bool: true, Valid: true}},
}
}