This repository has been archived by the owner on Jul 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hstore_test.go
181 lines (161 loc) · 5.87 KB
/
hstore_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
package pgx_test
import (
"github.com/jackc/pgx"
"testing"
)
func TestHstoreTranscode(t *testing.T) {
t.Parallel()
conn := mustConnect(t, *defaultConnConfig)
defer closeConn(t, conn)
type test struct {
hstore pgx.Hstore
description string
}
tests := []test{
{pgx.Hstore{}, "empty"},
{pgx.Hstore{"foo": "bar"}, "single key/value"},
{pgx.Hstore{"foo": "bar", "baz": "quz"}, "multiple key/values"},
{pgx.Hstore{"NULL": "bar"}, `string "NULL" key`},
{pgx.Hstore{"foo": "NULL"}, `string "NULL" value`},
}
specialStringTests := []struct {
input string
description string
}{
{`"`, `double quote (")`},
{`'`, `single quote (')`},
{`\`, `backslash (\)`},
{`\\`, `multiple backslashes (\\)`},
{`=>`, `separator (=>)`},
{` `, `space`},
{`\ / / \\ => " ' " '`, `multiple special characters`},
}
for _, sst := range specialStringTests {
tests = append(tests, test{pgx.Hstore{sst.input + "foo": "bar"}, "key with " + sst.description + " at beginning"})
tests = append(tests, test{pgx.Hstore{"foo" + sst.input + "foo": "bar"}, "key with " + sst.description + " in middle"})
tests = append(tests, test{pgx.Hstore{"foo" + sst.input: "bar"}, "key with " + sst.description + " at end"})
tests = append(tests, test{pgx.Hstore{sst.input: "bar"}, "key is " + sst.description})
tests = append(tests, test{pgx.Hstore{"foo": sst.input + "bar"}, "value with " + sst.description + " at beginning"})
tests = append(tests, test{pgx.Hstore{"foo": "bar" + sst.input + "bar"}, "value with " + sst.description + " in middle"})
tests = append(tests, test{pgx.Hstore{"foo": "bar" + sst.input}, "value with " + sst.description + " at end"})
tests = append(tests, test{pgx.Hstore{"foo": sst.input}, "value is " + sst.description})
}
for _, tt := range tests {
var result pgx.Hstore
err := conn.QueryRow("select $1::hstore", tt.hstore).Scan(&result)
if err != nil {
t.Errorf(`%s: QueryRow.Scan returned an error: %v`, tt.description, err)
}
for key, inValue := range tt.hstore {
outValue, ok := result[key]
if ok {
if inValue != outValue {
t.Errorf(`%s: Key %s mismatch - expected %s, received %s`, tt.description, key, inValue, outValue)
}
} else {
t.Errorf(`%s: Missing key %s`, tt.description, key)
}
}
ensureConnValid(t, conn)
}
}
func TestNullHstoreTranscode(t *testing.T) {
t.Parallel()
conn := mustConnect(t, *defaultConnConfig)
defer closeConn(t, conn)
type test struct {
nullHstore pgx.NullHstore
description string
}
tests := []test{
{pgx.NullHstore{}, "null"},
{pgx.NullHstore{Valid: true}, "empty"},
{pgx.NullHstore{
Hstore: map[string]pgx.NullString{"foo": {String: "bar", Valid: true}},
Valid: true},
"single key/value"},
{pgx.NullHstore{
Hstore: map[string]pgx.NullString{"foo": {String: "bar", Valid: true}, "baz": {String: "quz", Valid: true}},
Valid: true},
"multiple key/values"},
{pgx.NullHstore{
Hstore: map[string]pgx.NullString{"NULL": {String: "bar", Valid: true}},
Valid: true},
`string "NULL" key`},
{pgx.NullHstore{
Hstore: map[string]pgx.NullString{"foo": {String: "NULL", Valid: true}},
Valid: true},
`string "NULL" value`},
{pgx.NullHstore{
Hstore: map[string]pgx.NullString{"foo": {String: "", Valid: false}},
Valid: true},
`NULL value`},
}
specialStringTests := []struct {
input string
description string
}{
{`"`, `double quote (")`},
{`'`, `single quote (')`},
{`\`, `backslash (\)`},
{`\\`, `multiple backslashes (\\)`},
{`=>`, `separator (=>)`},
{` `, `space`},
{`\ / / \\ => " ' " '`, `multiple special characters`},
}
for _, sst := range specialStringTests {
tests = append(tests, test{pgx.NullHstore{
Hstore: map[string]pgx.NullString{sst.input + "foo": {String: "bar", Valid: true}},
Valid: true},
"key with " + sst.description + " at beginning"})
tests = append(tests, test{pgx.NullHstore{
Hstore: map[string]pgx.NullString{"foo" + sst.input + "foo": {String: "bar", Valid: true}},
Valid: true},
"key with " + sst.description + " in middle"})
tests = append(tests, test{pgx.NullHstore{
Hstore: map[string]pgx.NullString{"foo" + sst.input: {String: "bar", Valid: true}},
Valid: true},
"key with " + sst.description + " at end"})
tests = append(tests, test{pgx.NullHstore{
Hstore: map[string]pgx.NullString{sst.input: {String: "bar", Valid: true}},
Valid: true},
"key is " + sst.description})
tests = append(tests, test{pgx.NullHstore{
Hstore: map[string]pgx.NullString{"foo": {String: sst.input + "bar", Valid: true}},
Valid: true},
"value with " + sst.description + " at beginning"})
tests = append(tests, test{pgx.NullHstore{
Hstore: map[string]pgx.NullString{"foo": {String: "bar" + sst.input + "bar", Valid: true}},
Valid: true},
"value with " + sst.description + " in middle"})
tests = append(tests, test{pgx.NullHstore{
Hstore: map[string]pgx.NullString{"foo": {String: "bar" + sst.input, Valid: true}},
Valid: true},
"value with " + sst.description + " at end"})
tests = append(tests, test{pgx.NullHstore{
Hstore: map[string]pgx.NullString{"foo": {String: sst.input, Valid: true}},
Valid: true},
"value is " + sst.description})
}
for _, tt := range tests {
var result pgx.NullHstore
err := conn.QueryRow("select $1::hstore", tt.nullHstore).Scan(&result)
if err != nil {
t.Errorf(`%s: QueryRow.Scan returned an error: %v`, tt.description, err)
}
if result.Valid != tt.nullHstore.Valid {
t.Errorf(`%s: Valid mismatch - expected %v, received %v`, tt.description, tt.nullHstore.Valid, result.Valid)
}
for key, inValue := range tt.nullHstore.Hstore {
outValue, ok := result.Hstore[key]
if ok {
if inValue != outValue {
t.Errorf(`%s: Key %s mismatch - expected %v, received %v`, tt.description, key, inValue, outValue)
}
} else {
t.Errorf(`%s: Missing key %s`, tt.description, key)
}
}
ensureConnValid(t, conn)
}
}