-
Notifications
You must be signed in to change notification settings - Fork 3
/
int_test.go
189 lines (147 loc) · 4.8 KB
/
int_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
package null_test
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"testing"
_ "github.com/lib/pq"
"github.com/nyaruka/null/v3"
"github.com/stretchr/testify/assert"
)
func TestInt(t *testing.T) {
db := getTestDB()
mustExec(db, `DROP TABLE IF EXISTS test; CREATE TABLE test(value VARCHAR(255) NULL);`)
tcs := []struct {
value null.Int
dbValue driver.Value
marshaled []byte
}{
{null.Int(123), int64(123), []byte(`123`)},
{null.NullInt, nil, []byte(`null`)},
}
for _, tc := range tcs {
mustExec(db, `DELETE FROM test`)
dbValue, err := tc.value.Value()
assert.NoError(t, err)
assert.Equal(t, tc.dbValue, dbValue, "db value mismatch for %v", tc.value)
// check writing the value to the database
_, err = db.Exec(`INSERT INTO test(value) VALUES($1)`, tc.value)
assert.NoError(t, err, "unexpected error writing %v", tc.value)
rows, err := db.Query(`SELECT value FROM test;`)
assert.NoError(t, err)
var scanned null.Int
assert.True(t, rows.Next())
err = rows.Scan(&scanned)
assert.NoError(t, err)
assert.Equal(t, tc.value, scanned, "scanned value mismatch for %v", tc.value)
marshaled, err := json.Marshal(tc.value)
assert.NoError(t, err)
assert.Equal(t, tc.marshaled, marshaled, "marshaled mismatch for %v", tc.value)
var unmarshaled null.Int
err = json.Unmarshal(marshaled, &unmarshaled)
assert.NoError(t, err)
assert.Equal(t, tc.value, unmarshaled, "unmarshaled mismatch for %v", tc.value)
}
}
func TestInt64(t *testing.T) {
db := getTestDB()
mustExec(db, `DROP TABLE IF EXISTS test; CREATE TABLE test(value VARCHAR(255) NULL);`)
tcs := []struct {
value null.Int64
dbValue driver.Value
marshaled []byte
}{
{null.Int64(123), int64(123), []byte(`123`)},
{null.NullInt64, nil, []byte(`null`)},
}
for _, tc := range tcs {
mustExec(db, `DELETE FROM test`)
dbValue, err := tc.value.Value()
assert.NoError(t, err)
assert.Equal(t, tc.dbValue, dbValue, "db value mismatch for %v", tc.value)
// check writing the value to the database
_, err = db.Exec(`INSERT INTO test(value) VALUES($1)`, tc.value)
assert.NoError(t, err, "unexpected error writing %v", tc.value)
rows, err := db.Query(`SELECT value FROM test;`)
assert.NoError(t, err)
var scanned null.Int64
assert.True(t, rows.Next())
err = rows.Scan(&scanned)
assert.NoError(t, err)
assert.Equal(t, tc.value, scanned, "scanned value mismatch for %v", tc.value)
marshaled, err := json.Marshal(tc.value)
assert.NoError(t, err)
assert.Equal(t, tc.marshaled, marshaled, "marshaled mismatch for %v", tc.value)
var unmarshaled null.Int64
err = json.Unmarshal(marshaled, &unmarshaled)
assert.NoError(t, err)
assert.Equal(t, tc.value, unmarshaled, "unmarshaled mismatch for %v", tc.value)
}
}
type CustomID int64
const NullCustomID = CustomID(0)
func (i *CustomID) Scan(value any) error { return null.ScanInt(value, i) }
func (i CustomID) Value() (driver.Value, error) { return null.IntValue(i) }
func (i *CustomID) UnmarshalJSON(b []byte) error { return null.UnmarshalInt(b, i) }
func (i CustomID) MarshalJSON() ([]byte, error) { return null.MarshalInt(i) }
func TestCustomInt64(t *testing.T) {
db := getTestDB()
mustExec(db, `DROP TABLE IF EXISTS test; CREATE TABLE test(id INT NULL);`)
ten := int64(10)
tcs := []struct {
Value CustomID
JSON string
DB *int64
Test CustomID
}{
{CustomID(10), "10", &ten, CustomID(10)},
{CustomID(0), "null", nil, NullCustomID},
{10, "10", &ten, CustomID(10)},
{NullCustomID, "null", nil, CustomID(0)},
}
for i, tc := range tcs {
mustExec(db, `DELETE FROM test`)
b, err := json.Marshal(tc.Value)
assert.NoError(t, err)
assert.True(t, tc.JSON == string(b), "%d: %s not equal to %s", i, tc.JSON, string(b))
id := CustomID(10)
err = json.Unmarshal(b, &id)
assert.NoError(t, err)
assert.True(t, tc.Value == id, "%d: %s not equal to %s", i, tc.Value, id)
assert.True(t, tc.Test == id, "%d: %s not equal to %s", i, tc.Test, id)
_, err = db.Exec(`INSERT INTO test(id) VALUES($1)`, tc.Value)
assert.NoError(t, err)
rows, err := db.Query(`SELECT id FROM test;`)
assert.NoError(t, err)
var intID *int64
assert.True(t, rows.Next())
err = rows.Scan(&intID)
assert.NoError(t, err)
if tc.DB == nil {
assert.Nil(t, intID)
} else {
assert.True(t, *tc.DB == *intID)
}
rows, err = db.Query(`SELECT id FROM test;`)
assert.NoError(t, err)
assert.True(t, rows.Next())
err = rows.Scan(&id)
assert.NoError(t, err)
assert.True(t, tc.Value == id)
assert.True(t, tc.Test == id)
}
}
func getTestDB() *sql.DB {
db, err := sql.Open("postgres", "postgres://null_test:temba@localhost/null_test?sslmode=disable&Timezone=UTC")
if err != nil {
panic(err)
}
return db
}
func mustExec(db *sql.DB, q string) sql.Result {
res, err := db.Exec(q)
if err != nil {
panic(err)
}
return res
}