-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnisql_test.go
288 lines (235 loc) · 6.57 KB
/
nisql_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package nisql
import (
"database/sql"
"encoding/json"
"fmt"
"os"
"reflect"
"testing"
"time"
_ "github.com/lib/pq"
)
type nullable struct {
StringNVal NullString
StringVal string
Int64NVal NullInt64
Int64Val int64
Float64NVal NullFloat64
Float64Val float64
BoolNVal NullBool
BoolVal bool
TimeNVal NullTime
TimeVal time.Time
}
func TestInit(t *testing.T) {
db, err := sql.Open(
os.Getenv("NISQL_TEST_DIALECT"),
os.Getenv("NISQL_TEST_DSN"),
)
if err != nil {
t.Fatalf("err while creating connection: %s", err.Error())
}
sql := `CREATE TABLE nullable (
string_n_val VARCHAR (255) DEFAULT NULL,
string_val VARCHAR (255) DEFAULT 'empty',
int64_n_val BIGINT DEFAULT NULL,
int64_val BIGINT DEFAULT 1,
float64_n_val NUMERIC DEFAULT NULL,
float64_val NUMERIC DEFAULT 1,
bool_n_val BOOLEAN,
bool_val BOOLEAN NOT NULL,
time_n_val timestamp,
time_val timestamp NOT NULL
)`
if _, err = db.Exec(sql); err != nil {
t.Fatalf("err while creating table: %s", err.Error())
}
sql = `INSERT INTO nullable
VALUES
(
NULL,
'NULLABLE',
NULL,
42,
NULL,
12,
NULL,
true,
NULL,
NOW()
)`
if _, err := db.Exec(sql); err != nil {
t.Fatalf("err while adding null item: %s", err.Error())
}
n := &nullable{}
err = db.QueryRow("SELECT * FROM nullable").
Scan(&n.StringNVal,
&n.StringVal,
&n.Int64NVal,
&n.Int64Val,
&n.Float64NVal,
&n.Float64Val,
&n.BoolNVal,
&n.BoolVal,
&n.TimeNVal,
&n.TimeVal,
)
if err != nil {
t.Fatalf(err.Error())
}
if n.StringVal != "NULLABLE" {
t.Fatalf("expected NULLABLE, got: ", n.StringVal)
}
if n.StringNVal.Valid {
t.Fatalf("expected invalid, got valid for string_n_val")
}
if n.Int64Val != int64(42) {
t.Fatalf("expected 42, got: %d", n.Int64Val)
}
if n.Int64NVal.Valid {
t.Fatalf("expected invalid, got valid for int64_n_val")
}
if n.Float64Val != float64(12) {
t.Fatalf("expected 12, got: %f", n.Float64Val)
}
if n.Float64NVal.Valid {
t.Fatalf("expected invalid, got valid for float64_n_val")
}
if n.BoolVal != true {
t.Fatalf("expected true, got: %t", n.BoolVal)
}
if n.BoolNVal.Valid {
t.Fatalf("expected invalid, got valid for bool_n_val")
}
if n.TimeNVal.Valid {
t.Fatalf("expected false, got: %t", n.TimeNVal)
}
if n.TimeVal.IsZero() {
t.Fatalf("expected valid, got invalid for TimeVal: %+v", n.TimeVal)
}
if _, err := db.Exec("DELETE FROM nullable"); err != nil {
t.Fatalf("err while clearing nullable table: %s", err.Error())
}
testGetNil(t, n)
}
func dummy(now time.Time) *nullable {
return &nullable{
StringNVal: String("string_n_val"),
StringVal: "string_val",
Int64NVal: Int64(123),
Int64Val: int64(123),
Float64NVal: Float64(12),
Float64Val: float64(12),
BoolNVal: Bool(true),
BoolVal: true,
TimeNVal: Time(now),
TimeVal: now,
}
}
func TestMarshal(t *testing.T) {
now := time.Now().UTC()
nset := dummy(now)
allSetRes, err := json.Marshal(nset)
if err != nil {
t.Fatalf("err while marshaling: %s", err.Error())
}
// test all-set variables marshaling
allSetExpectedResString := fmt.Sprintf(
`{"StringNVal":"string_n_val","StringVal":"string_val","Int64NVal":123,"Int64Val":123,"Float64NVal":12,"Float64Val":12,"BoolNVal":true,"BoolVal":true,"TimeNVal":"%s","TimeVal":"%s"}`,
now.Format(time.RFC3339Nano),
now.Format(time.RFC3339Nano),
)
if allSetExpectedResString != string(allSetRes) {
t.Fatalf("Marshal err: expected: %s, got: %s", allSetExpectedResString, string(allSetRes))
}
// test not-set variables marshalling
nnonset := &nullable{}
nonSetRes, err := json.Marshal(nnonset)
if err != nil {
t.Fatalf("err while marshaling:%s", err.Error())
}
nonSetExpectedResString := fmt.Sprintf(
`{"StringNVal":null,"StringVal":"","Int64NVal":null,"Int64Val":0,"Float64NVal":null,"Float64Val":0,"BoolNVal":null,"BoolVal":false,"TimeNVal":null,"TimeVal":"%s"}`,
zeroDate().Format(time.RFC3339Nano),
)
if nonSetExpectedResString != string(nonSetRes) {
t.Fatalf("Marshal err: expected: %s, got: %s", nonSetExpectedResString, string(nonSetRes))
}
}
func TestUnMarshal(t *testing.T) {
now := time.Now().UTC()
nset := dummy(now)
allSetRes, err := json.Marshal(nset)
if err != nil {
t.Fatalf("err while marshaling: %s", err.Error())
}
// test not-set variables marshalling
nnonset := &nullable{}
nonSetRes, err := json.Marshal(nnonset)
if err != nil {
t.Fatalf("err while marshaling:%s", err.Error())
}
// test all set variables unmarshalling
nset2 := &nullable{}
if err := json.Unmarshal(allSetRes, nset2); err != nil {
t.Fatalf("Unmarshal err: %s", err.Error())
}
if !reflect.DeepEqual(nset, nset2) {
t.Fatalf("not same: \nn:%#v,\nn2:%#v", nset, nset2)
}
// test not-set variables unmarshaling
nnonset2 := &nullable{}
if err := json.Unmarshal(nonSetRes, nnonset2); err != nil {
t.Fatalf("Unmarshal err: %s", err.Error())
}
// deep equal fails on zerodate's loc property, check it manually
if nnonset.TimeVal.UnixNano() != nnonset2.TimeVal.UnixNano() {
t.Fatalf("time marshaling is not correct")
}
nnonset.TimeVal = zeroDate()
nnonset2.TimeVal = zeroDate()
if !reflect.DeepEqual(nnonset, nnonset2) {
t.Fatalf("not same: \nn:%#v,\nn2:%#v", nnonset, nnonset2)
}
testGetNonNil(t, nset)
testGetNil(t, nnonset)
testGetNonNil(t, nset2)
testGetNil(t, nnonset2)
}
func TestMarshalNullTime(t *testing.T) {
nt := &NullTime{}
err := nt.UnmarshalJSON([]byte("null1"))
if err == nil {
t.Fatal("null1 is not a valid time")
}
}
func TestMarshalNullBool(t *testing.T) {
nb := &NullBool{}
err := nb.UnmarshalJSON([]byte("null1"))
if err == nil {
t.Fatal("null1 is not a valid bool")
}
}
func zeroDate() time.Time {
return time.Date(1, time.January, 1, 0, 0, 0, 0, time.FixedZone("", 0))
}
func testGetNil(t *testing.T, n *nullable) {
testF(t, "n.StringNVal.Get() == nil", n.StringNVal.Get() == nil)
testF(t, "n.Int64NVal.Get() == nil", n.Int64NVal.Get() == nil)
testF(t, "n.Float64NVal.Get() == nil", n.Float64NVal.Get() == nil)
testF(t, "n.BoolNVal.Get() == nil", n.BoolNVal.Get() == nil)
testF(t, "n.TimeNVal.Get() == nil", n.TimeNVal.Get() == nil)
}
func testGetNonNil(t *testing.T, n *nullable) {
testF(t, "n.StringNVal.Get() != nil", n.StringNVal.Get() != nil)
testF(t, "n.Int64NVal.Get() != nil", n.Int64NVal.Get() != nil)
testF(t, "n.Float64NVal.Get() != nil", n.Float64NVal.Get() != nil)
testF(t, "n.BoolNVal.Get() != nil", n.BoolNVal.Get() != nil)
testF(t, "n.TimeNVal.Get() != nil", n.TimeNVal.Get() != nil)
}
func testF(tb testing.TB, msg string, res bool) {
if !res {
fmt.Printf("exp: %s\n", msg)
tb.Fail()
}
}