2
0

Add tests for Hstore

This commit is contained in:
Jack Christensen
2014-09-19 16:30:43 -05:00
parent 2f1ec582d7
commit 890357732b
3 changed files with 78 additions and 0 deletions
+4
View File
@@ -174,6 +174,10 @@ To setup the normal test environment run the following SQL:
create user pgx_md5 password 'secret';
create database pgx_test;
Connect to database pgx_test and run:
create extension hstore;
Next open connection_settings_test.go.example and make a copy without the
.example. If your PostgreSQL server is accepting connections on 127.0.0.1,
then you are done.
+3
View File
@@ -81,6 +81,9 @@ func parseHstoreToNullHstore(s string) (store map[string]NullString, err error)
}
func ParseHstore(s string) (k []string, v []NullString, err error) {
if s == "" {
return
}
buf := bytes.Buffer{}
keys := []string{}
+71
View File
@@ -0,0 +1,71 @@
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)
}
}