-
Notifications
You must be signed in to change notification settings - Fork 3
/
string_test.go
111 lines (84 loc) · 3.14 KB
/
string_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
package null_test
import (
"database/sql/driver"
"encoding/json"
"testing"
"github.com/nyaruka/null/v3"
"github.com/stretchr/testify/assert"
)
func TestString(t *testing.T) {
db := getTestDB()
mustExec(db, `DROP TABLE IF EXISTS test; CREATE TABLE test(value VARCHAR(255) NULL);`)
tcs := []struct {
value null.String
dbValue driver.Value
marshaled []byte
}{
{null.String("foo"), "foo", []byte(`"foo"`)},
{null.NullString, 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.String
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.String
err = json.Unmarshal(marshaled, &unmarshaled)
assert.NoError(t, err)
assert.Equal(t, tc.value, unmarshaled, "unmarshaled mismatch for %v", tc.value)
}
}
type CustomString string
const NullCustomString = CustomString("")
func (s *CustomString) Scan(value any) error { return null.ScanString(value, s) }
func (s CustomString) Value() (driver.Value, error) { return null.StringValue(s) }
func (s CustomString) MarshalJSON() ([]byte, error) { return null.MarshalString(s) }
func (s *CustomString) UnmarshalJSON(b []byte) error { return null.UnmarshalString(b, s) }
func TestCustomString(t *testing.T) {
db := getTestDB()
mustExec(db, `DROP TABLE IF EXISTS test; CREATE TABLE test(value VARCHAR(255) NULL);`)
tcs := []struct {
value CustomString
dbValue driver.Value
marshaled []byte
}{
{CustomString("foo"), "foo", []byte(`"foo"`)},
{CustomString(""), 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 CustomString
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 CustomString
err = json.Unmarshal(marshaled, &unmarshaled)
assert.NoError(t, err)
assert.Equal(t, tc.value, unmarshaled, "unmarshaled mismatch for %v", tc.value)
}
}