From 11a75aa29eb8a470768b5b8f0e1c739170187f38 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Fri, 19 Sep 2014 17:34:02 -0500 Subject: [PATCH] Add tests for NullHstore --- hstore_test.go | 110 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/hstore_test.go b/hstore_test.go index 1308cf43..f556b55e 100644 --- a/hstore_test.go +++ b/hstore_test.go @@ -69,3 +69,113 @@ func TestHstoreTranscode(t *testing.T) { 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) + } +}