Add tests for NullHstore
This commit is contained in:
+110
@@ -69,3 +69,113 @@ func TestHstoreTranscode(t *testing.T) {
|
|||||||
ensureConnValid(t, conn)
|
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": pgx.NullString{"bar", true}},
|
||||||
|
Valid: true},
|
||||||
|
"single key/value"},
|
||||||
|
{pgx.NullHstore{
|
||||||
|
Hstore: map[string]pgx.NullString{"foo": pgx.NullString{"bar", true}, "baz": pgx.NullString{"quz", true}},
|
||||||
|
Valid: true},
|
||||||
|
"multiple key/values"},
|
||||||
|
{pgx.NullHstore{
|
||||||
|
Hstore: map[string]pgx.NullString{"NULL": pgx.NullString{"bar", true}},
|
||||||
|
Valid: true},
|
||||||
|
`string "NULL" key`},
|
||||||
|
{pgx.NullHstore{
|
||||||
|
Hstore: map[string]pgx.NullString{"foo": pgx.NullString{"NULL", true}},
|
||||||
|
Valid: true},
|
||||||
|
`string "NULL" value`},
|
||||||
|
{pgx.NullHstore{
|
||||||
|
Hstore: map[string]pgx.NullString{"foo": pgx.NullString{"", 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": pgx.NullString{"bar", true}},
|
||||||
|
Valid: true},
|
||||||
|
"key with " + sst.description + " at beginning"})
|
||||||
|
tests = append(tests, test{pgx.NullHstore{
|
||||||
|
Hstore: map[string]pgx.NullString{"foo" + sst.input + "foo": pgx.NullString{"bar", true}},
|
||||||
|
Valid: true},
|
||||||
|
"key with " + sst.description + " in middle"})
|
||||||
|
tests = append(tests, test{pgx.NullHstore{
|
||||||
|
Hstore: map[string]pgx.NullString{"foo" + sst.input: pgx.NullString{"bar", true}},
|
||||||
|
Valid: true},
|
||||||
|
"key with " + sst.description + " at end"})
|
||||||
|
tests = append(tests, test{pgx.NullHstore{
|
||||||
|
Hstore: map[string]pgx.NullString{sst.input: pgx.NullString{"bar", true}},
|
||||||
|
Valid: true},
|
||||||
|
"key is " + sst.description})
|
||||||
|
|
||||||
|
tests = append(tests, test{pgx.NullHstore{
|
||||||
|
Hstore: map[string]pgx.NullString{"foo": pgx.NullString{sst.input + "bar", true}},
|
||||||
|
Valid: true},
|
||||||
|
"value with " + sst.description + " at beginning"})
|
||||||
|
tests = append(tests, test{pgx.NullHstore{
|
||||||
|
Hstore: map[string]pgx.NullString{"foo": pgx.NullString{"bar" + sst.input + "bar", true}},
|
||||||
|
Valid: true},
|
||||||
|
"value with " + sst.description + " in middle"})
|
||||||
|
tests = append(tests, test{pgx.NullHstore{
|
||||||
|
Hstore: map[string]pgx.NullString{"foo": pgx.NullString{"bar" + sst.input, true}},
|
||||||
|
Valid: true},
|
||||||
|
"value with " + sst.description + " at end"})
|
||||||
|
tests = append(tests, test{pgx.NullHstore{
|
||||||
|
Hstore: map[string]pgx.NullString{"foo": pgx.NullString{sst.input, 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 %s, received %s`, tt.description, key, inValue, outValue)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
t.Errorf(`%s: Missing key %s`, tt.description, key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ensureConnValid(t, conn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user