From c16a4f7d6a7cfac78cfa7e927264e21346bbdc20 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sat, 24 Jul 2021 10:40:30 -0500 Subject: [PATCH] Revert "Temporarily delete tests and pgxtype to break recursive dependency with pgx" This reverts commit 32e20a603178b49fb189d1be971d0fb6960cabb2. --- aclitem_array_test.go | 329 +++++++++++++++++ aclitem_test.go | 97 +++++ array_test.go | 127 +++++++ array_type_test.go | 84 +++++ bit_test.go | 25 ++ bool_array_test.go | 283 +++++++++++++++ bool_test.go | 140 ++++++++ box_test.go | 34 ++ bpchar_array_test.go | 55 +++ bpchar_test.go | 51 +++ bytea_array_test.go | 229 ++++++++++++ bytea_test.go | 73 ++++ cid_test.go | 105 ++++++ cidr_array_test.go | 319 ++++++++++++++++ circle_test.go | 16 + composite_bench_test.go | 192 ++++++++++ composite_fields_test.go | 273 ++++++++++++++ composite_type_test.go | 320 +++++++++++++++++ custom_composite_test.go | 87 +++++ date_array_test.go | 327 +++++++++++++++++ date_test.go | 168 +++++++++ daterange_test.go | 133 +++++++ enum_array_test.go | 281 +++++++++++++++ enum_type_test.go | 148 ++++++++ ext/gofrs-uuid/uuid_test.go | 101 ++++++ ext/shopspring-numeric/decimal_test.go | 330 +++++++++++++++++ float4_array_test.go | 282 +++++++++++++++ float4_test.go | 149 ++++++++ float8_array_test.go | 258 +++++++++++++ float8_test.go | 149 ++++++++ go.mod | 4 + go.sum | 480 +++++++++++++++++++++++++ hstore_array_test.go | 436 ++++++++++++++++++++++ hstore_test.go | 111 ++++++ inet_array_test.go | 319 ++++++++++++++++ inet_test.go | 134 +++++++ int2_array_test.go | 342 ++++++++++++++++++ int2_test.go | 144 ++++++++ int4_array_test.go | 356 ++++++++++++++++++ int4_test.go | 186 ++++++++++ int4range_test.go | 28 ++ int8_array_test.go | 349 ++++++++++++++++++ int8_test.go | 187 ++++++++++ int8range_test.go | 28 ++ interval_test.go | 74 ++++ json_test.go | 177 +++++++++ jsonb_array_test.go | 36 ++ jsonb_test.go | 142 ++++++++ line_test.go | 38 ++ lseg_test.go | 22 ++ macaddr_array_test.go | 262 ++++++++++++++ macaddr_test.go | 78 ++++ name_test.go | 98 +++++ numeric_array_test.go | 305 ++++++++++++++++ numeric_test.go | 389 ++++++++++++++++++++ numrange_test.go | 46 +++ oid_value_test.go | 95 +++++ path_test.go | 29 ++ pgtype_test.go | 292 +++++++++++++++ pgxtype/README.md | 3 + pgxtype/pgxtype.go | 145 ++++++++ point_test.go | 150 ++++++++ polygon_test.go | 89 +++++ qchar_test.go | 143 ++++++++ range_test.go | 177 +++++++++ record_test.go | 186 ++++++++++ testutil/testutil.go | 436 ++++++++++++++++++++++ text_array_test.go | 294 +++++++++++++++ text_test.go | 164 +++++++++ tid_test.go | 63 ++++ time_test.go | 131 +++++++ timestamp_array_test.go | 307 ++++++++++++++++ timestamp_test.go | 178 +++++++++ timestamptz_array_test.go | 343 ++++++++++++++++++ timestamptz_test.go | 224 ++++++++++++ tsrange_test.go | 41 +++ tstzrange_test.go | 49 +++ uuid_array_test.go | 368 +++++++++++++++++++ uuid_test.go | 245 +++++++++++++ varbit_test.go | 26 ++ varchar_array_test.go | 282 +++++++++++++++ xid_test.go | 105 ++++++ zeronull/int2_test.go | 23 ++ zeronull/int4_test.go | 23 ++ zeronull/int8_test.go | 23 ++ zeronull/text_test.go | 23 ++ zeronull/timestamp_test.go | 29 ++ zeronull/timestamptz_test.go | 29 ++ zeronull/uuid_test.go | 23 ++ 89 files changed, 14674 insertions(+) create mode 100644 aclitem_array_test.go create mode 100644 aclitem_test.go create mode 100644 array_test.go create mode 100644 array_type_test.go create mode 100644 bit_test.go create mode 100644 bool_array_test.go create mode 100644 bool_test.go create mode 100644 box_test.go create mode 100644 bpchar_array_test.go create mode 100644 bpchar_test.go create mode 100644 bytea_array_test.go create mode 100644 bytea_test.go create mode 100644 cid_test.go create mode 100644 cidr_array_test.go create mode 100644 circle_test.go create mode 100644 composite_bench_test.go create mode 100644 composite_fields_test.go create mode 100644 composite_type_test.go create mode 100644 custom_composite_test.go create mode 100644 date_array_test.go create mode 100644 date_test.go create mode 100644 daterange_test.go create mode 100644 enum_array_test.go create mode 100644 enum_type_test.go create mode 100644 ext/gofrs-uuid/uuid_test.go create mode 100644 ext/shopspring-numeric/decimal_test.go create mode 100644 float4_array_test.go create mode 100644 float4_test.go create mode 100644 float8_array_test.go create mode 100644 float8_test.go create mode 100644 hstore_array_test.go create mode 100644 hstore_test.go create mode 100644 inet_array_test.go create mode 100644 inet_test.go create mode 100644 int2_array_test.go create mode 100644 int2_test.go create mode 100644 int4_array_test.go create mode 100644 int4_test.go create mode 100644 int4range_test.go create mode 100644 int8_array_test.go create mode 100644 int8_test.go create mode 100644 int8range_test.go create mode 100644 interval_test.go create mode 100644 json_test.go create mode 100644 jsonb_array_test.go create mode 100644 jsonb_test.go create mode 100644 line_test.go create mode 100644 lseg_test.go create mode 100644 macaddr_array_test.go create mode 100644 macaddr_test.go create mode 100644 name_test.go create mode 100644 numeric_array_test.go create mode 100644 numeric_test.go create mode 100644 numrange_test.go create mode 100644 oid_value_test.go create mode 100644 path_test.go create mode 100644 pgtype_test.go create mode 100644 pgxtype/README.md create mode 100644 pgxtype/pgxtype.go create mode 100644 point_test.go create mode 100644 polygon_test.go create mode 100644 qchar_test.go create mode 100644 range_test.go create mode 100644 record_test.go create mode 100644 testutil/testutil.go create mode 100644 text_array_test.go create mode 100644 text_test.go create mode 100644 tid_test.go create mode 100644 time_test.go create mode 100644 timestamp_array_test.go create mode 100644 timestamp_test.go create mode 100644 timestamptz_array_test.go create mode 100644 timestamptz_test.go create mode 100644 tsrange_test.go create mode 100644 tstzrange_test.go create mode 100644 uuid_array_test.go create mode 100644 uuid_test.go create mode 100644 varbit_test.go create mode 100644 varchar_array_test.go create mode 100644 xid_test.go create mode 100644 zeronull/int2_test.go create mode 100644 zeronull/int4_test.go create mode 100644 zeronull/int8_test.go create mode 100644 zeronull/text_test.go create mode 100644 zeronull/timestamp_test.go create mode 100644 zeronull/timestamptz_test.go create mode 100644 zeronull/uuid_test.go diff --git a/aclitem_array_test.go b/aclitem_array_test.go new file mode 100644 index 00000000..8f015f40 --- /dev/null +++ b/aclitem_array_test.go @@ -0,0 +1,329 @@ +package pgtype_test + +import ( + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestACLItemArrayTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "aclitem[]", []interface{}{ + &pgtype.ACLItemArray{ + Elements: nil, + Dimensions: nil, + Status: pgtype.Present, + }, + &pgtype.ACLItemArray{ + Elements: []pgtype.ACLItem{ + {String: "=r/postgres", Status: pgtype.Present}, + {Status: pgtype.Null}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.ACLItemArray{Status: pgtype.Null}, + &pgtype.ACLItemArray{ + Elements: []pgtype.ACLItem{ + {String: "=r/postgres", Status: pgtype.Present}, + {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}, + //{String: `postgres=arwdDxt/" tricky, ' } "" \ test user "`, Status: pgtype.Present}, + {String: `postgres=arwdDxt/postgres`, Status: pgtype.Present}, // todo: remove after fixing above case + {String: "=r/postgres", Status: pgtype.Present}, + {Status: pgtype.Null}, + {String: "=r/postgres", Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}, {Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.ACLItemArray{ + Elements: []pgtype.ACLItem{ + {String: "=r/postgres", Status: pgtype.Present}, + {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}, + {String: "=r/postgres", Status: pgtype.Present}, + {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{ + {Length: 2, LowerBound: 4}, + {Length: 2, LowerBound: 2}, + }, + Status: pgtype.Present, + }, + }) +} + +func TestACLItemArraySet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.ACLItemArray + }{ + { + source: []string{"=r/postgres"}, + result: pgtype.ACLItemArray{ + Elements: []pgtype.ACLItem{{String: "=r/postgres", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: (([]string)(nil)), + result: pgtype.ACLItemArray{Status: pgtype.Null}, + }, + { + source: [][]string{{"=r/postgres"}, {"postgres=arwdDxt/postgres"}}, + result: pgtype.ACLItemArray{ + Elements: []pgtype.ACLItem{ + {String: "=r/postgres", Status: pgtype.Present}, + {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [][][][]string{ + {{{ + "=r/postgres", + "postgres=arwdDxt/postgres", + "=r/postgres"}}}, + {{{ + "postgres=arwdDxt/postgres", + "=r/postgres", + "postgres=arwdDxt/postgres"}}}}, + result: pgtype.ACLItemArray{ + Elements: []pgtype.ACLItem{ + {String: "=r/postgres", Status: pgtype.Present}, + {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}, + {String: "=r/postgres", Status: pgtype.Present}, + {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}, + {String: "=r/postgres", Status: pgtype.Present}, + {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + { + source: [2][1]string{{"=r/postgres"}, {"postgres=arwdDxt/postgres"}}, + result: pgtype.ACLItemArray{ + Elements: []pgtype.ACLItem{ + {String: "=r/postgres", Status: pgtype.Present}, + {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [2][1][1][3]string{ + {{{ + "=r/postgres", + "postgres=arwdDxt/postgres", + "=r/postgres"}}}, + {{{ + "postgres=arwdDxt/postgres", + "=r/postgres", + "postgres=arwdDxt/postgres"}}}}, + result: pgtype.ACLItemArray{ + Elements: []pgtype.ACLItem{ + {String: "=r/postgres", Status: pgtype.Present}, + {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}, + {String: "=r/postgres", Status: pgtype.Present}, + {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}, + {String: "=r/postgres", Status: pgtype.Present}, + {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + } + + for i, tt := range successfulTests { + var r pgtype.ACLItemArray + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !reflect.DeepEqual(r, tt.result) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestACLItemArrayAssignTo(t *testing.T) { + var stringSlice []string + type _stringSlice []string + var namedStringSlice _stringSlice + var stringSliceDim2 [][]string + var stringSliceDim4 [][][][]string + var stringArrayDim2 [2][1]string + var stringArrayDim4 [2][1][1][3]string + + simpleTests := []struct { + src pgtype.ACLItemArray + dst interface{} + expected interface{} + }{ + { + src: pgtype.ACLItemArray{ + Elements: []pgtype.ACLItem{{String: "=r/postgres", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &stringSlice, + expected: []string{"=r/postgres"}, + }, + { + src: pgtype.ACLItemArray{ + Elements: []pgtype.ACLItem{{String: "=r/postgres", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &namedStringSlice, + expected: _stringSlice{"=r/postgres"}, + }, + { + src: pgtype.ACLItemArray{Status: pgtype.Null}, + dst: &stringSlice, + expected: (([]string)(nil)), + }, + { + src: pgtype.ACLItemArray{Status: pgtype.Present}, + dst: &stringSlice, + expected: []string{}, + }, + { + src: pgtype.ACLItemArray{ + Elements: []pgtype.ACLItem{ + {String: "=r/postgres", Status: pgtype.Present}, + {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &stringSliceDim2, + expected: [][]string{{"=r/postgres"}, {"postgres=arwdDxt/postgres"}}, + }, + { + src: pgtype.ACLItemArray{ + Elements: []pgtype.ACLItem{ + {String: "=r/postgres", Status: pgtype.Present}, + {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}, + {String: "=r/postgres", Status: pgtype.Present}, + {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}, + {String: "=r/postgres", Status: pgtype.Present}, + {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + dst: &stringSliceDim4, + expected: [][][][]string{ + {{{ + "=r/postgres", + "postgres=arwdDxt/postgres", + "=r/postgres"}}}, + {{{ + "postgres=arwdDxt/postgres", + "=r/postgres", + "postgres=arwdDxt/postgres"}}}}, + }, + { + src: pgtype.ACLItemArray{ + Elements: []pgtype.ACLItem{ + {String: "=r/postgres", Status: pgtype.Present}, + {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &stringArrayDim2, + expected: [2][1]string{{"=r/postgres"}, {"postgres=arwdDxt/postgres"}}, + }, + { + src: pgtype.ACLItemArray{ + Elements: []pgtype.ACLItem{ + {String: "=r/postgres", Status: pgtype.Present}, + {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}, + {String: "=r/postgres", Status: pgtype.Present}, + {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}, + {String: "=r/postgres", Status: pgtype.Present}, + {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + dst: &stringArrayDim4, + expected: [2][1][1][3]string{ + {{{ + "=r/postgres", + "postgres=arwdDxt/postgres", + "=r/postgres"}}}, + {{{ + "postgres=arwdDxt/postgres", + "=r/postgres", + "postgres=arwdDxt/postgres"}}}}, + }, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.ACLItemArray + dst interface{} + }{ + { + src: pgtype.ACLItemArray{ + Elements: []pgtype.ACLItem{{Status: pgtype.Null}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &stringSlice, + }, + { + src: pgtype.ACLItemArray{ + Elements: []pgtype.ACLItem{ + {String: "=r/postgres", Status: pgtype.Present}, + {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &stringArrayDim2, + }, + { + src: pgtype.ACLItemArray{ + Elements: []pgtype.ACLItem{ + {String: "=r/postgres", Status: pgtype.Present}, + {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &stringSlice, + }, + { + src: pgtype.ACLItemArray{ + Elements: []pgtype.ACLItem{ + {String: "=r/postgres", Status: pgtype.Present}, + {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &stringArrayDim4, + }, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } +} diff --git a/aclitem_test.go b/aclitem_test.go new file mode 100644 index 00000000..a37d7657 --- /dev/null +++ b/aclitem_test.go @@ -0,0 +1,97 @@ +package pgtype_test + +import ( + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestACLItemTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "aclitem", []interface{}{ + &pgtype.ACLItem{String: "postgres=arwdDxt/postgres", Status: pgtype.Present}, + //&pgtype.ACLItem{String: `postgres=arwdDxt/" tricky, ' } "" \ test user "`, Status: pgtype.Present}, + &pgtype.ACLItem{Status: pgtype.Null}, + }) +} + +func TestACLItemSet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.ACLItem + }{ + {source: "postgres=arwdDxt/postgres", result: pgtype.ACLItem{String: "postgres=arwdDxt/postgres", Status: pgtype.Present}}, + {source: (*string)(nil), result: pgtype.ACLItem{Status: pgtype.Null}}, + } + + for i, tt := range successfulTests { + var d pgtype.ACLItem + err := d.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if d != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, d) + } + } +} + +func TestACLItemAssignTo(t *testing.T) { + var s string + var ps *string + + simpleTests := []struct { + src pgtype.ACLItem + dst interface{} + expected interface{} + }{ + {src: pgtype.ACLItem{String: "postgres=arwdDxt/postgres", Status: pgtype.Present}, dst: &s, expected: "postgres=arwdDxt/postgres"}, + {src: pgtype.ACLItem{Status: pgtype.Null}, dst: &ps, expected: ((*string)(nil))}, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + pointerAllocTests := []struct { + src pgtype.ACLItem + dst interface{} + expected interface{} + }{ + {src: pgtype.ACLItem{String: "postgres=arwdDxt/postgres", Status: pgtype.Present}, dst: &ps, expected: "postgres=arwdDxt/postgres"}, + } + + for i, tt := range pointerAllocTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.ACLItem + dst interface{} + }{ + {src: pgtype.ACLItem{Status: pgtype.Null}, dst: &s}, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } +} diff --git a/array_test.go b/array_test.go new file mode 100644 index 00000000..d2120677 --- /dev/null +++ b/array_test.go @@ -0,0 +1,127 @@ +package pgtype_test + +import ( + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/stretchr/testify/require" +) + +func TestParseUntypedTextArray(t *testing.T) { + tests := []struct { + source string + result pgtype.UntypedTextArray + }{ + { + source: "{}", + result: pgtype.UntypedTextArray{ + Elements: nil, + Quoted: nil, + Dimensions: nil, + }, + }, + { + source: "{1}", + result: pgtype.UntypedTextArray{ + Elements: []string{"1"}, + Quoted: []bool{false}, + Dimensions: []pgtype.ArrayDimension{{Length: 1, LowerBound: 1}}, + }, + }, + { + source: "{a,b}", + result: pgtype.UntypedTextArray{ + Elements: []string{"a", "b"}, + Quoted: []bool{false, false}, + Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}}, + }, + }, + { + source: `{"NULL"}`, + result: pgtype.UntypedTextArray{ + Elements: []string{"NULL"}, + Quoted: []bool{true}, + Dimensions: []pgtype.ArrayDimension{{Length: 1, LowerBound: 1}}, + }, + }, + { + source: `{""}`, + result: pgtype.UntypedTextArray{ + Elements: []string{""}, + Quoted: []bool{true}, + Dimensions: []pgtype.ArrayDimension{{Length: 1, LowerBound: 1}}, + }, + }, + { + source: `{"He said, \"Hello.\""}`, + result: pgtype.UntypedTextArray{ + Elements: []string{`He said, "Hello."`}, + Quoted: []bool{true}, + Dimensions: []pgtype.ArrayDimension{{Length: 1, LowerBound: 1}}, + }, + }, + { + source: "{{a,b},{c,d},{e,f}}", + result: pgtype.UntypedTextArray{ + Elements: []string{"a", "b", "c", "d", "e", "f"}, + Quoted: []bool{false, false, false, false, false, false}, + Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}, {Length: 2, LowerBound: 1}}, + }, + }, + { + source: "{{{a,b},{c,d},{e,f}},{{a,b},{c,d},{e,f}}}", + result: pgtype.UntypedTextArray{ + Elements: []string{"a", "b", "c", "d", "e", "f", "a", "b", "c", "d", "e", "f"}, + Quoted: []bool{false, false, false, false, false, false, false, false, false, false, false, false}, + Dimensions: []pgtype.ArrayDimension{ + {Length: 2, LowerBound: 1}, + {Length: 3, LowerBound: 1}, + {Length: 2, LowerBound: 1}, + }, + }, + }, + { + source: "[4:4]={1}", + result: pgtype.UntypedTextArray{ + Elements: []string{"1"}, + Quoted: []bool{false}, + Dimensions: []pgtype.ArrayDimension{{Length: 1, LowerBound: 4}}, + }, + }, + { + source: "[4:5][2:3]={{a,b},{c,d}}", + result: pgtype.UntypedTextArray{ + Elements: []string{"a", "b", "c", "d"}, + Quoted: []bool{false, false, false, false}, + Dimensions: []pgtype.ArrayDimension{ + {Length: 2, LowerBound: 4}, + {Length: 2, LowerBound: 2}, + }, + }, + }, + } + + for i, tt := range tests { + r, err := pgtype.ParseUntypedTextArray(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + continue + } + + if !reflect.DeepEqual(*r, tt.result) { + t.Errorf("%d: expected %+v to be parsed to %+v, but it was %+v", i, tt.source, tt.result, *r) + } + } +} + +// https://github.com/jackc/pgx/issues/881 +func TestArrayAssignToEmptyToNonSlice(t *testing.T) { + var a pgtype.Int4Array + err := a.Set([]int32{}) + require.NoError(t, err) + + var iface interface{} + err = a.AssignTo(&iface) + require.EqualError(t, err, "cannot assign *pgtype.Int4Array to *interface {}") +} diff --git a/array_type_test.go b/array_type_test.go new file mode 100644 index 00000000..626df4dc --- /dev/null +++ b/array_type_test.go @@ -0,0 +1,84 @@ +package pgtype_test + +import ( + "context" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" + "github.com/stretchr/testify/require" +) + +func TestArrayTypeValue(t *testing.T) { + arrayType := pgtype.NewArrayType("_text", pgtype.TextOID, func() pgtype.ValueTranscoder { return &pgtype.Text{} }) + + err := arrayType.Set(nil) + require.NoError(t, err) + + gotValue := arrayType.Get() + require.Nil(t, gotValue) + + slice := []string{"foo", "bar"} + err = arrayType.AssignTo(&slice) + require.NoError(t, err) + require.Nil(t, slice) + + err = arrayType.Set([]string{}) + require.NoError(t, err) + + gotValue = arrayType.Get() + require.Len(t, gotValue, 0) + + err = arrayType.AssignTo(&slice) + require.NoError(t, err) + require.EqualValues(t, []string{}, slice) + + err = arrayType.Set([]string{"baz", "quz"}) + require.NoError(t, err) + + gotValue = arrayType.Get() + require.Len(t, gotValue, 2) + + err = arrayType.AssignTo(&slice) + require.NoError(t, err) + require.EqualValues(t, []string{"baz", "quz"}, slice) +} + +func TestArrayTypeTranscode(t *testing.T) { + conn := testutil.MustConnectPgx(t) + defer testutil.MustCloseContext(t, conn) + + conn.ConnInfo().RegisterDataType(pgtype.DataType{ + Value: pgtype.NewArrayType("_text", pgtype.TextOID, func() pgtype.ValueTranscoder { return &pgtype.Text{} }), + Name: "_text", + OID: pgtype.TextArrayOID, + }) + + var dstStrings []string + err := conn.QueryRow(context.Background(), "select $1::text[]", []string{"red", "green", "blue"}).Scan(&dstStrings) + require.NoError(t, err) + + require.EqualValues(t, []string{"red", "green", "blue"}, dstStrings) +} + +func TestArrayTypeEmptyArrayDoesNotBreakArrayType(t *testing.T) { + conn := testutil.MustConnectPgx(t) + defer testutil.MustCloseContext(t, conn) + + conn.ConnInfo().RegisterDataType(pgtype.DataType{ + Value: pgtype.NewArrayType("_text", pgtype.TextOID, func() pgtype.ValueTranscoder { return &pgtype.Text{} }), + Name: "_text", + OID: pgtype.TextArrayOID, + }) + + var dstStrings []string + err := conn.QueryRow(context.Background(), "select '{}'::text[]").Scan(&dstStrings) + require.NoError(t, err) + + require.EqualValues(t, []string{}, dstStrings) + + err = conn.QueryRow(context.Background(), "select $1::text[]", []string{"red", "green", "blue"}).Scan(&dstStrings) + require.NoError(t, err) + + require.EqualValues(t, []string{"red", "green", "blue"}, dstStrings) +} diff --git a/bit_test.go b/bit_test.go new file mode 100644 index 00000000..2e9c9b6e --- /dev/null +++ b/bit_test.go @@ -0,0 +1,25 @@ +package pgtype_test + +import ( + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestBitTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "bit(40)", []interface{}{ + &pgtype.Varbit{Bytes: []byte{0, 0, 0, 0, 0}, Len: 40, Status: pgtype.Present}, + &pgtype.Varbit{Bytes: []byte{0, 1, 128, 254, 255}, Len: 40, Status: pgtype.Present}, + &pgtype.Varbit{Status: pgtype.Null}, + }) +} + +func TestBitNormalize(t *testing.T) { + testutil.TestSuccessfulNormalize(t, []testutil.NormalizeTest{ + { + SQL: "select B'111111111'", + Value: &pgtype.Bit{Bytes: []byte{255, 128}, Len: 9, Status: pgtype.Present}, + }, + }) +} diff --git a/bool_array_test.go b/bool_array_test.go new file mode 100644 index 00000000..be567e59 --- /dev/null +++ b/bool_array_test.go @@ -0,0 +1,283 @@ +package pgtype_test + +import ( + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestBoolArrayTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "bool[]", []interface{}{ + &pgtype.BoolArray{ + Elements: nil, + Dimensions: nil, + Status: pgtype.Present, + }, + &pgtype.BoolArray{ + Elements: []pgtype.Bool{ + {Bool: true, Status: pgtype.Present}, + {Status: pgtype.Null}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.BoolArray{Status: pgtype.Null}, + &pgtype.BoolArray{ + Elements: []pgtype.Bool{ + {Bool: true, Status: pgtype.Present}, + {Bool: true, Status: pgtype.Present}, + {Bool: false, Status: pgtype.Present}, + {Bool: true, Status: pgtype.Present}, + {Status: pgtype.Null}, + {Bool: false, Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}, {Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.BoolArray{ + Elements: []pgtype.Bool{ + {Bool: true, Status: pgtype.Present}, + {Bool: false, Status: pgtype.Present}, + {Bool: true, Status: pgtype.Present}, + {Bool: false, Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{ + {Length: 2, LowerBound: 4}, + {Length: 2, LowerBound: 2}, + }, + Status: pgtype.Present, + }, + }) +} + +func TestBoolArraySet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.BoolArray + }{ + { + source: []bool{true}, + result: pgtype.BoolArray{ + Elements: []pgtype.Bool{{Bool: true, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: (([]bool)(nil)), + result: pgtype.BoolArray{Status: pgtype.Null}, + }, + { + source: [][]bool{{true}, {false}}, + result: pgtype.BoolArray{ + Elements: []pgtype.Bool{{Bool: true, Status: pgtype.Present}, {Bool: false, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [][][][]bool{{{{true, false, true}}}, {{{false, true, false}}}}, + result: pgtype.BoolArray{ + Elements: []pgtype.Bool{ + {Bool: true, Status: pgtype.Present}, + {Bool: false, Status: pgtype.Present}, + {Bool: true, Status: pgtype.Present}, + {Bool: false, Status: pgtype.Present}, + {Bool: true, Status: pgtype.Present}, + {Bool: false, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + { + source: [2][1]bool{{true}, {false}}, + result: pgtype.BoolArray{ + Elements: []pgtype.Bool{{Bool: true, Status: pgtype.Present}, {Bool: false, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [2][1][1][3]bool{{{{true, false, true}}}, {{{false, true, false}}}}, + result: pgtype.BoolArray{ + Elements: []pgtype.Bool{ + {Bool: true, Status: pgtype.Present}, + {Bool: false, Status: pgtype.Present}, + {Bool: true, Status: pgtype.Present}, + {Bool: false, Status: pgtype.Present}, + {Bool: true, Status: pgtype.Present}, + {Bool: false, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + } + + for i, tt := range successfulTests { + var r pgtype.BoolArray + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !reflect.DeepEqual(r, tt.result) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestBoolArrayAssignTo(t *testing.T) { + var boolSlice []bool + type _boolSlice []bool + var namedBoolSlice _boolSlice + var boolSliceDim2 [][]bool + var boolSliceDim4 [][][][]bool + var boolArrayDim2 [2][1]bool + var boolArrayDim4 [2][1][1][3]bool + + simpleTests := []struct { + src pgtype.BoolArray + dst interface{} + expected interface{} + }{ + { + src: pgtype.BoolArray{ + Elements: []pgtype.Bool{{Bool: true, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &boolSlice, + expected: []bool{true}, + }, + { + src: pgtype.BoolArray{ + Elements: []pgtype.Bool{{Bool: true, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &namedBoolSlice, + expected: _boolSlice{true}, + }, + { + src: pgtype.BoolArray{Status: pgtype.Null}, + dst: &boolSlice, + expected: (([]bool)(nil)), + }, + { + src: pgtype.BoolArray{Status: pgtype.Present}, + dst: &boolSlice, + expected: []bool{}, + }, + { + src: pgtype.BoolArray{ + Elements: []pgtype.Bool{{Bool: true, Status: pgtype.Present}, {Bool: false, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + expected: [][]bool{{true}, {false}}, + dst: &boolSliceDim2, + }, + { + src: pgtype.BoolArray{ + Elements: []pgtype.Bool{ + {Bool: true, Status: pgtype.Present}, + {Bool: false, Status: pgtype.Present}, + {Bool: true, Status: pgtype.Present}, + {Bool: false, Status: pgtype.Present}, + {Bool: true, Status: pgtype.Present}, + {Bool: false, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + expected: [][][][]bool{{{{true, false, true}}}, {{{false, true, false}}}}, + dst: &boolSliceDim4, + }, + { + src: pgtype.BoolArray{ + Elements: []pgtype.Bool{{Bool: true, Status: pgtype.Present}, {Bool: false, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + expected: [2][1]bool{{true}, {false}}, + dst: &boolArrayDim2, + }, + { + src: pgtype.BoolArray{ + Elements: []pgtype.Bool{ + {Bool: true, Status: pgtype.Present}, + {Bool: false, Status: pgtype.Present}, + {Bool: true, Status: pgtype.Present}, + {Bool: false, Status: pgtype.Present}, + {Bool: true, Status: pgtype.Present}, + {Bool: false, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + expected: [2][1][1][3]bool{{{{true, false, true}}}, {{{false, true, false}}}}, + dst: &boolArrayDim4, + }, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.BoolArray + dst interface{} + }{ + { + src: pgtype.BoolArray{ + Elements: []pgtype.Bool{{Status: pgtype.Null}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &boolSlice, + }, + { + src: pgtype.BoolArray{ + Elements: []pgtype.Bool{{Bool: true, Status: pgtype.Present}, {Bool: false, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &boolArrayDim2, + }, + { + src: pgtype.BoolArray{ + Elements: []pgtype.Bool{{Bool: true, Status: pgtype.Present}, {Bool: false, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &boolSlice, + }, + { + src: pgtype.BoolArray{ + Elements: []pgtype.Bool{{Bool: true, Status: pgtype.Present}, {Bool: false, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &boolArrayDim4, + }, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } + +} diff --git a/bool_test.go b/bool_test.go new file mode 100644 index 00000000..8e7a5220 --- /dev/null +++ b/bool_test.go @@ -0,0 +1,140 @@ +package pgtype_test + +import ( + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestBoolTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "bool", []interface{}{ + &pgtype.Bool{Bool: false, Status: pgtype.Present}, + &pgtype.Bool{Bool: true, Status: pgtype.Present}, + &pgtype.Bool{Bool: false, Status: pgtype.Null}, + }) +} + +func TestBoolSet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.Bool + }{ + {source: true, result: pgtype.Bool{Bool: true, Status: pgtype.Present}}, + {source: false, result: pgtype.Bool{Bool: false, Status: pgtype.Present}}, + {source: "true", result: pgtype.Bool{Bool: true, Status: pgtype.Present}}, + {source: "false", result: pgtype.Bool{Bool: false, Status: pgtype.Present}}, + {source: "t", result: pgtype.Bool{Bool: true, Status: pgtype.Present}}, + {source: "f", result: pgtype.Bool{Bool: false, Status: pgtype.Present}}, + {source: _bool(true), result: pgtype.Bool{Bool: true, Status: pgtype.Present}}, + {source: _bool(false), result: pgtype.Bool{Bool: false, Status: pgtype.Present}}, + {source: nil, result: pgtype.Bool{Status: pgtype.Null}}, + } + + for i, tt := range successfulTests { + var r pgtype.Bool + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if r != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestBoolAssignTo(t *testing.T) { + var b bool + var _b _bool + var pb *bool + var _pb *_bool + + simpleTests := []struct { + src pgtype.Bool + dst interface{} + expected interface{} + }{ + {src: pgtype.Bool{Bool: false, Status: pgtype.Present}, dst: &b, expected: false}, + {src: pgtype.Bool{Bool: true, Status: pgtype.Present}, dst: &b, expected: true}, + {src: pgtype.Bool{Bool: false, Status: pgtype.Present}, dst: &_b, expected: _bool(false)}, + {src: pgtype.Bool{Bool: true, Status: pgtype.Present}, dst: &_b, expected: _bool(true)}, + {src: pgtype.Bool{Bool: false, Status: pgtype.Null}, dst: &pb, expected: ((*bool)(nil))}, + {src: pgtype.Bool{Bool: false, Status: pgtype.Null}, dst: &_pb, expected: ((*_bool)(nil))}, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + pointerAllocTests := []struct { + src pgtype.Bool + dst interface{} + expected interface{} + }{ + {src: pgtype.Bool{Bool: true, Status: pgtype.Present}, dst: &pb, expected: true}, + {src: pgtype.Bool{Bool: true, Status: pgtype.Present}, dst: &_pb, expected: _bool(true)}, + } + + for i, tt := range pointerAllocTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } +} + +func TestBoolMarshalJSON(t *testing.T) { + successfulTests := []struct { + source pgtype.Bool + result string + }{ + {source: pgtype.Bool{Status: pgtype.Null}, result: "null"}, + {source: pgtype.Bool{Bool: true, Status: pgtype.Present}, result: "true"}, + {source: pgtype.Bool{Bool: false, Status: pgtype.Present}, result: "false"}, + } + for i, tt := range successfulTests { + r, err := tt.source.MarshalJSON() + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if string(r) != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, string(r)) + } + } +} + +func TestBoolUnmarshalJSON(t *testing.T) { + successfulTests := []struct { + source string + result pgtype.Bool + }{ + {source: "null", result: pgtype.Bool{Status: pgtype.Null}}, + {source: "true", result: pgtype.Bool{Bool: true, Status: pgtype.Present}}, + {source: "false", result: pgtype.Bool{Bool: false, Status: pgtype.Present}}, + } + for i, tt := range successfulTests { + var r pgtype.Bool + err := r.UnmarshalJSON([]byte(tt.source)) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if r != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} diff --git a/box_test.go b/box_test.go new file mode 100644 index 00000000..643c74ec --- /dev/null +++ b/box_test.go @@ -0,0 +1,34 @@ +package pgtype_test + +import ( + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestBoxTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "box", []interface{}{ + &pgtype.Box{ + P: [2]pgtype.Vec2{{7.1, 5.2345678}, {3.14, 1.678}}, + Status: pgtype.Present, + }, + &pgtype.Box{ + P: [2]pgtype.Vec2{{7.1, 1.678}, {-13.14, -5.234}}, + Status: pgtype.Present, + }, + &pgtype.Box{Status: pgtype.Null}, + }) +} + +func TestBoxNormalize(t *testing.T) { + testutil.TestSuccessfulNormalize(t, []testutil.NormalizeTest{ + { + SQL: "select '3.14, 1.678, 7.1, 5.234'::box", + Value: &pgtype.Box{ + P: [2]pgtype.Vec2{{7.1, 5.234}, {3.14, 1.678}}, + Status: pgtype.Present, + }, + }, + }) +} diff --git a/bpchar_array_test.go b/bpchar_array_test.go new file mode 100644 index 00000000..af6bf09a --- /dev/null +++ b/bpchar_array_test.go @@ -0,0 +1,55 @@ +package pgtype_test + +import ( + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestBPCharArrayTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "char(8)[]", []interface{}{ + &pgtype.BPCharArray{ + Elements: nil, + Dimensions: nil, + Status: pgtype.Present, + }, + &pgtype.BPCharArray{ + Elements: []pgtype.BPChar{ + pgtype.BPChar{String: "foo ", Status: pgtype.Present}, + pgtype.BPChar{Status: pgtype.Null}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.BPCharArray{Status: pgtype.Null}, + &pgtype.BPCharArray{ + Elements: []pgtype.BPChar{ + pgtype.BPChar{String: "bar ", Status: pgtype.Present}, + pgtype.BPChar{String: "NuLL ", Status: pgtype.Present}, + pgtype.BPChar{String: `wow"quz\`, Status: pgtype.Present}, + pgtype.BPChar{String: "1 ", Status: pgtype.Present}, + pgtype.BPChar{String: "1 ", Status: pgtype.Present}, + pgtype.BPChar{String: "null ", Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{ + {Length: 3, LowerBound: 1}, + {Length: 2, LowerBound: 1}, + }, + Status: pgtype.Present, + }, + &pgtype.BPCharArray{ + Elements: []pgtype.BPChar{ + pgtype.BPChar{String: " bar ", Status: pgtype.Present}, + pgtype.BPChar{String: " baz ", Status: pgtype.Present}, + pgtype.BPChar{String: " quz ", Status: pgtype.Present}, + pgtype.BPChar{String: "foo ", Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{ + {Length: 2, LowerBound: 4}, + {Length: 2, LowerBound: 2}, + }, + Status: pgtype.Present, + }, + }) +} diff --git a/bpchar_test.go b/bpchar_test.go new file mode 100644 index 00000000..7b8c1da3 --- /dev/null +++ b/bpchar_test.go @@ -0,0 +1,51 @@ +package pgtype_test + +import ( + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestChar3Transcode(t *testing.T) { + testutil.TestSuccessfulTranscodeEqFunc(t, "char(3)", []interface{}{ + &pgtype.BPChar{String: "a ", Status: pgtype.Present}, + &pgtype.BPChar{String: " a ", Status: pgtype.Present}, + &pgtype.BPChar{String: "嗨 ", Status: pgtype.Present}, + &pgtype.BPChar{String: " ", Status: pgtype.Present}, + &pgtype.BPChar{Status: pgtype.Null}, + }, func(aa, bb interface{}) bool { + a := aa.(pgtype.BPChar) + b := bb.(pgtype.BPChar) + + return a.Status == b.Status && a.String == b.String + }) +} + +func TestBPCharAssignTo(t *testing.T) { + var ( + str string + run rune + ) + simpleTests := []struct { + src pgtype.BPChar + dst interface{} + expected interface{} + }{ + {src: pgtype.BPChar{String: "simple", Status: pgtype.Present}, dst: &str, expected: "simple"}, + {src: pgtype.BPChar{String: "嗨", Status: pgtype.Present}, dst: &run, expected: '嗨'}, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + +} diff --git a/bytea_array_test.go b/bytea_array_test.go new file mode 100644 index 00000000..27c0382e --- /dev/null +++ b/bytea_array_test.go @@ -0,0 +1,229 @@ +package pgtype_test + +import ( + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestByteaArrayTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "bytea[]", []interface{}{ + &pgtype.ByteaArray{ + Elements: nil, + Dimensions: nil, + Status: pgtype.Present, + }, + &pgtype.ByteaArray{ + Elements: []pgtype.Bytea{ + {Bytes: []byte{1, 2, 3}, Status: pgtype.Present}, + {Status: pgtype.Null}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.ByteaArray{Status: pgtype.Null}, + &pgtype.ByteaArray{ + Elements: []pgtype.Bytea{ + {Bytes: []byte{1, 2, 3}, Status: pgtype.Present}, + {Bytes: []byte{1, 2, 3}, Status: pgtype.Present}, + {Bytes: []byte{}, Status: pgtype.Present}, + {Bytes: []byte{1, 2, 3}, Status: pgtype.Present}, + {Status: pgtype.Null}, + {Bytes: []byte{1}, Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}, {Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.ByteaArray{ + Elements: []pgtype.Bytea{ + {Bytes: []byte{1, 2, 3}, Status: pgtype.Present}, + {Bytes: []byte{}, Status: pgtype.Present}, + {Bytes: []byte{1, 2, 3}, Status: pgtype.Present}, + {Bytes: []byte{1}, Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{ + {Length: 2, LowerBound: 4}, + {Length: 2, LowerBound: 2}, + }, + Status: pgtype.Present, + }, + }) +} + +func TestByteaArraySet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.ByteaArray + }{ + { + source: [][]byte{{1, 2, 3}}, + result: pgtype.ByteaArray{ + Elements: []pgtype.Bytea{{Bytes: []byte{1, 2, 3}, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: (([][]byte)(nil)), + result: pgtype.ByteaArray{Status: pgtype.Null}, + }, + { + source: [][][]byte{{{1}}, {{2}}}, + result: pgtype.ByteaArray{ + Elements: []pgtype.Bytea{{Bytes: []byte{1}, Status: pgtype.Present}, {Bytes: []byte{2}, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [][][][][]byte{{{{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}}}, {{{{10, 11, 12}, {13, 14, 15}, {16, 17, 18}}}}}, + result: pgtype.ByteaArray{ + Elements: []pgtype.Bytea{ + {Bytes: []byte{1, 2, 3}, Status: pgtype.Present}, + {Bytes: []byte{4, 5, 6}, Status: pgtype.Present}, + {Bytes: []byte{7, 8, 9}, Status: pgtype.Present}, + {Bytes: []byte{10, 11, 12}, Status: pgtype.Present}, + {Bytes: []byte{13, 14, 15}, Status: pgtype.Present}, + {Bytes: []byte{16, 17, 18}, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + { + source: [2][1][]byte{{{1}}, {{2}}}, + result: pgtype.ByteaArray{ + Elements: []pgtype.Bytea{{Bytes: []byte{1}, Status: pgtype.Present}, {Bytes: []byte{2}, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [2][1][1][3][]byte{{{{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}}}, {{{{10, 11, 12}, {13, 14, 15}, {16, 17, 18}}}}}, + result: pgtype.ByteaArray{ + Elements: []pgtype.Bytea{ + {Bytes: []byte{1, 2, 3}, Status: pgtype.Present}, + {Bytes: []byte{4, 5, 6}, Status: pgtype.Present}, + {Bytes: []byte{7, 8, 9}, Status: pgtype.Present}, + {Bytes: []byte{10, 11, 12}, Status: pgtype.Present}, + {Bytes: []byte{13, 14, 15}, Status: pgtype.Present}, + {Bytes: []byte{16, 17, 18}, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + } + + for i, tt := range successfulTests { + var r pgtype.ByteaArray + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !reflect.DeepEqual(r, tt.result) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestByteaArrayAssignTo(t *testing.T) { + var byteByteSlice [][]byte + var byteByteSliceDim2 [][][]byte + var byteByteSliceDim4 [][][][][]byte + var byteByteArraySliceDim2 [2][1][]byte + var byteByteArraySliceDim4 [2][1][1][3][]byte + + simpleTests := []struct { + src pgtype.ByteaArray + dst interface{} + expected interface{} + }{ + { + src: pgtype.ByteaArray{ + Elements: []pgtype.Bytea{{Bytes: []byte{1, 2, 3}, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &byteByteSlice, + expected: [][]byte{{1, 2, 3}}, + }, + { + src: pgtype.ByteaArray{Status: pgtype.Null}, + dst: &byteByteSlice, + expected: (([][]byte)(nil)), + }, + { + src: pgtype.ByteaArray{Status: pgtype.Present}, + dst: &byteByteSlice, + expected: [][]byte{}, + }, + { + src: pgtype.ByteaArray{ + Elements: []pgtype.Bytea{{Bytes: []byte{1}, Status: pgtype.Present}, {Bytes: []byte{2}, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &byteByteSliceDim2, + expected: [][][]byte{{{1}}, {{2}}}, + }, + { + src: pgtype.ByteaArray{ + Elements: []pgtype.Bytea{ + {Bytes: []byte{1, 2, 3}, Status: pgtype.Present}, + {Bytes: []byte{4, 5, 6}, Status: pgtype.Present}, + {Bytes: []byte{7, 8, 9}, Status: pgtype.Present}, + {Bytes: []byte{10, 11, 12}, Status: pgtype.Present}, + {Bytes: []byte{13, 14, 15}, Status: pgtype.Present}, + {Bytes: []byte{16, 17, 18}, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + dst: &byteByteSliceDim4, + expected: [][][][][]byte{{{{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}}}, {{{{10, 11, 12}, {13, 14, 15}, {16, 17, 18}}}}}, + }, + { + src: pgtype.ByteaArray{ + Elements: []pgtype.Bytea{{Bytes: []byte{1}, Status: pgtype.Present}, {Bytes: []byte{2}, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &byteByteArraySliceDim2, + expected: [2][1][]byte{{{1}}, {{2}}}, + }, + { + src: pgtype.ByteaArray{ + Elements: []pgtype.Bytea{ + {Bytes: []byte{1, 2, 3}, Status: pgtype.Present}, + {Bytes: []byte{4, 5, 6}, Status: pgtype.Present}, + {Bytes: []byte{7, 8, 9}, Status: pgtype.Present}, + {Bytes: []byte{10, 11, 12}, Status: pgtype.Present}, + {Bytes: []byte{13, 14, 15}, Status: pgtype.Present}, + {Bytes: []byte{16, 17, 18}, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + dst: &byteByteArraySliceDim4, + expected: [2][1][1][3][]byte{{{{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}}}, {{{{10, 11, 12}, {13, 14, 15}, {16, 17, 18}}}}}, + }, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } +} diff --git a/bytea_test.go b/bytea_test.go new file mode 100644 index 00000000..c8c49ff7 --- /dev/null +++ b/bytea_test.go @@ -0,0 +1,73 @@ +package pgtype_test + +import ( + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestByteaTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "bytea", []interface{}{ + &pgtype.Bytea{Bytes: []byte{1, 2, 3}, Status: pgtype.Present}, + &pgtype.Bytea{Bytes: []byte{}, Status: pgtype.Present}, + &pgtype.Bytea{Bytes: nil, Status: pgtype.Null}, + }) +} + +func TestByteaSet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.Bytea + }{ + {source: []byte{1, 2, 3}, result: pgtype.Bytea{Bytes: []byte{1, 2, 3}, Status: pgtype.Present}}, + {source: []byte{}, result: pgtype.Bytea{Bytes: []byte{}, Status: pgtype.Present}}, + {source: []byte(nil), result: pgtype.Bytea{Status: pgtype.Null}}, + {source: _byteSlice{1, 2, 3}, result: pgtype.Bytea{Bytes: []byte{1, 2, 3}, Status: pgtype.Present}}, + {source: _byteSlice(nil), result: pgtype.Bytea{Status: pgtype.Null}}, + } + + for i, tt := range successfulTests { + var r pgtype.Bytea + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !reflect.DeepEqual(r, tt.result) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestByteaAssignTo(t *testing.T) { + var buf []byte + var _buf _byteSlice + var pbuf *[]byte + var _pbuf *_byteSlice + + simpleTests := []struct { + src pgtype.Bytea + dst interface{} + expected interface{} + }{ + {src: pgtype.Bytea{Bytes: []byte{1, 2, 3}, Status: pgtype.Present}, dst: &buf, expected: []byte{1, 2, 3}}, + {src: pgtype.Bytea{Bytes: []byte{1, 2, 3}, Status: pgtype.Present}, dst: &_buf, expected: _byteSlice{1, 2, 3}}, + {src: pgtype.Bytea{Bytes: []byte{1, 2, 3}, Status: pgtype.Present}, dst: &pbuf, expected: &[]byte{1, 2, 3}}, + {src: pgtype.Bytea{Bytes: []byte{1, 2, 3}, Status: pgtype.Present}, dst: &_pbuf, expected: &_byteSlice{1, 2, 3}}, + {src: pgtype.Bytea{Status: pgtype.Null}, dst: &pbuf, expected: ((*[]byte)(nil))}, + {src: pgtype.Bytea{Status: pgtype.Null}, dst: &_pbuf, expected: ((*_byteSlice)(nil))}, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } +} diff --git a/cid_test.go b/cid_test.go new file mode 100644 index 00000000..50e50cd8 --- /dev/null +++ b/cid_test.go @@ -0,0 +1,105 @@ +package pgtype_test + +import ( + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestCIDTranscode(t *testing.T) { + pgTypeName := "cid" + values := []interface{}{ + &pgtype.CID{Uint: 42, Status: pgtype.Present}, + &pgtype.CID{Status: pgtype.Null}, + } + eqFunc := func(a, b interface{}) bool { + return reflect.DeepEqual(a, b) + } + + testutil.TestPgxSuccessfulTranscodeEqFunc(t, pgTypeName, values, eqFunc) + + for _, driverName := range []string{"github.com/lib/pq", "github.com/jackc/pgx/stdlib"} { + testutil.TestDatabaseSQLSuccessfulTranscodeEqFunc(t, driverName, pgTypeName, values, eqFunc) + } +} + +func TestCIDSet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.CID + }{ + {source: uint32(1), result: pgtype.CID{Uint: 1, Status: pgtype.Present}}, + } + + for i, tt := range successfulTests { + var r pgtype.CID + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if r != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestCIDAssignTo(t *testing.T) { + var ui32 uint32 + var pui32 *uint32 + + simpleTests := []struct { + src pgtype.CID + dst interface{} + expected interface{} + }{ + {src: pgtype.CID{Uint: 42, Status: pgtype.Present}, dst: &ui32, expected: uint32(42)}, + {src: pgtype.CID{Status: pgtype.Null}, dst: &pui32, expected: ((*uint32)(nil))}, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + pointerAllocTests := []struct { + src pgtype.CID + dst interface{} + expected interface{} + }{ + {src: pgtype.CID{Uint: 42, Status: pgtype.Present}, dst: &pui32, expected: uint32(42)}, + } + + for i, tt := range pointerAllocTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.CID + dst interface{} + }{ + {src: pgtype.CID{Status: pgtype.Null}, dst: &ui32}, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } +} diff --git a/cidr_array_test.go b/cidr_array_test.go new file mode 100644 index 00000000..74c063fa --- /dev/null +++ b/cidr_array_test.go @@ -0,0 +1,319 @@ +package pgtype_test + +import ( + "net" + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestCIDRArrayTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "cidr[]", []interface{}{ + &pgtype.CIDRArray{ + Elements: nil, + Dimensions: nil, + Status: pgtype.Present, + }, + &pgtype.CIDRArray{ + Elements: []pgtype.CIDR{ + {IPNet: mustParseCIDR(t, "12.34.56.0/32"), Status: pgtype.Present}, + {Status: pgtype.Null}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.CIDRArray{Status: pgtype.Null}, + &pgtype.CIDRArray{ + Elements: []pgtype.CIDR{ + {IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "12.34.56.0/32"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "192.168.0.1/32"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "2607:f8b0:4009:80b::200e/128"), Status: pgtype.Present}, + {Status: pgtype.Null}, + {IPNet: mustParseCIDR(t, "255.0.0.0/8"), Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}, {Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.CIDRArray{ + Elements: []pgtype.CIDR{ + {IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "12.34.56.0/32"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "192.168.0.1/32"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "2607:f8b0:4009:80b::200e/128"), Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{ + {Length: 2, LowerBound: 4}, + {Length: 2, LowerBound: 2}, + }, + Status: pgtype.Present, + }, + }) +} + +func TestCIDRArraySet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.CIDRArray + }{ + { + source: []*net.IPNet{mustParseCIDR(t, "127.0.0.1/32")}, + result: pgtype.CIDRArray{ + Elements: []pgtype.CIDR{{IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: (([]*net.IPNet)(nil)), + result: pgtype.CIDRArray{Status: pgtype.Null}, + }, + { + source: []net.IP{mustParseCIDR(t, "127.0.0.1/32").IP}, + result: pgtype.CIDRArray{ + Elements: []pgtype.CIDR{{IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: (([]net.IP)(nil)), + result: pgtype.CIDRArray{Status: pgtype.Null}, + }, + { + source: [][]net.IP{{mustParseCIDR(t, "127.0.0.1/32").IP}, {mustParseCIDR(t, "10.0.0.1/32").IP}}, + result: pgtype.CIDRArray{ + Elements: []pgtype.CIDR{ + {IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "10.0.0.1/32"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [][][][]*net.IPNet{ + {{{ + mustParseCIDR(t, "127.0.0.1/24"), + mustParseCIDR(t, "10.0.0.1/24"), + mustParseCIDR(t, "172.16.0.1/16")}}}, + {{{ + mustParseCIDR(t, "192.168.0.1/16"), + mustParseCIDR(t, "224.0.0.1/24"), + mustParseCIDR(t, "169.168.0.1/16")}}}}, + result: pgtype.CIDRArray{ + Elements: []pgtype.CIDR{ + {IPNet: mustParseCIDR(t, "127.0.0.1/24"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "10.0.0.1/24"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "172.16.0.1/16"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "192.168.0.1/16"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "224.0.0.1/24"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "169.168.0.1/16"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + { + source: [2][1]net.IP{{mustParseCIDR(t, "127.0.0.1/32").IP}, {mustParseCIDR(t, "10.0.0.1/32").IP}}, + result: pgtype.CIDRArray{ + Elements: []pgtype.CIDR{ + {IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "10.0.0.1/32"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [2][1][1][3]*net.IPNet{ + {{{ + mustParseCIDR(t, "127.0.0.1/24"), + mustParseCIDR(t, "10.0.0.1/24"), + mustParseCIDR(t, "172.16.0.1/16")}}}, + {{{ + mustParseCIDR(t, "192.168.0.1/16"), + mustParseCIDR(t, "224.0.0.1/24"), + mustParseCIDR(t, "169.168.0.1/16")}}}}, + result: pgtype.CIDRArray{ + Elements: []pgtype.CIDR{ + {IPNet: mustParseCIDR(t, "127.0.0.1/24"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "10.0.0.1/24"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "172.16.0.1/16"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "192.168.0.1/16"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "224.0.0.1/24"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "169.168.0.1/16"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + } + + for i, tt := range successfulTests { + var r pgtype.CIDRArray + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !reflect.DeepEqual(r, tt.result) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestCIDRArrayAssignTo(t *testing.T) { + var ipnetSlice []*net.IPNet + var ipSlice []net.IP + var ipSliceDim2 [][]net.IP + var ipnetSliceDim4 [][][][]*net.IPNet + var ipArrayDim2 [2][1]net.IP + var ipnetArrayDim4 [2][1][1][3]*net.IPNet + + simpleTests := []struct { + src pgtype.CIDRArray + dst interface{} + expected interface{} + }{ + { + src: pgtype.CIDRArray{ + Elements: []pgtype.CIDR{{IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &ipnetSlice, + expected: []*net.IPNet{mustParseCIDR(t, "127.0.0.1/32")}, + }, + { + src: pgtype.CIDRArray{ + Elements: []pgtype.CIDR{{Status: pgtype.Null}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &ipnetSlice, + expected: []*net.IPNet{nil}, + }, + { + src: pgtype.CIDRArray{ + Elements: []pgtype.CIDR{{IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &ipSlice, + expected: []net.IP{mustParseCIDR(t, "127.0.0.1/32").IP}, + }, + { + src: pgtype.CIDRArray{ + Elements: []pgtype.CIDR{{Status: pgtype.Null}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &ipSlice, + expected: []net.IP{nil}, + }, + { + src: pgtype.CIDRArray{Status: pgtype.Null}, + dst: &ipnetSlice, + expected: (([]*net.IPNet)(nil)), + }, + { + src: pgtype.CIDRArray{Status: pgtype.Present}, + dst: &ipnetSlice, + expected: []*net.IPNet{}, + }, + { + src: pgtype.CIDRArray{Status: pgtype.Null}, + dst: &ipSlice, + expected: (([]net.IP)(nil)), + }, + { + src: pgtype.CIDRArray{Status: pgtype.Present}, + dst: &ipSlice, + expected: []net.IP{}, + }, + { + src: pgtype.CIDRArray{ + Elements: []pgtype.CIDR{ + {IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "10.0.0.1/32"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &ipSliceDim2, + expected: [][]net.IP{{mustParseCIDR(t, "127.0.0.1/32").IP}, {mustParseCIDR(t, "10.0.0.1/32").IP}}, + }, + { + src: pgtype.CIDRArray{ + Elements: []pgtype.CIDR{ + {IPNet: mustParseCIDR(t, "127.0.0.1/24"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "10.0.0.1/24"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "172.16.0.1/16"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "192.168.0.1/16"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "224.0.0.1/24"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "169.168.0.1/16"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + dst: &ipnetSliceDim4, + expected: [][][][]*net.IPNet{ + {{{ + mustParseCIDR(t, "127.0.0.1/24"), + mustParseCIDR(t, "10.0.0.1/24"), + mustParseCIDR(t, "172.16.0.1/16")}}}, + {{{ + mustParseCIDR(t, "192.168.0.1/16"), + mustParseCIDR(t, "224.0.0.1/24"), + mustParseCIDR(t, "169.168.0.1/16")}}}}, + }, + { + src: pgtype.CIDRArray{ + Elements: []pgtype.CIDR{ + {IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "10.0.0.1/32"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &ipArrayDim2, + expected: [2][1]net.IP{{mustParseCIDR(t, "127.0.0.1/32").IP}, {mustParseCIDR(t, "10.0.0.1/32").IP}}, + }, + { + src: pgtype.CIDRArray{ + Elements: []pgtype.CIDR{ + {IPNet: mustParseCIDR(t, "127.0.0.1/24"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "10.0.0.1/24"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "172.16.0.1/16"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "192.168.0.1/16"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "224.0.0.1/24"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "169.168.0.1/16"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + dst: &ipnetArrayDim4, + expected: [2][1][1][3]*net.IPNet{ + {{{ + mustParseCIDR(t, "127.0.0.1/24"), + mustParseCIDR(t, "10.0.0.1/24"), + mustParseCIDR(t, "172.16.0.1/16")}}}, + {{{ + mustParseCIDR(t, "192.168.0.1/16"), + mustParseCIDR(t, "224.0.0.1/24"), + mustParseCIDR(t, "169.168.0.1/16")}}}}, + }, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } +} diff --git a/circle_test.go b/circle_test.go new file mode 100644 index 00000000..ba4f408b --- /dev/null +++ b/circle_test.go @@ -0,0 +1,16 @@ +package pgtype_test + +import ( + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestCircleTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "circle", []interface{}{ + &pgtype.Circle{P: pgtype.Vec2{1.234, 5.67890123}, R: 3.5, Status: pgtype.Present}, + &pgtype.Circle{P: pgtype.Vec2{-1.234, -5.6789}, R: 12.9, Status: pgtype.Present}, + &pgtype.Circle{Status: pgtype.Null}, + }) +} diff --git a/composite_bench_test.go b/composite_bench_test.go new file mode 100644 index 00000000..7aef8c4f --- /dev/null +++ b/composite_bench_test.go @@ -0,0 +1,192 @@ +package pgtype_test + +import ( + "testing" + + "github.com/jackc/pgio" + "github.com/jackc/pgtype" + "github.com/stretchr/testify/require" +) + +type MyCompositeRaw struct { + A int32 + B *string +} + +func (src MyCompositeRaw) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) { + buf = pgio.AppendUint32(buf, 2) + + buf = pgio.AppendUint32(buf, pgtype.Int4OID) + buf = pgio.AppendInt32(buf, 4) + buf = pgio.AppendInt32(buf, src.A) + + buf = pgio.AppendUint32(buf, pgtype.TextOID) + if src.B != nil { + buf = pgio.AppendInt32(buf, int32(len(*src.B))) + buf = append(buf, (*src.B)...) + } else { + buf = pgio.AppendInt32(buf, -1) + } + + return buf, nil +} + +func (dst *MyCompositeRaw) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error { + a := pgtype.Int4{} + b := pgtype.Text{} + + scanner := pgtype.NewCompositeBinaryScanner(ci, src) + scanner.ScanDecoder(&a) + scanner.ScanDecoder(&b) + + if scanner.Err() != nil { + return scanner.Err() + } + + dst.A = a.Int + if b.Status == pgtype.Present { + dst.B = &b.String + } else { + dst.B = nil + } + + return nil +} + +var x []byte + +func BenchmarkBinaryEncodingManual(b *testing.B) { + buf := make([]byte, 0, 128) + ci := pgtype.NewConnInfo() + v := MyCompositeRaw{4, ptrS("ABCDEFG")} + + b.ResetTimer() + for n := 0; n < b.N; n++ { + buf, _ = v.EncodeBinary(ci, buf[:0]) + } + x = buf +} + +func BenchmarkBinaryEncodingHelper(b *testing.B) { + buf := make([]byte, 0, 128) + ci := pgtype.NewConnInfo() + v := MyType{4, ptrS("ABCDEFG")} + + b.ResetTimer() + for n := 0; n < b.N; n++ { + buf, _ = v.EncodeBinary(ci, buf[:0]) + } + x = buf +} + +func BenchmarkBinaryEncodingComposite(b *testing.B) { + buf := make([]byte, 0, 128) + ci := pgtype.NewConnInfo() + f1 := 2 + f2 := ptrS("bar") + c, err := pgtype.NewCompositeType("test", []pgtype.CompositeTypeField{ + {"a", pgtype.Int4OID}, + {"b", pgtype.TextOID}, + }, ci) + require.NoError(b, err) + + b.ResetTimer() + for n := 0; n < b.N; n++ { + c.Set([]interface{}{f1, f2}) + buf, _ = c.EncodeBinary(ci, buf[:0]) + } + x = buf +} + +func BenchmarkBinaryEncodingJSON(b *testing.B) { + buf := make([]byte, 0, 128) + ci := pgtype.NewConnInfo() + v := MyCompositeRaw{4, ptrS("ABCDEFG")} + j := pgtype.JSON{} + + b.ResetTimer() + for n := 0; n < b.N; n++ { + j.Set(v) + buf, _ = j.EncodeBinary(ci, buf[:0]) + } + x = buf +} + +var dstRaw MyCompositeRaw + +func BenchmarkBinaryDecodingManual(b *testing.B) { + ci := pgtype.NewConnInfo() + buf, _ := MyType{4, ptrS("ABCDEFG")}.EncodeBinary(ci, nil) + dst := MyCompositeRaw{} + + b.ResetTimer() + for n := 0; n < b.N; n++ { + err := dst.DecodeBinary(ci, buf) + E(err) + } + dstRaw = dst +} + +var dstMyType MyType + +func BenchmarkBinaryDecodingHelpers(b *testing.B) { + ci := pgtype.NewConnInfo() + buf, _ := MyType{4, ptrS("ABCDEFG")}.EncodeBinary(ci, nil) + dst := MyType{} + + b.ResetTimer() + for n := 0; n < b.N; n++ { + err := dst.DecodeBinary(ci, buf) + E(err) + } + dstMyType = dst +} + +var gf1 int +var gf2 *string + +func BenchmarkBinaryDecodingCompositeScan(b *testing.B) { + ci := pgtype.NewConnInfo() + buf, _ := MyType{4, ptrS("ABCDEFG")}.EncodeBinary(ci, nil) + var f1 int + var f2 *string + + c, err := pgtype.NewCompositeType("test", []pgtype.CompositeTypeField{ + {"a", pgtype.Int4OID}, + {"b", pgtype.TextOID}, + }, ci) + require.NoError(b, err) + + b.ResetTimer() + for n := 0; n < b.N; n++ { + err := c.DecodeBinary(ci, buf) + if err != nil { + b.Fatal(err) + } + err = c.AssignTo([]interface{}{&f1, &f2}) + if err != nil { + b.Fatal(err) + } + } + gf1 = f1 + gf2 = f2 +} + +func BenchmarkBinaryDecodingJSON(b *testing.B) { + ci := pgtype.NewConnInfo() + j := pgtype.JSON{} + j.Set(MyCompositeRaw{4, ptrS("ABCDEFG")}) + buf, _ := j.EncodeBinary(ci, nil) + + j = pgtype.JSON{} + dst := MyCompositeRaw{} + + b.ResetTimer() + for n := 0; n < b.N; n++ { + err := j.DecodeBinary(ci, buf) + E(err) + err = j.AssignTo(&dst) + E(err) + } + dstRaw = dst +} diff --git a/composite_fields_test.go b/composite_fields_test.go new file mode 100644 index 00000000..dc4d4c29 --- /dev/null +++ b/composite_fields_test.go @@ -0,0 +1,273 @@ +package pgtype_test + +import ( + "context" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" + "github.com/jackc/pgx/v4" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestCompositeFieldsDecode(t *testing.T) { + conn := testutil.MustConnectPgx(t) + defer testutil.MustCloseContext(t, conn) + + formats := []int16{pgx.TextFormatCode, pgx.BinaryFormatCode} + + // Assorted values + { + var a int32 + var b string + var c float64 + + for _, format := range formats { + err := conn.QueryRow(context.Background(), "select row(1,'hi',2.1)", pgx.QueryResultFormats{format}).Scan( + pgtype.CompositeFields{&a, &b, &c}, + ) + if !assert.NoErrorf(t, err, "Format: %v", format) { + continue + } + + assert.EqualValuesf(t, 1, a, "Format: %v", format) + assert.EqualValuesf(t, "hi", b, "Format: %v", format) + assert.EqualValuesf(t, 2.1, c, "Format: %v", format) + } + } + + // nulls, string "null", and empty string fields + { + var a pgtype.Text + var b string + var c pgtype.Text + var d string + var e pgtype.Text + + for _, format := range formats { + err := conn.QueryRow(context.Background(), "select row(null,'null',null,'',null)", pgx.QueryResultFormats{format}).Scan( + pgtype.CompositeFields{&a, &b, &c, &d, &e}, + ) + if !assert.NoErrorf(t, err, "Format: %v", format) { + continue + } + + assert.Nilf(t, a.Get(), "Format: %v", format) + assert.EqualValuesf(t, "null", b, "Format: %v", format) + assert.Nilf(t, c.Get(), "Format: %v", format) + assert.EqualValuesf(t, "", d, "Format: %v", format) + assert.Nilf(t, e.Get(), "Format: %v", format) + } + } + + // null record + { + var a pgtype.Text + var b string + cf := pgtype.CompositeFields{&a, &b} + + for _, format := range formats { + // Cannot scan nil into + err := conn.QueryRow(context.Background(), "select null::record", pgx.QueryResultFormats{format}).Scan( + cf, + ) + if assert.Errorf(t, err, "Format: %v", format) { + continue + } + assert.NotNilf(t, cf, "Format: %v", format) + + // But can scan nil into *pgtype.CompositeFields + err = conn.QueryRow(context.Background(), "select null::record", pgx.QueryResultFormats{format}).Scan( + &cf, + ) + if assert.Errorf(t, err, "Format: %v", format) { + continue + } + assert.Nilf(t, cf, "Format: %v", format) + } + } + + // quotes and special characters + { + var a, b, c, d string + + for _, format := range formats { + err := conn.QueryRow(context.Background(), `select row('"', 'foo bar', 'foo''bar', 'baz)bar')`, pgx.QueryResultFormats{format}).Scan( + pgtype.CompositeFields{&a, &b, &c, &d}, + ) + if !assert.NoErrorf(t, err, "Format: %v", format) { + continue + } + + assert.Equalf(t, `"`, a, "Format: %v", format) + assert.Equalf(t, `foo bar`, b, "Format: %v", format) + assert.Equalf(t, `foo'bar`, c, "Format: %v", format) + assert.Equalf(t, `baz)bar`, d, "Format: %v", format) + } + } + + // arrays + { + var a []string + var b []int64 + + for _, format := range formats { + err := conn.QueryRow(context.Background(), `select row(array['foo', 'bar', 'baz'], array[1,2,3])`, pgx.QueryResultFormats{format}).Scan( + pgtype.CompositeFields{&a, &b}, + ) + if !assert.NoErrorf(t, err, "Format: %v", format) { + continue + } + + assert.EqualValuesf(t, []string{"foo", "bar", "baz"}, a, "Format: %v", format) + assert.EqualValuesf(t, []int64{1, 2, 3}, b, "Format: %v", format) + } + } + + // Skip nil fields + { + var a int32 + var c float64 + + for _, format := range formats { + err := conn.QueryRow(context.Background(), "select row(1,'hi',2.1)", pgx.QueryResultFormats{format}).Scan( + pgtype.CompositeFields{&a, nil, &c}, + ) + if !assert.NoErrorf(t, err, "Format: %v", format) { + continue + } + + assert.EqualValuesf(t, 1, a, "Format: %v", format) + assert.EqualValuesf(t, 2.1, c, "Format: %v", format) + } + } +} + +func TestCompositeFieldsEncode(t *testing.T) { + conn := testutil.MustConnectPgx(t) + defer testutil.MustCloseContext(t, conn) + + _, err := conn.Exec(context.Background(), `drop type if exists cf_encode; + +create type cf_encode as ( + a text, + b int4, + c text, + d float8, + e text +);`) + require.NoError(t, err) + defer conn.Exec(context.Background(), "drop type cf_encode") + + // Use simple protocol to force text or binary encoding + simpleProtocols := []bool{true, false} + + // Assorted values + { + var a string + var b int32 + var c string + var d float64 + var e string + + for _, simpleProtocol := range simpleProtocols { + err := conn.QueryRow(context.Background(), "select $1::cf_encode", pgx.QuerySimpleProtocol(simpleProtocol), + pgtype.CompositeFields{"hi", int32(1), "ok", float64(2.1), "bye"}, + ).Scan( + pgtype.CompositeFields{&a, &b, &c, &d, &e}, + ) + if assert.NoErrorf(t, err, "Simple Protocol: %v", simpleProtocol) { + assert.EqualValuesf(t, "hi", a, "Simple Protocol: %v", simpleProtocol) + assert.EqualValuesf(t, 1, b, "Simple Protocol: %v", simpleProtocol) + assert.EqualValuesf(t, "ok", c, "Simple Protocol: %v", simpleProtocol) + assert.EqualValuesf(t, 2.1, d, "Simple Protocol: %v", simpleProtocol) + assert.EqualValuesf(t, "bye", e, "Simple Protocol: %v", simpleProtocol) + } + } + } + + // untyped nil + { + var a pgtype.Text + var b int32 + var c string + var d pgtype.Float8 + var e pgtype.Text + + simpleProtocol := true + err := conn.QueryRow(context.Background(), "select $1::cf_encode", pgx.QuerySimpleProtocol(simpleProtocol), + pgtype.CompositeFields{nil, int32(1), "null", nil, nil}, + ).Scan( + pgtype.CompositeFields{&a, &b, &c, &d, &e}, + ) + if assert.NoErrorf(t, err, "Simple Protocol: %v", simpleProtocol) { + assert.Nilf(t, a.Get(), "Simple Protocol: %v", simpleProtocol) + assert.EqualValuesf(t, 1, b, "Simple Protocol: %v", simpleProtocol) + assert.EqualValuesf(t, "null", c, "Simple Protocol: %v", simpleProtocol) + assert.Nilf(t, d.Get(), "Simple Protocol: %v", simpleProtocol) + assert.Nilf(t, e.Get(), "Simple Protocol: %v", simpleProtocol) + } + + // untyped nil cannot be represented in binary format because CompositeFields does not know the PostgreSQL schema + // of the composite type. + simpleProtocol = false + err = conn.QueryRow(context.Background(), "select $1::cf_encode", pgx.QuerySimpleProtocol(simpleProtocol), + pgtype.CompositeFields{nil, int32(1), "null", nil, nil}, + ).Scan( + pgtype.CompositeFields{&a, &b, &c, &d, &e}, + ) + assert.Errorf(t, err, "Simple Protocol: %v", simpleProtocol) + } + + // nulls, string "null", and empty string fields + { + var a pgtype.Text + var b int32 + var c string + var d pgtype.Float8 + var e pgtype.Text + + for _, simpleProtocol := range simpleProtocols { + err := conn.QueryRow(context.Background(), "select $1::cf_encode", pgx.QuerySimpleProtocol(simpleProtocol), + pgtype.CompositeFields{&pgtype.Text{Status: pgtype.Null}, int32(1), "null", &pgtype.Float8{Status: pgtype.Null}, &pgtype.Text{Status: pgtype.Null}}, + ).Scan( + pgtype.CompositeFields{&a, &b, &c, &d, &e}, + ) + if assert.NoErrorf(t, err, "Simple Protocol: %v", simpleProtocol) { + assert.Nilf(t, a.Get(), "Simple Protocol: %v", simpleProtocol) + assert.EqualValuesf(t, 1, b, "Simple Protocol: %v", simpleProtocol) + assert.EqualValuesf(t, "null", c, "Simple Protocol: %v", simpleProtocol) + assert.Nilf(t, d.Get(), "Simple Protocol: %v", simpleProtocol) + assert.Nilf(t, e.Get(), "Simple Protocol: %v", simpleProtocol) + } + } + } + + // quotes and special characters + { + var a string + var b int32 + var c string + var d float64 + var e string + + for _, simpleProtocol := range simpleProtocols { + err := conn.QueryRow( + context.Background(), + `select $1::cf_encode`, + pgx.QuerySimpleProtocol(simpleProtocol), + pgtype.CompositeFields{`"`, int32(42), `foo'bar`, float64(1.2), `baz)bar`}, + ).Scan( + pgtype.CompositeFields{&a, &b, &c, &d, &e}, + ) + if assert.NoErrorf(t, err, "Simple Protocol: %v", simpleProtocol) { + assert.Equalf(t, `"`, a, "Simple Protocol: %v", simpleProtocol) + assert.Equalf(t, int32(42), b, "Simple Protocol: %v", simpleProtocol) + assert.Equalf(t, `foo'bar`, c, "Simple Protocol: %v", simpleProtocol) + assert.Equalf(t, float64(1.2), d, "Simple Protocol: %v", simpleProtocol) + assert.Equalf(t, `baz)bar`, e, "Simple Protocol: %v", simpleProtocol) + } + } + } +} diff --git a/composite_type_test.go b/composite_type_test.go new file mode 100644 index 00000000..2349a67d --- /dev/null +++ b/composite_type_test.go @@ -0,0 +1,320 @@ +package pgtype_test + +import ( + "context" + "fmt" + "os" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" + pgx "github.com/jackc/pgx/v4" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestCompositeTypeSetAndGet(t *testing.T) { + ci := pgtype.NewConnInfo() + ct, err := pgtype.NewCompositeType("test", []pgtype.CompositeTypeField{ + {"a", pgtype.TextOID}, + {"b", pgtype.Int4OID}, + }, ci) + require.NoError(t, err) + assert.Equal(t, pgtype.Undefined, ct.Get()) + + nilTests := []struct { + src interface{} + }{ + {nil}, // nil interface + {(*[]interface{})(nil)}, // typed nil + } + + for i, tt := range nilTests { + err := ct.Set(tt.src) + assert.NoErrorf(t, err, "%d", i) + assert.Equal(t, nil, ct.Get()) + } + + compatibleValuesTests := []struct { + src []interface{} + expected map[string]interface{} + }{ + { + src: []interface{}{"foo", int32(42)}, + expected: map[string]interface{}{"a": "foo", "b": int32(42)}, + }, + { + src: []interface{}{nil, nil}, + expected: map[string]interface{}{"a": nil, "b": nil}, + }, + { + src: []interface{}{&pgtype.Text{String: "hi", Status: pgtype.Present}, &pgtype.Int4{Int: 7, Status: pgtype.Present}}, + expected: map[string]interface{}{"a": "hi", "b": int32(7)}, + }, + } + + for i, tt := range compatibleValuesTests { + err := ct.Set(tt.src) + assert.NoErrorf(t, err, "%d", i) + assert.EqualValues(t, tt.expected, ct.Get()) + } +} + +func TestCompositeTypeAssignTo(t *testing.T) { + ci := pgtype.NewConnInfo() + ct, err := pgtype.NewCompositeType("test", []pgtype.CompositeTypeField{ + {"a", pgtype.TextOID}, + {"b", pgtype.Int4OID}, + }, ci) + require.NoError(t, err) + + { + err := ct.Set([]interface{}{"foo", int32(42)}) + assert.NoError(t, err) + + var a string + var b int32 + + err = ct.AssignTo([]interface{}{&a, &b}) + assert.NoError(t, err) + + assert.Equal(t, "foo", a) + assert.Equal(t, int32(42), b) + } + + { + err := ct.Set([]interface{}{"foo", int32(42)}) + assert.NoError(t, err) + + var a pgtype.Text + var b pgtype.Int4 + + err = ct.AssignTo([]interface{}{&a, &b}) + assert.NoError(t, err) + + assert.Equal(t, pgtype.Text{String: "foo", Status: pgtype.Present}, a) + assert.Equal(t, pgtype.Int4{Int: 42, Status: pgtype.Present}, b) + } + + // Allow nil destination component as no-op + { + err := ct.Set([]interface{}{"foo", int32(42)}) + assert.NoError(t, err) + + var b int32 + + err = ct.AssignTo([]interface{}{nil, &b}) + assert.NoError(t, err) + + assert.Equal(t, int32(42), b) + } + + // *[]interface{} dest when null + { + err := ct.Set(nil) + assert.NoError(t, err) + + var a pgtype.Text + var b pgtype.Int4 + dst := []interface{}{&a, &b} + + err = ct.AssignTo(&dst) + assert.NoError(t, err) + + assert.Nil(t, dst) + } + + // *[]interface{} dest when not null + { + err := ct.Set([]interface{}{"foo", int32(42)}) + assert.NoError(t, err) + + var a pgtype.Text + var b pgtype.Int4 + dst := []interface{}{&a, &b} + + err = ct.AssignTo(&dst) + assert.NoError(t, err) + + assert.NotNil(t, dst) + assert.Equal(t, pgtype.Text{String: "foo", Status: pgtype.Present}, a) + assert.Equal(t, pgtype.Int4{Int: 42, Status: pgtype.Present}, b) + } + + // Struct fields positionally via reflection + { + err := ct.Set([]interface{}{"foo", int32(42)}) + assert.NoError(t, err) + + s := struct { + A string + B int32 + }{} + + err = ct.AssignTo(&s) + if assert.NoError(t, err) { + assert.Equal(t, "foo", s.A) + assert.Equal(t, int32(42), s.B) + } + } +} + +func TestCompositeTypeTranscode(t *testing.T) { + conn := testutil.MustConnectPgx(t) + defer testutil.MustCloseContext(t, conn) + + _, err := conn.Exec(context.Background(), `drop type if exists ct_test; + +create type ct_test as ( + a text, + b int4 +);`) + require.NoError(t, err) + defer conn.Exec(context.Background(), "drop type ct_test") + + var oid uint32 + err = conn.QueryRow(context.Background(), `select 'ct_test'::regtype::oid`).Scan(&oid) + require.NoError(t, err) + + defer conn.Exec(context.Background(), "drop type ct_test") + + ct, err := pgtype.NewCompositeType("ct_test", []pgtype.CompositeTypeField{ + {"a", pgtype.TextOID}, + {"b", pgtype.Int4OID}, + }, conn.ConnInfo()) + require.NoError(t, err) + conn.ConnInfo().RegisterDataType(pgtype.DataType{Value: ct, Name: ct.TypeName(), OID: oid}) + + // Use simple protocol to force text or binary encoding + simpleProtocols := []bool{true, false} + + var a string + var b int32 + + for _, simpleProtocol := range simpleProtocols { + err := conn.QueryRow(context.Background(), "select $1::ct_test", pgx.QuerySimpleProtocol(simpleProtocol), + pgtype.CompositeFields{"hi", int32(42)}, + ).Scan( + []interface{}{&a, &b}, + ) + if assert.NoErrorf(t, err, "Simple Protocol: %v", simpleProtocol) { + assert.EqualValuesf(t, "hi", a, "Simple Protocol: %v", simpleProtocol) + assert.EqualValuesf(t, 42, b, "Simple Protocol: %v", simpleProtocol) + } + } +} + +// https://github.com/jackc/pgx/issues/874 +func TestCompositeTypeTextDecodeNested(t *testing.T) { + newCompositeType := func(name string, fieldNames []string, vals ...pgtype.ValueTranscoder) *pgtype.CompositeType { + fields := make([]pgtype.CompositeTypeField, len(fieldNames)) + for i, name := range fieldNames { + fields[i] = pgtype.CompositeTypeField{Name: name} + } + + rowType, err := pgtype.NewCompositeTypeValues(name, fields, vals) + require.NoError(t, err) + return rowType + } + + dimensionsType := func() pgtype.ValueTranscoder { + return newCompositeType( + "dimensions", + []string{"width", "height"}, + &pgtype.Int4{}, + &pgtype.Int4{}, + ) + } + productImageType := func() pgtype.ValueTranscoder { + return newCompositeType( + "product_image_type", + []string{"source", "dimensions"}, + &pgtype.Text{}, + dimensionsType(), + ) + } + productImageSetType := newCompositeType( + "product_image_set_type", + []string{"name", "orig_image", "images"}, + &pgtype.Text{}, + productImageType(), + pgtype.NewArrayType("product_image", 0, func() pgtype.ValueTranscoder { + return productImageType() + }), + ) + + err := productImageSetType.DecodeText(nil, []byte(`(name,"(img1,""(11,11)"")","{""(img2,\\""(22,22)\\"")"",""(img3,\\""(33,33)\\"")""}")`)) + require.NoError(t, err) +} + +func Example_composite() { + conn, err := pgx.Connect(context.Background(), os.Getenv("PGX_TEST_DATABASE")) + if err != nil { + fmt.Println(err) + return + } + + defer conn.Close(context.Background()) + _, err = conn.Exec(context.Background(), `drop type if exists mytype;`) + if err != nil { + fmt.Println(err) + return + } + + _, err = conn.Exec(context.Background(), `create type mytype as ( + a int4, + b text +);`) + if err != nil { + fmt.Println(err) + return + } + defer conn.Exec(context.Background(), "drop type mytype") + + var oid uint32 + err = conn.QueryRow(context.Background(), `select 'mytype'::regtype::oid`).Scan(&oid) + if err != nil { + fmt.Println(err) + return + } + + ct, err := pgtype.NewCompositeType("mytype", []pgtype.CompositeTypeField{ + {"a", pgtype.Int4OID}, + {"b", pgtype.TextOID}, + }, conn.ConnInfo()) + if err != nil { + fmt.Println(err) + return + } + conn.ConnInfo().RegisterDataType(pgtype.DataType{Value: ct, Name: ct.TypeName(), OID: oid}) + + var a int + var b *string + + err = conn.QueryRow(context.Background(), "select $1::mytype", []interface{}{2, "bar"}).Scan([]interface{}{&a, &b}) + if err != nil { + fmt.Println(err) + return + } + + fmt.Printf("First: a=%d b=%s\n", a, *b) + + err = conn.QueryRow(context.Background(), "select (1, NULL)::mytype").Scan([]interface{}{&a, &b}) + if err != nil { + fmt.Println(err) + return + } + + fmt.Printf("Second: a=%d b=%v\n", a, b) + + scanTarget := []interface{}{&a, &b} + err = conn.QueryRow(context.Background(), "select NULL::mytype").Scan(&scanTarget) + E(err) + + fmt.Printf("Third: isNull=%v\n", scanTarget == nil) + + // Output: + // First: a=2 b=bar + // Second: a=1 b= + // Third: isNull=true +} diff --git a/custom_composite_test.go b/custom_composite_test.go new file mode 100644 index 00000000..9ca8dd5e --- /dev/null +++ b/custom_composite_test.go @@ -0,0 +1,87 @@ +package pgtype_test + +import ( + "context" + "errors" + "fmt" + "os" + + "github.com/jackc/pgtype" + pgx "github.com/jackc/pgx/v4" +) + +type MyType struct { + a int32 // NULL will cause decoding error + b *string // there can be NULL in this position in SQL +} + +func (dst *MyType) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error { + if src == nil { + return errors.New("NULL values can't be decoded. Scan into a &*MyType to handle NULLs") + } + + if err := (pgtype.CompositeFields{&dst.a, &dst.b}).DecodeBinary(ci, src); err != nil { + return err + } + + return nil +} + +func (src MyType) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) (newBuf []byte, err error) { + a := pgtype.Int4{src.a, pgtype.Present} + var b pgtype.Text + if src.b != nil { + b = pgtype.Text{*src.b, pgtype.Present} + } else { + b = pgtype.Text{Status: pgtype.Null} + } + + return (pgtype.CompositeFields{&a, &b}).EncodeBinary(ci, buf) +} + +func ptrS(s string) *string { + return &s +} + +func E(err error) { + if err != nil { + panic(err) + } +} + +// ExampleCustomCompositeTypes demonstrates how support for custom types mappable to SQL +// composites can be added. +func Example_customCompositeTypes() { + conn, err := pgx.Connect(context.Background(), os.Getenv("PGX_TEST_DATABASE")) + E(err) + + defer conn.Close(context.Background()) + _, err = conn.Exec(context.Background(), `drop type if exists mytype; + +create type mytype as ( + a int4, + b text +);`) + E(err) + defer conn.Exec(context.Background(), "drop type mytype") + + var result *MyType + + // Demonstrates both passing and reading back composite values + err = conn.QueryRow(context.Background(), "select $1::mytype", + pgx.QueryResultFormats{pgx.BinaryFormatCode}, MyType{1, ptrS("foo")}). + Scan(&result) + E(err) + + fmt.Printf("First row: a=%d b=%s\n", result.a, *result.b) + + // Because we scan into &*MyType, NULLs are handled generically by assigning nil to result + err = conn.QueryRow(context.Background(), "select NULL::mytype", pgx.QueryResultFormats{pgx.BinaryFormatCode}).Scan(&result) + E(err) + + fmt.Printf("Second row: %v\n", result) + + // Output: + // First row: a=1 b=foo + // Second row: +} diff --git a/date_array_test.go b/date_array_test.go new file mode 100644 index 00000000..4458abfe --- /dev/null +++ b/date_array_test.go @@ -0,0 +1,327 @@ +package pgtype_test + +import ( + "reflect" + "testing" + "time" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestDateArrayTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "date[]", []interface{}{ + &pgtype.DateArray{ + Elements: nil, + Dimensions: nil, + Status: pgtype.Present, + }, + &pgtype.DateArray{ + Elements: []pgtype.Date{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Status: pgtype.Null}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.DateArray{Status: pgtype.Null}, + &pgtype.DateArray{ + Elements: []pgtype.Date{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2017, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Status: pgtype.Null}, + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}, {Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.DateArray{ + Elements: []pgtype.Date{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2015, 2, 2, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2015, 2, 3, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2015, 2, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{ + {Length: 2, LowerBound: 4}, + {Length: 2, LowerBound: 2}, + }, + Status: pgtype.Present, + }, + }) +} + +func TestDateArraySet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.DateArray + }{ + { + source: []time.Time{time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC)}, + result: pgtype.DateArray{ + Elements: []pgtype.Date{{Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: (([]time.Time)(nil)), + result: pgtype.DateArray{Status: pgtype.Null}, + }, + { + source: [][]time.Time{ + {time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC)}, + {time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC)}}, + result: pgtype.DateArray{ + Elements: []pgtype.Date{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [][][][]time.Time{ + {{{ + time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), + time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), + time.Date(2017, 5, 6, 0, 0, 0, 0, time.UTC)}}}, + {{{ + time.Date(2018, 7, 8, 0, 0, 0, 0, time.UTC), + time.Date(2019, 9, 10, 0, 0, 0, 0, time.UTC), + time.Date(2020, 11, 12, 0, 0, 0, 0, time.UTC)}}}}, + result: pgtype.DateArray{ + Elements: []pgtype.Date{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2017, 5, 6, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2018, 7, 8, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2019, 9, 10, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2020, 11, 12, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + { + source: [2][1]time.Time{ + {time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC)}, + {time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC)}}, + result: pgtype.DateArray{ + Elements: []pgtype.Date{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [2][1][1][3]time.Time{ + {{{ + time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), + time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), + time.Date(2017, 5, 6, 0, 0, 0, 0, time.UTC)}}}, + {{{ + time.Date(2018, 7, 8, 0, 0, 0, 0, time.UTC), + time.Date(2019, 9, 10, 0, 0, 0, 0, time.UTC), + time.Date(2020, 11, 12, 0, 0, 0, 0, time.UTC)}}}}, + result: pgtype.DateArray{ + Elements: []pgtype.Date{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2017, 5, 6, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2018, 7, 8, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2019, 9, 10, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2020, 11, 12, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + } + + for i, tt := range successfulTests { + var r pgtype.DateArray + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !reflect.DeepEqual(r, tt.result) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestDateArrayAssignTo(t *testing.T) { + var timeSlice []time.Time + var timeSliceDim2 [][]time.Time + var timeSliceDim4 [][][][]time.Time + var timeArrayDim2 [2][1]time.Time + var timeArrayDim4 [2][1][1][3]time.Time + + simpleTests := []struct { + src pgtype.DateArray + dst interface{} + expected interface{} + }{ + { + src: pgtype.DateArray{ + Elements: []pgtype.Date{{Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &timeSlice, + expected: []time.Time{time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC)}, + }, + { + src: pgtype.DateArray{Status: pgtype.Null}, + dst: &timeSlice, + expected: (([]time.Time)(nil)), + }, + { + src: pgtype.DateArray{Status: pgtype.Present}, + dst: &timeSlice, + expected: []time.Time{}, + }, + { + src: pgtype.DateArray{ + Elements: []pgtype.Date{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &timeSliceDim2, + expected: [][]time.Time{ + {time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC)}, + {time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC)}}, + }, + { + src: pgtype.DateArray{ + Elements: []pgtype.Date{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2017, 5, 6, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2018, 7, 8, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2019, 9, 10, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2020, 11, 12, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + dst: &timeSliceDim4, + expected: [][][][]time.Time{ + {{{ + time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), + time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), + time.Date(2017, 5, 6, 0, 0, 0, 0, time.UTC)}}}, + {{{ + time.Date(2018, 7, 8, 0, 0, 0, 0, time.UTC), + time.Date(2019, 9, 10, 0, 0, 0, 0, time.UTC), + time.Date(2020, 11, 12, 0, 0, 0, 0, time.UTC)}}}}, + }, + { + src: pgtype.DateArray{ + Elements: []pgtype.Date{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &timeArrayDim2, + expected: [2][1]time.Time{ + {time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC)}, + {time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC)}}, + }, + { + src: pgtype.DateArray{ + Elements: []pgtype.Date{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2017, 5, 6, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2018, 7, 8, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2019, 9, 10, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2020, 11, 12, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + dst: &timeArrayDim4, + expected: [2][1][1][3]time.Time{ + {{{ + time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), + time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), + time.Date(2017, 5, 6, 0, 0, 0, 0, time.UTC)}}}, + {{{ + time.Date(2018, 7, 8, 0, 0, 0, 0, time.UTC), + time.Date(2019, 9, 10, 0, 0, 0, 0, time.UTC), + time.Date(2020, 11, 12, 0, 0, 0, 0, time.UTC)}}}}, + }, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.DateArray + dst interface{} + }{ + { + src: pgtype.DateArray{ + Elements: []pgtype.Date{{Status: pgtype.Null}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &timeSlice, + }, + { + src: pgtype.DateArray{ + Elements: []pgtype.Date{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &timeArrayDim2, + }, + { + src: pgtype.DateArray{ + Elements: []pgtype.Date{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &timeSlice, + }, + { + src: pgtype.DateArray{ + Elements: []pgtype.Date{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &timeArrayDim4, + }, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } + +} diff --git a/date_test.go b/date_test.go new file mode 100644 index 00000000..5c38e7a3 --- /dev/null +++ b/date_test.go @@ -0,0 +1,168 @@ +package pgtype_test + +import ( + "reflect" + "testing" + "time" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestDateTranscode(t *testing.T) { + testutil.TestSuccessfulTranscodeEqFunc(t, "date", []interface{}{ + &pgtype.Date{Time: time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + &pgtype.Date{Time: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + &pgtype.Date{Time: time.Date(1999, 12, 31, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + &pgtype.Date{Time: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + &pgtype.Date{Time: time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + &pgtype.Date{Time: time.Date(2200, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + &pgtype.Date{Status: pgtype.Null}, + &pgtype.Date{Status: pgtype.Present, InfinityModifier: pgtype.Infinity}, + &pgtype.Date{Status: pgtype.Present, InfinityModifier: -pgtype.Infinity}, + }, func(a, b interface{}) bool { + at := a.(pgtype.Date) + bt := b.(pgtype.Date) + + return at.Time.Equal(bt.Time) && at.Status == bt.Status && at.InfinityModifier == bt.InfinityModifier + }) +} + +func TestDateSet(t *testing.T) { + type _time time.Time + + successfulTests := []struct { + source interface{} + result pgtype.Date + }{ + {source: time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC), result: pgtype.Date{Time: time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + {source: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC), result: pgtype.Date{Time: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + {source: time.Date(1999, 12, 31, 0, 0, 0, 0, time.UTC), result: pgtype.Date{Time: time.Date(1999, 12, 31, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + {source: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), result: pgtype.Date{Time: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + {source: time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC), result: pgtype.Date{Time: time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + {source: time.Date(2200, 1, 1, 0, 0, 0, 0, time.UTC), result: pgtype.Date{Time: time.Date(2200, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + {source: _time(time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)), result: pgtype.Date{Time: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + {source: "1999-12-31", result: pgtype.Date{Time: time.Date(1999, 12, 31, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + } + + for i, tt := range successfulTests { + var d pgtype.Date + err := d.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if d != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, d) + } + } +} + +func TestDateAssignTo(t *testing.T) { + var tim time.Time + var ptim *time.Time + + simpleTests := []struct { + src pgtype.Date + dst interface{} + expected interface{} + }{ + {src: pgtype.Date{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}, dst: &tim, expected: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local)}, + {src: pgtype.Date{Time: time.Time{}, Status: pgtype.Null}, dst: &ptim, expected: ((*time.Time)(nil))}, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + pointerAllocTests := []struct { + src pgtype.Date + dst interface{} + expected interface{} + }{ + {src: pgtype.Date{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}, dst: &ptim, expected: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local)}, + } + + for i, tt := range pointerAllocTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.Date + dst interface{} + }{ + {src: pgtype.Date{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), InfinityModifier: pgtype.Infinity, Status: pgtype.Present}, dst: &tim}, + {src: pgtype.Date{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), InfinityModifier: pgtype.NegativeInfinity, Status: pgtype.Present}, dst: &tim}, + {src: pgtype.Date{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Null}, dst: &tim}, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } +} + +func TestDateMarshalJSON(t *testing.T) { + successfulTests := []struct { + source pgtype.Date + result string + }{ + {source: pgtype.Date{Status: pgtype.Null}, result: "null"}, + {source: pgtype.Date{Time: time.Date(2012, 3, 29, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, result: "\"2012-03-29\""}, + {source: pgtype.Date{Time: time.Date(2012, 3, 29, 10, 5, 45, 0, time.FixedZone("", -6*60*60)), Status: pgtype.Present}, result: "\"2012-03-29\""}, + {source: pgtype.Date{Time: time.Date(2012, 3, 29, 10, 5, 45, 555*1000*1000, time.FixedZone("", -6*60*60)), Status: pgtype.Present}, result: "\"2012-03-29\""}, + {source: pgtype.Date{InfinityModifier: pgtype.Infinity, Status: pgtype.Present}, result: "\"infinity\""}, + {source: pgtype.Date{InfinityModifier: pgtype.NegativeInfinity, Status: pgtype.Present}, result: "\"-infinity\""}, + } + for i, tt := range successfulTests { + r, err := tt.source.MarshalJSON() + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if string(r) != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, string(r)) + } + } +} + +func TestDateUnmarshalJSON(t *testing.T) { + successfulTests := []struct { + source string + result pgtype.Date + }{ + {source: "null", result: pgtype.Date{Status: pgtype.Null}}, + {source: "\"2012-03-29\"", result: pgtype.Date{Time: time.Date(2012, 3, 29, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + {source: "\"2012-03-29\"", result: pgtype.Date{Time: time.Date(2012, 3, 29, 10, 5, 45, 0, time.FixedZone("", -6*60*60)), Status: pgtype.Present}}, + {source: "\"2012-03-29\"", result: pgtype.Date{Time: time.Date(2012, 3, 29, 10, 5, 45, 555*1000*1000, time.FixedZone("", -6*60*60)), Status: pgtype.Present}}, + {source: "\"infinity\"", result: pgtype.Date{InfinityModifier: pgtype.Infinity, Status: pgtype.Present}}, + {source: "\"-infinity\"", result: pgtype.Date{InfinityModifier: pgtype.NegativeInfinity, Status: pgtype.Present}}, + } + for i, tt := range successfulTests { + var r pgtype.Date + err := r.UnmarshalJSON([]byte(tt.source)) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if r.Time.Year() != tt.result.Time.Year() || r.Time.Month() != tt.result.Time.Month() || r.Time.Day() != tt.result.Time.Day() || r.Status != tt.result.Status || r.InfinityModifier != tt.result.InfinityModifier { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} diff --git a/daterange_test.go b/daterange_test.go new file mode 100644 index 00000000..54d51e2d --- /dev/null +++ b/daterange_test.go @@ -0,0 +1,133 @@ +package pgtype_test + +import ( + "testing" + "time" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestDaterangeTranscode(t *testing.T) { + testutil.TestSuccessfulTranscodeEqFunc(t, "daterange", []interface{}{ + &pgtype.Daterange{LowerType: pgtype.Empty, UpperType: pgtype.Empty, Status: pgtype.Present}, + &pgtype.Daterange{ + Lower: pgtype.Date{Time: time.Date(1990, 12, 31, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + Upper: pgtype.Date{Time: time.Date(2028, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + LowerType: pgtype.Inclusive, + UpperType: pgtype.Exclusive, + Status: pgtype.Present, + }, + &pgtype.Daterange{ + Lower: pgtype.Date{Time: time.Date(1800, 12, 31, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + Upper: pgtype.Date{Time: time.Date(2200, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + LowerType: pgtype.Inclusive, + UpperType: pgtype.Exclusive, + Status: pgtype.Present, + }, + &pgtype.Daterange{Status: pgtype.Null}, + }, func(aa, bb interface{}) bool { + a := aa.(pgtype.Daterange) + b := bb.(pgtype.Daterange) + + return a.Status == b.Status && + a.Lower.Time.Equal(b.Lower.Time) && + a.Lower.Status == b.Lower.Status && + a.Lower.InfinityModifier == b.Lower.InfinityModifier && + a.Upper.Time.Equal(b.Upper.Time) && + a.Upper.Status == b.Upper.Status && + a.Upper.InfinityModifier == b.Upper.InfinityModifier + }) +} + +func TestDaterangeNormalize(t *testing.T) { + testutil.TestSuccessfulNormalizeEqFunc(t, []testutil.NormalizeTest{ + { + SQL: "select daterange('2010-01-01', '2010-01-11', '(]')", + Value: pgtype.Daterange{ + Lower: pgtype.Date{Time: time.Date(2010, 1, 2, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + Upper: pgtype.Date{Time: time.Date(2010, 1, 12, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + LowerType: pgtype.Inclusive, + UpperType: pgtype.Exclusive, + Status: pgtype.Present, + }, + }, + }, func(aa, bb interface{}) bool { + a := aa.(pgtype.Daterange) + b := bb.(pgtype.Daterange) + + return a.Status == b.Status && + a.Lower.Time.Equal(b.Lower.Time) && + a.Lower.Status == b.Lower.Status && + a.Lower.InfinityModifier == b.Lower.InfinityModifier && + a.Upper.Time.Equal(b.Upper.Time) && + a.Upper.Status == b.Upper.Status && + a.Upper.InfinityModifier == b.Upper.InfinityModifier + }) +} + +func TestDaterangeSet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.Daterange + }{ + { + source: nil, + result: pgtype.Daterange{Status: pgtype.Null}, + }, + { + source: &pgtype.Daterange{ + Lower: pgtype.Date{Time: time.Date(1990, 12, 31, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + Upper: pgtype.Date{Time: time.Date(2028, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + LowerType: pgtype.Inclusive, + UpperType: pgtype.Exclusive, + Status: pgtype.Present, + }, + result: pgtype.Daterange{ + Lower: pgtype.Date{Time: time.Date(1990, 12, 31, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + Upper: pgtype.Date{Time: time.Date(2028, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + LowerType: pgtype.Inclusive, + UpperType: pgtype.Exclusive, + Status: pgtype.Present, + }, + }, + { + source: pgtype.Daterange{ + Lower: pgtype.Date{Time: time.Date(1990, 12, 31, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + Upper: pgtype.Date{Time: time.Date(2028, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + LowerType: pgtype.Inclusive, + UpperType: pgtype.Exclusive, + Status: pgtype.Present, + }, + result: pgtype.Daterange{ + Lower: pgtype.Date{Time: time.Date(1990, 12, 31, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + Upper: pgtype.Date{Time: time.Date(2028, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + LowerType: pgtype.Inclusive, + UpperType: pgtype.Exclusive, + Status: pgtype.Present, + }, + }, + { + source: "[1990-12-31,2028-01-01)", + result: pgtype.Daterange{ + Lower: pgtype.Date{Time: time.Date(1990, 12, 31, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + Upper: pgtype.Date{Time: time.Date(2028, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + LowerType: pgtype.Inclusive, + UpperType: pgtype.Exclusive, + Status: pgtype.Present, + }, + }, + } + + for i, tt := range successfulTests { + var r pgtype.Daterange + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if r != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} diff --git a/enum_array_test.go b/enum_array_test.go new file mode 100644 index 00000000..659340f0 --- /dev/null +++ b/enum_array_test.go @@ -0,0 +1,281 @@ +package pgtype_test + +import ( + "context" + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestEnumArrayTranscode(t *testing.T) { + setupConn := testutil.MustConnectPgx(t) + defer testutil.MustCloseContext(t, setupConn) + + if _, err := setupConn.Exec(context.Background(), "drop type if exists color"); err != nil { + t.Fatal(err) + } + if _, err := setupConn.Exec(context.Background(), "create type color as enum ('red', 'green', 'blue')"); err != nil { + t.Fatal(err) + } + + testutil.TestSuccessfulTranscode(t, "color[]", []interface{}{ + &pgtype.EnumArray{ + Elements: nil, + Dimensions: nil, + Status: pgtype.Present, + }, + &pgtype.EnumArray{ + Elements: []pgtype.GenericText{ + {String: "red", Status: pgtype.Present}, + {Status: pgtype.Null}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.EnumArray{Status: pgtype.Null}, + &pgtype.EnumArray{ + Elements: []pgtype.GenericText{ + {String: "red", Status: pgtype.Present}, + {String: "green", Status: pgtype.Present}, + {String: "blue", Status: pgtype.Present}, + {String: "red", Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{ + {Length: 2, LowerBound: 4}, + {Length: 2, LowerBound: 2}, + }, + Status: pgtype.Present, + }, + }) +} + +func TestEnumArrayArraySet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.EnumArray + }{ + { + source: []string{"foo"}, + result: pgtype.EnumArray{ + Elements: []pgtype.GenericText{{String: "foo", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: (([]string)(nil)), + result: pgtype.EnumArray{Status: pgtype.Null}, + }, + { + source: [][]string{{"foo"}, {"bar"}}, + result: pgtype.EnumArray{ + Elements: []pgtype.GenericText{{String: "foo", Status: pgtype.Present}, {String: "bar", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [][][][]string{{{{"foo", "bar", "baz"}}}, {{{"wibble", "wobble", "wubble"}}}}, + result: pgtype.EnumArray{ + Elements: []pgtype.GenericText{ + {String: "foo", Status: pgtype.Present}, + {String: "bar", Status: pgtype.Present}, + {String: "baz", Status: pgtype.Present}, + {String: "wibble", Status: pgtype.Present}, + {String: "wobble", Status: pgtype.Present}, + {String: "wubble", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + { + source: [2][1]string{{"foo"}, {"bar"}}, + result: pgtype.EnumArray{ + Elements: []pgtype.GenericText{{String: "foo", Status: pgtype.Present}, {String: "bar", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [2][1][1][3]string{{{{"foo", "bar", "baz"}}}, {{{"wibble", "wobble", "wubble"}}}}, + result: pgtype.EnumArray{ + Elements: []pgtype.GenericText{ + {String: "foo", Status: pgtype.Present}, + {String: "bar", Status: pgtype.Present}, + {String: "baz", Status: pgtype.Present}, + {String: "wibble", Status: pgtype.Present}, + {String: "wobble", Status: pgtype.Present}, + {String: "wubble", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + } + + for i, tt := range successfulTests { + var r pgtype.EnumArray + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !reflect.DeepEqual(r, tt.result) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestEnumArrayArrayAssignTo(t *testing.T) { + var stringSlice []string + type _stringSlice []string + var namedStringSlice _stringSlice + var stringSliceDim2 [][]string + var stringSliceDim4 [][][][]string + var stringArrayDim2 [2][1]string + var stringArrayDim4 [2][1][1][3]string + + simpleTests := []struct { + src pgtype.EnumArray + dst interface{} + expected interface{} + }{ + { + src: pgtype.EnumArray{ + Elements: []pgtype.GenericText{{String: "foo", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &stringSlice, + expected: []string{"foo"}, + }, + { + src: pgtype.EnumArray{ + Elements: []pgtype.GenericText{{String: "bar", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &namedStringSlice, + expected: _stringSlice{"bar"}, + }, + { + src: pgtype.EnumArray{Status: pgtype.Null}, + dst: &stringSlice, + expected: (([]string)(nil)), + }, + { + src: pgtype.EnumArray{Status: pgtype.Present}, + dst: &stringSlice, + expected: []string{}, + }, + { + src: pgtype.EnumArray{ + Elements: []pgtype.GenericText{{String: "foo", Status: pgtype.Present}, {String: "bar", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &stringSliceDim2, + expected: [][]string{{"foo"}, {"bar"}}, + }, + { + src: pgtype.EnumArray{ + Elements: []pgtype.GenericText{ + {String: "foo", Status: pgtype.Present}, + {String: "bar", Status: pgtype.Present}, + {String: "baz", Status: pgtype.Present}, + {String: "wibble", Status: pgtype.Present}, + {String: "wobble", Status: pgtype.Present}, + {String: "wubble", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + dst: &stringSliceDim4, + expected: [][][][]string{{{{"foo", "bar", "baz"}}}, {{{"wibble", "wobble", "wubble"}}}}, + }, + { + src: pgtype.EnumArray{ + Elements: []pgtype.GenericText{{String: "foo", Status: pgtype.Present}, {String: "bar", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &stringArrayDim2, + expected: [2][1]string{{"foo"}, {"bar"}}, + }, + { + src: pgtype.EnumArray{ + Elements: []pgtype.GenericText{ + {String: "foo", Status: pgtype.Present}, + {String: "bar", Status: pgtype.Present}, + {String: "baz", Status: pgtype.Present}, + {String: "wibble", Status: pgtype.Present}, + {String: "wobble", Status: pgtype.Present}, + {String: "wubble", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + dst: &stringArrayDim4, + expected: [2][1][1][3]string{{{{"foo", "bar", "baz"}}}, {{{"wibble", "wobble", "wubble"}}}}, + }, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.EnumArray + dst interface{} + }{ + { + src: pgtype.EnumArray{ + Elements: []pgtype.GenericText{{Status: pgtype.Null}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &stringSlice, + }, + { + src: pgtype.EnumArray{ + Elements: []pgtype.GenericText{{String: "foo", Status: pgtype.Present}, {String: "bar", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &stringArrayDim2, + }, + { + src: pgtype.EnumArray{ + Elements: []pgtype.GenericText{{String: "foo", Status: pgtype.Present}, {String: "bar", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &stringSlice, + }, + { + src: pgtype.EnumArray{ + Elements: []pgtype.GenericText{{String: "foo", Status: pgtype.Present}, {String: "bar", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &stringArrayDim4, + }, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } +} diff --git a/enum_type_test.go b/enum_type_test.go new file mode 100644 index 00000000..4dd88f2a --- /dev/null +++ b/enum_type_test.go @@ -0,0 +1,148 @@ +package pgtype_test + +import ( + "bytes" + "context" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" + "github.com/jackc/pgx/v4" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func setupEnum(t *testing.T, conn *pgx.Conn) *pgtype.EnumType { + _, err := conn.Exec(context.Background(), "drop type if exists pgtype_enum_color;") + require.NoError(t, err) + + _, err = conn.Exec(context.Background(), "create type pgtype_enum_color as enum ('blue', 'green', 'purple');") + require.NoError(t, err) + + var oid uint32 + err = conn.QueryRow(context.Background(), "select oid from pg_type where typname=$1;", "pgtype_enum_color").Scan(&oid) + require.NoError(t, err) + + et := pgtype.NewEnumType("pgtype_enum_color", []string{"blue", "green", "purple"}) + conn.ConnInfo().RegisterDataType(pgtype.DataType{Value: et, Name: "pgtype_enum_color", OID: oid}) + + return et +} + +func cleanupEnum(t *testing.T, conn *pgx.Conn) { + _, err := conn.Exec(context.Background(), "drop type if exists pgtype_enum_color;") + require.NoError(t, err) +} + +func TestEnumTypeTranscode(t *testing.T) { + conn := testutil.MustConnectPgx(t) + defer testutil.MustCloseContext(t, conn) + + setupEnum(t, conn) + defer cleanupEnum(t, conn) + + var dst string + err := conn.QueryRow(context.Background(), "select $1::pgtype_enum_color", "blue").Scan(&dst) + require.NoError(t, err) + require.EqualValues(t, "blue", dst) +} + +func TestEnumTypeSet(t *testing.T) { + conn := testutil.MustConnectPgx(t) + defer testutil.MustCloseContext(t, conn) + + enumType := setupEnum(t, conn) + defer cleanupEnum(t, conn) + + successfulTests := []struct { + source interface{} + result interface{} + }{ + {source: "blue", result: "blue"}, + {source: _string("green"), result: "green"}, + {source: (*string)(nil), result: nil}, + } + + for i, tt := range successfulTests { + err := enumType.Set(tt.source) + assert.NoErrorf(t, err, "%d", i) + assert.Equalf(t, tt.result, enumType.Get(), "%d", i) + } +} + +func TestEnumTypeAssignTo(t *testing.T) { + conn := testutil.MustConnectPgx(t) + defer testutil.MustCloseContext(t, conn) + + enumType := setupEnum(t, conn) + defer cleanupEnum(t, conn) + + { + var s string + + err := enumType.Set("blue") + require.NoError(t, err) + + err = enumType.AssignTo(&s) + require.NoError(t, err) + + assert.EqualValues(t, "blue", s) + } + + { + var ps *string + + err := enumType.Set("blue") + require.NoError(t, err) + + err = enumType.AssignTo(&ps) + require.NoError(t, err) + + assert.EqualValues(t, "blue", *ps) + } + + { + var ps *string + + err := enumType.Set(nil) + require.NoError(t, err) + + err = enumType.AssignTo(&ps) + require.NoError(t, err) + + assert.EqualValues(t, (*string)(nil), ps) + } + + var buf []byte + bytesTests := []struct { + src interface{} + dst *[]byte + expected []byte + }{ + {src: "blue", dst: &buf, expected: []byte("blue")}, + {src: nil, dst: &buf, expected: nil}, + } + + for i, tt := range bytesTests { + err := enumType.Set(tt.src) + require.NoError(t, err, "%d", i) + + err = enumType.AssignTo(tt.dst) + require.NoError(t, err, "%d", i) + + if bytes.Compare(*tt.dst, tt.expected) != 0 { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, tt.dst) + } + } + + { + var s string + + err := enumType.Set(nil) + require.NoError(t, err) + + err = enumType.AssignTo(&s) + require.Error(t, err) + } + +} diff --git a/ext/gofrs-uuid/uuid_test.go b/ext/gofrs-uuid/uuid_test.go new file mode 100644 index 00000000..56814524 --- /dev/null +++ b/ext/gofrs-uuid/uuid_test.go @@ -0,0 +1,101 @@ +package uuid_test + +import ( + "bytes" + "testing" + + "github.com/jackc/pgtype" + gofrs "github.com/jackc/pgtype/ext/gofrs-uuid" + "github.com/jackc/pgtype/testutil" +) + +func TestUUIDTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "uuid", []interface{}{ + &gofrs.UUID{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}, + &gofrs.UUID{Status: pgtype.Null}, + }) +} + +func TestUUIDSet(t *testing.T) { + successfulTests := []struct { + source interface{} + result gofrs.UUID + }{ + { + source: &gofrs.UUID{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}, + result: gofrs.UUID{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}, + }, + { + source: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, + result: gofrs.UUID{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}, + }, + { + source: []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, + result: gofrs.UUID{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}, + }, + { + source: "00010203-0405-0607-0809-0a0b0c0d0e0f", + result: gofrs.UUID{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}, + }, + } + + for i, tt := range successfulTests { + var r gofrs.UUID + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if r != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestUUIDAssignTo(t *testing.T) { + { + src := gofrs.UUID{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present} + var dst [16]byte + expected := [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} + + err := src.AssignTo(&dst) + if err != nil { + t.Error(err) + } + + if dst != expected { + t.Errorf("expected %v to assign %v, but result was %v", src, expected, dst) + } + } + + { + src := gofrs.UUID{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present} + var dst []byte + expected := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} + + err := src.AssignTo(&dst) + if err != nil { + t.Error(err) + } + + if bytes.Compare(dst, expected) != 0 { + t.Errorf("expected %v to assign %v, but result was %v", src, expected, dst) + } + } + + { + src := gofrs.UUID{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present} + var dst string + expected := "00010203-0405-0607-0809-0a0b0c0d0e0f" + + err := src.AssignTo(&dst) + if err != nil { + t.Error(err) + } + + if dst != expected { + t.Errorf("expected %v to assign %v, but result was %v", src, expected, dst) + } + } + +} diff --git a/ext/shopspring-numeric/decimal_test.go b/ext/shopspring-numeric/decimal_test.go new file mode 100644 index 00000000..bf34e0dd --- /dev/null +++ b/ext/shopspring-numeric/decimal_test.go @@ -0,0 +1,330 @@ +package numeric_test + +import ( + "fmt" + "math/big" + "math/rand" + "reflect" + "testing" + + "github.com/jackc/pgtype" + shopspring "github.com/jackc/pgtype/ext/shopspring-numeric" + "github.com/jackc/pgtype/testutil" + "github.com/shopspring/decimal" + "github.com/stretchr/testify/require" +) + +func mustParseDecimal(t *testing.T, src string) decimal.Decimal { + dec, err := decimal.NewFromString(src) + if err != nil { + t.Fatal(err) + } + return dec +} + +func TestNumericNormalize(t *testing.T) { + testutil.TestSuccessfulNormalizeEqFunc(t, []testutil.NormalizeTest{ + { + SQL: "select '0'::numeric", + Value: &shopspring.Numeric{Decimal: mustParseDecimal(t, "0"), Status: pgtype.Present}, + }, + { + SQL: "select '1'::numeric", + Value: &shopspring.Numeric{Decimal: mustParseDecimal(t, "1"), Status: pgtype.Present}, + }, + { + SQL: "select '10.00'::numeric", + Value: &shopspring.Numeric{Decimal: mustParseDecimal(t, "10.00"), Status: pgtype.Present}, + }, + { + SQL: "select '1e-3'::numeric", + Value: &shopspring.Numeric{Decimal: mustParseDecimal(t, "0.001"), Status: pgtype.Present}, + }, + { + SQL: "select '-1'::numeric", + Value: &shopspring.Numeric{Decimal: mustParseDecimal(t, "-1"), Status: pgtype.Present}, + }, + { + SQL: "select '10000'::numeric", + Value: &shopspring.Numeric{Decimal: mustParseDecimal(t, "10000"), Status: pgtype.Present}, + }, + { + SQL: "select '3.14'::numeric", + Value: &shopspring.Numeric{Decimal: mustParseDecimal(t, "3.14"), Status: pgtype.Present}, + }, + { + SQL: "select '1.1'::numeric", + Value: &shopspring.Numeric{Decimal: mustParseDecimal(t, "1.1"), Status: pgtype.Present}, + }, + { + SQL: "select '100010001'::numeric", + Value: &shopspring.Numeric{Decimal: mustParseDecimal(t, "100010001"), Status: pgtype.Present}, + }, + { + SQL: "select '100010001.0001'::numeric", + Value: &shopspring.Numeric{Decimal: mustParseDecimal(t, "100010001.0001"), Status: pgtype.Present}, + }, + { + SQL: "select '4237234789234789289347892374324872138321894178943189043890124832108934.43219085471578891547854892438945012347981'::numeric", + Value: &shopspring.Numeric{ + Decimal: mustParseDecimal(t, "4237234789234789289347892374324872138321894178943189043890124832108934.43219085471578891547854892438945012347981"), + Status: pgtype.Present, + }, + }, + { + SQL: "select '0.8925092023480223478923478978978937897879595901237890234789243679037419057877231734823098432903527585734549035904590854890345905434578345789347890402348952348905890489054234237489234987723894789234'::numeric", + Value: &shopspring.Numeric{ + Decimal: mustParseDecimal(t, "0.8925092023480223478923478978978937897879595901237890234789243679037419057877231734823098432903527585734549035904590854890345905434578345789347890402348952348905890489054234237489234987723894789234"), + Status: pgtype.Present, + }, + }, + { + SQL: "select '0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000123'::numeric", + Value: &shopspring.Numeric{ + Decimal: mustParseDecimal(t, "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000123"), + Status: pgtype.Present, + }, + }, + }, func(aa, bb interface{}) bool { + a := aa.(shopspring.Numeric) + b := bb.(shopspring.Numeric) + + return a.Status == b.Status && a.Decimal.Equal(b.Decimal) + }) +} + +func TestNumericTranscode(t *testing.T) { + testutil.TestSuccessfulTranscodeEqFunc(t, "numeric", []interface{}{ + &shopspring.Numeric{Decimal: mustParseDecimal(t, "0"), Status: pgtype.Present}, + &shopspring.Numeric{Decimal: mustParseDecimal(t, "1"), Status: pgtype.Present}, + &shopspring.Numeric{Decimal: mustParseDecimal(t, "-1"), Status: pgtype.Present}, + &shopspring.Numeric{Decimal: mustParseDecimal(t, "100000"), Status: pgtype.Present}, + + &shopspring.Numeric{Decimal: mustParseDecimal(t, "0.1"), Status: pgtype.Present}, + &shopspring.Numeric{Decimal: mustParseDecimal(t, "0.01"), Status: pgtype.Present}, + &shopspring.Numeric{Decimal: mustParseDecimal(t, "0.001"), Status: pgtype.Present}, + &shopspring.Numeric{Decimal: mustParseDecimal(t, "0.0001"), Status: pgtype.Present}, + &shopspring.Numeric{Decimal: mustParseDecimal(t, "0.00001"), Status: pgtype.Present}, + &shopspring.Numeric{Decimal: mustParseDecimal(t, "0.000001"), Status: pgtype.Present}, + + &shopspring.Numeric{Decimal: mustParseDecimal(t, "3.14"), Status: pgtype.Present}, + &shopspring.Numeric{Decimal: mustParseDecimal(t, "0.00000123"), Status: pgtype.Present}, + &shopspring.Numeric{Decimal: mustParseDecimal(t, "0.000000123"), Status: pgtype.Present}, + &shopspring.Numeric{Decimal: mustParseDecimal(t, "0.0000000123"), Status: pgtype.Present}, + &shopspring.Numeric{Decimal: mustParseDecimal(t, "0.00000000123"), Status: pgtype.Present}, + &shopspring.Numeric{Decimal: mustParseDecimal(t, "0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001234567890123456789"), Status: pgtype.Present}, + &shopspring.Numeric{Decimal: mustParseDecimal(t, "4309132809320932980457137401234890237489238912983572189348951289375283573984571892758234678903467889512893489128589347891272139.8489235871258912789347891235879148795891238915678189467128957812395781238579189025891238901583915890128973578957912385798125789012378905238905471598123758923478294374327894237892234"), Status: pgtype.Present}, + &shopspring.Numeric{Status: pgtype.Null}, + }, func(aa, bb interface{}) bool { + a := aa.(shopspring.Numeric) + b := bb.(shopspring.Numeric) + + return a.Status == b.Status && a.Decimal.Equal(b.Decimal) + }) + +} + +func TestNumericTranscodeFuzz(t *testing.T) { + r := rand.New(rand.NewSource(0)) + max := &big.Int{} + max.SetString("9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999", 10) + + values := make([]interface{}, 0, 2000) + for i := 0; i < 500; i++ { + num := fmt.Sprintf("%s.%s", (&big.Int{}).Rand(r, max).String(), (&big.Int{}).Rand(r, max).String()) + negNum := "-" + num + values = append(values, &shopspring.Numeric{Decimal: mustParseDecimal(t, num), Status: pgtype.Present}) + values = append(values, &shopspring.Numeric{Decimal: mustParseDecimal(t, negNum), Status: pgtype.Present}) + } + + testutil.TestSuccessfulTranscodeEqFunc(t, "numeric", values, + func(aa, bb interface{}) bool { + a := aa.(shopspring.Numeric) + b := bb.(shopspring.Numeric) + + return a.Status == b.Status && a.Decimal.Equal(b.Decimal) + }) +} + +func TestNumericSet(t *testing.T) { + type _int8 int8 + + successfulTests := []struct { + source interface{} + result *shopspring.Numeric + }{ + {source: float32(1), result: &shopspring.Numeric{Decimal: mustParseDecimal(t, "1"), Status: pgtype.Present}}, + {source: float64(1), result: &shopspring.Numeric{Decimal: mustParseDecimal(t, "1"), Status: pgtype.Present}}, + {source: int8(1), result: &shopspring.Numeric{Decimal: mustParseDecimal(t, "1"), Status: pgtype.Present}}, + {source: int16(1), result: &shopspring.Numeric{Decimal: mustParseDecimal(t, "1"), Status: pgtype.Present}}, + {source: int32(1), result: &shopspring.Numeric{Decimal: mustParseDecimal(t, "1"), Status: pgtype.Present}}, + {source: int64(1), result: &shopspring.Numeric{Decimal: mustParseDecimal(t, "1"), Status: pgtype.Present}}, + {source: int8(-1), result: &shopspring.Numeric{Decimal: mustParseDecimal(t, "-1"), Status: pgtype.Present}}, + {source: int16(-1), result: &shopspring.Numeric{Decimal: mustParseDecimal(t, "-1"), Status: pgtype.Present}}, + {source: int32(-1), result: &shopspring.Numeric{Decimal: mustParseDecimal(t, "-1"), Status: pgtype.Present}}, + {source: int64(-1), result: &shopspring.Numeric{Decimal: mustParseDecimal(t, "-1"), Status: pgtype.Present}}, + {source: uint8(1), result: &shopspring.Numeric{Decimal: mustParseDecimal(t, "1"), Status: pgtype.Present}}, + {source: uint16(1), result: &shopspring.Numeric{Decimal: mustParseDecimal(t, "1"), Status: pgtype.Present}}, + {source: uint32(1), result: &shopspring.Numeric{Decimal: mustParseDecimal(t, "1"), Status: pgtype.Present}}, + {source: uint64(1), result: &shopspring.Numeric{Decimal: mustParseDecimal(t, "1"), Status: pgtype.Present}}, + {source: "1", result: &shopspring.Numeric{Decimal: mustParseDecimal(t, "1"), Status: pgtype.Present}}, + {source: _int8(1), result: &shopspring.Numeric{Decimal: mustParseDecimal(t, "1"), Status: pgtype.Present}}, + {source: float64(1000), result: &shopspring.Numeric{Decimal: mustParseDecimal(t, "1000"), Status: pgtype.Present}}, + {source: float64(1234), result: &shopspring.Numeric{Decimal: mustParseDecimal(t, "1234"), Status: pgtype.Present}}, + {source: float64(12345678900), result: &shopspring.Numeric{Decimal: mustParseDecimal(t, "12345678900"), Status: pgtype.Present}}, + {source: float64(1.25), result: &shopspring.Numeric{Decimal: mustParseDecimal(t, "1.25"), Status: pgtype.Present}}, + } + + for i, tt := range successfulTests { + r := &shopspring.Numeric{} + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !(r.Status == tt.result.Status && r.Decimal.Equal(tt.result.Decimal)) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestNumericAssignTo(t *testing.T) { + type _int8 int8 + + var i8 int8 + var i16 int16 + var i32 int32 + var i64 int64 + var i int + var ui8 uint8 + var ui16 uint16 + var ui32 uint32 + var ui64 uint64 + var ui uint + var pi8 *int8 + var _i8 _int8 + var _pi8 *_int8 + var f32 float32 + var f64 float64 + var pf32 *float32 + var pf64 *float64 + + simpleTests := []struct { + src *shopspring.Numeric + dst interface{} + expected interface{} + }{ + {src: &shopspring.Numeric{Decimal: mustParseDecimal(t, "42"), Status: pgtype.Present}, dst: &f32, expected: float32(42)}, + {src: &shopspring.Numeric{Decimal: mustParseDecimal(t, "42"), Status: pgtype.Present}, dst: &f64, expected: float64(42)}, + {src: &shopspring.Numeric{Decimal: mustParseDecimal(t, "4.2"), Status: pgtype.Present}, dst: &f32, expected: float32(4.2)}, + {src: &shopspring.Numeric{Decimal: mustParseDecimal(t, "4.2"), Status: pgtype.Present}, dst: &f64, expected: float64(4.2)}, + {src: &shopspring.Numeric{Decimal: mustParseDecimal(t, "42"), Status: pgtype.Present}, dst: &i16, expected: int16(42)}, + {src: &shopspring.Numeric{Decimal: mustParseDecimal(t, "42"), Status: pgtype.Present}, dst: &i32, expected: int32(42)}, + {src: &shopspring.Numeric{Decimal: mustParseDecimal(t, "42"), Status: pgtype.Present}, dst: &i64, expected: int64(42)}, + {src: &shopspring.Numeric{Decimal: mustParseDecimal(t, "42000"), Status: pgtype.Present}, dst: &i64, expected: int64(42000)}, + {src: &shopspring.Numeric{Decimal: mustParseDecimal(t, "42"), Status: pgtype.Present}, dst: &i, expected: int(42)}, + {src: &shopspring.Numeric{Decimal: mustParseDecimal(t, "42"), Status: pgtype.Present}, dst: &ui8, expected: uint8(42)}, + {src: &shopspring.Numeric{Decimal: mustParseDecimal(t, "42"), Status: pgtype.Present}, dst: &ui16, expected: uint16(42)}, + {src: &shopspring.Numeric{Decimal: mustParseDecimal(t, "42"), Status: pgtype.Present}, dst: &ui32, expected: uint32(42)}, + {src: &shopspring.Numeric{Decimal: mustParseDecimal(t, "42"), Status: pgtype.Present}, dst: &ui64, expected: uint64(42)}, + {src: &shopspring.Numeric{Decimal: mustParseDecimal(t, "42"), Status: pgtype.Present}, dst: &ui, expected: uint(42)}, + {src: &shopspring.Numeric{Decimal: mustParseDecimal(t, "42"), Status: pgtype.Present}, dst: &_i8, expected: _int8(42)}, + {src: &shopspring.Numeric{Status: pgtype.Null}, dst: &pi8, expected: ((*int8)(nil))}, + {src: &shopspring.Numeric{Status: pgtype.Null}, dst: &_pi8, expected: ((*_int8)(nil))}, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + pointerAllocTests := []struct { + src *shopspring.Numeric + dst interface{} + expected interface{} + }{ + {src: &shopspring.Numeric{Decimal: mustParseDecimal(t, "42"), Status: pgtype.Present}, dst: &pf32, expected: float32(42)}, + {src: &shopspring.Numeric{Decimal: mustParseDecimal(t, "42"), Status: pgtype.Present}, dst: &pf64, expected: float64(42)}, + } + + for i, tt := range pointerAllocTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src *shopspring.Numeric + dst interface{} + }{ + {src: &shopspring.Numeric{Decimal: mustParseDecimal(t, "150"), Status: pgtype.Present}, dst: &i8}, + {src: &shopspring.Numeric{Decimal: mustParseDecimal(t, "40000"), Status: pgtype.Present}, dst: &i16}, + {src: &shopspring.Numeric{Decimal: mustParseDecimal(t, "-1"), Status: pgtype.Present}, dst: &ui8}, + {src: &shopspring.Numeric{Decimal: mustParseDecimal(t, "-1"), Status: pgtype.Present}, dst: &ui16}, + {src: &shopspring.Numeric{Decimal: mustParseDecimal(t, "-1"), Status: pgtype.Present}, dst: &ui32}, + {src: &shopspring.Numeric{Decimal: mustParseDecimal(t, "-1"), Status: pgtype.Present}, dst: &ui64}, + {src: &shopspring.Numeric{Decimal: mustParseDecimal(t, "-1"), Status: pgtype.Present}, dst: &ui}, + {src: &shopspring.Numeric{Status: pgtype.Null}, dst: &i32}, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } +} + +func BenchmarkDecode(b *testing.B) { + benchmarks := []struct { + name string + numberStr string + }{ + {"Zero", "0"}, + {"Small", "12345"}, + {"Medium", "12345.12345"}, + {"Large", "123457890.1234567890"}, + {"Huge", "123457890123457890123457890.1234567890123457890123457890"}, + } + + for _, bm := range benchmarks { + src := &shopspring.Numeric{} + err := src.Set(bm.numberStr) + require.NoError(b, err) + textFormat, err := src.EncodeText(nil, nil) + require.NoError(b, err) + binaryFormat, err := src.EncodeBinary(nil, nil) + require.NoError(b, err) + + b.Run(fmt.Sprintf("%s-Text", bm.name), func(b *testing.B) { + dst := &shopspring.Numeric{} + for i := 0; i < b.N; i++ { + err := dst.DecodeText(nil, textFormat) + if err != nil { + b.Fatal(err) + } + } + }) + + b.Run(fmt.Sprintf("%s-Binary", bm.name), func(b *testing.B) { + dst := &shopspring.Numeric{} + for i := 0; i < b.N; i++ { + err := dst.DecodeBinary(nil, binaryFormat) + if err != nil { + b.Fatal(err) + } + } + }) + } +} diff --git a/float4_array_test.go b/float4_array_test.go new file mode 100644 index 00000000..db438999 --- /dev/null +++ b/float4_array_test.go @@ -0,0 +1,282 @@ +package pgtype_test + +import ( + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestFloat4ArrayTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "float4[]", []interface{}{ + &pgtype.Float4Array{ + Elements: nil, + Dimensions: nil, + Status: pgtype.Present, + }, + &pgtype.Float4Array{ + Elements: []pgtype.Float4{ + {Float: 1, Status: pgtype.Present}, + {Status: pgtype.Null}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.Float4Array{Status: pgtype.Null}, + &pgtype.Float4Array{ + Elements: []pgtype.Float4{ + {Float: 1, Status: pgtype.Present}, + {Float: 2, Status: pgtype.Present}, + {Float: 3, Status: pgtype.Present}, + {Float: 4, Status: pgtype.Present}, + {Status: pgtype.Null}, + {Float: 6, Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}, {Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.Float4Array{ + Elements: []pgtype.Float4{ + {Float: 1, Status: pgtype.Present}, + {Float: 2, Status: pgtype.Present}, + {Float: 3, Status: pgtype.Present}, + {Float: 4, Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{ + {Length: 2, LowerBound: 4}, + {Length: 2, LowerBound: 2}, + }, + Status: pgtype.Present, + }, + }) +} + +func TestFloat4ArraySet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.Float4Array + }{ + { + source: []float32{1}, + result: pgtype.Float4Array{ + Elements: []pgtype.Float4{{Float: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: (([]float32)(nil)), + result: pgtype.Float4Array{Status: pgtype.Null}, + }, + { + source: [][]float32{{1}, {2}}, + result: pgtype.Float4Array{ + Elements: []pgtype.Float4{{Float: 1, Status: pgtype.Present}, {Float: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [][][][]float32{{{{1, 2, 3}}}, {{{4, 5, 6}}}}, + result: pgtype.Float4Array{ + Elements: []pgtype.Float4{ + {Float: 1, Status: pgtype.Present}, + {Float: 2, Status: pgtype.Present}, + {Float: 3, Status: pgtype.Present}, + {Float: 4, Status: pgtype.Present}, + {Float: 5, Status: pgtype.Present}, + {Float: 6, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + { + source: [2][1]float32{{1}, {2}}, + result: pgtype.Float4Array{ + Elements: []pgtype.Float4{{Float: 1, Status: pgtype.Present}, {Float: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [2][1][1][3]float32{{{{1, 2, 3}}}, {{{4, 5, 6}}}}, + result: pgtype.Float4Array{ + Elements: []pgtype.Float4{ + {Float: 1, Status: pgtype.Present}, + {Float: 2, Status: pgtype.Present}, + {Float: 3, Status: pgtype.Present}, + {Float: 4, Status: pgtype.Present}, + {Float: 5, Status: pgtype.Present}, + {Float: 6, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + } + + for i, tt := range successfulTests { + var r pgtype.Float4Array + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !reflect.DeepEqual(r, tt.result) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestFloat4ArrayAssignTo(t *testing.T) { + var float32Slice []float32 + var namedFloat32Slice _float32Slice + var float32SliceDim2 [][]float32 + var float32SliceDim4 [][][][]float32 + var float32ArrayDim2 [2][1]float32 + var float32ArrayDim4 [2][1][1][3]float32 + + simpleTests := []struct { + src pgtype.Float4Array + dst interface{} + expected interface{} + }{ + { + src: pgtype.Float4Array{ + Elements: []pgtype.Float4{{Float: 1.23, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &float32Slice, + expected: []float32{1.23}, + }, + { + src: pgtype.Float4Array{ + Elements: []pgtype.Float4{{Float: 1.23, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &namedFloat32Slice, + expected: _float32Slice{1.23}, + }, + { + src: pgtype.Float4Array{Status: pgtype.Null}, + dst: &float32Slice, + expected: (([]float32)(nil)), + }, + { + src: pgtype.Float4Array{Status: pgtype.Present}, + dst: &float32Slice, + expected: []float32{}, + }, + { + src: pgtype.Float4Array{ + Elements: []pgtype.Float4{{Float: 1, Status: pgtype.Present}, {Float: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + expected: [][]float32{{1}, {2}}, + dst: &float32SliceDim2, + }, + { + src: pgtype.Float4Array{ + Elements: []pgtype.Float4{ + {Float: 1, Status: pgtype.Present}, + {Float: 2, Status: pgtype.Present}, + {Float: 3, Status: pgtype.Present}, + {Float: 4, Status: pgtype.Present}, + {Float: 5, Status: pgtype.Present}, + {Float: 6, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + expected: [][][][]float32{{{{1, 2, 3}}}, {{{4, 5, 6}}}}, + dst: &float32SliceDim4, + }, + { + src: pgtype.Float4Array{ + Elements: []pgtype.Float4{{Float: 1, Status: pgtype.Present}, {Float: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + expected: [2][1]float32{{1}, {2}}, + dst: &float32ArrayDim2, + }, + { + src: pgtype.Float4Array{ + Elements: []pgtype.Float4{ + {Float: 1, Status: pgtype.Present}, + {Float: 2, Status: pgtype.Present}, + {Float: 3, Status: pgtype.Present}, + {Float: 4, Status: pgtype.Present}, + {Float: 5, Status: pgtype.Present}, + {Float: 6, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + expected: [2][1][1][3]float32{{{{1, 2, 3}}}, {{{4, 5, 6}}}}, + dst: &float32ArrayDim4, + }, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.Float4Array + dst interface{} + }{ + { + src: pgtype.Float4Array{ + Elements: []pgtype.Float4{{Status: pgtype.Null}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &float32Slice, + }, + { + src: pgtype.Float4Array{ + Elements: []pgtype.Float4{{Float: 1, Status: pgtype.Present}, {Float: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &float32ArrayDim2, + }, + { + src: pgtype.Float4Array{ + Elements: []pgtype.Float4{{Float: 1, Status: pgtype.Present}, {Float: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &float32Slice, + }, + { + src: pgtype.Float4Array{ + Elements: []pgtype.Float4{{Float: 1, Status: pgtype.Present}, {Float: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &float32ArrayDim4, + }, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } + +} diff --git a/float4_test.go b/float4_test.go new file mode 100644 index 00000000..d2524cda --- /dev/null +++ b/float4_test.go @@ -0,0 +1,149 @@ +package pgtype_test + +import ( + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestFloat4Transcode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "float4", []interface{}{ + &pgtype.Float4{Float: -1, Status: pgtype.Present}, + &pgtype.Float4{Float: 0, Status: pgtype.Present}, + &pgtype.Float4{Float: 0.00001, Status: pgtype.Present}, + &pgtype.Float4{Float: 1, Status: pgtype.Present}, + &pgtype.Float4{Float: 9999.99, Status: pgtype.Present}, + &pgtype.Float4{Float: 0, Status: pgtype.Null}, + }) +} + +func TestFloat4Set(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.Float4 + }{ + {source: float32(1), result: pgtype.Float4{Float: 1, Status: pgtype.Present}}, + {source: float64(1), result: pgtype.Float4{Float: 1, Status: pgtype.Present}}, + {source: int8(1), result: pgtype.Float4{Float: 1, Status: pgtype.Present}}, + {source: int16(1), result: pgtype.Float4{Float: 1, Status: pgtype.Present}}, + {source: int32(1), result: pgtype.Float4{Float: 1, Status: pgtype.Present}}, + {source: int64(1), result: pgtype.Float4{Float: 1, Status: pgtype.Present}}, + {source: int8(-1), result: pgtype.Float4{Float: -1, Status: pgtype.Present}}, + {source: int16(-1), result: pgtype.Float4{Float: -1, Status: pgtype.Present}}, + {source: int32(-1), result: pgtype.Float4{Float: -1, Status: pgtype.Present}}, + {source: int64(-1), result: pgtype.Float4{Float: -1, Status: pgtype.Present}}, + {source: uint8(1), result: pgtype.Float4{Float: 1, Status: pgtype.Present}}, + {source: uint16(1), result: pgtype.Float4{Float: 1, Status: pgtype.Present}}, + {source: uint32(1), result: pgtype.Float4{Float: 1, Status: pgtype.Present}}, + {source: uint64(1), result: pgtype.Float4{Float: 1, Status: pgtype.Present}}, + {source: "1", result: pgtype.Float4{Float: 1, Status: pgtype.Present}}, + {source: _int8(1), result: pgtype.Float4{Float: 1, Status: pgtype.Present}}, + } + + for i, tt := range successfulTests { + var r pgtype.Float4 + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if r != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestFloat4AssignTo(t *testing.T) { + var i8 int8 + var i16 int16 + var i32 int32 + var i64 int64 + var i int + var ui8 uint8 + var ui16 uint16 + var ui32 uint32 + var ui64 uint64 + var ui uint + var pi8 *int8 + var _i8 _int8 + var _pi8 *_int8 + var f32 float32 + var f64 float64 + var pf32 *float32 + var pf64 *float64 + + simpleTests := []struct { + src pgtype.Float4 + dst interface{} + expected interface{} + }{ + {src: pgtype.Float4{Float: 42, Status: pgtype.Present}, dst: &f32, expected: float32(42)}, + {src: pgtype.Float4{Float: 42, Status: pgtype.Present}, dst: &f64, expected: float64(42)}, + {src: pgtype.Float4{Float: 42, Status: pgtype.Present}, dst: &i16, expected: int16(42)}, + {src: pgtype.Float4{Float: 42, Status: pgtype.Present}, dst: &i32, expected: int32(42)}, + {src: pgtype.Float4{Float: 42, Status: pgtype.Present}, dst: &i64, expected: int64(42)}, + {src: pgtype.Float4{Float: 42, Status: pgtype.Present}, dst: &i, expected: int(42)}, + {src: pgtype.Float4{Float: 42, Status: pgtype.Present}, dst: &ui8, expected: uint8(42)}, + {src: pgtype.Float4{Float: 42, Status: pgtype.Present}, dst: &ui16, expected: uint16(42)}, + {src: pgtype.Float4{Float: 42, Status: pgtype.Present}, dst: &ui32, expected: uint32(42)}, + {src: pgtype.Float4{Float: 42, Status: pgtype.Present}, dst: &ui64, expected: uint64(42)}, + {src: pgtype.Float4{Float: 42, Status: pgtype.Present}, dst: &ui, expected: uint(42)}, + {src: pgtype.Float4{Float: 42, Status: pgtype.Present}, dst: &_i8, expected: _int8(42)}, + {src: pgtype.Float4{Float: 0, Status: pgtype.Null}, dst: &pi8, expected: ((*int8)(nil))}, + {src: pgtype.Float4{Float: 0, Status: pgtype.Null}, dst: &_pi8, expected: ((*_int8)(nil))}, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + pointerAllocTests := []struct { + src pgtype.Float4 + dst interface{} + expected interface{} + }{ + {src: pgtype.Float4{Float: 42, Status: pgtype.Present}, dst: &pf32, expected: float32(42)}, + {src: pgtype.Float4{Float: 42, Status: pgtype.Present}, dst: &pf64, expected: float64(42)}, + } + + for i, tt := range pointerAllocTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.Float4 + dst interface{} + }{ + {src: pgtype.Float4{Float: 150, Status: pgtype.Present}, dst: &i8}, + {src: pgtype.Float4{Float: 40000, Status: pgtype.Present}, dst: &i16}, + {src: pgtype.Float4{Float: -1, Status: pgtype.Present}, dst: &ui8}, + {src: pgtype.Float4{Float: -1, Status: pgtype.Present}, dst: &ui16}, + {src: pgtype.Float4{Float: -1, Status: pgtype.Present}, dst: &ui32}, + {src: pgtype.Float4{Float: -1, Status: pgtype.Present}, dst: &ui64}, + {src: pgtype.Float4{Float: -1, Status: pgtype.Present}, dst: &ui}, + {src: pgtype.Float4{Float: 0, Status: pgtype.Null}, dst: &i32}, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } +} diff --git a/float8_array_test.go b/float8_array_test.go new file mode 100644 index 00000000..85cb8f43 --- /dev/null +++ b/float8_array_test.go @@ -0,0 +1,258 @@ +package pgtype_test + +import ( + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestFloat8ArrayTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "float8[]", []interface{}{ + &pgtype.Float8Array{ + Elements: nil, + Dimensions: nil, + Status: pgtype.Present, + }, + &pgtype.Float8Array{ + Elements: []pgtype.Float8{ + {Float: 1, Status: pgtype.Present}, + {Status: pgtype.Null}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.Float8Array{Status: pgtype.Null}, + &pgtype.Float8Array{ + Elements: []pgtype.Float8{ + {Float: 1, Status: pgtype.Present}, + {Float: 2, Status: pgtype.Present}, + {Float: 3, Status: pgtype.Present}, + {Float: 4, Status: pgtype.Present}, + {Status: pgtype.Null}, + {Float: 6, Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}, {Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.Float8Array{ + Elements: []pgtype.Float8{ + {Float: 1, Status: pgtype.Present}, + {Float: 2, Status: pgtype.Present}, + {Float: 3, Status: pgtype.Present}, + {Float: 4, Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{ + {Length: 2, LowerBound: 4}, + {Length: 2, LowerBound: 2}, + }, + Status: pgtype.Present, + }, + }) +} + +func TestFloat8ArraySet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.Float8Array + }{ + { + source: []float64{1}, + result: pgtype.Float8Array{ + Elements: []pgtype.Float8{{Float: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: (([]float64)(nil)), + result: pgtype.Float8Array{Status: pgtype.Null}, + }, + { + source: [][]float64{{1}, {2}}, + result: pgtype.Float8Array{ + Elements: []pgtype.Float8{{Float: 1, Status: pgtype.Present}, {Float: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [][][][]float64{{{{1, 2, 3}}}, {{{4, 5, 6}}}}, + result: pgtype.Float8Array{ + Elements: []pgtype.Float8{ + {Float: 1, Status: pgtype.Present}, + {Float: 2, Status: pgtype.Present}, + {Float: 3, Status: pgtype.Present}, + {Float: 4, Status: pgtype.Present}, + {Float: 5, Status: pgtype.Present}, + {Float: 6, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + } + + for i, tt := range successfulTests { + var r pgtype.Float8Array + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !reflect.DeepEqual(r, tt.result) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestFloat8ArrayAssignTo(t *testing.T) { + var float64Slice []float64 + var namedFloat64Slice _float64Slice + var float64SliceDim2 [][]float64 + var float64SliceDim4 [][][][]float64 + var float64ArrayDim2 [2][1]float64 + var float64ArrayDim4 [2][1][1][3]float64 + + simpleTests := []struct { + src pgtype.Float8Array + dst interface{} + expected interface{} + }{ + { + src: pgtype.Float8Array{ + Elements: []pgtype.Float8{{Float: 1.23, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &float64Slice, + expected: []float64{1.23}, + }, + { + src: pgtype.Float8Array{ + Elements: []pgtype.Float8{{Float: 1.23, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &namedFloat64Slice, + expected: _float64Slice{1.23}, + }, + { + src: pgtype.Float8Array{Status: pgtype.Null}, + dst: &float64Slice, + expected: (([]float64)(nil)), + }, + { + src: pgtype.Float8Array{Status: pgtype.Present}, + dst: &float64Slice, + expected: []float64{}, + }, + { + src: pgtype.Float8Array{ + Elements: []pgtype.Float8{{Float: 1, Status: pgtype.Present}, {Float: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + expected: [][]float64{{1}, {2}}, + dst: &float64SliceDim2, + }, + { + src: pgtype.Float8Array{ + Elements: []pgtype.Float8{ + {Float: 1, Status: pgtype.Present}, + {Float: 2, Status: pgtype.Present}, + {Float: 3, Status: pgtype.Present}, + {Float: 4, Status: pgtype.Present}, + {Float: 5, Status: pgtype.Present}, + {Float: 6, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + expected: [][][][]float64{{{{1, 2, 3}}}, {{{4, 5, 6}}}}, + dst: &float64SliceDim4, + }, + { + src: pgtype.Float8Array{ + Elements: []pgtype.Float8{{Float: 1, Status: pgtype.Present}, {Float: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + expected: [2][1]float64{{1}, {2}}, + dst: &float64ArrayDim2, + }, + { + src: pgtype.Float8Array{ + Elements: []pgtype.Float8{ + {Float: 1, Status: pgtype.Present}, + {Float: 2, Status: pgtype.Present}, + {Float: 3, Status: pgtype.Present}, + {Float: 4, Status: pgtype.Present}, + {Float: 5, Status: pgtype.Present}, + {Float: 6, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + expected: [2][1][1][3]float64{{{{1, 2, 3}}}, {{{4, 5, 6}}}}, + dst: &float64ArrayDim4, + }, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.Float8Array + dst interface{} + }{ + { + src: pgtype.Float8Array{ + Elements: []pgtype.Float8{{Status: pgtype.Null}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &float64Slice, + }, + { + src: pgtype.Float8Array{ + Elements: []pgtype.Float8{{Float: 1, Status: pgtype.Present}, {Float: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &float64ArrayDim2, + }, + { + src: pgtype.Float8Array{ + Elements: []pgtype.Float8{{Float: 1, Status: pgtype.Present}, {Float: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &float64Slice, + }, + { + src: pgtype.Float8Array{ + Elements: []pgtype.Float8{{Float: 1, Status: pgtype.Present}, {Float: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &float64ArrayDim4, + }, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } + +} diff --git a/float8_test.go b/float8_test.go new file mode 100644 index 00000000..6bc7c652 --- /dev/null +++ b/float8_test.go @@ -0,0 +1,149 @@ +package pgtype_test + +import ( + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestFloat8Transcode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "float8", []interface{}{ + &pgtype.Float8{Float: -1, Status: pgtype.Present}, + &pgtype.Float8{Float: 0, Status: pgtype.Present}, + &pgtype.Float8{Float: 0.00001, Status: pgtype.Present}, + &pgtype.Float8{Float: 1, Status: pgtype.Present}, + &pgtype.Float8{Float: 9999.99, Status: pgtype.Present}, + &pgtype.Float8{Float: 0, Status: pgtype.Null}, + }) +} + +func TestFloat8Set(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.Float8 + }{ + {source: float32(1), result: pgtype.Float8{Float: 1, Status: pgtype.Present}}, + {source: float64(1), result: pgtype.Float8{Float: 1, Status: pgtype.Present}}, + {source: int8(1), result: pgtype.Float8{Float: 1, Status: pgtype.Present}}, + {source: int16(1), result: pgtype.Float8{Float: 1, Status: pgtype.Present}}, + {source: int32(1), result: pgtype.Float8{Float: 1, Status: pgtype.Present}}, + {source: int64(1), result: pgtype.Float8{Float: 1, Status: pgtype.Present}}, + {source: int8(-1), result: pgtype.Float8{Float: -1, Status: pgtype.Present}}, + {source: int16(-1), result: pgtype.Float8{Float: -1, Status: pgtype.Present}}, + {source: int32(-1), result: pgtype.Float8{Float: -1, Status: pgtype.Present}}, + {source: int64(-1), result: pgtype.Float8{Float: -1, Status: pgtype.Present}}, + {source: uint8(1), result: pgtype.Float8{Float: 1, Status: pgtype.Present}}, + {source: uint16(1), result: pgtype.Float8{Float: 1, Status: pgtype.Present}}, + {source: uint32(1), result: pgtype.Float8{Float: 1, Status: pgtype.Present}}, + {source: uint64(1), result: pgtype.Float8{Float: 1, Status: pgtype.Present}}, + {source: "1", result: pgtype.Float8{Float: 1, Status: pgtype.Present}}, + {source: _int8(1), result: pgtype.Float8{Float: 1, Status: pgtype.Present}}, + } + + for i, tt := range successfulTests { + var r pgtype.Float8 + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if r != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestFloat8AssignTo(t *testing.T) { + var i8 int8 + var i16 int16 + var i32 int32 + var i64 int64 + var i int + var ui8 uint8 + var ui16 uint16 + var ui32 uint32 + var ui64 uint64 + var ui uint + var pi8 *int8 + var _i8 _int8 + var _pi8 *_int8 + var f32 float32 + var f64 float64 + var pf32 *float32 + var pf64 *float64 + + simpleTests := []struct { + src pgtype.Float8 + dst interface{} + expected interface{} + }{ + {src: pgtype.Float8{Float: 42, Status: pgtype.Present}, dst: &f32, expected: float32(42)}, + {src: pgtype.Float8{Float: 42, Status: pgtype.Present}, dst: &f64, expected: float64(42)}, + {src: pgtype.Float8{Float: 42, Status: pgtype.Present}, dst: &i16, expected: int16(42)}, + {src: pgtype.Float8{Float: 42, Status: pgtype.Present}, dst: &i32, expected: int32(42)}, + {src: pgtype.Float8{Float: 42, Status: pgtype.Present}, dst: &i64, expected: int64(42)}, + {src: pgtype.Float8{Float: 42, Status: pgtype.Present}, dst: &i, expected: int(42)}, + {src: pgtype.Float8{Float: 42, Status: pgtype.Present}, dst: &ui8, expected: uint8(42)}, + {src: pgtype.Float8{Float: 42, Status: pgtype.Present}, dst: &ui16, expected: uint16(42)}, + {src: pgtype.Float8{Float: 42, Status: pgtype.Present}, dst: &ui32, expected: uint32(42)}, + {src: pgtype.Float8{Float: 42, Status: pgtype.Present}, dst: &ui64, expected: uint64(42)}, + {src: pgtype.Float8{Float: 42, Status: pgtype.Present}, dst: &ui, expected: uint(42)}, + {src: pgtype.Float8{Float: 42, Status: pgtype.Present}, dst: &_i8, expected: _int8(42)}, + {src: pgtype.Float8{Float: 0, Status: pgtype.Null}, dst: &pi8, expected: ((*int8)(nil))}, + {src: pgtype.Float8{Float: 0, Status: pgtype.Null}, dst: &_pi8, expected: ((*_int8)(nil))}, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + pointerAllocTests := []struct { + src pgtype.Float8 + dst interface{} + expected interface{} + }{ + {src: pgtype.Float8{Float: 42, Status: pgtype.Present}, dst: &pf32, expected: float32(42)}, + {src: pgtype.Float8{Float: 42, Status: pgtype.Present}, dst: &pf64, expected: float64(42)}, + } + + for i, tt := range pointerAllocTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.Float8 + dst interface{} + }{ + {src: pgtype.Float8{Float: 150, Status: pgtype.Present}, dst: &i8}, + {src: pgtype.Float8{Float: 40000, Status: pgtype.Present}, dst: &i16}, + {src: pgtype.Float8{Float: -1, Status: pgtype.Present}, dst: &ui8}, + {src: pgtype.Float8{Float: -1, Status: pgtype.Present}, dst: &ui16}, + {src: pgtype.Float8{Float: -1, Status: pgtype.Present}, dst: &ui32}, + {src: pgtype.Float8{Float: -1, Status: pgtype.Present}, dst: &ui64}, + {src: pgtype.Float8{Float: -1, Status: pgtype.Present}, dst: &ui}, + {src: pgtype.Float8{Float: 0, Status: pgtype.Null}, dst: &i32}, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } +} diff --git a/go.mod b/go.mod index 42ee3838..29e6f628 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,10 @@ go 1.13 require ( github.com/gofrs/uuid v4.0.0+incompatible + github.com/jackc/pgconn v1.9.0 github.com/jackc/pgio v1.0.0 + github.com/jackc/pgx/v4 v4.12.0 + github.com/lib/pq v1.10.2 github.com/shopspring/decimal v1.2.0 + github.com/stretchr/testify v1.7.0 ) diff --git a/go.sum b/go.sum index da822c7d..e49ce26f 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,486 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0= +github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc= +github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= +github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo= +github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI= +github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g= +github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c= +github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ= +github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ= +github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= +github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= +github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= +github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a/go.mod h1:DAHtR1m6lCRdSC2Tm3DSWRPvIPr6xNKyeHdqDQSQT+A= +github.com/aws/aws-lambda-go v1.13.3/go.mod h1:4UKl9IzQMoD+QF79YdCuzCwp8VbmG4VAQwij/eHl5CU= +github.com/aws/aws-sdk-go v1.27.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= +github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g= +github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= +github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= +github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= +github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ= +github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/clbanning/x2j v0.0.0-20191024224557-825249438eec/go.mod h1:jMjuTZXRI4dUb/I5gc9Hdhagfvm9+RyrPryS/auMzxE= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I= +github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ= +github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8= +github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI= +github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= +github.com/coreos/go-systemd v0.0.0-20180511133405-39ca1b05acc7/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= +github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= +github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= +github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= +github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs= +github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU= +github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I= +github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M= +github.com/envoyproxy/go-control-plane v0.6.9/go.mod h1:SBwIajubJHhxtWwsL9s8ss4safvEdbitLhGGK48rN6g= +github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= +github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db/go.mod h1:7dvUGVsVBjqR7JHJk0brhHOZYGmfBYOrK0ZhYMEtBr4= +github.com/franela/goreq v0.0.0-20171204163338-bcd34c9993f8/go.mod h1:ZhphrRTfi2rbfLwlschooIH4+wKKDR4Pdxhh+TRoA20= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-kit/kit v0.10.0/go.mod h1:xUsJbQ/Fp4kEt7AFgCuvyX4a71u8h9jB8tj/ORgOZ7o= +github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= +github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= +github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= +github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw= github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= +github.com/gogo/googleapis v1.1.0/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s= +github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= +github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= +github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= +github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg= +github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= +github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= +github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= +github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= +github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= +github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= +github.com/hashicorp/consul/api v1.3.0/go.mod h1:MmDNSzIMUjNpY/mQ398R4bk2FnqQLoPndWW5VkKPlCE= +github.com/hashicorp/consul/sdk v0.3.0/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8= +github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= +github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= +github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM= +github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk= +github.com/hashicorp/go-rootcerts v1.0.0/go.mod h1:K6zTfqpRlCUIjkwsN4Z+hiSfzSTQa6eBIzfwKfwNnHU= +github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU= +github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4= +github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= +github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90= +github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= +github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ= +github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I= +github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo= +github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0= +github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo= +github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk= +github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8= +github.com/jackc/chunkreader/v2 v2.0.1/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk= +github.com/jackc/pgconn v0.0.0-20190420214824-7e0022ef6ba3/go.mod h1:jkELnwuX+w9qN5YIfX0fl88Ehu4XC3keFuOJJk9pcnA= +github.com/jackc/pgconn v0.0.0-20190824142844-760dd75542eb/go.mod h1:lLjNuW/+OfW9/pnVKPazfWOgNfH2aPem8YQ7ilXGvJE= +github.com/jackc/pgconn v0.0.0-20190831204454-2fabfa3c18b7/go.mod h1:ZJKsE/KZfsUgOEh9hBm+xYTstcNHg7UPMVJqRfQxq4s= +github.com/jackc/pgconn v1.4.0/go.mod h1:Y2O3ZDF0q4mMacyWV3AstPJpeHXWGEetiFttmq5lahk= +github.com/jackc/pgconn v1.5.0/go.mod h1:QeD3lBfpTFe8WUnPZWN5KY/mB8FGMIYRdd8P8Jr0fAI= +github.com/jackc/pgconn v1.5.1-0.20200601181101-fa742c524853/go.mod h1:QeD3lBfpTFe8WUnPZWN5KY/mB8FGMIYRdd8P8Jr0fAI= +github.com/jackc/pgconn v1.8.0/go.mod h1:1C2Pb36bGIP9QHGBYCjnyhqu7Rv3sGshaQUvmfGIB/o= +github.com/jackc/pgconn v1.8.1/go.mod h1:JV6m6b6jhjdmzchES0drzCcYcAHS1OPD5xu3OZ/lE2g= +github.com/jackc/pgconn v1.9.0 h1:gqibKSTJup/ahCsNKyMZAniPuZEfIqfXFc8FOWVYR+Q= +github.com/jackc/pgconn v1.9.0/go.mod h1:YctiPyvzfU11JFxoXokUOOKQXQmDMoJL9vJzHH8/2JY= github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE= github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8= +github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2/go.mod h1:fGZlG77KXmcq05nJLRkk0+p82V8B8Dw8KN2/V9c/OAE= +github.com/jackc/pgmock v0.0.0-20201204152224-4fe30f7445fd h1:eDErF6V/JPJON/B7s68BxwHgfmyOntHJQ8IOaz0x4R8= +github.com/jackc/pgmock v0.0.0-20201204152224-4fe30f7445fd/go.mod h1:hrBW0Enj2AZTNpt/7Y5rr2xe/9Mn757Wtb2xeBzPv2c= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgproto3 v1.1.0 h1:FYYE4yRw+AgI8wXIinMlNjBbp/UitDJwfj5LqqewP1A= +github.com/jackc/pgproto3 v1.1.0/go.mod h1:eR5FA3leWg7p9aeAqi37XOTgTIbkABlvcPB3E5rlc78= +github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190420180111-c116219b62db/go.mod h1:bhq50y+xrl9n5mRYyCBFKkpRVTLYJVWeCc+mEAI3yXA= +github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190609003834-432c2951c711/go.mod h1:uH0AWtUmuShn0bcesswc4aBTWGvw0cAxIJp+6OB//Wg= +github.com/jackc/pgproto3/v2 v2.0.0-rc3/go.mod h1:ryONWYqW6dqSg1Lw6vXNMXoBJhpzvWKnT95C46ckYeM= +github.com/jackc/pgproto3/v2 v2.0.0-rc3.0.20190831210041-4c03ce451f29/go.mod h1:ryONWYqW6dqSg1Lw6vXNMXoBJhpzvWKnT95C46ckYeM= +github.com/jackc/pgproto3/v2 v2.0.1/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= +github.com/jackc/pgproto3/v2 v2.0.6/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= +github.com/jackc/pgproto3/v2 v2.1.1 h1:7PQ/4gLoqnl87ZxL7xjO0DR5gYuviDCZxQJsUlFW1eI= +github.com/jackc/pgproto3/v2 v2.1.1/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= +github.com/jackc/pgservicefile v0.0.0-20200307190119-3430c5407db8/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E= +github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b h1:C8S2+VttkHFdOOCXJe+YGfa4vHYwlt4Zx+IVXQ97jYg= +github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E= +github.com/jackc/pgtype v0.0.0-20190421001408-4ed0de4755e0/go.mod h1:hdSHsc1V01CGwFsrv11mJRHWJ6aifDLfdV3aVjFF0zg= +github.com/jackc/pgtype v0.0.0-20190824184912-ab885b375b90/go.mod h1:KcahbBH1nCMSo2DXpzsoWOAfFkdEtEJpPbVLq8eE+mc= +github.com/jackc/pgtype v0.0.0-20190828014616-a8802b16cc59/go.mod h1:MWlu30kVJrUS8lot6TQqcg7mtthZ9T0EoIBFiJcmcyw= +github.com/jackc/pgtype v1.2.0/go.mod h1:5m2OfMh1wTK7x+Fk952IDmI4nw3nPrvtQdM0ZT4WpC0= +github.com/jackc/pgtype v1.3.1-0.20200510190516-8cd94a14c75a/go.mod h1:vaogEUkALtxZMCH411K+tKzNpwzCKU+AnPzBKZ+I+Po= +github.com/jackc/pgtype v1.3.1-0.20200606141011-f6355165a91c/go.mod h1:cvk9Bgu/VzJ9/lxTO5R5sf80p0DiucVtN7ZxvaC4GmQ= +github.com/jackc/pgtype v1.7.0/go.mod h1:ZnHF+rMePVqDKaOfJVI4Q8IVvAQMryDlDkZnKOI75BE= +github.com/jackc/pgtype v1.8.0/go.mod h1:PqDKcEBtllAtk/2p6z6SHdXW5UB+MhE75tUol2OKexE= +github.com/jackc/pgx/v4 v4.0.0-20190420224344-cc3461e65d96/go.mod h1:mdxmSJJuR08CZQyj1PVQBHy9XOp5p8/SHH6a0psbY9Y= +github.com/jackc/pgx/v4 v4.0.0-20190421002000-1b8f0016e912/go.mod h1:no/Y67Jkk/9WuGR0JG/JseM9irFbnEPbuWV2EELPNuM= +github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQnOEnf1dqHGpw7JmHqHc1NxDoalibchSk9/RWuDc= +github.com/jackc/pgx/v4 v4.5.0/go.mod h1:EpAKPLdnTorwmPUUsqrPxy5fphV18j9q3wrfRXgo+kA= +github.com/jackc/pgx/v4 v4.6.1-0.20200510190926-94ba730bb1e9/go.mod h1:t3/cdRQl6fOLDxqtlyhe9UWgfIi9R8+8v8GKV5TRA/o= +github.com/jackc/pgx/v4 v4.6.1-0.20200606145419-4e5062306904/go.mod h1:ZDaNWkt9sW1JMiNn0kdYBaLelIhw7Pg4qd+Vk6tw7Hg= +github.com/jackc/pgx/v4 v4.11.0/go.mod h1:i62xJgdrtVDsnL3U8ekyrQXEwGNTRoG7/8r+CIdYfcc= +github.com/jackc/pgx/v4 v4.12.0 h1:xiP3TdnkwyslWNp77yE5XAPfxAsU9RMFDe0c1SwN8h4= +github.com/jackc/pgx/v4 v4.12.0/go.mod h1:fE547h6VulLPA3kySjfnSG/e2D861g/50JlVUa/ub60= +github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= +github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= +github.com/jackc/puddle v1.1.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= +github.com/jackc/puddle v1.1.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= +github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= +github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= +github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= +github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/json-iterator/go v1.1.8/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= +github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= +github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8= +github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM= +github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4= +github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ= +github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= +github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= +github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= +github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= +github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= +github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= +github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= +github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI= +github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg= +github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY= +github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/nats-io/jwt v0.3.0/go.mod h1:fRYCDE99xlTsqUzISS1Bi75UBJ6ljOJQOAAu5VglpSg= +github.com/nats-io/jwt v0.3.2/go.mod h1:/euKqTS1ZD+zzjYrY7pseZrTtWQSjujC7xjPc8wL6eU= +github.com/nats-io/nats-server/v2 v2.1.2/go.mod h1:Afk+wRZqkMQs/p45uXdrVLuab3gwv3Z8C4HTBu8GD/k= +github.com/nats-io/nats.go v1.9.1/go.mod h1:ZjDU1L/7fJ09jvUSRVBR2e7+RnLiiIQyqyzEE/Zbp4w= +github.com/nats-io/nkeys v0.1.0/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w= +github.com/nats-io/nkeys v0.1.3/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w= +github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= +github.com/oklog/oklog v0.3.2/go.mod h1:FCV+B7mhrz4o+ueLpx+KqkyXRGMWOYEvfiXtdGtbWGs= +github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= +github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= +github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk= +github.com/opentracing-contrib/go-observer v0.0.0-20170622124052-a52f23424492/go.mod h1:Ngi6UdF0k5OKD5t5wlmGhe/EDKPoUM3BXZSSfIuJbis= +github.com/opentracing/basictracer-go v1.0.0/go.mod h1:QfBfYuafItcjQuMwinw9GhYKwFXS9KnPs5lxoYwgW74= +github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= +github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= +github.com/openzipkin-contrib/zipkin-go-opentracing v0.4.5/go.mod h1:/wsWhb9smxSfWAKL3wpBW7V8scJMt8N8gnaMCS9E/cA= +github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw= +github.com/openzipkin/zipkin-go v0.2.1/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnhQw8ySjnjRyN4= +github.com/openzipkin/zipkin-go v0.2.2/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnhQw8ySjnjRyN4= +github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM= +github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= +github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k= +github.com/performancecopilot/speed v3.0.0+incompatible/go.mod h1:/CLtqpZ5gBg1M9iaPbIdPPGyKcA8hKdoy6hAWba7Yac= +github.com/pierrec/lz4 v1.0.2-0.20190131084431-473cd7ce01a1/go.mod h1:3/3N9NVKO0jef7pBehbT1qWhCMrIgbYNnFAZCqQ5LRc= +github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= +github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= +github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829/go.mod h1:p2iRAGwDERtqlqzRXnrOVns+ignqQo//hLXqYxZYVNs= +github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= +github.com/prometheus/client_golang v1.3.0/go.mod h1:hJaj2vgQTGQmVCsAACORcieXFeDPbaTKGT+JTgUa3og= +github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= +github.com/prometheus/client_model v0.0.0-20190115171406-56726106282f/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= +github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/client_model v0.1.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/common v0.2.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= +github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= +github.com/prometheus/common v0.7.0/go.mod h1:DjGbpBbp5NYNiECxcL/VnbXCCaQpKd3tt26CguLLsqA= +github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= +github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= +github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= +github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A= +github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= +github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= +github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= +github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU= +github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= +github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E= +github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= +github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= +github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4= +github.com/shopspring/decimal v0.0.0-20200227202807-02e2044944cc/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ= github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= +github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= +github.com/sony/gobreaker v0.4.1/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJOjmxWY= +github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= +github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw= +github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw= +github.com/streadway/handy v0.0.0-20190108123426-d5acb3125c2a/go.mod h1:qNTQ5P5JnDBl6z3cMAg/SywNDC5ABu5ApDIw6lUbRmI= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= +github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= +github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= +github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= +github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q= +go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= +go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738/go.mod h1:dnLIgRNXwCJa5e+c6mIZCrds/GIG4ncV9HhK5PX7jPg= +go.opencensus.io v0.20.1/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk= +go.opencensus.io v0.20.2/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk= +go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= +go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= +go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= +go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= +go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= +go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= +go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4= +go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= +go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA= +go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= +go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= +go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM= +golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE= +golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= +golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= +golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e h1:gsTQYXdTw2Gq7RBsWvlQ91b+aEQ6bXFUngBGuR8sPpI= +golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= +golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181023162649-9b4f9f5ad519/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= +golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181026203630-95b1ffbd15a5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191220142924-d4481acd189f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/tools v0.0.0-20190823170909-c4a336ef6a2f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200103221440-774c71fcf114/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/api v0.3.1/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.2.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/genproto v0.0.0-20190530194941-fb225487d101/go.mod h1:z3L6/3dTEVtUr6QSP8miRzeRqwQOioJ9I66odjN4I7s= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.20.0/go.mod h1:chYK+tFQF0nDUGJgXMSgLCQk3phJEuONr2DCgLDdAQM= +google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= +google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= +google.golang.org/grpc v1.22.1/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.23.1/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/cheggaaa/pb.v1 v1.0.25/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/gcfg.v1 v1.2.3/go.mod h1:yesOnuUOFQAhST5vPY4nbZsb/huCgGGXlipJsBn0b3o= +gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s= +gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= +gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= +sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o= +sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0/go.mod h1:hI742Nqp5OhwiqlzhgfbWU4mW4yO10fP+LoT9WOswdU= diff --git a/hstore_array_test.go b/hstore_array_test.go new file mode 100644 index 00000000..672eca4a --- /dev/null +++ b/hstore_array_test.go @@ -0,0 +1,436 @@ +package pgtype_test + +import ( + "context" + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" + "github.com/jackc/pgx/v4" +) + +func TestHstoreArrayTranscode(t *testing.T) { + conn := testutil.MustConnectPgx(t) + defer testutil.MustCloseContext(t, conn) + + var hstoreOID uint32 + err := conn.QueryRow(context.Background(), "select t.oid from pg_type t where t.typname='hstore';").Scan(&hstoreOID) + if err != nil { + t.Fatalf("did not find hstore OID, %v", err) + } + conn.ConnInfo().RegisterDataType(pgtype.DataType{Value: &pgtype.Hstore{}, Name: "hstore", OID: hstoreOID}) + + var hstoreArrayOID uint32 + err = conn.QueryRow(context.Background(), "select t.oid from pg_type t where t.typname='_hstore';").Scan(&hstoreArrayOID) + if err != nil { + t.Fatalf("did not find _hstore OID, %v", err) + } + conn.ConnInfo().RegisterDataType(pgtype.DataType{Value: &pgtype.HstoreArray{}, Name: "_hstore", OID: hstoreArrayOID}) + + text := func(s string) pgtype.Text { + return pgtype.Text{String: s, Status: pgtype.Present} + } + + values := []pgtype.Hstore{ + {Map: map[string]pgtype.Text{}, Status: pgtype.Present}, + {Map: map[string]pgtype.Text{"foo": text("bar")}, Status: pgtype.Present}, + {Map: map[string]pgtype.Text{"foo": text("bar"), "baz": text("quz")}, Status: pgtype.Present}, + {Map: map[string]pgtype.Text{"NULL": text("bar")}, Status: pgtype.Present}, + {Map: map[string]pgtype.Text{"foo": text("NULL")}, Status: pgtype.Present}, + {Status: pgtype.Null}, + } + + specialStrings := []string{ + `"`, + `'`, + `\`, + `\\`, + `=>`, + ` `, + `\ / / \\ => " ' " '`, + } + for _, s := range specialStrings { + // Special key values + values = append(values, pgtype.Hstore{Map: map[string]pgtype.Text{s + "foo": text("bar")}, Status: pgtype.Present}) // at beginning + values = append(values, pgtype.Hstore{Map: map[string]pgtype.Text{"foo" + s + "bar": text("bar")}, Status: pgtype.Present}) // in middle + values = append(values, pgtype.Hstore{Map: map[string]pgtype.Text{"foo" + s: text("bar")}, Status: pgtype.Present}) // at end + values = append(values, pgtype.Hstore{Map: map[string]pgtype.Text{s: text("bar")}, Status: pgtype.Present}) // is key + + // Special value values + values = append(values, pgtype.Hstore{Map: map[string]pgtype.Text{"foo": text(s + "bar")}, Status: pgtype.Present}) // at beginning + values = append(values, pgtype.Hstore{Map: map[string]pgtype.Text{"foo": text("foo" + s + "bar")}, Status: pgtype.Present}) // in middle + values = append(values, pgtype.Hstore{Map: map[string]pgtype.Text{"foo": text("foo" + s)}, Status: pgtype.Present}) // at end + values = append(values, pgtype.Hstore{Map: map[string]pgtype.Text{"foo": text(s)}, Status: pgtype.Present}) // is key + } + + src := &pgtype.HstoreArray{ + Elements: values, + Dimensions: []pgtype.ArrayDimension{{Length: int32(len(values)), LowerBound: 1}}, + Status: pgtype.Present, + } + + _, err = conn.Prepare(context.Background(), "test", "select $1::hstore[]") + if err != nil { + t.Fatal(err) + } + + formats := []struct { + name string + formatCode int16 + }{ + {name: "TextFormat", formatCode: pgx.TextFormatCode}, + {name: "BinaryFormat", formatCode: pgx.BinaryFormatCode}, + } + + for _, fc := range formats { + queryResultFormats := pgx.QueryResultFormats{fc.formatCode} + vEncoder := testutil.ForceEncoder(src, fc.formatCode) + if vEncoder == nil { + t.Logf("%#v does not implement %v", src, fc.name) + continue + } + + var result pgtype.HstoreArray + err := conn.QueryRow(context.Background(), "test", queryResultFormats, vEncoder).Scan(&result) + if err != nil { + t.Errorf("%v: %v", fc.name, err) + continue + } + + if result.Status != src.Status { + t.Errorf("%v: expected Status %v, got %v", fc.formatCode, src.Status, result.Status) + continue + } + + if len(result.Elements) != len(src.Elements) { + t.Errorf("%v: expected %v elements, got %v", fc.formatCode, len(src.Elements), len(result.Elements)) + continue + } + + for i := range result.Elements { + a := src.Elements[i] + b := result.Elements[i] + + if a.Status != b.Status { + t.Errorf("%v element idx %d: expected status %v, got %v", fc.formatCode, i, a.Status, b.Status) + } + + if len(a.Map) != len(b.Map) { + t.Errorf("%v element idx %d: expected %v pairs, got %v", fc.formatCode, i, len(a.Map), len(b.Map)) + } + + for k := range a.Map { + if a.Map[k] != b.Map[k] { + t.Errorf("%v element idx %d: expected key %v to be %v, got %v", fc.formatCode, i, k, a.Map[k], b.Map[k]) + } + } + } + } +} + +func TestHstoreArraySet(t *testing.T) { + successfulTests := []struct { + src interface{} + result pgtype.HstoreArray + }{ + { + src: []map[string]string{{"foo": "bar"}}, + result: pgtype.HstoreArray{ + Elements: []pgtype.Hstore{ + { + Map: map[string]pgtype.Text{"foo": {String: "bar", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + }, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + }, + { + src: [][]map[string]string{{{"foo": "bar"}}, {{"baz": "quz"}}}, + result: pgtype.HstoreArray{ + Elements: []pgtype.Hstore{ + { + Map: map[string]pgtype.Text{"foo": {String: "bar", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + { + Map: map[string]pgtype.Text{"baz": {String: "quz", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + }, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + }, + { + src: [][][][]map[string]string{ + {{{{"foo": "bar"}, {"baz": "quz"}, {"bar": "baz"}}}}, + {{{{"wibble": "wobble"}, {"wubble": "wabble"}, {"wabble": "wobble"}}}}}, + result: pgtype.HstoreArray{ + Elements: []pgtype.Hstore{ + { + Map: map[string]pgtype.Text{"foo": {String: "bar", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + { + Map: map[string]pgtype.Text{"baz": {String: "quz", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + { + Map: map[string]pgtype.Text{"bar": {String: "baz", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + { + Map: map[string]pgtype.Text{"wibble": {String: "wobble", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + { + Map: map[string]pgtype.Text{"wubble": {String: "wabble", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + { + Map: map[string]pgtype.Text{"wabble": {String: "wobble", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + }, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present, + }, + }, + { + src: [2][1]map[string]string{{{"foo": "bar"}}, {{"baz": "quz"}}}, + result: pgtype.HstoreArray{ + Elements: []pgtype.Hstore{ + { + Map: map[string]pgtype.Text{"foo": {String: "bar", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + { + Map: map[string]pgtype.Text{"baz": {String: "quz", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + }, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + }, + { + src: [2][1][1][3]map[string]string{ + {{{{"foo": "bar"}, {"baz": "quz"}, {"bar": "baz"}}}}, + {{{{"wibble": "wobble"}, {"wubble": "wabble"}, {"wabble": "wobble"}}}}}, + result: pgtype.HstoreArray{ + Elements: []pgtype.Hstore{ + { + Map: map[string]pgtype.Text{"foo": {String: "bar", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + { + Map: map[string]pgtype.Text{"baz": {String: "quz", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + { + Map: map[string]pgtype.Text{"bar": {String: "baz", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + { + Map: map[string]pgtype.Text{"wibble": {String: "wobble", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + { + Map: map[string]pgtype.Text{"wubble": {String: "wabble", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + { + Map: map[string]pgtype.Text{"wabble": {String: "wobble", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + }, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present, + }, + }, + } + + for i, tt := range successfulTests { + var dst pgtype.HstoreArray + err := dst.Set(tt.src) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !reflect.DeepEqual(dst, tt.result) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.src, tt.result, dst) + } + } +} + +func TestHstoreArrayAssignTo(t *testing.T) { + var hstoreSlice []map[string]string + var hstoreSliceDim2 [][]map[string]string + var hstoreSliceDim4 [][][][]map[string]string + var hstoreArrayDim2 [2][1]map[string]string + var hstoreArrayDim4 [2][1][1][3]map[string]string + + simpleTests := []struct { + src pgtype.HstoreArray + dst interface{} + expected interface{} + }{ + { + src: pgtype.HstoreArray{ + Elements: []pgtype.Hstore{ + { + Map: map[string]pgtype.Text{"foo": {String: "bar", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + }, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &hstoreSlice, + expected: []map[string]string{{"foo": "bar"}}}, + { + src: pgtype.HstoreArray{Status: pgtype.Null}, dst: &hstoreSlice, expected: (([]map[string]string)(nil)), + }, + { + src: pgtype.HstoreArray{Status: pgtype.Present}, dst: &hstoreSlice, expected: []map[string]string{}, + }, + { + src: pgtype.HstoreArray{ + Elements: []pgtype.Hstore{ + { + Map: map[string]pgtype.Text{"foo": {String: "bar", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + { + Map: map[string]pgtype.Text{"baz": {String: "quz", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + }, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &hstoreSliceDim2, + expected: [][]map[string]string{{{"foo": "bar"}}, {{"baz": "quz"}}}, + }, + { + src: pgtype.HstoreArray{ + Elements: []pgtype.Hstore{ + { + Map: map[string]pgtype.Text{"foo": {String: "bar", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + { + Map: map[string]pgtype.Text{"baz": {String: "quz", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + { + Map: map[string]pgtype.Text{"bar": {String: "baz", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + { + Map: map[string]pgtype.Text{"wibble": {String: "wobble", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + { + Map: map[string]pgtype.Text{"wubble": {String: "wabble", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + { + Map: map[string]pgtype.Text{"wabble": {String: "wobble", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + }, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present, + }, + dst: &hstoreSliceDim4, + expected: [][][][]map[string]string{ + {{{{"foo": "bar"}, {"baz": "quz"}, {"bar": "baz"}}}}, + {{{{"wibble": "wobble"}, {"wubble": "wabble"}, {"wabble": "wobble"}}}}}, + }, + { + src: pgtype.HstoreArray{ + Elements: []pgtype.Hstore{ + { + Map: map[string]pgtype.Text{"foo": {String: "bar", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + { + Map: map[string]pgtype.Text{"baz": {String: "quz", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + }, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &hstoreArrayDim2, + expected: [2][1]map[string]string{{{"foo": "bar"}}, {{"baz": "quz"}}}, + }, + { + src: pgtype.HstoreArray{ + Elements: []pgtype.Hstore{ + { + Map: map[string]pgtype.Text{"foo": {String: "bar", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + { + Map: map[string]pgtype.Text{"baz": {String: "quz", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + { + Map: map[string]pgtype.Text{"bar": {String: "baz", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + { + Map: map[string]pgtype.Text{"wibble": {String: "wobble", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + { + Map: map[string]pgtype.Text{"wubble": {String: "wabble", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + { + Map: map[string]pgtype.Text{"wabble": {String: "wobble", Status: pgtype.Present}}, + Status: pgtype.Present, + }, + }, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present, + }, + dst: &hstoreArrayDim4, + expected: [2][1][1][3]map[string]string{ + {{{{"foo": "bar"}, {"baz": "quz"}, {"bar": "baz"}}}}, + {{{{"wibble": "wobble"}, {"wubble": "wabble"}, {"wabble": "wobble"}}}}}, + }, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } +} diff --git a/hstore_test.go b/hstore_test.go new file mode 100644 index 00000000..dce8baf2 --- /dev/null +++ b/hstore_test.go @@ -0,0 +1,111 @@ +package pgtype_test + +import ( + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestHstoreTranscode(t *testing.T) { + text := func(s string) pgtype.Text { + return pgtype.Text{String: s, Status: pgtype.Present} + } + + values := []interface{}{ + &pgtype.Hstore{Map: map[string]pgtype.Text{}, Status: pgtype.Present}, + &pgtype.Hstore{Map: map[string]pgtype.Text{"foo": text(""), "bar": text(""), "baz": text("123")}, Status: pgtype.Present}, + &pgtype.Hstore{Map: map[string]pgtype.Text{"foo": text("bar")}, Status: pgtype.Present}, + &pgtype.Hstore{Map: map[string]pgtype.Text{"foo": text("bar"), "baz": text("quz")}, Status: pgtype.Present}, + &pgtype.Hstore{Map: map[string]pgtype.Text{"NULL": text("bar")}, Status: pgtype.Present}, + &pgtype.Hstore{Map: map[string]pgtype.Text{"foo": text("NULL")}, Status: pgtype.Present}, + &pgtype.Hstore{Map: map[string]pgtype.Text{"": text("bar")}, Status: pgtype.Present}, + &pgtype.Hstore{Status: pgtype.Null}, + } + + specialStrings := []string{ + `"`, + `'`, + `\`, + `\\`, + `=>`, + ` `, + `\ / / \\ => " ' " '`, + } + for _, s := range specialStrings { + // Special key values + values = append(values, &pgtype.Hstore{Map: map[string]pgtype.Text{s + "foo": text("bar")}, Status: pgtype.Present}) // at beginning + values = append(values, &pgtype.Hstore{Map: map[string]pgtype.Text{"foo" + s + "bar": text("bar")}, Status: pgtype.Present}) // in middle + values = append(values, &pgtype.Hstore{Map: map[string]pgtype.Text{"foo" + s: text("bar")}, Status: pgtype.Present}) // at end + values = append(values, &pgtype.Hstore{Map: map[string]pgtype.Text{s: text("bar")}, Status: pgtype.Present}) // is key + + // Special value values + values = append(values, &pgtype.Hstore{Map: map[string]pgtype.Text{"foo": text(s + "bar")}, Status: pgtype.Present}) // at beginning + values = append(values, &pgtype.Hstore{Map: map[string]pgtype.Text{"foo": text("foo" + s + "bar")}, Status: pgtype.Present}) // in middle + values = append(values, &pgtype.Hstore{Map: map[string]pgtype.Text{"foo": text("foo" + s)}, Status: pgtype.Present}) // at end + values = append(values, &pgtype.Hstore{Map: map[string]pgtype.Text{"foo": text(s)}, Status: pgtype.Present}) // is key + } + + testutil.TestSuccessfulTranscodeEqFunc(t, "hstore", values, func(ai, bi interface{}) bool { + a := ai.(pgtype.Hstore) + b := bi.(pgtype.Hstore) + + if len(a.Map) != len(b.Map) || a.Status != b.Status { + return false + } + + for k := range a.Map { + if a.Map[k] != b.Map[k] { + return false + } + } + + return true + }) +} + +func TestHstoreSet(t *testing.T) { + successfulTests := []struct { + src map[string]string + result pgtype.Hstore + }{ + {src: map[string]string{"foo": "bar"}, result: pgtype.Hstore{Map: map[string]pgtype.Text{"foo": {String: "bar", Status: pgtype.Present}}, Status: pgtype.Present}}, + } + + for i, tt := range successfulTests { + var dst pgtype.Hstore + err := dst.Set(tt.src) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !reflect.DeepEqual(dst, tt.result) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.src, tt.result, dst) + } + } +} + +func TestHstoreAssignTo(t *testing.T) { + var m map[string]string + + simpleTests := []struct { + src pgtype.Hstore + dst *map[string]string + expected map[string]string + }{ + {src: pgtype.Hstore{Map: map[string]pgtype.Text{"foo": {String: "bar", Status: pgtype.Present}}, Status: pgtype.Present}, dst: &m, expected: map[string]string{"foo": "bar"}}, + {src: pgtype.Hstore{Status: pgtype.Null}, dst: &m, expected: ((map[string]string)(nil))}, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !reflect.DeepEqual(*tt.dst, tt.expected) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, *tt.dst) + } + } +} diff --git a/inet_array_test.go b/inet_array_test.go new file mode 100644 index 00000000..46dc7d12 --- /dev/null +++ b/inet_array_test.go @@ -0,0 +1,319 @@ +package pgtype_test + +import ( + "net" + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestInetArrayTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "inet[]", []interface{}{ + &pgtype.InetArray{ + Elements: nil, + Dimensions: nil, + Status: pgtype.Present, + }, + &pgtype.InetArray{ + Elements: []pgtype.Inet{ + {IPNet: mustParseCIDR(t, "12.34.56.0/32"), Status: pgtype.Present}, + {Status: pgtype.Null}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.InetArray{Status: pgtype.Null}, + &pgtype.InetArray{ + Elements: []pgtype.Inet{ + {IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "12.34.56.0/32"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "192.168.0.1/32"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "2607:f8b0:4009:80b::200e/128"), Status: pgtype.Present}, + {Status: pgtype.Null}, + {IPNet: mustParseCIDR(t, "255.0.0.0/8"), Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}, {Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.InetArray{ + Elements: []pgtype.Inet{ + {IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "12.34.56.0/32"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "192.168.0.1/32"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "2607:f8b0:4009:80b::200e/128"), Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{ + {Length: 2, LowerBound: 4}, + {Length: 2, LowerBound: 2}, + }, + Status: pgtype.Present, + }, + }) +} + +func TestInetArraySet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.InetArray + }{ + { + source: []*net.IPNet{mustParseCIDR(t, "127.0.0.1/32")}, + result: pgtype.InetArray{ + Elements: []pgtype.Inet{{IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: (([]*net.IPNet)(nil)), + result: pgtype.InetArray{Status: pgtype.Null}, + }, + { + source: []net.IP{mustParseCIDR(t, "127.0.0.1/32").IP}, + result: pgtype.InetArray{ + Elements: []pgtype.Inet{{IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: (([]net.IP)(nil)), + result: pgtype.InetArray{Status: pgtype.Null}, + }, + { + source: [][]net.IP{{mustParseCIDR(t, "127.0.0.1/32").IP}, {mustParseCIDR(t, "10.0.0.1/32").IP}}, + result: pgtype.InetArray{ + Elements: []pgtype.Inet{ + {IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "10.0.0.1/32"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [][][][]*net.IPNet{ + {{{ + mustParseCIDR(t, "127.0.0.1/24"), + mustParseCIDR(t, "10.0.0.1/24"), + mustParseCIDR(t, "172.16.0.1/16")}}}, + {{{ + mustParseCIDR(t, "192.168.0.1/16"), + mustParseCIDR(t, "224.0.0.1/24"), + mustParseCIDR(t, "169.168.0.1/16")}}}}, + result: pgtype.InetArray{ + Elements: []pgtype.Inet{ + {IPNet: mustParseCIDR(t, "127.0.0.1/24"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "10.0.0.1/24"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "172.16.0.1/16"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "192.168.0.1/16"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "224.0.0.1/24"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "169.168.0.1/16"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + { + source: [2][1]net.IP{{mustParseCIDR(t, "127.0.0.1/32").IP}, {mustParseCIDR(t, "10.0.0.1/32").IP}}, + result: pgtype.InetArray{ + Elements: []pgtype.Inet{ + {IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "10.0.0.1/32"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [2][1][1][3]*net.IPNet{ + {{{ + mustParseCIDR(t, "127.0.0.1/24"), + mustParseCIDR(t, "10.0.0.1/24"), + mustParseCIDR(t, "172.16.0.1/16")}}}, + {{{ + mustParseCIDR(t, "192.168.0.1/16"), + mustParseCIDR(t, "224.0.0.1/24"), + mustParseCIDR(t, "169.168.0.1/16")}}}}, + result: pgtype.InetArray{ + Elements: []pgtype.Inet{ + {IPNet: mustParseCIDR(t, "127.0.0.1/24"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "10.0.0.1/24"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "172.16.0.1/16"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "192.168.0.1/16"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "224.0.0.1/24"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "169.168.0.1/16"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + } + + for i, tt := range successfulTests { + var r pgtype.InetArray + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !reflect.DeepEqual(r, tt.result) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestInetArrayAssignTo(t *testing.T) { + var ipnetSlice []*net.IPNet + var ipSlice []net.IP + var ipSliceDim2 [][]net.IP + var ipnetSliceDim4 [][][][]*net.IPNet + var ipArrayDim2 [2][1]net.IP + var ipnetArrayDim4 [2][1][1][3]*net.IPNet + + simpleTests := []struct { + src pgtype.InetArray + dst interface{} + expected interface{} + }{ + { + src: pgtype.InetArray{ + Elements: []pgtype.Inet{{IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &ipnetSlice, + expected: []*net.IPNet{mustParseCIDR(t, "127.0.0.1/32")}, + }, + { + src: pgtype.InetArray{ + Elements: []pgtype.Inet{{Status: pgtype.Null}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &ipnetSlice, + expected: []*net.IPNet{nil}, + }, + { + src: pgtype.InetArray{ + Elements: []pgtype.Inet{{IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &ipSlice, + expected: []net.IP{mustParseCIDR(t, "127.0.0.1/32").IP}, + }, + { + src: pgtype.InetArray{ + Elements: []pgtype.Inet{{Status: pgtype.Null}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &ipSlice, + expected: []net.IP{nil}, + }, + { + src: pgtype.InetArray{Status: pgtype.Null}, + dst: &ipnetSlice, + expected: (([]*net.IPNet)(nil)), + }, + { + src: pgtype.InetArray{Status: pgtype.Present}, + dst: &ipnetSlice, + expected: []*net.IPNet{}, + }, + { + src: pgtype.InetArray{Status: pgtype.Null}, + dst: &ipSlice, + expected: (([]net.IP)(nil)), + }, + { + src: pgtype.InetArray{Status: pgtype.Present}, + dst: &ipSlice, + expected: []net.IP{}, + }, + { + src: pgtype.InetArray{ + Elements: []pgtype.Inet{ + {IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "10.0.0.1/32"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &ipSliceDim2, + expected: [][]net.IP{{mustParseCIDR(t, "127.0.0.1/32").IP}, {mustParseCIDR(t, "10.0.0.1/32").IP}}, + }, + { + src: pgtype.InetArray{ + Elements: []pgtype.Inet{ + {IPNet: mustParseCIDR(t, "127.0.0.1/24"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "10.0.0.1/24"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "172.16.0.1/16"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "192.168.0.1/16"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "224.0.0.1/24"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "169.168.0.1/16"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + dst: &ipnetSliceDim4, + expected: [][][][]*net.IPNet{ + {{{ + mustParseCIDR(t, "127.0.0.1/24"), + mustParseCIDR(t, "10.0.0.1/24"), + mustParseCIDR(t, "172.16.0.1/16")}}}, + {{{ + mustParseCIDR(t, "192.168.0.1/16"), + mustParseCIDR(t, "224.0.0.1/24"), + mustParseCIDR(t, "169.168.0.1/16")}}}}, + }, + { + src: pgtype.InetArray{ + Elements: []pgtype.Inet{ + {IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "10.0.0.1/32"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &ipArrayDim2, + expected: [2][1]net.IP{{mustParseCIDR(t, "127.0.0.1/32").IP}, {mustParseCIDR(t, "10.0.0.1/32").IP}}, + }, + { + src: pgtype.InetArray{ + Elements: []pgtype.Inet{ + {IPNet: mustParseCIDR(t, "127.0.0.1/24"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "10.0.0.1/24"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "172.16.0.1/16"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "192.168.0.1/16"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "224.0.0.1/24"), Status: pgtype.Present}, + {IPNet: mustParseCIDR(t, "169.168.0.1/16"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + dst: &ipnetArrayDim4, + expected: [2][1][1][3]*net.IPNet{ + {{{ + mustParseCIDR(t, "127.0.0.1/24"), + mustParseCIDR(t, "10.0.0.1/24"), + mustParseCIDR(t, "172.16.0.1/16")}}}, + {{{ + mustParseCIDR(t, "192.168.0.1/16"), + mustParseCIDR(t, "224.0.0.1/24"), + mustParseCIDR(t, "169.168.0.1/16")}}}}, + }, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } +} diff --git a/inet_test.go b/inet_test.go new file mode 100644 index 00000000..66fe777f --- /dev/null +++ b/inet_test.go @@ -0,0 +1,134 @@ +package pgtype_test + +import ( + "net" + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" + "github.com/stretchr/testify/assert" +) + +func TestInetTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "inet", []interface{}{ + &pgtype.Inet{IPNet: mustParseInet(t, "0.0.0.0/32"), Status: pgtype.Present}, + &pgtype.Inet{IPNet: mustParseInet(t, "127.0.0.1/8"), Status: pgtype.Present}, + &pgtype.Inet{IPNet: mustParseInet(t, "12.34.56.65/32"), Status: pgtype.Present}, + &pgtype.Inet{IPNet: mustParseInet(t, "192.168.1.16/24"), Status: pgtype.Present}, + &pgtype.Inet{IPNet: mustParseInet(t, "255.0.0.0/8"), Status: pgtype.Present}, + &pgtype.Inet{IPNet: mustParseInet(t, "255.255.255.255/32"), Status: pgtype.Present}, + &pgtype.Inet{IPNet: mustParseInet(t, "::1/64"), Status: pgtype.Present}, + &pgtype.Inet{IPNet: mustParseInet(t, "::/0"), Status: pgtype.Present}, + &pgtype.Inet{IPNet: mustParseInet(t, "::1/128"), Status: pgtype.Present}, + &pgtype.Inet{IPNet: mustParseInet(t, "2607:f8b0:4009:80b::200e/64"), Status: pgtype.Present}, + &pgtype.Inet{Status: pgtype.Null}, + }) +} + +func TestCidrTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "cidr", []interface{}{ + &pgtype.Inet{IPNet: mustParseCIDR(t, "0.0.0.0/32"), Status: pgtype.Present}, + &pgtype.Inet{IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}, + &pgtype.Inet{IPNet: mustParseCIDR(t, "12.34.56.0/32"), Status: pgtype.Present}, + &pgtype.Inet{IPNet: mustParseCIDR(t, "192.168.1.0/24"), Status: pgtype.Present}, + &pgtype.Inet{IPNet: mustParseCIDR(t, "255.0.0.0/8"), Status: pgtype.Present}, + &pgtype.Inet{IPNet: mustParseCIDR(t, "255.255.255.255/32"), Status: pgtype.Present}, + &pgtype.Inet{IPNet: mustParseCIDR(t, "::/128"), Status: pgtype.Present}, + &pgtype.Inet{IPNet: mustParseCIDR(t, "::/0"), Status: pgtype.Present}, + &pgtype.Inet{IPNet: mustParseCIDR(t, "::1/128"), Status: pgtype.Present}, + &pgtype.Inet{IPNet: mustParseCIDR(t, "2607:f8b0:4009:80b::200e/128"), Status: pgtype.Present}, + &pgtype.Inet{Status: pgtype.Null}, + }) +} + +func TestInetSet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.Inet + }{ + {source: mustParseCIDR(t, "127.0.0.1/32"), result: pgtype.Inet{IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}}, + {source: mustParseCIDR(t, "127.0.0.1/32").IP, result: pgtype.Inet{IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}}, + {source: "127.0.0.1/32", result: pgtype.Inet{IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}}, + {source: "1.2.3.4/24", result: pgtype.Inet{IPNet: &net.IPNet{IP: net.ParseIP("1.2.3.4"), Mask: net.CIDRMask(24, 32)}, Status: pgtype.Present}}, + {source: net.ParseIP(""), result: pgtype.Inet{Status: pgtype.Null}}, + } + + for i, tt := range successfulTests { + var r pgtype.Inet + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + assert.Equalf(t, tt.result.Status, r.Status, "%d: Status", i) + if tt.result.Status == pgtype.Present { + assert.Equalf(t, tt.result.IPNet.Mask, r.IPNet.Mask, "%d: IP", i) + assert.Truef(t, tt.result.IPNet.IP.Equal(r.IPNet.IP), "%d: Mask", i) + } + } +} + +func TestInetAssignTo(t *testing.T) { + var ipnet net.IPNet + var pipnet *net.IPNet + var ip net.IP + var pip *net.IP + + simpleTests := []struct { + src pgtype.Inet + dst interface{} + expected interface{} + }{ + {src: pgtype.Inet{IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}, dst: &ipnet, expected: *mustParseCIDR(t, "127.0.0.1/32")}, + {src: pgtype.Inet{IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}, dst: &ip, expected: mustParseCIDR(t, "127.0.0.1/32").IP}, + {src: pgtype.Inet{Status: pgtype.Null}, dst: &pipnet, expected: ((*net.IPNet)(nil))}, + {src: pgtype.Inet{Status: pgtype.Null}, dst: &pip, expected: ((*net.IP)(nil))}, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) { + t.Errorf("%d: expected %v to assign %#v, but result was %#v", i, tt.src, tt.expected, dst) + } + } + + pointerAllocTests := []struct { + src pgtype.Inet + dst interface{} + expected interface{} + }{ + {src: pgtype.Inet{IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}, dst: &pipnet, expected: *mustParseCIDR(t, "127.0.0.1/32")}, + {src: pgtype.Inet{IPNet: mustParseCIDR(t, "127.0.0.1/32"), Status: pgtype.Present}, dst: &pip, expected: mustParseCIDR(t, "127.0.0.1/32").IP}, + } + + for i, tt := range pointerAllocTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.Inet + dst interface{} + }{ + {src: pgtype.Inet{IPNet: mustParseCIDR(t, "192.168.0.0/16"), Status: pgtype.Present}, dst: &ip}, + {src: pgtype.Inet{Status: pgtype.Null}, dst: &ipnet}, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } +} diff --git a/int2_array_test.go b/int2_array_test.go new file mode 100644 index 00000000..17c37360 --- /dev/null +++ b/int2_array_test.go @@ -0,0 +1,342 @@ +package pgtype_test + +import ( + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestInt2ArrayTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "int2[]", []interface{}{ + &pgtype.Int2Array{ + Elements: nil, + Dimensions: nil, + Status: pgtype.Present, + }, + &pgtype.Int2Array{ + Elements: []pgtype.Int2{ + {Int: 1, Status: pgtype.Present}, + {Status: pgtype.Null}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.Int2Array{Status: pgtype.Null}, + &pgtype.Int2Array{ + Elements: []pgtype.Int2{ + {Int: 1, Status: pgtype.Present}, + {Int: 2, Status: pgtype.Present}, + {Int: 3, Status: pgtype.Present}, + {Int: 4, Status: pgtype.Present}, + {Status: pgtype.Null}, + {Int: 6, Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}, {Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.Int2Array{ + Elements: []pgtype.Int2{ + {Int: 1, Status: pgtype.Present}, + {Int: 2, Status: pgtype.Present}, + {Int: 3, Status: pgtype.Present}, + {Int: 4, Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{ + {Length: 2, LowerBound: 4}, + {Length: 2, LowerBound: 2}, + }, + Status: pgtype.Present, + }, + }) +} + +func TestInt2ArraySet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.Int2Array + }{ + { + source: []int64{1}, + result: pgtype.Int2Array{ + Elements: []pgtype.Int2{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: []int32{1}, + result: pgtype.Int2Array{ + Elements: []pgtype.Int2{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: []int16{1}, + result: pgtype.Int2Array{ + Elements: []pgtype.Int2{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: []int{1}, + result: pgtype.Int2Array{ + Elements: []pgtype.Int2{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: []uint64{1}, + result: pgtype.Int2Array{ + Elements: []pgtype.Int2{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: []uint32{1}, + result: pgtype.Int2Array{ + Elements: []pgtype.Int2{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: []uint16{1}, + result: pgtype.Int2Array{ + Elements: []pgtype.Int2{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: (([]int16)(nil)), + result: pgtype.Int2Array{Status: pgtype.Null}, + }, + { + source: [][]int16{{1}, {2}}, + result: pgtype.Int2Array{ + Elements: []pgtype.Int2{{Int: 1, Status: pgtype.Present}, {Int: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [][][][]int16{{{{1, 2, 3}}}, {{{4, 5, 6}}}}, + result: pgtype.Int2Array{ + Elements: []pgtype.Int2{ + {Int: 1, Status: pgtype.Present}, + {Int: 2, Status: pgtype.Present}, + {Int: 3, Status: pgtype.Present}, + {Int: 4, Status: pgtype.Present}, + {Int: 5, Status: pgtype.Present}, + {Int: 6, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + { + source: [2][1]int16{{1}, {2}}, + result: pgtype.Int2Array{ + Elements: []pgtype.Int2{{Int: 1, Status: pgtype.Present}, {Int: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [2][1][1][3]int16{{{{1, 2, 3}}}, {{{4, 5, 6}}}}, + result: pgtype.Int2Array{ + Elements: []pgtype.Int2{ + {Int: 1, Status: pgtype.Present}, + {Int: 2, Status: pgtype.Present}, + {Int: 3, Status: pgtype.Present}, + {Int: 4, Status: pgtype.Present}, + {Int: 5, Status: pgtype.Present}, + {Int: 6, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + } + + for i, tt := range successfulTests { + var r pgtype.Int2Array + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !reflect.DeepEqual(r, tt.result) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestInt2ArrayAssignTo(t *testing.T) { + var int16Slice []int16 + var uint16Slice []uint16 + var namedInt16Slice _int16Slice + var int16SliceDim2 [][]int16 + var int16SliceDim4 [][][][]int16 + var int16ArrayDim2 [2][1]int16 + var int16ArrayDim4 [2][1][1][3]int16 + + simpleTests := []struct { + src pgtype.Int2Array + dst interface{} + expected interface{} + }{ + { + src: pgtype.Int2Array{ + Elements: []pgtype.Int2{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &int16Slice, + expected: []int16{1}, + }, + { + src: pgtype.Int2Array{ + Elements: []pgtype.Int2{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &uint16Slice, + expected: []uint16{1}, + }, + { + src: pgtype.Int2Array{ + Elements: []pgtype.Int2{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &namedInt16Slice, + expected: _int16Slice{1}, + }, + { + src: pgtype.Int2Array{Status: pgtype.Null}, + dst: &int16Slice, + expected: (([]int16)(nil)), + }, + { + src: pgtype.Int2Array{Status: pgtype.Present}, + dst: &int16Slice, + expected: []int16{}, + }, + { + src: pgtype.Int2Array{ + Elements: []pgtype.Int2{{Int: 1, Status: pgtype.Present}, {Int: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + expected: [][]int16{{1}, {2}}, + dst: &int16SliceDim2, + }, + { + src: pgtype.Int2Array{ + Elements: []pgtype.Int2{ + {Int: 1, Status: pgtype.Present}, + {Int: 2, Status: pgtype.Present}, + {Int: 3, Status: pgtype.Present}, + {Int: 4, Status: pgtype.Present}, + {Int: 5, Status: pgtype.Present}, + {Int: 6, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + expected: [][][][]int16{{{{1, 2, 3}}}, {{{4, 5, 6}}}}, + dst: &int16SliceDim4, + }, + { + src: pgtype.Int2Array{ + Elements: []pgtype.Int2{{Int: 1, Status: pgtype.Present}, {Int: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + expected: [2][1]int16{{1}, {2}}, + dst: &int16ArrayDim2, + }, + { + src: pgtype.Int2Array{ + Elements: []pgtype.Int2{ + {Int: 1, Status: pgtype.Present}, + {Int: 2, Status: pgtype.Present}, + {Int: 3, Status: pgtype.Present}, + {Int: 4, Status: pgtype.Present}, + {Int: 5, Status: pgtype.Present}, + {Int: 6, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + expected: [2][1][1][3]int16{{{{1, 2, 3}}}, {{{4, 5, 6}}}}, + dst: &int16ArrayDim4, + }, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.Int2Array + dst interface{} + }{ + { + src: pgtype.Int2Array{ + Elements: []pgtype.Int2{{Status: pgtype.Null}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &int16Slice, + }, + { + src: pgtype.Int2Array{ + Elements: []pgtype.Int2{{Int: -1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &uint16Slice, + }, + { + src: pgtype.Int2Array{ + Elements: []pgtype.Int2{{Int: 1, Status: pgtype.Present}, {Int: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &int16ArrayDim2, + }, + { + src: pgtype.Int2Array{ + Elements: []pgtype.Int2{{Int: 1, Status: pgtype.Present}, {Int: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &int16Slice, + }, + { + src: pgtype.Int2Array{ + Elements: []pgtype.Int2{{Int: 1, Status: pgtype.Present}, {Int: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &int16ArrayDim4, + }, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } + +} diff --git a/int2_test.go b/int2_test.go new file mode 100644 index 00000000..178eb278 --- /dev/null +++ b/int2_test.go @@ -0,0 +1,144 @@ +package pgtype_test + +import ( + "math" + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestInt2Transcode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "int2", []interface{}{ + &pgtype.Int2{Int: math.MinInt16, Status: pgtype.Present}, + &pgtype.Int2{Int: -1, Status: pgtype.Present}, + &pgtype.Int2{Int: 0, Status: pgtype.Present}, + &pgtype.Int2{Int: 1, Status: pgtype.Present}, + &pgtype.Int2{Int: math.MaxInt16, Status: pgtype.Present}, + &pgtype.Int2{Int: 0, Status: pgtype.Null}, + }) +} + +func TestInt2Set(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.Int2 + }{ + {source: int8(1), result: pgtype.Int2{Int: 1, Status: pgtype.Present}}, + {source: int16(1), result: pgtype.Int2{Int: 1, Status: pgtype.Present}}, + {source: int32(1), result: pgtype.Int2{Int: 1, Status: pgtype.Present}}, + {source: int64(1), result: pgtype.Int2{Int: 1, Status: pgtype.Present}}, + {source: int8(-1), result: pgtype.Int2{Int: -1, Status: pgtype.Present}}, + {source: int16(-1), result: pgtype.Int2{Int: -1, Status: pgtype.Present}}, + {source: int32(-1), result: pgtype.Int2{Int: -1, Status: pgtype.Present}}, + {source: int64(-1), result: pgtype.Int2{Int: -1, Status: pgtype.Present}}, + {source: uint8(1), result: pgtype.Int2{Int: 1, Status: pgtype.Present}}, + {source: uint16(1), result: pgtype.Int2{Int: 1, Status: pgtype.Present}}, + {source: uint32(1), result: pgtype.Int2{Int: 1, Status: pgtype.Present}}, + {source: uint64(1), result: pgtype.Int2{Int: 1, Status: pgtype.Present}}, + {source: float32(1), result: pgtype.Int2{Int: 1, Status: pgtype.Present}}, + {source: float64(1), result: pgtype.Int2{Int: 1, Status: pgtype.Present}}, + {source: "1", result: pgtype.Int2{Int: 1, Status: pgtype.Present}}, + {source: _int8(1), result: pgtype.Int2{Int: 1, Status: pgtype.Present}}, + } + + for i, tt := range successfulTests { + var r pgtype.Int2 + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if r != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestInt2AssignTo(t *testing.T) { + var i8 int8 + var i16 int16 + var i32 int32 + var i64 int64 + var i int + var ui8 uint8 + var ui16 uint16 + var ui32 uint32 + var ui64 uint64 + var ui uint + var pi8 *int8 + var _i8 _int8 + var _pi8 *_int8 + + simpleTests := []struct { + src pgtype.Int2 + dst interface{} + expected interface{} + }{ + {src: pgtype.Int2{Int: 42, Status: pgtype.Present}, dst: &i8, expected: int8(42)}, + {src: pgtype.Int2{Int: 42, Status: pgtype.Present}, dst: &i16, expected: int16(42)}, + {src: pgtype.Int2{Int: 42, Status: pgtype.Present}, dst: &i32, expected: int32(42)}, + {src: pgtype.Int2{Int: 42, Status: pgtype.Present}, dst: &i64, expected: int64(42)}, + {src: pgtype.Int2{Int: 42, Status: pgtype.Present}, dst: &i, expected: int(42)}, + {src: pgtype.Int2{Int: 42, Status: pgtype.Present}, dst: &ui8, expected: uint8(42)}, + {src: pgtype.Int2{Int: 42, Status: pgtype.Present}, dst: &ui16, expected: uint16(42)}, + {src: pgtype.Int2{Int: 42, Status: pgtype.Present}, dst: &ui32, expected: uint32(42)}, + {src: pgtype.Int2{Int: 42, Status: pgtype.Present}, dst: &ui64, expected: uint64(42)}, + {src: pgtype.Int2{Int: 42, Status: pgtype.Present}, dst: &ui, expected: uint(42)}, + {src: pgtype.Int2{Int: 42, Status: pgtype.Present}, dst: &_i8, expected: _int8(42)}, + {src: pgtype.Int2{Int: 0, Status: pgtype.Null}, dst: &pi8, expected: ((*int8)(nil))}, + {src: pgtype.Int2{Int: 0, Status: pgtype.Null}, dst: &_pi8, expected: ((*_int8)(nil))}, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + pointerAllocTests := []struct { + src pgtype.Int2 + dst interface{} + expected interface{} + }{ + {src: pgtype.Int2{Int: 42, Status: pgtype.Present}, dst: &pi8, expected: int8(42)}, + {src: pgtype.Int2{Int: 42, Status: pgtype.Present}, dst: &_pi8, expected: _int8(42)}, + } + + for i, tt := range pointerAllocTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.Int2 + dst interface{} + }{ + {src: pgtype.Int2{Int: 150, Status: pgtype.Present}, dst: &i8}, + {src: pgtype.Int2{Int: -1, Status: pgtype.Present}, dst: &ui8}, + {src: pgtype.Int2{Int: -1, Status: pgtype.Present}, dst: &ui16}, + {src: pgtype.Int2{Int: -1, Status: pgtype.Present}, dst: &ui32}, + {src: pgtype.Int2{Int: -1, Status: pgtype.Present}, dst: &ui64}, + {src: pgtype.Int2{Int: -1, Status: pgtype.Present}, dst: &ui}, + {src: pgtype.Int2{Int: 0, Status: pgtype.Null}, dst: &i16}, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } +} diff --git a/int4_array_test.go b/int4_array_test.go new file mode 100644 index 00000000..110512a9 --- /dev/null +++ b/int4_array_test.go @@ -0,0 +1,356 @@ +package pgtype_test + +import ( + "math" + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestInt4ArrayTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "int4[]", []interface{}{ + &pgtype.Int4Array{ + Elements: nil, + Dimensions: nil, + Status: pgtype.Present, + }, + &pgtype.Int4Array{ + Elements: []pgtype.Int4{ + {Int: 1, Status: pgtype.Present}, + {Status: pgtype.Null}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.Int4Array{Status: pgtype.Null}, + &pgtype.Int4Array{ + Elements: []pgtype.Int4{ + {Int: 1, Status: pgtype.Present}, + {Int: 2, Status: pgtype.Present}, + {Int: 3, Status: pgtype.Present}, + {Int: 4, Status: pgtype.Present}, + {Status: pgtype.Null}, + {Int: 6, Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}, {Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.Int4Array{ + Elements: []pgtype.Int4{ + {Int: 1, Status: pgtype.Present}, + {Int: 2, Status: pgtype.Present}, + {Int: 3, Status: pgtype.Present}, + {Int: 4, Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{ + {Length: 2, LowerBound: 4}, + {Length: 2, LowerBound: 2}, + }, + Status: pgtype.Present, + }, + }) +} + +func TestInt4ArraySet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.Int4Array + expectedError bool + }{ + { + source: []int64{1}, + result: pgtype.Int4Array{ + Elements: []pgtype.Int4{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: []int32{1}, + result: pgtype.Int4Array{ + Elements: []pgtype.Int4{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: []int16{1}, + result: pgtype.Int4Array{ + Elements: []pgtype.Int4{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: []int{1}, + result: pgtype.Int4Array{ + Elements: []pgtype.Int4{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: []int{1, math.MaxInt32 + 1, 2}, + expectedError: true, + }, + { + source: []uint64{1}, + result: pgtype.Int4Array{ + Elements: []pgtype.Int4{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: []uint32{1}, + result: pgtype.Int4Array{ + Elements: []pgtype.Int4{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: []uint16{1}, + result: pgtype.Int4Array{ + Elements: []pgtype.Int4{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: (([]int32)(nil)), + result: pgtype.Int4Array{Status: pgtype.Null}, + }, + { + source: [][]int32{{1}, {2}}, + result: pgtype.Int4Array{ + Elements: []pgtype.Int4{{Int: 1, Status: pgtype.Present}, {Int: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [][][][]int32{{{{1, 2, 3}}}, {{{4, 5, 6}}}}, + result: pgtype.Int4Array{ + Elements: []pgtype.Int4{ + {Int: 1, Status: pgtype.Present}, + {Int: 2, Status: pgtype.Present}, + {Int: 3, Status: pgtype.Present}, + {Int: 4, Status: pgtype.Present}, + {Int: 5, Status: pgtype.Present}, + {Int: 6, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + { + source: [2][1]int32{{1}, {2}}, + result: pgtype.Int4Array{ + Elements: []pgtype.Int4{{Int: 1, Status: pgtype.Present}, {Int: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [2][1][1][3]int32{{{{1, 2, 3}}}, {{{4, 5, 6}}}}, + result: pgtype.Int4Array{ + Elements: []pgtype.Int4{ + {Int: 1, Status: pgtype.Present}, + {Int: 2, Status: pgtype.Present}, + {Int: 3, Status: pgtype.Present}, + {Int: 4, Status: pgtype.Present}, + {Int: 5, Status: pgtype.Present}, + {Int: 6, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + } + + for i, tt := range successfulTests { + var r pgtype.Int4Array + err := r.Set(tt.source) + if err != nil { + if tt.expectedError { + continue + } + t.Errorf("%d: %v", i, err) + } + + if tt.expectedError { + t.Errorf("%d: an error was expected, %v", i, tt) + continue + } + + if !reflect.DeepEqual(r, tt.result) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestInt4ArrayAssignTo(t *testing.T) { + var int32Slice []int32 + var uint32Slice []uint32 + var namedInt32Slice _int32Slice + var int32SliceDim2 [][]int32 + var int32SliceDim4 [][][][]int32 + var int32ArrayDim2 [2][1]int32 + var int32ArrayDim4 [2][1][1][3]int32 + + simpleTests := []struct { + src pgtype.Int4Array + dst interface{} + expected interface{} + }{ + { + src: pgtype.Int4Array{ + Elements: []pgtype.Int4{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &int32Slice, + expected: []int32{1}, + }, + { + src: pgtype.Int4Array{ + Elements: []pgtype.Int4{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &uint32Slice, + expected: []uint32{1}, + }, + { + src: pgtype.Int4Array{ + Elements: []pgtype.Int4{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &namedInt32Slice, + expected: _int32Slice{1}, + }, + { + src: pgtype.Int4Array{Status: pgtype.Null}, + dst: &int32Slice, + expected: (([]int32)(nil)), + }, + { + src: pgtype.Int4Array{Status: pgtype.Present}, + dst: &int32Slice, + expected: []int32{}, + }, + { + src: pgtype.Int4Array{ + Elements: []pgtype.Int4{{Int: 1, Status: pgtype.Present}, {Int: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + expected: [][]int32{{1}, {2}}, + dst: &int32SliceDim2, + }, + { + src: pgtype.Int4Array{ + Elements: []pgtype.Int4{ + {Int: 1, Status: pgtype.Present}, + {Int: 2, Status: pgtype.Present}, + {Int: 3, Status: pgtype.Present}, + {Int: 4, Status: pgtype.Present}, + {Int: 5, Status: pgtype.Present}, + {Int: 6, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + expected: [][][][]int32{{{{1, 2, 3}}}, {{{4, 5, 6}}}}, + dst: &int32SliceDim4, + }, + { + src: pgtype.Int4Array{ + Elements: []pgtype.Int4{{Int: 1, Status: pgtype.Present}, {Int: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + expected: [2][1]int32{{1}, {2}}, + dst: &int32ArrayDim2, + }, + { + src: pgtype.Int4Array{ + Elements: []pgtype.Int4{ + {Int: 1, Status: pgtype.Present}, + {Int: 2, Status: pgtype.Present}, + {Int: 3, Status: pgtype.Present}, + {Int: 4, Status: pgtype.Present}, + {Int: 5, Status: pgtype.Present}, + {Int: 6, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + expected: [2][1][1][3]int32{{{{1, 2, 3}}}, {{{4, 5, 6}}}}, + dst: &int32ArrayDim4, + }, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.Int4Array + dst interface{} + }{ + { + src: pgtype.Int4Array{ + Elements: []pgtype.Int4{{Status: pgtype.Null}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &int32Slice, + }, + { + src: pgtype.Int4Array{ + Elements: []pgtype.Int4{{Int: -1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &uint32Slice, + }, + { + src: pgtype.Int4Array{ + Elements: []pgtype.Int4{{Int: 1, Status: pgtype.Present}, {Int: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &int32ArrayDim2, + }, + { + src: pgtype.Int4Array{ + Elements: []pgtype.Int4{{Int: 1, Status: pgtype.Present}, {Int: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &int32Slice, + }, + { + src: pgtype.Int4Array{ + Elements: []pgtype.Int4{{Int: 1, Status: pgtype.Present}, {Int: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &int32ArrayDim4, + }, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } + +} diff --git a/int4_test.go b/int4_test.go new file mode 100644 index 00000000..ae01114f --- /dev/null +++ b/int4_test.go @@ -0,0 +1,186 @@ +package pgtype_test + +import ( + "math" + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestInt4Transcode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "int4", []interface{}{ + &pgtype.Int4{Int: math.MinInt32, Status: pgtype.Present}, + &pgtype.Int4{Int: -1, Status: pgtype.Present}, + &pgtype.Int4{Int: 0, Status: pgtype.Present}, + &pgtype.Int4{Int: 1, Status: pgtype.Present}, + &pgtype.Int4{Int: math.MaxInt32, Status: pgtype.Present}, + &pgtype.Int4{Int: 0, Status: pgtype.Null}, + }) +} + +func TestInt4Set(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.Int4 + }{ + {source: int8(1), result: pgtype.Int4{Int: 1, Status: pgtype.Present}}, + {source: int16(1), result: pgtype.Int4{Int: 1, Status: pgtype.Present}}, + {source: int32(1), result: pgtype.Int4{Int: 1, Status: pgtype.Present}}, + {source: int64(1), result: pgtype.Int4{Int: 1, Status: pgtype.Present}}, + {source: int8(-1), result: pgtype.Int4{Int: -1, Status: pgtype.Present}}, + {source: int16(-1), result: pgtype.Int4{Int: -1, Status: pgtype.Present}}, + {source: int32(-1), result: pgtype.Int4{Int: -1, Status: pgtype.Present}}, + {source: int64(-1), result: pgtype.Int4{Int: -1, Status: pgtype.Present}}, + {source: uint8(1), result: pgtype.Int4{Int: 1, Status: pgtype.Present}}, + {source: uint16(1), result: pgtype.Int4{Int: 1, Status: pgtype.Present}}, + {source: uint32(1), result: pgtype.Int4{Int: 1, Status: pgtype.Present}}, + {source: uint64(1), result: pgtype.Int4{Int: 1, Status: pgtype.Present}}, + {source: float32(1), result: pgtype.Int4{Int: 1, Status: pgtype.Present}}, + {source: float64(1), result: pgtype.Int4{Int: 1, Status: pgtype.Present}}, + {source: "1", result: pgtype.Int4{Int: 1, Status: pgtype.Present}}, + {source: _int8(1), result: pgtype.Int4{Int: 1, Status: pgtype.Present}}, + } + + for i, tt := range successfulTests { + var r pgtype.Int4 + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if r != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestInt4AssignTo(t *testing.T) { + var i8 int8 + var i16 int16 + var i32 int32 + var i64 int64 + var i int + var ui8 uint8 + var ui16 uint16 + var ui32 uint32 + var ui64 uint64 + var ui uint + var pi8 *int8 + var _i8 _int8 + var _pi8 *_int8 + + simpleTests := []struct { + src pgtype.Int4 + dst interface{} + expected interface{} + }{ + {src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &i8, expected: int8(42)}, + {src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &i16, expected: int16(42)}, + {src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &i32, expected: int32(42)}, + {src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &i64, expected: int64(42)}, + {src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &i, expected: int(42)}, + {src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &ui8, expected: uint8(42)}, + {src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &ui16, expected: uint16(42)}, + {src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &ui32, expected: uint32(42)}, + {src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &ui64, expected: uint64(42)}, + {src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &ui, expected: uint(42)}, + {src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &_i8, expected: _int8(42)}, + {src: pgtype.Int4{Int: 0, Status: pgtype.Null}, dst: &pi8, expected: ((*int8)(nil))}, + {src: pgtype.Int4{Int: 0, Status: pgtype.Null}, dst: &_pi8, expected: ((*_int8)(nil))}, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + pointerAllocTests := []struct { + src pgtype.Int4 + dst interface{} + expected interface{} + }{ + {src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &pi8, expected: int8(42)}, + {src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &_pi8, expected: _int8(42)}, + } + + for i, tt := range pointerAllocTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.Int4 + dst interface{} + }{ + {src: pgtype.Int4{Int: 150, Status: pgtype.Present}, dst: &i8}, + {src: pgtype.Int4{Int: 40000, Status: pgtype.Present}, dst: &i16}, + {src: pgtype.Int4{Int: -1, Status: pgtype.Present}, dst: &ui8}, + {src: pgtype.Int4{Int: -1, Status: pgtype.Present}, dst: &ui16}, + {src: pgtype.Int4{Int: -1, Status: pgtype.Present}, dst: &ui32}, + {src: pgtype.Int4{Int: -1, Status: pgtype.Present}, dst: &ui64}, + {src: pgtype.Int4{Int: -1, Status: pgtype.Present}, dst: &ui}, + {src: pgtype.Int4{Int: 0, Status: pgtype.Null}, dst: &i32}, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } +} + +func TestInt4MarshalJSON(t *testing.T) { + successfulTests := []struct { + source pgtype.Int4 + result string + }{ + {source: pgtype.Int4{Int: 0, Status: pgtype.Null}, result: "null"}, + {source: pgtype.Int4{Int: 1, Status: pgtype.Present}, result: "1"}, + } + for i, tt := range successfulTests { + r, err := tt.source.MarshalJSON() + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if string(r) != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, string(r)) + } + } +} + +func TestInt4UnmarshalJSON(t *testing.T) { + successfulTests := []struct { + source string + result pgtype.Int4 + }{ + {source: "null", result: pgtype.Int4{Int: 0, Status: pgtype.Null}}, + {source: "1", result: pgtype.Int4{Int: 1, Status: pgtype.Present}}, + } + for i, tt := range successfulTests { + var r pgtype.Int4 + err := r.UnmarshalJSON([]byte(tt.source)) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if r != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} diff --git a/int4range_test.go b/int4range_test.go new file mode 100644 index 00000000..43626189 --- /dev/null +++ b/int4range_test.go @@ -0,0 +1,28 @@ +package pgtype_test + +import ( + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestInt4rangeTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "int4range", []interface{}{ + &pgtype.Int4range{LowerType: pgtype.Empty, UpperType: pgtype.Empty, Status: pgtype.Present}, + &pgtype.Int4range{Lower: pgtype.Int4{Int: 1, Status: pgtype.Present}, Upper: pgtype.Int4{Int: 10, Status: pgtype.Present}, LowerType: pgtype.Inclusive, UpperType: pgtype.Exclusive, Status: pgtype.Present}, + &pgtype.Int4range{Lower: pgtype.Int4{Int: -42, Status: pgtype.Present}, Upper: pgtype.Int4{Int: -5, Status: pgtype.Present}, LowerType: pgtype.Inclusive, UpperType: pgtype.Exclusive, Status: pgtype.Present}, + &pgtype.Int4range{Lower: pgtype.Int4{Int: 1, Status: pgtype.Present}, LowerType: pgtype.Inclusive, UpperType: pgtype.Unbounded, Status: pgtype.Present}, + &pgtype.Int4range{Upper: pgtype.Int4{Int: 1, Status: pgtype.Present}, LowerType: pgtype.Unbounded, UpperType: pgtype.Exclusive, Status: pgtype.Present}, + &pgtype.Int4range{Status: pgtype.Null}, + }) +} + +func TestInt4rangeNormalize(t *testing.T) { + testutil.TestSuccessfulNormalize(t, []testutil.NormalizeTest{ + { + SQL: "select int4range(1, 10, '(]')", + Value: pgtype.Int4range{Lower: pgtype.Int4{Int: 2, Status: pgtype.Present}, Upper: pgtype.Int4{Int: 11, Status: pgtype.Present}, LowerType: pgtype.Inclusive, UpperType: pgtype.Exclusive, Status: pgtype.Present}, + }, + }) +} diff --git a/int8_array_test.go b/int8_array_test.go new file mode 100644 index 00000000..1d42a278 --- /dev/null +++ b/int8_array_test.go @@ -0,0 +1,349 @@ +package pgtype_test + +import ( + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestInt8ArrayTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "int8[]", []interface{}{ + &pgtype.Int8Array{ + Elements: nil, + Dimensions: nil, + Status: pgtype.Present, + }, + &pgtype.Int8Array{ + Elements: []pgtype.Int8{ + {Int: 1, Status: pgtype.Present}, + {Status: pgtype.Null}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.Int8Array{Status: pgtype.Null}, + &pgtype.Int8Array{ + Elements: []pgtype.Int8{ + {Int: 1, Status: pgtype.Present}, + {Int: 2, Status: pgtype.Present}, + {Int: 3, Status: pgtype.Present}, + {Int: 4, Status: pgtype.Present}, + {Status: pgtype.Null}, + {Int: 6, Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}, {Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.Int8Array{ + Elements: []pgtype.Int8{ + {Int: 1, Status: pgtype.Present}, + {Int: 2, Status: pgtype.Present}, + {Int: 3, Status: pgtype.Present}, + {Int: 4, Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{ + {Length: 2, LowerBound: 4}, + {Length: 2, LowerBound: 2}, + }, + Status: pgtype.Present, + }, + }) +} + +func TestInt8ArraySet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.Int8Array + }{ + { + source: []int64{1}, + result: pgtype.Int8Array{ + Elements: []pgtype.Int8{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: []int32{1}, + result: pgtype.Int8Array{ + Elements: []pgtype.Int8{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: []int16{1}, + result: pgtype.Int8Array{ + Elements: []pgtype.Int8{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: []int{1}, + result: pgtype.Int8Array{ + Elements: []pgtype.Int8{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: []uint64{1}, + result: pgtype.Int8Array{ + Elements: []pgtype.Int8{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: []uint32{1}, + result: pgtype.Int8Array{ + Elements: []pgtype.Int8{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: []uint16{1}, + result: pgtype.Int8Array{ + Elements: []pgtype.Int8{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: []uint{1}, + result: pgtype.Int8Array{ + Elements: []pgtype.Int8{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: (([]int64)(nil)), + result: pgtype.Int8Array{Status: pgtype.Null}, + }, + { + source: [][]int64{{1}, {2}}, + result: pgtype.Int8Array{ + Elements: []pgtype.Int8{{Int: 1, Status: pgtype.Present}, {Int: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [][][][]int64{{{{1, 2, 3}}}, {{{4, 5, 6}}}}, + result: pgtype.Int8Array{ + Elements: []pgtype.Int8{ + {Int: 1, Status: pgtype.Present}, + {Int: 2, Status: pgtype.Present}, + {Int: 3, Status: pgtype.Present}, + {Int: 4, Status: pgtype.Present}, + {Int: 5, Status: pgtype.Present}, + {Int: 6, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + { + source: [2][1]int64{{1}, {2}}, + result: pgtype.Int8Array{ + Elements: []pgtype.Int8{{Int: 1, Status: pgtype.Present}, {Int: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [2][1][1][3]int64{{{{1, 2, 3}}}, {{{4, 5, 6}}}}, + result: pgtype.Int8Array{ + Elements: []pgtype.Int8{ + {Int: 1, Status: pgtype.Present}, + {Int: 2, Status: pgtype.Present}, + {Int: 3, Status: pgtype.Present}, + {Int: 4, Status: pgtype.Present}, + {Int: 5, Status: pgtype.Present}, + {Int: 6, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + } + + for i, tt := range successfulTests { + var r pgtype.Int8Array + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !reflect.DeepEqual(r, tt.result) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestInt8ArrayAssignTo(t *testing.T) { + var int64Slice []int64 + var uint64Slice []uint64 + var namedInt64Slice _int64Slice + var int64SliceDim2 [][]int64 + var int64SliceDim4 [][][][]int64 + var int64ArrayDim2 [2][1]int64 + var int64ArrayDim4 [2][1][1][3]int64 + + simpleTests := []struct { + src pgtype.Int8Array + dst interface{} + expected interface{} + }{ + { + src: pgtype.Int8Array{ + Elements: []pgtype.Int8{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &int64Slice, + expected: []int64{1}, + }, + { + src: pgtype.Int8Array{ + Elements: []pgtype.Int8{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &uint64Slice, + expected: []uint64{1}, + }, + { + src: pgtype.Int8Array{ + Elements: []pgtype.Int8{{Int: 1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &namedInt64Slice, + expected: _int64Slice{1}, + }, + { + src: pgtype.Int8Array{Status: pgtype.Null}, + dst: &int64Slice, + expected: (([]int64)(nil)), + }, + { + src: pgtype.Int8Array{Status: pgtype.Present}, + dst: &int64Slice, + expected: []int64{}, + }, + { + src: pgtype.Int8Array{ + Elements: []pgtype.Int8{{Int: 1, Status: pgtype.Present}, {Int: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + expected: [][]int64{{1}, {2}}, + dst: &int64SliceDim2, + }, + { + src: pgtype.Int8Array{ + Elements: []pgtype.Int8{ + {Int: 1, Status: pgtype.Present}, + {Int: 2, Status: pgtype.Present}, + {Int: 3, Status: pgtype.Present}, + {Int: 4, Status: pgtype.Present}, + {Int: 5, Status: pgtype.Present}, + {Int: 6, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + expected: [][][][]int64{{{{1, 2, 3}}}, {{{4, 5, 6}}}}, + dst: &int64SliceDim4, + }, + { + src: pgtype.Int8Array{ + Elements: []pgtype.Int8{{Int: 1, Status: pgtype.Present}, {Int: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + expected: [2][1]int64{{1}, {2}}, + dst: &int64ArrayDim2, + }, + { + src: pgtype.Int8Array{ + Elements: []pgtype.Int8{ + {Int: 1, Status: pgtype.Present}, + {Int: 2, Status: pgtype.Present}, + {Int: 3, Status: pgtype.Present}, + {Int: 4, Status: pgtype.Present}, + {Int: 5, Status: pgtype.Present}, + {Int: 6, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + expected: [2][1][1][3]int64{{{{1, 2, 3}}}, {{{4, 5, 6}}}}, + dst: &int64ArrayDim4, + }, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.Int8Array + dst interface{} + }{ + { + src: pgtype.Int8Array{ + Elements: []pgtype.Int8{{Status: pgtype.Null}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &int64Slice, + }, + { + src: pgtype.Int8Array{ + Elements: []pgtype.Int8{{Int: -1, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &uint64Slice, + }, + { + src: pgtype.Int8Array{ + Elements: []pgtype.Int8{{Int: 1, Status: pgtype.Present}, {Int: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &int64ArrayDim2, + }, + { + src: pgtype.Int8Array{ + Elements: []pgtype.Int8{{Int: 1, Status: pgtype.Present}, {Int: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &int64Slice, + }, + { + src: pgtype.Int8Array{ + Elements: []pgtype.Int8{{Int: 1, Status: pgtype.Present}, {Int: 2, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &int64ArrayDim4, + }, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } + +} diff --git a/int8_test.go b/int8_test.go new file mode 100644 index 00000000..4e28e374 --- /dev/null +++ b/int8_test.go @@ -0,0 +1,187 @@ +package pgtype_test + +import ( + "math" + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestInt8Transcode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "int8", []interface{}{ + &pgtype.Int8{Int: math.MinInt64, Status: pgtype.Present}, + &pgtype.Int8{Int: -1, Status: pgtype.Present}, + &pgtype.Int8{Int: 0, Status: pgtype.Present}, + &pgtype.Int8{Int: 1, Status: pgtype.Present}, + &pgtype.Int8{Int: math.MaxInt64, Status: pgtype.Present}, + &pgtype.Int8{Int: 0, Status: pgtype.Null}, + }) +} + +func TestInt8Set(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.Int8 + }{ + {source: int8(1), result: pgtype.Int8{Int: 1, Status: pgtype.Present}}, + {source: int16(1), result: pgtype.Int8{Int: 1, Status: pgtype.Present}}, + {source: int32(1), result: pgtype.Int8{Int: 1, Status: pgtype.Present}}, + {source: int64(1), result: pgtype.Int8{Int: 1, Status: pgtype.Present}}, + {source: int8(-1), result: pgtype.Int8{Int: -1, Status: pgtype.Present}}, + {source: int16(-1), result: pgtype.Int8{Int: -1, Status: pgtype.Present}}, + {source: int32(-1), result: pgtype.Int8{Int: -1, Status: pgtype.Present}}, + {source: int64(-1), result: pgtype.Int8{Int: -1, Status: pgtype.Present}}, + {source: uint8(1), result: pgtype.Int8{Int: 1, Status: pgtype.Present}}, + {source: uint16(1), result: pgtype.Int8{Int: 1, Status: pgtype.Present}}, + {source: uint32(1), result: pgtype.Int8{Int: 1, Status: pgtype.Present}}, + {source: uint64(1), result: pgtype.Int8{Int: 1, Status: pgtype.Present}}, + {source: float32(1), result: pgtype.Int8{Int: 1, Status: pgtype.Present}}, + {source: float64(1), result: pgtype.Int8{Int: 1, Status: pgtype.Present}}, + {source: "1", result: pgtype.Int8{Int: 1, Status: pgtype.Present}}, + {source: _int8(1), result: pgtype.Int8{Int: 1, Status: pgtype.Present}}, + } + + for i, tt := range successfulTests { + var r pgtype.Int8 + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if r != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestInt8AssignTo(t *testing.T) { + var i8 int8 + var i16 int16 + var i32 int32 + var i64 int64 + var i int + var ui8 uint8 + var ui16 uint16 + var ui32 uint32 + var ui64 uint64 + var ui uint + var pi8 *int8 + var _i8 _int8 + var _pi8 *_int8 + + simpleTests := []struct { + src pgtype.Int8 + dst interface{} + expected interface{} + }{ + {src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &i8, expected: int8(42)}, + {src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &i16, expected: int16(42)}, + {src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &i32, expected: int32(42)}, + {src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &i64, expected: int64(42)}, + {src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &i, expected: int(42)}, + {src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &ui8, expected: uint8(42)}, + {src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &ui16, expected: uint16(42)}, + {src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &ui32, expected: uint32(42)}, + {src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &ui64, expected: uint64(42)}, + {src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &ui, expected: uint(42)}, + {src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &_i8, expected: _int8(42)}, + {src: pgtype.Int8{Int: 0, Status: pgtype.Null}, dst: &pi8, expected: ((*int8)(nil))}, + {src: pgtype.Int8{Int: 0, Status: pgtype.Null}, dst: &_pi8, expected: ((*_int8)(nil))}, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + pointerAllocTests := []struct { + src pgtype.Int8 + dst interface{} + expected interface{} + }{ + {src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &pi8, expected: int8(42)}, + {src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &_pi8, expected: _int8(42)}, + } + + for i, tt := range pointerAllocTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.Int8 + dst interface{} + }{ + {src: pgtype.Int8{Int: 150, Status: pgtype.Present}, dst: &i8}, + {src: pgtype.Int8{Int: 40000, Status: pgtype.Present}, dst: &i16}, + {src: pgtype.Int8{Int: 5000000000, Status: pgtype.Present}, dst: &i32}, + {src: pgtype.Int8{Int: -1, Status: pgtype.Present}, dst: &ui8}, + {src: pgtype.Int8{Int: -1, Status: pgtype.Present}, dst: &ui16}, + {src: pgtype.Int8{Int: -1, Status: pgtype.Present}, dst: &ui32}, + {src: pgtype.Int8{Int: -1, Status: pgtype.Present}, dst: &ui64}, + {src: pgtype.Int8{Int: -1, Status: pgtype.Present}, dst: &ui}, + {src: pgtype.Int8{Int: 0, Status: pgtype.Null}, dst: &i64}, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } +} + +func TestInt8MarshalJSON(t *testing.T) { + successfulTests := []struct { + source pgtype.Int8 + result string + }{ + {source: pgtype.Int8{Int: 0, Status: pgtype.Null}, result: "null"}, + {source: pgtype.Int8{Int: 1, Status: pgtype.Present}, result: "1"}, + } + for i, tt := range successfulTests { + r, err := tt.source.MarshalJSON() + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if string(r) != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, string(r)) + } + } +} + +func TestInt8UnmarshalJSON(t *testing.T) { + successfulTests := []struct { + source string + result pgtype.Int8 + }{ + {source: "null", result: pgtype.Int8{Int: 0, Status: pgtype.Null}}, + {source: "1", result: pgtype.Int8{Int: 1, Status: pgtype.Present}}, + } + for i, tt := range successfulTests { + var r pgtype.Int8 + err := r.UnmarshalJSON([]byte(tt.source)) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if r != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} diff --git a/int8range_test.go b/int8range_test.go new file mode 100644 index 00000000..99d4e8a3 --- /dev/null +++ b/int8range_test.go @@ -0,0 +1,28 @@ +package pgtype_test + +import ( + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestInt8rangeTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "Int8range", []interface{}{ + &pgtype.Int8range{LowerType: pgtype.Empty, UpperType: pgtype.Empty, Status: pgtype.Present}, + &pgtype.Int8range{Lower: pgtype.Int8{Int: 1, Status: pgtype.Present}, Upper: pgtype.Int8{Int: 10, Status: pgtype.Present}, LowerType: pgtype.Inclusive, UpperType: pgtype.Exclusive, Status: pgtype.Present}, + &pgtype.Int8range{Lower: pgtype.Int8{Int: -42, Status: pgtype.Present}, Upper: pgtype.Int8{Int: -5, Status: pgtype.Present}, LowerType: pgtype.Inclusive, UpperType: pgtype.Exclusive, Status: pgtype.Present}, + &pgtype.Int8range{Lower: pgtype.Int8{Int: 1, Status: pgtype.Present}, LowerType: pgtype.Inclusive, UpperType: pgtype.Unbounded, Status: pgtype.Present}, + &pgtype.Int8range{Upper: pgtype.Int8{Int: 1, Status: pgtype.Present}, LowerType: pgtype.Unbounded, UpperType: pgtype.Exclusive, Status: pgtype.Present}, + &pgtype.Int8range{Status: pgtype.Null}, + }) +} + +func TestInt8rangeNormalize(t *testing.T) { + testutil.TestSuccessfulNormalize(t, []testutil.NormalizeTest{ + { + SQL: "select Int8range(1, 10, '(]')", + Value: pgtype.Int8range{Lower: pgtype.Int8{Int: 2, Status: pgtype.Present}, Upper: pgtype.Int8{Int: 11, Status: pgtype.Present}, LowerType: pgtype.Inclusive, UpperType: pgtype.Exclusive, Status: pgtype.Present}, + }, + }) +} diff --git a/interval_test.go b/interval_test.go new file mode 100644 index 00000000..1ee094d7 --- /dev/null +++ b/interval_test.go @@ -0,0 +1,74 @@ +package pgtype_test + +import ( + "testing" + "time" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestIntervalTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "interval", []interface{}{ + &pgtype.Interval{Microseconds: 1, Status: pgtype.Present}, + &pgtype.Interval{Microseconds: 1000000, Status: pgtype.Present}, + &pgtype.Interval{Microseconds: 1000001, Status: pgtype.Present}, + &pgtype.Interval{Microseconds: 123202800000000, Status: pgtype.Present}, + &pgtype.Interval{Days: 1, Status: pgtype.Present}, + &pgtype.Interval{Months: 1, Status: pgtype.Present}, + &pgtype.Interval{Months: 12, Status: pgtype.Present}, + &pgtype.Interval{Months: 13, Days: 15, Microseconds: 1000001, Status: pgtype.Present}, + &pgtype.Interval{Microseconds: -1, Status: pgtype.Present}, + &pgtype.Interval{Microseconds: -1000000, Status: pgtype.Present}, + &pgtype.Interval{Microseconds: -1000001, Status: pgtype.Present}, + &pgtype.Interval{Microseconds: -123202800000000, Status: pgtype.Present}, + &pgtype.Interval{Days: -1, Status: pgtype.Present}, + &pgtype.Interval{Months: -1, Status: pgtype.Present}, + &pgtype.Interval{Months: -12, Status: pgtype.Present}, + &pgtype.Interval{Months: -13, Days: -15, Microseconds: -1000001, Status: pgtype.Present}, + &pgtype.Interval{Status: pgtype.Null}, + }) +} + +func TestIntervalNormalize(t *testing.T) { + testutil.TestSuccessfulNormalize(t, []testutil.NormalizeTest{ + { + SQL: "select '1 second'::interval", + Value: &pgtype.Interval{Microseconds: 1000000, Status: pgtype.Present}, + }, + { + SQL: "select '1.000001 second'::interval", + Value: &pgtype.Interval{Microseconds: 1000001, Status: pgtype.Present}, + }, + { + SQL: "select '34223 hours'::interval", + Value: &pgtype.Interval{Microseconds: 123202800000000, Status: pgtype.Present}, + }, + { + SQL: "select '1 day'::interval", + Value: &pgtype.Interval{Days: 1, Status: pgtype.Present}, + }, + { + SQL: "select '1 month'::interval", + Value: &pgtype.Interval{Months: 1, Status: pgtype.Present}, + }, + { + SQL: "select '1 year'::interval", + Value: &pgtype.Interval{Months: 12, Status: pgtype.Present}, + }, + { + SQL: "select '-13 mon'::interval", + Value: &pgtype.Interval{Months: -13, Status: pgtype.Present}, + }, + }) +} + +func TestIntervalLossyConversionToDuration(t *testing.T) { + interval := &pgtype.Interval{Months: 1, Days: 1, Status: pgtype.Present} + var d time.Duration + err := interval.AssignTo(&d) + require.NoError(t, err) + assert.EqualValues(t, int64(2678400000000000), d.Nanoseconds()) +} diff --git a/json_test.go b/json_test.go new file mode 100644 index 00000000..bbd3959e --- /dev/null +++ b/json_test.go @@ -0,0 +1,177 @@ +package pgtype_test + +import ( + "bytes" + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestJSONTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "json", []interface{}{ + &pgtype.JSON{Bytes: []byte("{}"), Status: pgtype.Present}, + &pgtype.JSON{Bytes: []byte("null"), Status: pgtype.Present}, + &pgtype.JSON{Bytes: []byte("42"), Status: pgtype.Present}, + &pgtype.JSON{Bytes: []byte(`"hello"`), Status: pgtype.Present}, + &pgtype.JSON{Status: pgtype.Null}, + }) +} + +func TestJSONSet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.JSON + }{ + {source: "{}", result: pgtype.JSON{Bytes: []byte("{}"), Status: pgtype.Present}}, + {source: []byte("{}"), result: pgtype.JSON{Bytes: []byte("{}"), Status: pgtype.Present}}, + {source: ([]byte)(nil), result: pgtype.JSON{Status: pgtype.Null}}, + {source: (*string)(nil), result: pgtype.JSON{Status: pgtype.Null}}, + {source: []int{1, 2, 3}, result: pgtype.JSON{Bytes: []byte("[1,2,3]"), Status: pgtype.Present}}, + {source: map[string]interface{}{"foo": "bar"}, result: pgtype.JSON{Bytes: []byte(`{"foo":"bar"}`), Status: pgtype.Present}}, + } + + for i, tt := range successfulTests { + var d pgtype.JSON + err := d.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !reflect.DeepEqual(d, tt.result) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, d) + } + } +} + +func TestJSONAssignTo(t *testing.T) { + var s string + var ps *string + var b []byte + + rawStringTests := []struct { + src pgtype.JSON + dst *string + expected string + }{ + {src: pgtype.JSON{Bytes: []byte("{}"), Status: pgtype.Present}, dst: &s, expected: "{}"}, + } + + for i, tt := range rawStringTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if *tt.dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, *tt.dst) + } + } + + rawBytesTests := []struct { + src pgtype.JSON + dst *[]byte + expected []byte + }{ + {src: pgtype.JSON{Bytes: []byte("{}"), Status: pgtype.Present}, dst: &b, expected: []byte("{}")}, + {src: pgtype.JSON{Status: pgtype.Null}, dst: &b, expected: (([]byte)(nil))}, + } + + for i, tt := range rawBytesTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if bytes.Compare(tt.expected, *tt.dst) != 0 { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, *tt.dst) + } + } + + var mapDst map[string]interface{} + type structDst struct { + Name string `json:"name"` + Age int `json:"age"` + } + var strDst structDst + + unmarshalTests := []struct { + src pgtype.JSON + dst interface{} + expected interface{} + }{ + {src: pgtype.JSON{Bytes: []byte(`{"foo":"bar"}`), Status: pgtype.Present}, dst: &mapDst, expected: map[string]interface{}{"foo": "bar"}}, + {src: pgtype.JSON{Bytes: []byte(`{"name":"John","age":42}`), Status: pgtype.Present}, dst: &strDst, expected: structDst{Name: "John", Age: 42}}, + } + for i, tt := range unmarshalTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + pointerAllocTests := []struct { + src pgtype.JSON + dst **string + expected *string + }{ + {src: pgtype.JSON{Status: pgtype.Null}, dst: &ps, expected: ((*string)(nil))}, + } + + for i, tt := range pointerAllocTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if *tt.dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, *tt.dst) + } + } +} + +func TestJSONMarshalJSON(t *testing.T) { + successfulTests := []struct { + source pgtype.JSON + result string + }{ + {source: pgtype.JSON{Status: pgtype.Null}, result: "null"}, + {source: pgtype.JSON{Bytes: []byte("{\"a\": 1}"), Status: pgtype.Present}, result: "{\"a\": 1}"}, + } + for i, tt := range successfulTests { + r, err := tt.source.MarshalJSON() + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if string(r) != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, string(r)) + } + } +} + +func TestJSONUnmarshalJSON(t *testing.T) { + successfulTests := []struct { + source string + result pgtype.JSON + }{ + {source: "null", result: pgtype.JSON{Status: pgtype.Null}}, + {source: "{\"a\": 1}", result: pgtype.JSON{Bytes: []byte("{\"a\": 1}"), Status: pgtype.Present}}, + } + for i, tt := range successfulTests { + var r pgtype.JSON + err := r.UnmarshalJSON([]byte(tt.source)) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if string(r.Bytes) != string(tt.result.Bytes) || r.Status != tt.result.Status { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} diff --git a/jsonb_array_test.go b/jsonb_array_test.go new file mode 100644 index 00000000..65f1777a --- /dev/null +++ b/jsonb_array_test.go @@ -0,0 +1,36 @@ +package pgtype_test + +import ( + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestJSONBArrayTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "jsonb[]", []interface{}{ + &pgtype.JSONBArray{ + Elements: nil, + Dimensions: nil, + Status: pgtype.Present, + }, + &pgtype.JSONBArray{ + Elements: []pgtype.JSONB{ + {Bytes: []byte(`"foo"`), Status: pgtype.Present}, + {Status: pgtype.Null}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.JSONBArray{Status: pgtype.Null}, + &pgtype.JSONBArray{ + Elements: []pgtype.JSONB{ + {Bytes: []byte(`"foo"`), Status: pgtype.Present}, + {Bytes: []byte("null"), Status: pgtype.Present}, + {Bytes: []byte("42"), Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}}, + Status: pgtype.Present, + }, + }) +} diff --git a/jsonb_test.go b/jsonb_test.go new file mode 100644 index 00000000..9ce80d42 --- /dev/null +++ b/jsonb_test.go @@ -0,0 +1,142 @@ +package pgtype_test + +import ( + "bytes" + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestJSONBTranscode(t *testing.T) { + conn := testutil.MustConnectPgx(t) + defer testutil.MustCloseContext(t, conn) + if _, ok := conn.ConnInfo().DataTypeForName("jsonb"); !ok { + t.Skip("Skipping due to no jsonb type") + } + + testutil.TestSuccessfulTranscode(t, "jsonb", []interface{}{ + &pgtype.JSONB{Bytes: []byte("{}"), Status: pgtype.Present}, + &pgtype.JSONB{Bytes: []byte("null"), Status: pgtype.Present}, + &pgtype.JSONB{Bytes: []byte("42"), Status: pgtype.Present}, + &pgtype.JSONB{Bytes: []byte(`"hello"`), Status: pgtype.Present}, + &pgtype.JSONB{Status: pgtype.Null}, + }) +} + +func TestJSONBSet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.JSONB + }{ + {source: "{}", result: pgtype.JSONB{Bytes: []byte("{}"), Status: pgtype.Present}}, + {source: []byte("{}"), result: pgtype.JSONB{Bytes: []byte("{}"), Status: pgtype.Present}}, + {source: ([]byte)(nil), result: pgtype.JSONB{Status: pgtype.Null}}, + {source: (*string)(nil), result: pgtype.JSONB{Status: pgtype.Null}}, + {source: []int{1, 2, 3}, result: pgtype.JSONB{Bytes: []byte("[1,2,3]"), Status: pgtype.Present}}, + {source: map[string]interface{}{"foo": "bar"}, result: pgtype.JSONB{Bytes: []byte(`{"foo":"bar"}`), Status: pgtype.Present}}, + } + + for i, tt := range successfulTests { + var d pgtype.JSONB + err := d.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !reflect.DeepEqual(d, tt.result) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, d) + } + } +} + +func TestJSONBAssignTo(t *testing.T) { + var s string + var ps *string + var b []byte + + rawStringTests := []struct { + src pgtype.JSONB + dst *string + expected string + }{ + {src: pgtype.JSONB{Bytes: []byte("{}"), Status: pgtype.Present}, dst: &s, expected: "{}"}, + } + + for i, tt := range rawStringTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if *tt.dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, *tt.dst) + } + } + + rawBytesTests := []struct { + src pgtype.JSONB + dst *[]byte + expected []byte + }{ + {src: pgtype.JSONB{Bytes: []byte("{}"), Status: pgtype.Present}, dst: &b, expected: []byte("{}")}, + {src: pgtype.JSONB{Status: pgtype.Null}, dst: &b, expected: (([]byte)(nil))}, + } + + for i, tt := range rawBytesTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if bytes.Compare(tt.expected, *tt.dst) != 0 { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, *tt.dst) + } + } + + var mapDst map[string]interface{} + type structDst struct { + Name string `json:"name"` + Age int `json:"age"` + } + var strDst structDst + + unmarshalTests := []struct { + src pgtype.JSONB + dst interface{} + expected interface{} + }{ + {src: pgtype.JSONB{Bytes: []byte(`{"foo":"bar"}`), Status: pgtype.Present}, dst: &mapDst, expected: map[string]interface{}{"foo": "bar"}}, + {src: pgtype.JSONB{Bytes: []byte(`{"name":"John","age":42}`), Status: pgtype.Present}, dst: &strDst, expected: structDst{Name: "John", Age: 42}}, + } + for i, tt := range unmarshalTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + pointerAllocTests := []struct { + src pgtype.JSONB + dst **string + expected *string + }{ + {src: pgtype.JSONB{Status: pgtype.Null}, dst: &ps, expected: ((*string)(nil))}, + } + + for i, tt := range pointerAllocTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if *tt.dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, *tt.dst) + } + } +} diff --git a/line_test.go b/line_test.go new file mode 100644 index 00000000..f697ac43 --- /dev/null +++ b/line_test.go @@ -0,0 +1,38 @@ +package pgtype_test + +import ( + "context" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestLineTranscode(t *testing.T) { + conn := testutil.MustConnectPgx(t) + if _, ok := conn.ConnInfo().DataTypeForName("line"); !ok { + t.Skip("Skipping due to no line type") + } + + // line may exist but not be usable on 9.3 :( + var isPG93 bool + err := conn.QueryRow(context.Background(), "select version() ~ '9.3'").Scan(&isPG93) + if err != nil { + t.Fatal(err) + } + if isPG93 { + t.Skip("Skipping due to unimplemented line type in PG 9.3") + } + + testutil.TestSuccessfulTranscode(t, "line", []interface{}{ + &pgtype.Line{ + A: 1.23, B: 4.56, C: 7.89012345, + Status: pgtype.Present, + }, + &pgtype.Line{ + A: -1.23, B: -4.56, C: -7.89, + Status: pgtype.Present, + }, + &pgtype.Line{Status: pgtype.Null}, + }) +} diff --git a/lseg_test.go b/lseg_test.go new file mode 100644 index 00000000..b75297cc --- /dev/null +++ b/lseg_test.go @@ -0,0 +1,22 @@ +package pgtype_test + +import ( + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestLsegTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "lseg", []interface{}{ + &pgtype.Lseg{ + P: [2]pgtype.Vec2{{3.14, 1.678}, {7.1, 5.2345678901}}, + Status: pgtype.Present, + }, + &pgtype.Lseg{ + P: [2]pgtype.Vec2{{7.1, 1.678}, {-13.14, -5.234}}, + Status: pgtype.Present, + }, + &pgtype.Lseg{Status: pgtype.Null}, + }) +} diff --git a/macaddr_array_test.go b/macaddr_array_test.go new file mode 100644 index 00000000..c1a8b72d --- /dev/null +++ b/macaddr_array_test.go @@ -0,0 +1,262 @@ +package pgtype_test + +import ( + "net" + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestMacaddrArrayTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "macaddr[]", []interface{}{ + &pgtype.MacaddrArray{ + Elements: nil, + Dimensions: nil, + Status: pgtype.Present, + }, + &pgtype.MacaddrArray{ + Elements: []pgtype.Macaddr{ + {Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present}, + {Status: pgtype.Null}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.MacaddrArray{Status: pgtype.Null}, + }) +} + +func TestMacaddrArraySet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.MacaddrArray + }{ + { + source: []net.HardwareAddr{mustParseMacaddr(t, "01:23:45:67:89:ab")}, + result: pgtype.MacaddrArray{ + Elements: []pgtype.Macaddr{{Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: (([]net.HardwareAddr)(nil)), + result: pgtype.MacaddrArray{Status: pgtype.Null}, + }, + { + source: [][]net.HardwareAddr{ + {mustParseMacaddr(t, "01:23:45:67:89:ab")}, + {mustParseMacaddr(t, "cd:ef:01:23:45:67")}}, + result: pgtype.MacaddrArray{ + Elements: []pgtype.Macaddr{ + {Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present}, + {Addr: mustParseMacaddr(t, "cd:ef:01:23:45:67"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [][][][]net.HardwareAddr{ + {{{ + mustParseMacaddr(t, "01:23:45:67:89:ab"), + mustParseMacaddr(t, "cd:ef:01:23:45:67"), + mustParseMacaddr(t, "89:ab:cd:ef:01:23")}}}, + {{{ + mustParseMacaddr(t, "45:67:89:ab:cd:ef"), + mustParseMacaddr(t, "fe:dc:ba:98:76:54"), + mustParseMacaddr(t, "32:10:fe:dc:ba:98")}}}}, + result: pgtype.MacaddrArray{ + Elements: []pgtype.Macaddr{ + {Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present}, + {Addr: mustParseMacaddr(t, "cd:ef:01:23:45:67"), Status: pgtype.Present}, + {Addr: mustParseMacaddr(t, "89:ab:cd:ef:01:23"), Status: pgtype.Present}, + {Addr: mustParseMacaddr(t, "45:67:89:ab:cd:ef"), Status: pgtype.Present}, + {Addr: mustParseMacaddr(t, "fe:dc:ba:98:76:54"), Status: pgtype.Present}, + {Addr: mustParseMacaddr(t, "32:10:fe:dc:ba:98"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + { + source: [2][1]net.HardwareAddr{ + {mustParseMacaddr(t, "01:23:45:67:89:ab")}, + {mustParseMacaddr(t, "cd:ef:01:23:45:67")}}, + result: pgtype.MacaddrArray{ + Elements: []pgtype.Macaddr{ + {Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present}, + {Addr: mustParseMacaddr(t, "cd:ef:01:23:45:67"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [2][1][1][3]net.HardwareAddr{ + {{{ + mustParseMacaddr(t, "01:23:45:67:89:ab"), + mustParseMacaddr(t, "cd:ef:01:23:45:67"), + mustParseMacaddr(t, "89:ab:cd:ef:01:23")}}}, + {{{ + mustParseMacaddr(t, "45:67:89:ab:cd:ef"), + mustParseMacaddr(t, "fe:dc:ba:98:76:54"), + mustParseMacaddr(t, "32:10:fe:dc:ba:98")}}}}, + result: pgtype.MacaddrArray{ + Elements: []pgtype.Macaddr{ + {Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present}, + {Addr: mustParseMacaddr(t, "cd:ef:01:23:45:67"), Status: pgtype.Present}, + {Addr: mustParseMacaddr(t, "89:ab:cd:ef:01:23"), Status: pgtype.Present}, + {Addr: mustParseMacaddr(t, "45:67:89:ab:cd:ef"), Status: pgtype.Present}, + {Addr: mustParseMacaddr(t, "fe:dc:ba:98:76:54"), Status: pgtype.Present}, + {Addr: mustParseMacaddr(t, "32:10:fe:dc:ba:98"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + } + + for i, tt := range successfulTests { + var r pgtype.MacaddrArray + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !reflect.DeepEqual(r, tt.result) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestMacaddrArrayAssignTo(t *testing.T) { + var macaddrSlice []net.HardwareAddr + var macaddrSliceDim2 [][]net.HardwareAddr + var macaddrSliceDim4 [][][][]net.HardwareAddr + var macaddrArrayDim2 [2][1]net.HardwareAddr + var macaddrArrayDim4 [2][1][1][3]net.HardwareAddr + + simpleTests := []struct { + src pgtype.MacaddrArray + dst interface{} + expected interface{} + }{ + { + src: pgtype.MacaddrArray{ + Elements: []pgtype.Macaddr{{Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &macaddrSlice, + expected: []net.HardwareAddr{mustParseMacaddr(t, "01:23:45:67:89:ab")}, + }, + { + src: pgtype.MacaddrArray{ + Elements: []pgtype.Macaddr{{Status: pgtype.Null}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &macaddrSlice, + expected: []net.HardwareAddr{nil}, + }, + { + src: pgtype.MacaddrArray{Status: pgtype.Null}, + dst: &macaddrSlice, + expected: (([]net.HardwareAddr)(nil)), + }, + { + src: pgtype.MacaddrArray{Status: pgtype.Present}, + dst: &macaddrSlice, + expected: []net.HardwareAddr{}, + }, + { + src: pgtype.MacaddrArray{ + Elements: []pgtype.Macaddr{ + {Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present}, + {Addr: mustParseMacaddr(t, "cd:ef:01:23:45:67"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &macaddrSliceDim2, + expected: [][]net.HardwareAddr{ + {mustParseMacaddr(t, "01:23:45:67:89:ab")}, + {mustParseMacaddr(t, "cd:ef:01:23:45:67")}}, + }, + { + src: pgtype.MacaddrArray{ + Elements: []pgtype.Macaddr{ + {Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present}, + {Addr: mustParseMacaddr(t, "cd:ef:01:23:45:67"), Status: pgtype.Present}, + {Addr: mustParseMacaddr(t, "89:ab:cd:ef:01:23"), Status: pgtype.Present}, + {Addr: mustParseMacaddr(t, "45:67:89:ab:cd:ef"), Status: pgtype.Present}, + {Addr: mustParseMacaddr(t, "fe:dc:ba:98:76:54"), Status: pgtype.Present}, + {Addr: mustParseMacaddr(t, "32:10:fe:dc:ba:98"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + dst: &macaddrSliceDim4, + expected: [][][][]net.HardwareAddr{ + {{{ + mustParseMacaddr(t, "01:23:45:67:89:ab"), + mustParseMacaddr(t, "cd:ef:01:23:45:67"), + mustParseMacaddr(t, "89:ab:cd:ef:01:23")}}}, + {{{ + mustParseMacaddr(t, "45:67:89:ab:cd:ef"), + mustParseMacaddr(t, "fe:dc:ba:98:76:54"), + mustParseMacaddr(t, "32:10:fe:dc:ba:98")}}}}, + }, + { + src: pgtype.MacaddrArray{ + Elements: []pgtype.Macaddr{ + {Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present}, + {Addr: mustParseMacaddr(t, "cd:ef:01:23:45:67"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &macaddrArrayDim2, + expected: [2][1]net.HardwareAddr{ + {mustParseMacaddr(t, "01:23:45:67:89:ab")}, + {mustParseMacaddr(t, "cd:ef:01:23:45:67")}}, + }, + { + src: pgtype.MacaddrArray{ + Elements: []pgtype.Macaddr{ + {Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present}, + {Addr: mustParseMacaddr(t, "cd:ef:01:23:45:67"), Status: pgtype.Present}, + {Addr: mustParseMacaddr(t, "89:ab:cd:ef:01:23"), Status: pgtype.Present}, + {Addr: mustParseMacaddr(t, "45:67:89:ab:cd:ef"), Status: pgtype.Present}, + {Addr: mustParseMacaddr(t, "fe:dc:ba:98:76:54"), Status: pgtype.Present}, + {Addr: mustParseMacaddr(t, "32:10:fe:dc:ba:98"), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + dst: &macaddrArrayDim4, + expected: [2][1][1][3]net.HardwareAddr{ + {{{ + mustParseMacaddr(t, "01:23:45:67:89:ab"), + mustParseMacaddr(t, "cd:ef:01:23:45:67"), + mustParseMacaddr(t, "89:ab:cd:ef:01:23")}}}, + {{{ + mustParseMacaddr(t, "45:67:89:ab:cd:ef"), + mustParseMacaddr(t, "fe:dc:ba:98:76:54"), + mustParseMacaddr(t, "32:10:fe:dc:ba:98")}}}}, + }, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } +} diff --git a/macaddr_test.go b/macaddr_test.go new file mode 100644 index 00000000..364a8914 --- /dev/null +++ b/macaddr_test.go @@ -0,0 +1,78 @@ +package pgtype_test + +import ( + "bytes" + "net" + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestMacaddrTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "macaddr", []interface{}{ + &pgtype.Macaddr{Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present}, + &pgtype.Macaddr{Status: pgtype.Null}, + }) +} + +func TestMacaddrSet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.Macaddr + }{ + { + source: mustParseMacaddr(t, "01:23:45:67:89:ab"), + result: pgtype.Macaddr{Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present}, + }, + { + source: "01:23:45:67:89:ab", + result: pgtype.Macaddr{Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present}, + }, + } + + for i, tt := range successfulTests { + var r pgtype.Macaddr + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !reflect.DeepEqual(r, tt.result) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestMacaddrAssignTo(t *testing.T) { + { + src := pgtype.Macaddr{Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present} + var dst net.HardwareAddr + expected := mustParseMacaddr(t, "01:23:45:67:89:ab") + + err := src.AssignTo(&dst) + if err != nil { + t.Error(err) + } + + if bytes.Compare([]byte(dst), []byte(expected)) != 0 { + t.Errorf("expected %v to assign %v, but result was %v", src, expected, dst) + } + } + + { + src := pgtype.Macaddr{Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present} + var dst string + expected := "01:23:45:67:89:ab" + + err := src.AssignTo(&dst) + if err != nil { + t.Error(err) + } + + if dst != expected { + t.Errorf("expected %v to assign %v, but result was %v", src, expected, dst) + } + } +} diff --git a/name_test.go b/name_test.go new file mode 100644 index 00000000..75329b01 --- /dev/null +++ b/name_test.go @@ -0,0 +1,98 @@ +package pgtype_test + +import ( + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestNameTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "name", []interface{}{ + &pgtype.Name{String: "", Status: pgtype.Present}, + &pgtype.Name{String: "foo", Status: pgtype.Present}, + &pgtype.Name{Status: pgtype.Null}, + }) +} + +func TestNameSet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.Name + }{ + {source: "foo", result: pgtype.Name{String: "foo", Status: pgtype.Present}}, + {source: _string("bar"), result: pgtype.Name{String: "bar", Status: pgtype.Present}}, + {source: (*string)(nil), result: pgtype.Name{Status: pgtype.Null}}, + } + + for i, tt := range successfulTests { + var d pgtype.Name + err := d.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if d != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, d) + } + } +} + +func TestNameAssignTo(t *testing.T) { + var s string + var ps *string + + simpleTests := []struct { + src pgtype.Name + dst interface{} + expected interface{} + }{ + {src: pgtype.Name{String: "foo", Status: pgtype.Present}, dst: &s, expected: "foo"}, + {src: pgtype.Name{Status: pgtype.Null}, dst: &ps, expected: ((*string)(nil))}, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + pointerAllocTests := []struct { + src pgtype.Name + dst interface{} + expected interface{} + }{ + {src: pgtype.Name{String: "foo", Status: pgtype.Present}, dst: &ps, expected: "foo"}, + } + + for i, tt := range pointerAllocTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.Name + dst interface{} + }{ + {src: pgtype.Name{Status: pgtype.Null}, dst: &s}, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } +} diff --git a/numeric_array_test.go b/numeric_array_test.go new file mode 100644 index 00000000..7c1e8c3b --- /dev/null +++ b/numeric_array_test.go @@ -0,0 +1,305 @@ +package pgtype_test + +import ( + "math" + "math/big" + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestNumericArrayTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "numeric[]", []interface{}{ + &pgtype.NumericArray{ + Elements: nil, + Dimensions: nil, + Status: pgtype.Present, + }, + &pgtype.NumericArray{ + Elements: []pgtype.Numeric{ + {Int: big.NewInt(1), Status: pgtype.Present}, + {Status: pgtype.Null}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.NumericArray{Status: pgtype.Null}, + &pgtype.NumericArray{ + Elements: []pgtype.Numeric{ + {Int: big.NewInt(1), Status: pgtype.Present}, + {Int: big.NewInt(2), Status: pgtype.Present}, + {Int: big.NewInt(3), Status: pgtype.Present}, + {Int: big.NewInt(4), Status: pgtype.Present}, + {Status: pgtype.Null}, + {Int: big.NewInt(6), Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}, {Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.NumericArray{ + Elements: []pgtype.Numeric{ + {Int: big.NewInt(1), Status: pgtype.Present}, + {Int: big.NewInt(2), Status: pgtype.Present}, + {Int: big.NewInt(3), Status: pgtype.Present}, + {Int: big.NewInt(4), Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{ + {Length: 2, LowerBound: 4}, + {Length: 2, LowerBound: 2}, + }, + Status: pgtype.Present, + }, + }) +} + +func TestNumericArraySet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.NumericArray + }{ + { + source: []float32{1}, + result: pgtype.NumericArray{ + Elements: []pgtype.Numeric{{Int: big.NewInt(1), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: []float32{float32(math.Copysign(0, -1))}, + result: pgtype.NumericArray{ + Elements: []pgtype.Numeric{{Int: big.NewInt(0), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: []float64{1}, + result: pgtype.NumericArray{ + Elements: []pgtype.Numeric{{Int: big.NewInt(1), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: []float64{math.Copysign(0, -1)}, + result: pgtype.NumericArray{ + Elements: []pgtype.Numeric{{Int: big.NewInt(0), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: (([]float32)(nil)), + result: pgtype.NumericArray{Status: pgtype.Null}, + }, + { + source: [][]float32{{1}, {2}}, + result: pgtype.NumericArray{ + Elements: []pgtype.Numeric{{Int: big.NewInt(1), Status: pgtype.Present}, {Int: big.NewInt(2), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [][][][]float32{{{{1, 2, 3}}}, {{{4, 5, 6}}}}, + result: pgtype.NumericArray{ + Elements: []pgtype.Numeric{ + {Int: big.NewInt(1), Status: pgtype.Present}, + {Int: big.NewInt(2), Status: pgtype.Present}, + {Int: big.NewInt(3), Status: pgtype.Present}, + {Int: big.NewInt(4), Status: pgtype.Present}, + {Int: big.NewInt(5), Status: pgtype.Present}, + {Int: big.NewInt(6), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + { + source: [2][1]float32{{1}, {2}}, + result: pgtype.NumericArray{ + Elements: []pgtype.Numeric{{Int: big.NewInt(1), Status: pgtype.Present}, {Int: big.NewInt(2), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [2][1][1][3]float32{{{{1, 2, 3}}}, {{{4, 5, 6}}}}, + result: pgtype.NumericArray{ + Elements: []pgtype.Numeric{ + {Int: big.NewInt(1), Status: pgtype.Present}, + {Int: big.NewInt(2), Status: pgtype.Present}, + {Int: big.NewInt(3), Status: pgtype.Present}, + {Int: big.NewInt(4), Status: pgtype.Present}, + {Int: big.NewInt(5), Status: pgtype.Present}, + {Int: big.NewInt(6), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + } + + for i, tt := range successfulTests { + var r pgtype.NumericArray + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !reflect.DeepEqual(r, tt.result) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestNumericArrayAssignTo(t *testing.T) { + var float32Slice []float32 + var float64Slice []float64 + var float32SliceDim2 [][]float32 + var float32SliceDim4 [][][][]float32 + var float32ArrayDim2 [2][1]float32 + var float32ArrayDim4 [2][1][1][3]float32 + + simpleTests := []struct { + src pgtype.NumericArray + dst interface{} + expected interface{} + }{ + { + src: pgtype.NumericArray{ + Elements: []pgtype.Numeric{{Int: big.NewInt(1), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &float32Slice, + expected: []float32{1}, + }, + { + src: pgtype.NumericArray{ + Elements: []pgtype.Numeric{{Int: big.NewInt(1), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &float64Slice, + expected: []float64{1}, + }, + { + src: pgtype.NumericArray{Status: pgtype.Null}, + dst: &float32Slice, + expected: (([]float32)(nil)), + }, + { + src: pgtype.NumericArray{Status: pgtype.Present}, + dst: &float32Slice, + expected: []float32{}, + }, + { + src: pgtype.NumericArray{ + Elements: []pgtype.Numeric{{Int: big.NewInt(1), Status: pgtype.Present}, {Int: big.NewInt(2), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &float32SliceDim2, + expected: [][]float32{{1}, {2}}, + }, + { + src: pgtype.NumericArray{ + Elements: []pgtype.Numeric{ + {Int: big.NewInt(1), Status: pgtype.Present}, + {Int: big.NewInt(2), Status: pgtype.Present}, + {Int: big.NewInt(3), Status: pgtype.Present}, + {Int: big.NewInt(4), Status: pgtype.Present}, + {Int: big.NewInt(5), Status: pgtype.Present}, + {Int: big.NewInt(6), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + dst: &float32SliceDim4, + expected: [][][][]float32{{{{1, 2, 3}}}, {{{4, 5, 6}}}}, + }, + { + src: pgtype.NumericArray{ + Elements: []pgtype.Numeric{{Int: big.NewInt(1), Status: pgtype.Present}, {Int: big.NewInt(2), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &float32ArrayDim2, + expected: [2][1]float32{{1}, {2}}, + }, + { + src: pgtype.NumericArray{ + Elements: []pgtype.Numeric{ + {Int: big.NewInt(1), Status: pgtype.Present}, + {Int: big.NewInt(2), Status: pgtype.Present}, + {Int: big.NewInt(3), Status: pgtype.Present}, + {Int: big.NewInt(4), Status: pgtype.Present}, + {Int: big.NewInt(5), Status: pgtype.Present}, + {Int: big.NewInt(6), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + dst: &float32ArrayDim4, + expected: [2][1][1][3]float32{{{{1, 2, 3}}}, {{{4, 5, 6}}}}, + }, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.NumericArray + dst interface{} + }{ + { + src: pgtype.NumericArray{ + Elements: []pgtype.Numeric{{Status: pgtype.Null}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &float32Slice, + }, + { + src: pgtype.NumericArray{ + Elements: []pgtype.Numeric{{Int: big.NewInt(1), Status: pgtype.Present}, {Int: big.NewInt(2), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &float32ArrayDim2, + }, + { + src: pgtype.NumericArray{ + Elements: []pgtype.Numeric{{Int: big.NewInt(1), Status: pgtype.Present}, {Int: big.NewInt(2), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &float32Slice, + }, + { + src: pgtype.NumericArray{ + Elements: []pgtype.Numeric{{Int: big.NewInt(1), Status: pgtype.Present}, {Int: big.NewInt(2), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &float32ArrayDim4, + }, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } + +} diff --git a/numeric_test.go b/numeric_test.go new file mode 100644 index 00000000..81595cb3 --- /dev/null +++ b/numeric_test.go @@ -0,0 +1,389 @@ +package pgtype_test + +import ( + "math" + "math/big" + "math/rand" + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +// For test purposes only. Note that it does not normalize values. e.g. (Int: 1, Exp: 3) will not equal (Int: 1000, Exp: 0) +func numericEqual(left, right *pgtype.Numeric) bool { + return left.Status == right.Status && + left.Exp == right.Exp && + ((left.Int == nil && right.Int == nil) || (left.Int != nil && right.Int != nil && left.Int.Cmp(right.Int) == 0)) && + left.NaN == right.NaN +} + +// For test purposes only. +func numericNormalizedEqual(left, right *pgtype.Numeric) bool { + if left.Status != right.Status { + return false + } + + normLeft := &pgtype.Numeric{Int: (&big.Int{}).Set(left.Int), Status: left.Status} + normRight := &pgtype.Numeric{Int: (&big.Int{}).Set(right.Int), Status: right.Status} + + if left.Exp < right.Exp { + mul := (&big.Int{}).Exp(big.NewInt(10), big.NewInt(int64(right.Exp-left.Exp)), nil) + normRight.Int.Mul(normRight.Int, mul) + } else if left.Exp > right.Exp { + mul := (&big.Int{}).Exp(big.NewInt(10), big.NewInt(int64(left.Exp-right.Exp)), nil) + normLeft.Int.Mul(normLeft.Int, mul) + } + + return normLeft.Int.Cmp(normRight.Int) == 0 +} + +func mustParseBigInt(t *testing.T, src string) *big.Int { + i := &big.Int{} + if _, ok := i.SetString(src, 10); !ok { + t.Fatalf("could not parse big.Int: %s", src) + } + return i +} + +func TestNumericNormalize(t *testing.T) { + testutil.TestSuccessfulNormalize(t, []testutil.NormalizeTest{ + { + SQL: "select '0'::numeric", + Value: &pgtype.Numeric{Int: big.NewInt(0), Exp: 0, Status: pgtype.Present}, + }, + { + SQL: "select '1'::numeric", + Value: &pgtype.Numeric{Int: big.NewInt(1), Exp: 0, Status: pgtype.Present}, + }, + { + SQL: "select '10.00'::numeric", + Value: &pgtype.Numeric{Int: big.NewInt(1000), Exp: -2, Status: pgtype.Present}, + }, + { + SQL: "select '1e-3'::numeric", + Value: &pgtype.Numeric{Int: big.NewInt(1), Exp: -3, Status: pgtype.Present}, + }, + { + SQL: "select '-1'::numeric", + Value: &pgtype.Numeric{Int: big.NewInt(-1), Exp: 0, Status: pgtype.Present}, + }, + { + SQL: "select '10000'::numeric", + Value: &pgtype.Numeric{Int: big.NewInt(1), Exp: 4, Status: pgtype.Present}, + }, + { + SQL: "select '3.14'::numeric", + Value: &pgtype.Numeric{Int: big.NewInt(314), Exp: -2, Status: pgtype.Present}, + }, + { + SQL: "select '1.1'::numeric", + Value: &pgtype.Numeric{Int: big.NewInt(11), Exp: -1, Status: pgtype.Present}, + }, + { + SQL: "select '100010001'::numeric", + Value: &pgtype.Numeric{Int: big.NewInt(100010001), Exp: 0, Status: pgtype.Present}, + }, + { + SQL: "select '100010001.0001'::numeric", + Value: &pgtype.Numeric{Int: big.NewInt(1000100010001), Exp: -4, Status: pgtype.Present}, + }, + { + SQL: "select '4237234789234789289347892374324872138321894178943189043890124832108934.43219085471578891547854892438945012347981'::numeric", + Value: &pgtype.Numeric{ + Int: mustParseBigInt(t, "423723478923478928934789237432487213832189417894318904389012483210893443219085471578891547854892438945012347981"), + Exp: -41, + Status: pgtype.Present, + }, + }, + { + SQL: "select '0.8925092023480223478923478978978937897879595901237890234789243679037419057877231734823098432903527585734549035904590854890345905434578345789347890402348952348905890489054234237489234987723894789234'::numeric", + Value: &pgtype.Numeric{ + Int: mustParseBigInt(t, "8925092023480223478923478978978937897879595901237890234789243679037419057877231734823098432903527585734549035904590854890345905434578345789347890402348952348905890489054234237489234987723894789234"), + Exp: -196, + Status: pgtype.Present, + }, + }, + { + SQL: "select '0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000123'::numeric", + Value: &pgtype.Numeric{ + Int: mustParseBigInt(t, "123"), + Exp: -186, + Status: pgtype.Present, + }, + }, + }) +} + +func TestNumericTranscode(t *testing.T) { + testutil.TestSuccessfulTranscodeEqFunc(t, "numeric", []interface{}{ + &pgtype.Numeric{NaN: true, Status: pgtype.Present}, + + &pgtype.Numeric{Int: big.NewInt(0), Exp: 0, Status: pgtype.Present}, + &pgtype.Numeric{Int: big.NewInt(1), Exp: 0, Status: pgtype.Present}, + &pgtype.Numeric{Int: big.NewInt(-1), Exp: 0, Status: pgtype.Present}, + &pgtype.Numeric{Int: big.NewInt(1), Exp: 6, Status: pgtype.Present}, + + // preserves significant zeroes + &pgtype.Numeric{Int: big.NewInt(10000000), Exp: -1, Status: pgtype.Present}, + &pgtype.Numeric{Int: big.NewInt(10000000), Exp: -2, Status: pgtype.Present}, + &pgtype.Numeric{Int: big.NewInt(10000000), Exp: -3, Status: pgtype.Present}, + &pgtype.Numeric{Int: big.NewInt(10000000), Exp: -4, Status: pgtype.Present}, + &pgtype.Numeric{Int: big.NewInt(10000000), Exp: -5, Status: pgtype.Present}, + &pgtype.Numeric{Int: big.NewInt(10000000), Exp: -6, Status: pgtype.Present}, + + &pgtype.Numeric{Int: big.NewInt(314), Exp: -2, Status: pgtype.Present}, + &pgtype.Numeric{Int: big.NewInt(123), Exp: -7, Status: pgtype.Present}, + &pgtype.Numeric{Int: big.NewInt(123), Exp: -8, Status: pgtype.Present}, + &pgtype.Numeric{Int: big.NewInt(123), Exp: -9, Status: pgtype.Present}, + &pgtype.Numeric{Int: big.NewInt(123), Exp: -1500, Status: pgtype.Present}, + &pgtype.Numeric{Int: mustParseBigInt(t, "2437"), Exp: 23790, Status: pgtype.Present}, + &pgtype.Numeric{Int: mustParseBigInt(t, "243723409723490243842378942378901237502734019231380123"), Exp: 23790, Status: pgtype.Present}, + &pgtype.Numeric{Int: mustParseBigInt(t, "43723409723490243842378942378901237502734019231380123"), Exp: 80, Status: pgtype.Present}, + &pgtype.Numeric{Int: mustParseBigInt(t, "3723409723490243842378942378901237502734019231380123"), Exp: 81, Status: pgtype.Present}, + &pgtype.Numeric{Int: mustParseBigInt(t, "723409723490243842378942378901237502734019231380123"), Exp: 82, Status: pgtype.Present}, + &pgtype.Numeric{Int: mustParseBigInt(t, "23409723490243842378942378901237502734019231380123"), Exp: 83, Status: pgtype.Present}, + &pgtype.Numeric{Int: mustParseBigInt(t, "3409723490243842378942378901237502734019231380123"), Exp: 84, Status: pgtype.Present}, + &pgtype.Numeric{Int: mustParseBigInt(t, "913423409823409243892349028349023482934092340892390101"), Exp: -14021, Status: pgtype.Present}, + &pgtype.Numeric{Int: mustParseBigInt(t, "13423409823409243892349028349023482934092340892390101"), Exp: -90, Status: pgtype.Present}, + &pgtype.Numeric{Int: mustParseBigInt(t, "3423409823409243892349028349023482934092340892390101"), Exp: -91, Status: pgtype.Present}, + &pgtype.Numeric{Int: mustParseBigInt(t, "423409823409243892349028349023482934092340892390101"), Exp: -92, Status: pgtype.Present}, + &pgtype.Numeric{Int: mustParseBigInt(t, "23409823409243892349028349023482934092340892390101"), Exp: -93, Status: pgtype.Present}, + &pgtype.Numeric{Int: mustParseBigInt(t, "3409823409243892349028349023482934092340892390101"), Exp: -94, Status: pgtype.Present}, + &pgtype.Numeric{Status: pgtype.Null}, + }, func(aa, bb interface{}) bool { + a := aa.(pgtype.Numeric) + b := bb.(pgtype.Numeric) + + return numericEqual(&a, &b) + }) + +} + +func TestNumericTranscodeFuzz(t *testing.T) { + r := rand.New(rand.NewSource(0)) + max := &big.Int{} + max.SetString("9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999", 10) + + values := make([]interface{}, 0, 2000) + for i := 0; i < 10; i++ { + for j := -50; j < 50; j++ { + num := (&big.Int{}).Rand(r, max) + negNum := &big.Int{} + negNum.Neg(num) + values = append(values, &pgtype.Numeric{Int: num, Exp: int32(j), Status: pgtype.Present}) + values = append(values, &pgtype.Numeric{Int: negNum, Exp: int32(j), Status: pgtype.Present}) + } + } + + testutil.TestSuccessfulTranscodeEqFunc(t, "numeric", values, + func(aa, bb interface{}) bool { + a := aa.(pgtype.Numeric) + b := bb.(pgtype.Numeric) + + return numericNormalizedEqual(&a, &b) + }) +} + +func TestNumericSet(t *testing.T) { + successfulTests := []struct { + source interface{} + result *pgtype.Numeric + }{ + {source: float32(1), result: &pgtype.Numeric{Int: big.NewInt(1), Status: pgtype.Present}}, + {source: float32(math.Copysign(0, -1)), result: &pgtype.Numeric{Int: big.NewInt(0), Status: pgtype.Present}}, + {source: float64(1), result: &pgtype.Numeric{Int: big.NewInt(1), Status: pgtype.Present}}, + {source: float64(math.Copysign(0, -1)), result: &pgtype.Numeric{Int: big.NewInt(0), Status: pgtype.Present}}, + {source: int8(1), result: &pgtype.Numeric{Int: big.NewInt(1), Status: pgtype.Present}}, + {source: int16(1), result: &pgtype.Numeric{Int: big.NewInt(1), Status: pgtype.Present}}, + {source: int32(1), result: &pgtype.Numeric{Int: big.NewInt(1), Status: pgtype.Present}}, + {source: int64(1), result: &pgtype.Numeric{Int: big.NewInt(1), Status: pgtype.Present}}, + {source: int8(-1), result: &pgtype.Numeric{Int: big.NewInt(-1), Status: pgtype.Present}}, + {source: int16(-1), result: &pgtype.Numeric{Int: big.NewInt(-1), Status: pgtype.Present}}, + {source: int32(-1), result: &pgtype.Numeric{Int: big.NewInt(-1), Status: pgtype.Present}}, + {source: int64(-1), result: &pgtype.Numeric{Int: big.NewInt(-1), Status: pgtype.Present}}, + {source: uint8(1), result: &pgtype.Numeric{Int: big.NewInt(1), Status: pgtype.Present}}, + {source: uint16(1), result: &pgtype.Numeric{Int: big.NewInt(1), Status: pgtype.Present}}, + {source: uint32(1), result: &pgtype.Numeric{Int: big.NewInt(1), Status: pgtype.Present}}, + {source: uint64(1), result: &pgtype.Numeric{Int: big.NewInt(1), Status: pgtype.Present}}, + {source: "1", result: &pgtype.Numeric{Int: big.NewInt(1), Status: pgtype.Present}}, + {source: _int8(1), result: &pgtype.Numeric{Int: big.NewInt(1), Status: pgtype.Present}}, + {source: float64(1000), result: &pgtype.Numeric{Int: big.NewInt(1), Exp: 3, Status: pgtype.Present}}, + {source: float64(1234), result: &pgtype.Numeric{Int: big.NewInt(1234), Exp: 0, Status: pgtype.Present}}, + {source: float64(12345678900), result: &pgtype.Numeric{Int: big.NewInt(123456789), Exp: 2, Status: pgtype.Present}}, + {source: float64(12345.678901), result: &pgtype.Numeric{Int: big.NewInt(12345678901), Exp: -6, Status: pgtype.Present}}, + {source: math.NaN(), result: &pgtype.Numeric{Int: nil, Exp: 0, Status: pgtype.Present, NaN: true}}, + {source: float32(math.NaN()), result: &pgtype.Numeric{Int: nil, Exp: 0, Status: pgtype.Present, NaN: true}}, + } + + for i, tt := range successfulTests { + r := &pgtype.Numeric{} + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !numericEqual(r, tt.result) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestNumericAssignTo(t *testing.T) { + var i8 int8 + var i16 int16 + var i32 int32 + var i64 int64 + var i int + var ui8 uint8 + var ui16 uint16 + var ui32 uint32 + var ui64 uint64 + var ui uint + var pi8 *int8 + var _i8 _int8 + var _pi8 *_int8 + var f32 float32 + var f64 float64 + var pf32 *float32 + var pf64 *float64 + + simpleTests := []struct { + src *pgtype.Numeric + dst interface{} + expected interface{} + }{ + {src: &pgtype.Numeric{Int: big.NewInt(42), Status: pgtype.Present}, dst: &f32, expected: float32(42)}, + {src: &pgtype.Numeric{Int: big.NewInt(42), Status: pgtype.Present}, dst: &f64, expected: float64(42)}, + {src: &pgtype.Numeric{Int: big.NewInt(42), Exp: -1, Status: pgtype.Present}, dst: &f32, expected: float32(4.2)}, + {src: &pgtype.Numeric{Int: big.NewInt(42), Exp: -1, Status: pgtype.Present}, dst: &f64, expected: float64(4.2)}, + {src: &pgtype.Numeric{Int: big.NewInt(42), Status: pgtype.Present}, dst: &i16, expected: int16(42)}, + {src: &pgtype.Numeric{Int: big.NewInt(42), Status: pgtype.Present}, dst: &i32, expected: int32(42)}, + {src: &pgtype.Numeric{Int: big.NewInt(42), Status: pgtype.Present}, dst: &i64, expected: int64(42)}, + {src: &pgtype.Numeric{Int: big.NewInt(42), Exp: 3, Status: pgtype.Present}, dst: &i64, expected: int64(42000)}, + {src: &pgtype.Numeric{Int: big.NewInt(42), Status: pgtype.Present}, dst: &i, expected: int(42)}, + {src: &pgtype.Numeric{Int: big.NewInt(42), Status: pgtype.Present}, dst: &ui8, expected: uint8(42)}, + {src: &pgtype.Numeric{Int: big.NewInt(42), Status: pgtype.Present}, dst: &ui16, expected: uint16(42)}, + {src: &pgtype.Numeric{Int: big.NewInt(42), Status: pgtype.Present}, dst: &ui32, expected: uint32(42)}, + {src: &pgtype.Numeric{Int: big.NewInt(42), Status: pgtype.Present}, dst: &ui64, expected: uint64(42)}, + {src: &pgtype.Numeric{Int: big.NewInt(42), Status: pgtype.Present}, dst: &ui, expected: uint(42)}, + {src: &pgtype.Numeric{Int: big.NewInt(42), Status: pgtype.Present}, dst: &_i8, expected: _int8(42)}, + {src: &pgtype.Numeric{Int: big.NewInt(0), Status: pgtype.Null}, dst: &pi8, expected: ((*int8)(nil))}, + {src: &pgtype.Numeric{Int: big.NewInt(0), Status: pgtype.Null}, dst: &_pi8, expected: ((*_int8)(nil))}, + {src: &pgtype.Numeric{Int: big.NewInt(1006), Exp: -2, Status: pgtype.Present}, dst: &f64, expected: float64(10.06)}, // https://github.com/jackc/pgtype/issues/27 + {src: &pgtype.Numeric{Status: pgtype.Present, NaN: true}, dst: &f64, expected: math.NaN()}, + {src: &pgtype.Numeric{Status: pgtype.Present, NaN: true}, dst: &f32, expected: float32(math.NaN())}, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + dst := reflect.ValueOf(tt.dst).Elem().Interface() + switch dstTyped := dst.(type) { + case float32: + nanExpected := math.IsNaN(float64(tt.expected.(float32))) + if nanExpected && !math.IsNaN(float64(dstTyped)) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } else if !nanExpected && dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + case float64: + nanExpected := math.IsNaN(tt.expected.(float64)) + if nanExpected && !math.IsNaN(dstTyped) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } else if !nanExpected && dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + default: + if dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + } + + pointerAllocTests := []struct { + src *pgtype.Numeric + dst interface{} + expected interface{} + }{ + {src: &pgtype.Numeric{Int: big.NewInt(42), Status: pgtype.Present}, dst: &pf32, expected: float32(42)}, + {src: &pgtype.Numeric{Int: big.NewInt(42), Status: pgtype.Present}, dst: &pf64, expected: float64(42)}, + } + + for i, tt := range pointerAllocTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src *pgtype.Numeric + dst interface{} + }{ + {src: &pgtype.Numeric{Int: big.NewInt(150), Status: pgtype.Present}, dst: &i8}, + {src: &pgtype.Numeric{Int: big.NewInt(40000), Status: pgtype.Present}, dst: &i16}, + {src: &pgtype.Numeric{Int: big.NewInt(-1), Status: pgtype.Present}, dst: &ui8}, + {src: &pgtype.Numeric{Int: big.NewInt(-1), Status: pgtype.Present}, dst: &ui16}, + {src: &pgtype.Numeric{Int: big.NewInt(-1), Status: pgtype.Present}, dst: &ui32}, + {src: &pgtype.Numeric{Int: big.NewInt(-1), Status: pgtype.Present}, dst: &ui64}, + {src: &pgtype.Numeric{Int: big.NewInt(-1), Status: pgtype.Present}, dst: &ui}, + {src: &pgtype.Numeric{Int: big.NewInt(0), Status: pgtype.Null}, dst: &i32}, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } +} + +func TestNumericEncodeDecodeBinary(t *testing.T) { + ci := pgtype.NewConnInfo() + tests := []interface{}{ + 123, + 0.000012345, + 1.00002345, + math.NaN(), + float32(math.NaN()), + } + + for i, tt := range tests { + toString := func(n *pgtype.Numeric) string { + ci := pgtype.NewConnInfo() + text, err := n.EncodeText(ci, nil) + if err != nil { + t.Errorf("%d (EncodeText): %v", i, err) + } + return string(text) + } + numeric := &pgtype.Numeric{} + numeric.Set(tt) + + encoded, err := numeric.EncodeBinary(ci, nil) + if err != nil { + t.Errorf("%d (EncodeBinary): %v", i, err) + } + decoded := &pgtype.Numeric{} + err = decoded.DecodeBinary(ci, encoded) + if err != nil { + t.Errorf("%d (DecodeBinary): %v", i, err) + } + + text0 := toString(numeric) + text1 := toString(decoded) + + if text0 != text1 { + t.Errorf("%d: expected %v to equal to %v, but doesn't", i, text0, text1) + } + } +} diff --git a/numrange_test.go b/numrange_test.go new file mode 100644 index 00000000..0bbb26f0 --- /dev/null +++ b/numrange_test.go @@ -0,0 +1,46 @@ +package pgtype_test + +import ( + "math/big" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestNumrangeTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "numrange", []interface{}{ + &pgtype.Numrange{ + LowerType: pgtype.Empty, + UpperType: pgtype.Empty, + Status: pgtype.Present, + }, + &pgtype.Numrange{ + Lower: pgtype.Numeric{Int: big.NewInt(-543), Exp: 3, Status: pgtype.Present}, + Upper: pgtype.Numeric{Int: big.NewInt(342), Exp: 1, Status: pgtype.Present}, + LowerType: pgtype.Inclusive, + UpperType: pgtype.Exclusive, + Status: pgtype.Present, + }, + &pgtype.Numrange{ + Lower: pgtype.Numeric{Int: big.NewInt(-42), Exp: 1, Status: pgtype.Present}, + Upper: pgtype.Numeric{Int: big.NewInt(-5), Exp: 0, Status: pgtype.Present}, + LowerType: pgtype.Inclusive, + UpperType: pgtype.Exclusive, + Status: pgtype.Present, + }, + &pgtype.Numrange{ + Lower: pgtype.Numeric{Int: big.NewInt(-42), Exp: 1, Status: pgtype.Present}, + LowerType: pgtype.Inclusive, + UpperType: pgtype.Unbounded, + Status: pgtype.Present, + }, + &pgtype.Numrange{ + Upper: pgtype.Numeric{Int: big.NewInt(-42), Exp: 1, Status: pgtype.Present}, + LowerType: pgtype.Unbounded, + UpperType: pgtype.Exclusive, + Status: pgtype.Present, + }, + &pgtype.Numrange{Status: pgtype.Null}, + }) +} diff --git a/oid_value_test.go b/oid_value_test.go new file mode 100644 index 00000000..69742dd7 --- /dev/null +++ b/oid_value_test.go @@ -0,0 +1,95 @@ +package pgtype_test + +import ( + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestOIDValueTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "oid", []interface{}{ + &pgtype.OIDValue{Uint: 42, Status: pgtype.Present}, + &pgtype.OIDValue{Status: pgtype.Null}, + }) +} + +func TestOIDValueSet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.OIDValue + }{ + {source: uint32(1), result: pgtype.OIDValue{Uint: 1, Status: pgtype.Present}}, + } + + for i, tt := range successfulTests { + var r pgtype.OIDValue + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if r != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestOIDValueAssignTo(t *testing.T) { + var ui32 uint32 + var pui32 *uint32 + + simpleTests := []struct { + src pgtype.OIDValue + dst interface{} + expected interface{} + }{ + {src: pgtype.OIDValue{Uint: 42, Status: pgtype.Present}, dst: &ui32, expected: uint32(42)}, + {src: pgtype.OIDValue{Status: pgtype.Null}, dst: &pui32, expected: ((*uint32)(nil))}, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + pointerAllocTests := []struct { + src pgtype.OIDValue + dst interface{} + expected interface{} + }{ + {src: pgtype.OIDValue{Uint: 42, Status: pgtype.Present}, dst: &pui32, expected: uint32(42)}, + } + + for i, tt := range pointerAllocTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.OIDValue + dst interface{} + }{ + {src: pgtype.OIDValue{Status: pgtype.Null}, dst: &ui32}, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } +} diff --git a/path_test.go b/path_test.go new file mode 100644 index 00000000..969a89ec --- /dev/null +++ b/path_test.go @@ -0,0 +1,29 @@ +package pgtype_test + +import ( + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestPathTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "path", []interface{}{ + &pgtype.Path{ + P: []pgtype.Vec2{{3.14, 1.678901234}, {7.1, 5.234}}, + Closed: false, + Status: pgtype.Present, + }, + &pgtype.Path{ + P: []pgtype.Vec2{{3.14, 1.678}, {7.1, 5.234}, {23.1, 9.34}}, + Closed: true, + Status: pgtype.Present, + }, + &pgtype.Path{ + P: []pgtype.Vec2{{7.1, 1.678}, {-13.14, -5.234}}, + Closed: true, + Status: pgtype.Present, + }, + &pgtype.Path{Status: pgtype.Null}, + }) +} diff --git a/pgtype_test.go b/pgtype_test.go new file mode 100644 index 00000000..75e1909f --- /dev/null +++ b/pgtype_test.go @@ -0,0 +1,292 @@ +package pgtype_test + +import ( + "bytes" + "errors" + "net" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgx/v4" + _ "github.com/jackc/pgx/v4/stdlib" + _ "github.com/lib/pq" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// Test for renamed types +type _string string +type _bool bool +type _int8 int8 +type _int16 int16 +type _int16Slice []int16 +type _int32Slice []int32 +type _int64Slice []int64 +type _float32Slice []float32 +type _float64Slice []float64 +type _byteSlice []byte + +func mustParseCIDR(t testing.TB, s string) *net.IPNet { + _, ipnet, err := net.ParseCIDR(s) + if err != nil { + t.Fatal(err) + } + + return ipnet +} + +func mustParseInet(t testing.TB, s string) *net.IPNet { + ip, ipnet, err := net.ParseCIDR(s) + if err != nil { + t.Fatal(err) + } + if ipv4 := ip.To4(); ipv4 != nil { + ip = ipv4 + } + + ipnet.IP = ip + + return ipnet +} + +func mustParseMacaddr(t testing.TB, s string) net.HardwareAddr { + addr, err := net.ParseMAC(s) + if err != nil { + t.Fatal(err) + } + + return addr +} + +func TestConnInfoResultFormatCodeForOID(t *testing.T) { + ci := pgtype.NewConnInfo() + + // pgtype.JSONB implements BinaryDecoder but also implements ResultFormatPreferrer to override it to text. + assert.Equal(t, int16(pgtype.TextFormatCode), ci.ResultFormatCodeForOID(pgtype.JSONBOID)) + + // pgtype.Int4 implements BinaryDecoder but does not implement ResultFormatPreferrer so it should be binary. + assert.Equal(t, int16(pgtype.BinaryFormatCode), ci.ResultFormatCodeForOID(pgtype.Int4OID)) +} + +func TestConnInfoParamFormatCodeForOID(t *testing.T) { + ci := pgtype.NewConnInfo() + + // pgtype.JSONB implements BinaryEncoder but also implements ParamFormatPreferrer to override it to text. + assert.Equal(t, int16(pgtype.TextFormatCode), ci.ParamFormatCodeForOID(pgtype.JSONBOID)) + + // pgtype.Int4 implements BinaryEncoder but does not implement ParamFormatPreferrer so it should be binary. + assert.Equal(t, int16(pgtype.BinaryFormatCode), ci.ParamFormatCodeForOID(pgtype.Int4OID)) +} + +func TestConnInfoScanNilIsNoOp(t *testing.T) { + ci := pgtype.NewConnInfo() + + err := ci.Scan(pgtype.TextOID, pgx.TextFormatCode, []byte("foo"), nil) + assert.NoError(t, err) +} + +func TestConnInfoScanTextFormatInterfacePtr(t *testing.T) { + ci := pgtype.NewConnInfo() + var got interface{} + err := ci.Scan(pgtype.TextOID, pgx.TextFormatCode, []byte("foo"), &got) + require.NoError(t, err) + assert.Equal(t, "foo", got) +} + +func TestConnInfoScanTextFormatNonByteaIntoByteSlice(t *testing.T) { + ci := pgtype.NewConnInfo() + var got []byte + err := ci.Scan(pgtype.JSONBOID, pgx.TextFormatCode, []byte("{}"), &got) + require.NoError(t, err) + assert.Equal(t, []byte("{}"), got) +} + +func TestConnInfoScanBinaryFormatInterfacePtr(t *testing.T) { + ci := pgtype.NewConnInfo() + var got interface{} + err := ci.Scan(pgtype.TextOID, pgx.BinaryFormatCode, []byte("foo"), &got) + require.NoError(t, err) + assert.Equal(t, "foo", got) +} + +func TestConnInfoScanUnknownOIDToStringsAndBytes(t *testing.T) { + unknownOID := uint32(999999) + srcBuf := []byte("foo") + ci := pgtype.NewConnInfo() + + var s string + err := ci.Scan(unknownOID, pgx.TextFormatCode, srcBuf, &s) + assert.NoError(t, err) + assert.Equal(t, "foo", s) + + var rs _string + err = ci.Scan(unknownOID, pgx.TextFormatCode, srcBuf, &rs) + assert.NoError(t, err) + assert.Equal(t, "foo", string(rs)) + + var b []byte + err = ci.Scan(unknownOID, pgx.TextFormatCode, srcBuf, &b) + assert.NoError(t, err) + assert.Equal(t, []byte("foo"), b) + + err = ci.Scan(unknownOID, pgx.BinaryFormatCode, srcBuf, &b) + assert.NoError(t, err) + assert.Equal(t, []byte("foo"), b) + + var rb _byteSlice + err = ci.Scan(unknownOID, pgx.TextFormatCode, srcBuf, &rb) + assert.NoError(t, err) + assert.Equal(t, []byte("foo"), []byte(rb)) + + err = ci.Scan(unknownOID, pgx.BinaryFormatCode, srcBuf, &b) + assert.NoError(t, err) + assert.Equal(t, []byte("foo"), []byte(rb)) +} + +type pgCustomType struct { + a string + b string +} + +func (ct *pgCustomType) DecodeText(ci *pgtype.ConnInfo, buf []byte) error { + // This is not a complete parser for the text format of composite types. This is just for test purposes. + if buf == nil { + return errors.New("cannot parse null") + } + + if len(buf) < 2 { + return errors.New("invalid text format") + } + + parts := bytes.Split(buf[1:len(buf)-1], []byte(",")) + if len(parts) != 2 { + return errors.New("wrong number of parts") + } + + ct.a = string(parts[0]) + ct.b = string(parts[1]) + + return nil +} + +func TestConnInfoScanUnregisteredOIDToCustomType(t *testing.T) { + unregisteredOID := uint32(999999) + ci := pgtype.NewConnInfo() + + var ct pgCustomType + err := ci.Scan(unregisteredOID, pgx.TextFormatCode, []byte("(foo,bar)"), &ct) + assert.NoError(t, err) + assert.Equal(t, "foo", ct.a) + assert.Equal(t, "bar", ct.b) + + // Scan value into pointer to custom type + var pCt *pgCustomType + err = ci.Scan(unregisteredOID, pgx.TextFormatCode, []byte("(foo,bar)"), &pCt) + assert.NoError(t, err) + require.NotNil(t, pCt) + assert.Equal(t, "foo", pCt.a) + assert.Equal(t, "bar", pCt.b) + + // Scan null into pointer to custom type + err = ci.Scan(unregisteredOID, pgx.TextFormatCode, nil, &pCt) + assert.NoError(t, err) + assert.Nil(t, pCt) +} + +func TestConnInfoScanUnknownOIDTextFormat(t *testing.T) { + ci := pgtype.NewConnInfo() + + var n int32 + err := ci.Scan(0, pgx.TextFormatCode, []byte("123"), &n) + assert.NoError(t, err) + assert.EqualValues(t, 123, n) +} + +func BenchmarkConnInfoScanInt4IntoBinaryDecoder(b *testing.B) { + ci := pgtype.NewConnInfo() + src := []byte{0, 0, 0, 42} + var v pgtype.Int4 + + for i := 0; i < b.N; i++ { + v = pgtype.Int4{} + err := ci.Scan(pgtype.Int4OID, pgtype.BinaryFormatCode, src, &v) + if err != nil { + b.Fatal(err) + } + if v != (pgtype.Int4{Int: 42, Status: pgtype.Present}) { + b.Fatal("scan failed due to bad value") + } + } +} + +func TestScanPlanBinaryInt32ScanChangedType(t *testing.T) { + ci := pgtype.NewConnInfo() + src := []byte{0, 0, 0, 42} + var v int32 + + plan := ci.PlanScan(pgtype.Int4OID, pgtype.BinaryFormatCode, &v) + err := plan.Scan(ci, pgtype.Int4OID, pgtype.BinaryFormatCode, src, &v) + require.NoError(t, err) + require.EqualValues(t, 42, v) + + var d pgtype.Int4 + err = plan.Scan(ci, pgtype.Int4OID, pgtype.BinaryFormatCode, src, &d) + require.NoError(t, err) + require.EqualValues(t, 42, d.Int) + require.EqualValues(t, pgtype.Present, d.Status) +} + +func BenchmarkConnInfoScanInt4IntoGoInt32(b *testing.B) { + ci := pgtype.NewConnInfo() + src := []byte{0, 0, 0, 42} + var v int32 + + for i := 0; i < b.N; i++ { + v = 0 + err := ci.Scan(pgtype.Int4OID, pgtype.BinaryFormatCode, src, &v) + if err != nil { + b.Fatal(err) + } + if v != 42 { + b.Fatal("scan failed due to bad value") + } + } +} + +func BenchmarkScanPlanScanInt4IntoBinaryDecoder(b *testing.B) { + ci := pgtype.NewConnInfo() + src := []byte{0, 0, 0, 42} + var v pgtype.Int4 + + plan := ci.PlanScan(pgtype.Int4OID, pgtype.BinaryFormatCode, &v) + + for i := 0; i < b.N; i++ { + v = pgtype.Int4{} + err := plan.Scan(ci, pgtype.Int4OID, pgtype.BinaryFormatCode, src, &v) + if err != nil { + b.Fatal(err) + } + if v != (pgtype.Int4{Int: 42, Status: pgtype.Present}) { + b.Fatal("scan failed due to bad value") + } + } +} + +func BenchmarkScanPlanScanInt4IntoGoInt32(b *testing.B) { + ci := pgtype.NewConnInfo() + src := []byte{0, 0, 0, 42} + var v int32 + + plan := ci.PlanScan(pgtype.Int4OID, pgtype.BinaryFormatCode, &v) + + for i := 0; i < b.N; i++ { + v = 0 + err := plan.Scan(ci, pgtype.Int4OID, pgtype.BinaryFormatCode, src, &v) + if err != nil { + b.Fatal(err) + } + if v != 42 { + b.Fatal("scan failed due to bad value") + } + } +} diff --git a/pgxtype/README.md b/pgxtype/README.md new file mode 100644 index 00000000..a070111f --- /dev/null +++ b/pgxtype/README.md @@ -0,0 +1,3 @@ +# pgxtype + +pgxtype is a helper module that connects pgx and pgtype. This package is not currently covered by semantic version guarantees. i.e. The interfaces may change without a major version release of pgtype. diff --git a/pgxtype/pgxtype.go b/pgxtype/pgxtype.go new file mode 100644 index 00000000..041f2545 --- /dev/null +++ b/pgxtype/pgxtype.go @@ -0,0 +1,145 @@ +package pgxtype + +import ( + "context" + "errors" + + "github.com/jackc/pgconn" + "github.com/jackc/pgtype" + "github.com/jackc/pgx/v4" +) + +type Querier interface { + Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error) + Query(ctx context.Context, sql string, optionsAndArgs ...interface{}) (pgx.Rows, error) + QueryRow(ctx context.Context, sql string, optionsAndArgs ...interface{}) pgx.Row +} + +// LoadDataType uses conn to inspect the database for typeName and produces a pgtype.DataType suitable for +// registration on ci. +func LoadDataType(ctx context.Context, conn Querier, ci *pgtype.ConnInfo, typeName string) (pgtype.DataType, error) { + var oid uint32 + + err := conn.QueryRow(ctx, "select $1::text::regtype::oid;", typeName).Scan(&oid) + if err != nil { + return pgtype.DataType{}, err + } + + var typtype string + + err = conn.QueryRow(ctx, "select typtype::text from pg_type where oid=$1", oid).Scan(&typtype) + if err != nil { + return pgtype.DataType{}, err + } + + switch typtype { + case "b": // array + elementOID, err := GetArrayElementOID(ctx, conn, oid) + if err != nil { + return pgtype.DataType{}, err + } + + var element pgtype.ValueTranscoder + if dt, ok := ci.DataTypeForOID(elementOID); ok { + if element, ok = dt.Value.(pgtype.ValueTranscoder); !ok { + return pgtype.DataType{}, errors.New("array element OID not registered as ValueTranscoder") + } + } + + newElement := func() pgtype.ValueTranscoder { + return pgtype.NewValue(element).(pgtype.ValueTranscoder) + } + + at := pgtype.NewArrayType(typeName, elementOID, newElement) + return pgtype.DataType{Value: at, Name: typeName, OID: oid}, nil + case "c": // composite + fields, err := GetCompositeFields(ctx, conn, oid) + if err != nil { + return pgtype.DataType{}, err + } + ct, err := pgtype.NewCompositeType(typeName, fields, ci) + if err != nil { + return pgtype.DataType{}, err + } + return pgtype.DataType{Value: ct, Name: typeName, OID: oid}, nil + case "e": // enum + members, err := GetEnumMembers(ctx, conn, oid) + if err != nil { + return pgtype.DataType{}, err + } + return pgtype.DataType{Value: pgtype.NewEnumType(typeName, members), Name: typeName, OID: oid}, nil + default: + return pgtype.DataType{}, errors.New("unknown typtype") + } +} + +func GetArrayElementOID(ctx context.Context, conn Querier, oid uint32) (uint32, error) { + var typelem uint32 + + err := conn.QueryRow(ctx, "select typelem from pg_type where oid=$1", oid).Scan(&typelem) + if err != nil { + return 0, err + } + + return typelem, nil +} + +// GetCompositeFields gets the fields of a composite type. +func GetCompositeFields(ctx context.Context, conn Querier, oid uint32) ([]pgtype.CompositeTypeField, error) { + var typrelid uint32 + + err := conn.QueryRow(ctx, "select typrelid from pg_type where oid=$1", oid).Scan(&typrelid) + if err != nil { + return nil, err + } + + var fields []pgtype.CompositeTypeField + + rows, err := conn.Query(ctx, `select attname, atttypid +from pg_attribute +where attrelid=$1 +order by attnum`, typrelid) + if err != nil { + return nil, err + } + + for rows.Next() { + var f pgtype.CompositeTypeField + err := rows.Scan(&f.Name, &f.OID) + if err != nil { + return nil, err + } + fields = append(fields, f) + } + + if rows.Err() != nil { + return nil, rows.Err() + } + + return fields, nil +} + +// GetEnumMembers gets the possible values of the enum by oid. +func GetEnumMembers(ctx context.Context, conn Querier, oid uint32) ([]string, error) { + members := []string{} + + rows, err := conn.Query(ctx, "select enumlabel from pg_enum where enumtypid=$1 order by enumsortorder", oid) + if err != nil { + return nil, err + } + + for rows.Next() { + var m string + err := rows.Scan(&m) + if err != nil { + return nil, err + } + members = append(members, m) + } + + if rows.Err() != nil { + return nil, rows.Err() + } + + return members, nil +} diff --git a/point_test.go b/point_test.go new file mode 100644 index 00000000..63f8df07 --- /dev/null +++ b/point_test.go @@ -0,0 +1,150 @@ +package pgtype_test + +import ( + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestPointTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "point", []interface{}{ + &pgtype.Point{P: pgtype.Vec2{1.234, 5.6789012345}, Status: pgtype.Present}, + &pgtype.Point{P: pgtype.Vec2{-1.234, -5.6789}, Status: pgtype.Present}, + &pgtype.Point{Status: pgtype.Null}, + }) +} + +func TestPoint_Set(t *testing.T) { + tests := []struct { + name string + arg interface{} + status pgtype.Status + wantErr bool + }{ + { + name: "first", + arg: "(12312.123123,123123.123123)", + status: pgtype.Present, + wantErr: false, + }, + { + name: "second", + arg: "(1231s2.123123,123123.123123)", + status: pgtype.Undefined, + wantErr: true, + }, + { + name: "third", + arg: []byte("(122.123123,123.123123)"), + status: pgtype.Present, + wantErr: false, + }, + { + name: "third", + arg: nil, + status: pgtype.Null, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dst := &pgtype.Point{} + if err := dst.Set(tt.arg); (err != nil) != tt.wantErr { + t.Errorf("Set() error = %v, wantErr %v", err, tt.wantErr) + } + if dst.Status != tt.status { + t.Errorf("Expected status: %v; got: %v", tt.status, dst.Status) + } + }) + } +} + +func TestPoint_MarshalJSON(t *testing.T) { + tests := []struct { + name string + point pgtype.Point + want []byte + wantErr bool + }{ + { + name: "first", + point: pgtype.Point{ + P: pgtype.Vec2{}, + Status: pgtype.Undefined, + }, + want: nil, + wantErr: true, + }, + { + name: "second", + point: pgtype.Point{ + P: pgtype.Vec2{X: 12.245, Y: 432.12}, + Status: pgtype.Present, + }, + want: []byte(`"(12.245,432.12)"`), + wantErr: false, + }, + { + name: "third", + point: pgtype.Point{ + P: pgtype.Vec2{}, + Status: pgtype.Null, + }, + want: []byte("null"), + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := tt.point.MarshalJSON() + if (err != nil) != tt.wantErr { + t.Errorf("MarshalJSON() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("MarshalJSON() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestPoint_UnmarshalJSON(t *testing.T) { + tests := []struct { + name string + status pgtype.Status + arg []byte + wantErr bool + }{ + { + name: "first", + status: pgtype.Present, + arg: []byte(`"(123.123,54.12)"`), + wantErr: false, + }, + { + name: "second", + status: pgtype.Undefined, + arg: []byte(`"(123.123,54.1sad2)"`), + wantErr: true, + }, + { + name: "third", + status: pgtype.Null, + arg: []byte("null"), + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dst := &pgtype.Point{} + if err := dst.UnmarshalJSON(tt.arg); (err != nil) != tt.wantErr { + t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) + } + if dst.Status != tt.status { + t.Errorf("Status mismatch: %v != %v", dst.Status, tt.status) + } + }) + } +} diff --git a/polygon_test.go b/polygon_test.go new file mode 100644 index 00000000..1a139444 --- /dev/null +++ b/polygon_test.go @@ -0,0 +1,89 @@ +package pgtype_test + +import ( + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestPolygonTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "polygon", []interface{}{ + &pgtype.Polygon{ + P: []pgtype.Vec2{{3.14, 1.678901234}, {7.1, 5.234}, {5.0, 3.234}}, + Status: pgtype.Present, + }, + &pgtype.Polygon{ + P: []pgtype.Vec2{{3.14, -1.678}, {7.1, -5.234}, {23.1, 9.34}}, + Status: pgtype.Present, + }, + &pgtype.Polygon{Status: pgtype.Null}, + }) +} + +func TestPolygon_Set(t *testing.T) { + tests := []struct { + name string + arg interface{} + status pgtype.Status + wantErr bool + }{ + { + name: "string", + arg: "((3.14,1.678901234),(7.1,5.234),(5.0,3.234))", + status: pgtype.Present, + wantErr: false, + }, { + name: "[]float64", + arg: []float64{1, 2, 3.45, 6.78, 1.23, 4.567, 8.9, 1.0}, + status: pgtype.Present, + wantErr: false, + }, { + name: "[]Vec2", + arg: []pgtype.Vec2{{1, 2}, {2.3, 4.5}, {6.78, 9.123}}, + status: pgtype.Present, + wantErr: false, + }, { + name: "null", + arg: nil, + status: pgtype.Null, + wantErr: false, + }, { + name: "invalid_string_1", + arg: "((3.14,1.678901234),(7.1,5.234),(5.0,3.234x))", + status: pgtype.Undefined, + wantErr: true, + }, { + name: "invalid_string_2", + arg: "(3,4)", + status: pgtype.Undefined, + wantErr: true, + }, { + name: "invalid_[]float64", + arg: []float64{1, 2, 3.45, 6.78, 1.23, 4.567, 8.9}, + status: pgtype.Undefined, + wantErr: true, + }, { + name: "invalid_type", + arg: []int{1, 2, 3, 6}, + status: pgtype.Undefined, + wantErr: true, + }, { + name: "empty_[]float64", + arg: []float64{}, + status: pgtype.Null, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dst := &pgtype.Polygon{} + if err := dst.Set(tt.arg); (err != nil) != tt.wantErr { + t.Errorf("Set() error = %v, wantErr %v", err, tt.wantErr) + } + if dst.Status != tt.status { + t.Errorf("Expected status: %v; got: %v", tt.status, dst.Status) + } + }) + } +} diff --git a/qchar_test.go b/qchar_test.go new file mode 100644 index 00000000..4b60339c --- /dev/null +++ b/qchar_test.go @@ -0,0 +1,143 @@ +package pgtype_test + +import ( + "math" + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestQCharTranscode(t *testing.T) { + testutil.TestPgxSuccessfulTranscodeEqFunc(t, `"char"`, []interface{}{ + &pgtype.QChar{Int: math.MinInt8, Status: pgtype.Present}, + &pgtype.QChar{Int: -1, Status: pgtype.Present}, + &pgtype.QChar{Int: 0, Status: pgtype.Present}, + &pgtype.QChar{Int: 1, Status: pgtype.Present}, + &pgtype.QChar{Int: math.MaxInt8, Status: pgtype.Present}, + &pgtype.QChar{Int: 0, Status: pgtype.Null}, + }, func(a, b interface{}) bool { + return reflect.DeepEqual(a, b) + }) +} + +func TestQCharSet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.QChar + }{ + {source: int8(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}}, + {source: int16(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}}, + {source: int32(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}}, + {source: int64(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}}, + {source: int8(-1), result: pgtype.QChar{Int: -1, Status: pgtype.Present}}, + {source: int16(-1), result: pgtype.QChar{Int: -1, Status: pgtype.Present}}, + {source: int32(-1), result: pgtype.QChar{Int: -1, Status: pgtype.Present}}, + {source: int64(-1), result: pgtype.QChar{Int: -1, Status: pgtype.Present}}, + {source: uint8(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}}, + {source: uint16(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}}, + {source: uint32(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}}, + {source: uint64(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}}, + {source: "1", result: pgtype.QChar{Int: 1, Status: pgtype.Present}}, + {source: _int8(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}}, + } + + for i, tt := range successfulTests { + var r pgtype.QChar + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if r != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestQCharAssignTo(t *testing.T) { + var i8 int8 + var i16 int16 + var i32 int32 + var i64 int64 + var i int + var ui8 uint8 + var ui16 uint16 + var ui32 uint32 + var ui64 uint64 + var ui uint + var pi8 *int8 + var _i8 _int8 + var _pi8 *_int8 + + simpleTests := []struct { + src pgtype.QChar + dst interface{} + expected interface{} + }{ + {src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &i8, expected: int8(42)}, + {src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &i16, expected: int16(42)}, + {src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &i32, expected: int32(42)}, + {src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &i64, expected: int64(42)}, + {src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &i, expected: int(42)}, + {src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &ui8, expected: uint8(42)}, + {src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &ui16, expected: uint16(42)}, + {src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &ui32, expected: uint32(42)}, + {src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &ui64, expected: uint64(42)}, + {src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &ui, expected: uint(42)}, + {src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &_i8, expected: _int8(42)}, + {src: pgtype.QChar{Int: 0, Status: pgtype.Null}, dst: &pi8, expected: ((*int8)(nil))}, + {src: pgtype.QChar{Int: 0, Status: pgtype.Null}, dst: &_pi8, expected: ((*_int8)(nil))}, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + pointerAllocTests := []struct { + src pgtype.QChar + dst interface{} + expected interface{} + }{ + {src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &pi8, expected: int8(42)}, + {src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &_pi8, expected: _int8(42)}, + } + + for i, tt := range pointerAllocTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.QChar + dst interface{} + }{ + {src: pgtype.QChar{Int: -1, Status: pgtype.Present}, dst: &ui8}, + {src: pgtype.QChar{Int: -1, Status: pgtype.Present}, dst: &ui16}, + {src: pgtype.QChar{Int: -1, Status: pgtype.Present}, dst: &ui32}, + {src: pgtype.QChar{Int: -1, Status: pgtype.Present}, dst: &ui64}, + {src: pgtype.QChar{Int: -1, Status: pgtype.Present}, dst: &ui}, + {src: pgtype.QChar{Int: 0, Status: pgtype.Null}, dst: &i16}, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } +} diff --git a/range_test.go b/range_test.go new file mode 100644 index 00000000..9e16df59 --- /dev/null +++ b/range_test.go @@ -0,0 +1,177 @@ +package pgtype + +import ( + "bytes" + "testing" +) + +func TestParseUntypedTextRange(t *testing.T) { + tests := []struct { + src string + result UntypedTextRange + err error + }{ + { + src: `[1,2)`, + result: UntypedTextRange{Lower: "1", Upper: "2", LowerType: Inclusive, UpperType: Exclusive}, + err: nil, + }, + { + src: `[1,2]`, + result: UntypedTextRange{Lower: "1", Upper: "2", LowerType: Inclusive, UpperType: Inclusive}, + err: nil, + }, + { + src: `(1,3)`, + result: UntypedTextRange{Lower: "1", Upper: "3", LowerType: Exclusive, UpperType: Exclusive}, + err: nil, + }, + { + src: ` [1,2) `, + result: UntypedTextRange{Lower: "1", Upper: "2", LowerType: Inclusive, UpperType: Exclusive}, + err: nil, + }, + { + src: `[ foo , bar )`, + result: UntypedTextRange{Lower: " foo ", Upper: " bar ", LowerType: Inclusive, UpperType: Exclusive}, + err: nil, + }, + { + src: `["foo","bar")`, + result: UntypedTextRange{Lower: "foo", Upper: "bar", LowerType: Inclusive, UpperType: Exclusive}, + err: nil, + }, + { + src: `["f""oo","b""ar")`, + result: UntypedTextRange{Lower: `f"oo`, Upper: `b"ar`, LowerType: Inclusive, UpperType: Exclusive}, + err: nil, + }, + { + src: `["f""oo","b""ar")`, + result: UntypedTextRange{Lower: `f"oo`, Upper: `b"ar`, LowerType: Inclusive, UpperType: Exclusive}, + err: nil, + }, + { + src: `["","bar")`, + result: UntypedTextRange{Lower: ``, Upper: `bar`, LowerType: Inclusive, UpperType: Exclusive}, + err: nil, + }, + { + src: `[f\"oo\,,b\\ar\))`, + result: UntypedTextRange{Lower: `f"oo,`, Upper: `b\ar)`, LowerType: Inclusive, UpperType: Exclusive}, + err: nil, + }, + { + src: `empty`, + result: UntypedTextRange{Lower: "", Upper: "", LowerType: Empty, UpperType: Empty}, + err: nil, + }, + } + + for i, tt := range tests { + r, err := ParseUntypedTextRange(tt.src) + if err != tt.err { + t.Errorf("%d. `%v`: expected err %v, got %v", i, tt.src, tt.err, err) + continue + } + + if r.LowerType != tt.result.LowerType { + t.Errorf("%d. `%v`: expected result lower type %v, got %v", i, tt.src, string(tt.result.LowerType), string(r.LowerType)) + } + + if r.UpperType != tt.result.UpperType { + t.Errorf("%d. `%v`: expected result upper type %v, got %v", i, tt.src, string(tt.result.UpperType), string(r.UpperType)) + } + + if r.Lower != tt.result.Lower { + t.Errorf("%d. `%v`: expected result lower %v, got %v", i, tt.src, tt.result.Lower, r.Lower) + } + + if r.Upper != tt.result.Upper { + t.Errorf("%d. `%v`: expected result upper %v, got %v", i, tt.src, tt.result.Upper, r.Upper) + } + } +} + +func TestParseUntypedBinaryRange(t *testing.T) { + tests := []struct { + src []byte + result UntypedBinaryRange + err error + }{ + { + src: []byte{0, 0, 0, 0, 2, 0, 4, 0, 0, 0, 2, 0, 5}, + result: UntypedBinaryRange{Lower: []byte{0, 4}, Upper: []byte{0, 5}, LowerType: Exclusive, UpperType: Exclusive}, + err: nil, + }, + { + src: []byte{1}, + result: UntypedBinaryRange{Lower: nil, Upper: nil, LowerType: Empty, UpperType: Empty}, + err: nil, + }, + { + src: []byte{2, 0, 0, 0, 2, 0, 4, 0, 0, 0, 2, 0, 5}, + result: UntypedBinaryRange{Lower: []byte{0, 4}, Upper: []byte{0, 5}, LowerType: Inclusive, UpperType: Exclusive}, + err: nil, + }, + { + src: []byte{4, 0, 0, 0, 2, 0, 4, 0, 0, 0, 2, 0, 5}, + result: UntypedBinaryRange{Lower: []byte{0, 4}, Upper: []byte{0, 5}, LowerType: Exclusive, UpperType: Inclusive}, + err: nil, + }, + { + src: []byte{6, 0, 0, 0, 2, 0, 4, 0, 0, 0, 2, 0, 5}, + result: UntypedBinaryRange{Lower: []byte{0, 4}, Upper: []byte{0, 5}, LowerType: Inclusive, UpperType: Inclusive}, + err: nil, + }, + { + src: []byte{8, 0, 0, 0, 2, 0, 5}, + result: UntypedBinaryRange{Lower: nil, Upper: []byte{0, 5}, LowerType: Unbounded, UpperType: Exclusive}, + err: nil, + }, + { + src: []byte{12, 0, 0, 0, 2, 0, 5}, + result: UntypedBinaryRange{Lower: nil, Upper: []byte{0, 5}, LowerType: Unbounded, UpperType: Inclusive}, + err: nil, + }, + { + src: []byte{16, 0, 0, 0, 2, 0, 4}, + result: UntypedBinaryRange{Lower: []byte{0, 4}, Upper: nil, LowerType: Exclusive, UpperType: Unbounded}, + err: nil, + }, + { + src: []byte{18, 0, 0, 0, 2, 0, 4}, + result: UntypedBinaryRange{Lower: []byte{0, 4}, Upper: nil, LowerType: Inclusive, UpperType: Unbounded}, + err: nil, + }, + { + src: []byte{24}, + result: UntypedBinaryRange{Lower: nil, Upper: nil, LowerType: Unbounded, UpperType: Unbounded}, + err: nil, + }, + } + + for i, tt := range tests { + r, err := ParseUntypedBinaryRange(tt.src) + if err != tt.err { + t.Errorf("%d. `%v`: expected err %v, got %v", i, tt.src, tt.err, err) + continue + } + + if r.LowerType != tt.result.LowerType { + t.Errorf("%d. `%v`: expected result lower type %v, got %v", i, tt.src, string(tt.result.LowerType), string(r.LowerType)) + } + + if r.UpperType != tt.result.UpperType { + t.Errorf("%d. `%v`: expected result upper type %v, got %v", i, tt.src, string(tt.result.UpperType), string(r.UpperType)) + } + + if bytes.Compare(r.Lower, tt.result.Lower) != 0 { + t.Errorf("%d. `%v`: expected result lower %v, got %v", i, tt.src, tt.result.Lower, r.Lower) + } + + if bytes.Compare(r.Upper, tt.result.Upper) != 0 { + t.Errorf("%d. `%v`: expected result upper %v, got %v", i, tt.src, tt.result.Upper, r.Upper) + } + } +} diff --git a/record_test.go b/record_test.go new file mode 100644 index 00000000..240812a6 --- /dev/null +++ b/record_test.go @@ -0,0 +1,186 @@ +package pgtype_test + +import ( + "context" + "fmt" + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" + "github.com/jackc/pgx/v4" +) + +var recordTests = []struct { + sql string + expected pgtype.Record +}{ + { + sql: `select row()`, + expected: pgtype.Record{ + Fields: []pgtype.Value{}, + Status: pgtype.Present, + }, + }, + { + sql: `select row('foo'::text, 42::int4)`, + expected: pgtype.Record{ + Fields: []pgtype.Value{ + &pgtype.Text{String: "foo", Status: pgtype.Present}, + &pgtype.Int4{Int: 42, Status: pgtype.Present}, + }, + Status: pgtype.Present, + }, + }, + { + sql: `select row(100.0::float4, 1.09::float4)`, + expected: pgtype.Record{ + Fields: []pgtype.Value{ + &pgtype.Float4{Float: 100, Status: pgtype.Present}, + &pgtype.Float4{Float: 1.09, Status: pgtype.Present}, + }, + Status: pgtype.Present, + }, + }, + { + sql: `select row('foo'::text, array[1, 2, null, 4]::int4[], 42::int4)`, + expected: pgtype.Record{ + Fields: []pgtype.Value{ + &pgtype.Text{String: "foo", Status: pgtype.Present}, + &pgtype.Int4Array{ + Elements: []pgtype.Int4{ + {Int: 1, Status: pgtype.Present}, + {Int: 2, Status: pgtype.Present}, + {Status: pgtype.Null}, + {Int: 4, Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 4, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.Int4{Int: 42, Status: pgtype.Present}, + }, + Status: pgtype.Present, + }, + }, + { + sql: `select row(null)`, + expected: pgtype.Record{ + Fields: []pgtype.Value{ + &pgtype.Unknown{Status: pgtype.Null}, + }, + Status: pgtype.Present, + }, + }, + { + sql: `select null::record`, + expected: pgtype.Record{ + Status: pgtype.Null, + }, + }, +} + +func TestRecordTranscode(t *testing.T) { + conn := testutil.MustConnectPgx(t) + defer testutil.MustCloseContext(t, conn) + + for i, tt := range recordTests { + psName := fmt.Sprintf("test%d", i) + _, err := conn.Prepare(context.Background(), psName, tt.sql) + if err != nil { + t.Fatal(err) + } + + t.Run(tt.sql, func(t *testing.T) { + var result pgtype.Record + if err := conn.QueryRow(context.Background(), psName, pgx.QueryResultFormats{pgx.BinaryFormatCode}).Scan(&result); err != nil { + t.Errorf("%v", err) + return + } + + if !reflect.DeepEqual(tt.expected, result) { + t.Errorf("expected %#v, got %#v", tt.expected, result) + } + }) + + } +} + +func TestRecordWithUnknownOID(t *testing.T) { + conn := testutil.MustConnectPgx(t) + defer testutil.MustCloseContext(t, conn) + + _, err := conn.Exec(context.Background(), `drop type if exists floatrange; + +create type floatrange as range ( + subtype = float8, + subtype_diff = float8mi +);`) + if err != nil { + t.Fatal(err) + } + defer conn.Exec(context.Background(), "drop type floatrange") + + var result pgtype.Record + err = conn.QueryRow(context.Background(), "select row('foo'::text, floatrange(1, 10), 'bar'::text)").Scan(&result) + if err == nil { + t.Errorf("expected error but none") + } +} + +func TestRecordAssignTo(t *testing.T) { + var valueSlice []pgtype.Value + var interfaceSlice []interface{} + + simpleTests := []struct { + src pgtype.Record + dst interface{} + expected interface{} + }{ + { + src: pgtype.Record{ + Fields: []pgtype.Value{ + &pgtype.Text{String: "foo", Status: pgtype.Present}, + &pgtype.Int4{Int: 42, Status: pgtype.Present}, + }, + Status: pgtype.Present, + }, + dst: &valueSlice, + expected: []pgtype.Value{ + &pgtype.Text{String: "foo", Status: pgtype.Present}, + &pgtype.Int4{Int: 42, Status: pgtype.Present}, + }, + }, + { + src: pgtype.Record{ + Fields: []pgtype.Value{ + &pgtype.Text{String: "foo", Status: pgtype.Present}, + &pgtype.Int4{Int: 42, Status: pgtype.Present}, + }, + Status: pgtype.Present, + }, + dst: &interfaceSlice, + expected: []interface{}{"foo", int32(42)}, + }, + { + src: pgtype.Record{Status: pgtype.Null}, + dst: &valueSlice, + expected: (([]pgtype.Value)(nil)), + }, + { + src: pgtype.Record{Status: pgtype.Null}, + dst: &interfaceSlice, + expected: (([]interface{})(nil)), + }, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } +} diff --git a/testutil/testutil.go b/testutil/testutil.go new file mode 100644 index 00000000..e7b64b58 --- /dev/null +++ b/testutil/testutil.go @@ -0,0 +1,436 @@ +package testutil + +import ( + "context" + "database/sql" + "fmt" + "os" + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgx/v4" + _ "github.com/jackc/pgx/v4/stdlib" + _ "github.com/lib/pq" +) + +func MustConnectDatabaseSQL(t testing.TB, driverName string) *sql.DB { + var sqlDriverName string + switch driverName { + case "github.com/lib/pq": + sqlDriverName = "postgres" + case "github.com/jackc/pgx/stdlib": + sqlDriverName = "pgx" + default: + t.Fatalf("Unknown driver %v", driverName) + } + + db, err := sql.Open(sqlDriverName, os.Getenv("PGX_TEST_DATABASE")) + if err != nil { + t.Fatal(err) + } + + return db +} + +func MustConnectPgx(t testing.TB) *pgx.Conn { + conn, err := pgx.Connect(context.Background(), os.Getenv("PGX_TEST_DATABASE")) + if err != nil { + t.Fatal(err) + } + + return conn +} + +func MustClose(t testing.TB, conn interface { + Close() error +}) { + err := conn.Close() + if err != nil { + t.Fatal(err) + } +} + +func MustCloseContext(t testing.TB, conn interface { + Close(context.Context) error +}) { + err := conn.Close(context.Background()) + if err != nil { + t.Fatal(err) + } +} + +type forceTextEncoder struct { + e pgtype.TextEncoder +} + +func (f forceTextEncoder) EncodeText(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) { + return f.e.EncodeText(ci, buf) +} + +type forceBinaryEncoder struct { + e pgtype.BinaryEncoder +} + +func (f forceBinaryEncoder) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) { + return f.e.EncodeBinary(ci, buf) +} + +func ForceEncoder(e interface{}, formatCode int16) interface{} { + switch formatCode { + case pgx.TextFormatCode: + if e, ok := e.(pgtype.TextEncoder); ok { + return forceTextEncoder{e: e} + } + case pgx.BinaryFormatCode: + if e, ok := e.(pgtype.BinaryEncoder); ok { + return forceBinaryEncoder{e: e.(pgtype.BinaryEncoder)} + } + } + return nil +} + +func TestSuccessfulTranscode(t testing.TB, pgTypeName string, values []interface{}) { + TestSuccessfulTranscodeEqFunc(t, pgTypeName, values, func(a, b interface{}) bool { + return reflect.DeepEqual(a, b) + }) +} + +func TestSuccessfulTranscodeEqFunc(t testing.TB, pgTypeName string, values []interface{}, eqFunc func(a, b interface{}) bool) { + TestPgxSuccessfulTranscodeEqFunc(t, pgTypeName, values, eqFunc) + for _, driverName := range []string{"github.com/lib/pq", "github.com/jackc/pgx/stdlib"} { + TestDatabaseSQLSuccessfulTranscodeEqFunc(t, driverName, pgTypeName, values, eqFunc) + } +} + +func TestPgxSuccessfulTranscodeEqFunc(t testing.TB, pgTypeName string, values []interface{}, eqFunc func(a, b interface{}) bool) { + conn := MustConnectPgx(t) + defer MustCloseContext(t, conn) + + _, err := conn.Prepare(context.Background(), "test", fmt.Sprintf("select $1::%s", pgTypeName)) + if err != nil { + t.Fatal(err) + } + + formats := []struct { + name string + formatCode int16 + }{ + {name: "TextFormat", formatCode: pgx.TextFormatCode}, + {name: "BinaryFormat", formatCode: pgx.BinaryFormatCode}, + } + + for i, v := range values { + for _, paramFormat := range formats { + for _, resultFormat := range formats { + vEncoder := ForceEncoder(v, paramFormat.formatCode) + if vEncoder == nil { + t.Logf("Skipping Param %s Result %s: %#v does not implement %v for encoding", paramFormat.name, resultFormat.name, v, paramFormat.name) + continue + } + switch resultFormat.formatCode { + case pgx.TextFormatCode: + if _, ok := v.(pgtype.TextEncoder); !ok { + t.Logf("Skipping Param %s Result %s: %#v does not implement %v for decoding", paramFormat.name, resultFormat.name, v, resultFormat.name) + continue + } + case pgx.BinaryFormatCode: + if _, ok := v.(pgtype.BinaryEncoder); !ok { + t.Logf("Skipping Param %s Result %s: %#v does not implement %v for decoding", paramFormat.name, resultFormat.name, v, resultFormat.name) + continue + } + } + + // Derefence value if it is a pointer + derefV := v + refVal := reflect.ValueOf(v) + if refVal.Kind() == reflect.Ptr { + derefV = refVal.Elem().Interface() + } + + result := reflect.New(reflect.TypeOf(derefV)) + + err := conn.QueryRow(context.Background(), "test", pgx.QueryResultFormats{resultFormat.formatCode}, vEncoder).Scan(result.Interface()) + if err != nil { + t.Errorf("Param %s Result %s %d: %v", paramFormat.name, resultFormat.name, i, err) + } + + if !eqFunc(result.Elem().Interface(), derefV) { + t.Errorf("Param %s Result %s %d: expected %v, got %v", paramFormat.name, resultFormat.name, i, derefV, result.Elem().Interface()) + } + } + } + } +} + +func TestDatabaseSQLSuccessfulTranscodeEqFunc(t testing.TB, driverName, pgTypeName string, values []interface{}, eqFunc func(a, b interface{}) bool) { + conn := MustConnectDatabaseSQL(t, driverName) + defer MustClose(t, conn) + + ps, err := conn.Prepare(fmt.Sprintf("select $1::%s", pgTypeName)) + if err != nil { + t.Fatal(err) + } + + for i, v := range values { + // Derefence value if it is a pointer + derefV := v + refVal := reflect.ValueOf(v) + if refVal.Kind() == reflect.Ptr { + derefV = refVal.Elem().Interface() + } + + result := reflect.New(reflect.TypeOf(derefV)) + err := ps.QueryRow(v).Scan(result.Interface()) + if err != nil { + t.Errorf("%v %d: %v", driverName, i, err) + } + + if !eqFunc(result.Elem().Interface(), derefV) { + t.Errorf("%v %d: expected %v, got %v", driverName, i, derefV, result.Elem().Interface()) + } + } +} + +type NormalizeTest struct { + SQL string + Value interface{} +} + +func TestSuccessfulNormalize(t testing.TB, tests []NormalizeTest) { + TestSuccessfulNormalizeEqFunc(t, tests, func(a, b interface{}) bool { + return reflect.DeepEqual(a, b) + }) +} + +func TestSuccessfulNormalizeEqFunc(t testing.TB, tests []NormalizeTest, eqFunc func(a, b interface{}) bool) { + TestPgxSuccessfulNormalizeEqFunc(t, tests, eqFunc) + for _, driverName := range []string{"github.com/lib/pq", "github.com/jackc/pgx/stdlib"} { + TestDatabaseSQLSuccessfulNormalizeEqFunc(t, driverName, tests, eqFunc) + } +} + +func TestPgxSuccessfulNormalizeEqFunc(t testing.TB, tests []NormalizeTest, eqFunc func(a, b interface{}) bool) { + conn := MustConnectPgx(t) + defer MustCloseContext(t, conn) + + formats := []struct { + name string + formatCode int16 + }{ + {name: "TextFormat", formatCode: pgx.TextFormatCode}, + {name: "BinaryFormat", formatCode: pgx.BinaryFormatCode}, + } + + for i, tt := range tests { + for _, fc := range formats { + psName := fmt.Sprintf("test%d", i) + _, err := conn.Prepare(context.Background(), psName, tt.SQL) + if err != nil { + t.Fatal(err) + } + + queryResultFormats := pgx.QueryResultFormats{fc.formatCode} + if ForceEncoder(tt.Value, fc.formatCode) == nil { + t.Logf("Skipping: %#v does not implement %v", tt.Value, fc.name) + continue + } + // Derefence value if it is a pointer + derefV := tt.Value + refVal := reflect.ValueOf(tt.Value) + if refVal.Kind() == reflect.Ptr { + derefV = refVal.Elem().Interface() + } + + result := reflect.New(reflect.TypeOf(derefV)) + err = conn.QueryRow(context.Background(), psName, queryResultFormats).Scan(result.Interface()) + if err != nil { + t.Errorf("%v %d: %v", fc.name, i, err) + } + + if !eqFunc(result.Elem().Interface(), derefV) { + t.Errorf("%v %d: expected %v, got %v", fc.name, i, derefV, result.Elem().Interface()) + } + } + } +} + +func TestDatabaseSQLSuccessfulNormalizeEqFunc(t testing.TB, driverName string, tests []NormalizeTest, eqFunc func(a, b interface{}) bool) { + conn := MustConnectDatabaseSQL(t, driverName) + defer MustClose(t, conn) + + for i, tt := range tests { + ps, err := conn.Prepare(tt.SQL) + if err != nil { + t.Errorf("%d. %v", i, err) + continue + } + + // Derefence value if it is a pointer + derefV := tt.Value + refVal := reflect.ValueOf(tt.Value) + if refVal.Kind() == reflect.Ptr { + derefV = refVal.Elem().Interface() + } + + result := reflect.New(reflect.TypeOf(derefV)) + err = ps.QueryRow().Scan(result.Interface()) + if err != nil { + t.Errorf("%v %d: %v", driverName, i, err) + } + + if !eqFunc(result.Elem().Interface(), derefV) { + t.Errorf("%v %d: expected %v, got %v", driverName, i, derefV, result.Elem().Interface()) + } + } +} + +func TestGoZeroToNullConversion(t testing.TB, pgTypeName string, zero interface{}) { + TestPgxGoZeroToNullConversion(t, pgTypeName, zero) + for _, driverName := range []string{"github.com/lib/pq", "github.com/jackc/pgx/stdlib"} { + TestDatabaseSQLGoZeroToNullConversion(t, driverName, pgTypeName, zero) + } +} + +func TestNullToGoZeroConversion(t testing.TB, pgTypeName string, zero interface{}) { + TestPgxNullToGoZeroConversion(t, pgTypeName, zero) + for _, driverName := range []string{"github.com/lib/pq", "github.com/jackc/pgx/stdlib"} { + TestDatabaseSQLNullToGoZeroConversion(t, driverName, pgTypeName, zero) + } +} + +func TestPgxGoZeroToNullConversion(t testing.TB, pgTypeName string, zero interface{}) { + conn := MustConnectPgx(t) + defer MustCloseContext(t, conn) + + _, err := conn.Prepare(context.Background(), "test", fmt.Sprintf("select $1::%s is null", pgTypeName)) + if err != nil { + t.Fatal(err) + } + + formats := []struct { + name string + formatCode int16 + }{ + {name: "TextFormat", formatCode: pgx.TextFormatCode}, + {name: "BinaryFormat", formatCode: pgx.BinaryFormatCode}, + } + + for _, paramFormat := range formats { + vEncoder := ForceEncoder(zero, paramFormat.formatCode) + if vEncoder == nil { + t.Logf("Skipping Param %s: %#v does not implement %v for encoding", paramFormat.name, zero, paramFormat.name) + continue + } + + var result bool + err := conn.QueryRow(context.Background(), "test", vEncoder).Scan(&result) + if err != nil { + t.Errorf("Param %s: %v", paramFormat.name, err) + } + + if !result { + t.Errorf("Param %s: did not convert zero to null", paramFormat.name) + } + } +} + +func TestPgxNullToGoZeroConversion(t testing.TB, pgTypeName string, zero interface{}) { + conn := MustConnectPgx(t) + defer MustCloseContext(t, conn) + + _, err := conn.Prepare(context.Background(), "test", fmt.Sprintf("select null::%s", pgTypeName)) + if err != nil { + t.Fatal(err) + } + + formats := []struct { + name string + formatCode int16 + }{ + {name: "TextFormat", formatCode: pgx.TextFormatCode}, + {name: "BinaryFormat", formatCode: pgx.BinaryFormatCode}, + } + + for _, resultFormat := range formats { + + switch resultFormat.formatCode { + case pgx.TextFormatCode: + if _, ok := zero.(pgtype.TextEncoder); !ok { + t.Logf("Skipping Result %s: %#v does not implement %v for decoding", resultFormat.name, zero, resultFormat.name) + continue + } + case pgx.BinaryFormatCode: + if _, ok := zero.(pgtype.BinaryEncoder); !ok { + t.Logf("Skipping Result %s: %#v does not implement %v for decoding", resultFormat.name, zero, resultFormat.name) + continue + } + } + + // Derefence value if it is a pointer + derefZero := zero + refVal := reflect.ValueOf(zero) + if refVal.Kind() == reflect.Ptr { + derefZero = refVal.Elem().Interface() + } + + result := reflect.New(reflect.TypeOf(derefZero)) + + err := conn.QueryRow(context.Background(), "test").Scan(result.Interface()) + if err != nil { + t.Errorf("Result %s: %v", resultFormat.name, err) + } + + if !reflect.DeepEqual(result.Elem().Interface(), derefZero) { + t.Errorf("Result %s: did not convert null to zero", resultFormat.name) + } + } +} + +func TestDatabaseSQLGoZeroToNullConversion(t testing.TB, driverName, pgTypeName string, zero interface{}) { + conn := MustConnectDatabaseSQL(t, driverName) + defer MustClose(t, conn) + + ps, err := conn.Prepare(fmt.Sprintf("select $1::%s is null", pgTypeName)) + if err != nil { + t.Fatal(err) + } + + var result bool + err = ps.QueryRow(zero).Scan(&result) + if err != nil { + t.Errorf("%v %v", driverName, err) + } + + if !result { + t.Errorf("%v: did not convert zero to null", driverName) + } +} + +func TestDatabaseSQLNullToGoZeroConversion(t testing.TB, driverName, pgTypeName string, zero interface{}) { + conn := MustConnectDatabaseSQL(t, driverName) + defer MustClose(t, conn) + + ps, err := conn.Prepare(fmt.Sprintf("select null::%s", pgTypeName)) + if err != nil { + t.Fatal(err) + } + + // Derefence value if it is a pointer + derefZero := zero + refVal := reflect.ValueOf(zero) + if refVal.Kind() == reflect.Ptr { + derefZero = refVal.Elem().Interface() + } + + result := reflect.New(reflect.TypeOf(derefZero)) + + err = ps.QueryRow().Scan(result.Interface()) + if err != nil { + t.Errorf("%v %v", driverName, err) + } + + if !reflect.DeepEqual(result.Elem().Interface(), derefZero) { + t.Errorf("%s: did not convert null to zero", driverName) + } +} diff --git a/text_array_test.go b/text_array_test.go new file mode 100644 index 00000000..a5d050f6 --- /dev/null +++ b/text_array_test.go @@ -0,0 +1,294 @@ +package pgtype_test + +import ( + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// https://github.com/jackc/pgtype/issues/78 +func TestTextArrayDecodeTextNull(t *testing.T) { + textArray := &pgtype.TextArray{} + err := textArray.DecodeText(nil, []byte(`{abc,"NULL",NULL,def}`)) + require.NoError(t, err) + require.Len(t, textArray.Elements, 4) + assert.Equal(t, pgtype.Present, textArray.Elements[1].Status) + assert.Equal(t, pgtype.Null, textArray.Elements[2].Status) +} + +func TestTextArrayTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "text[]", []interface{}{ + &pgtype.TextArray{ + Elements: nil, + Dimensions: nil, + Status: pgtype.Present, + }, + &pgtype.TextArray{ + Elements: []pgtype.Text{ + {String: "foo", Status: pgtype.Present}, + {Status: pgtype.Null}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.TextArray{Status: pgtype.Null}, + &pgtype.TextArray{ + Elements: []pgtype.Text{ + {String: "bar ", Status: pgtype.Present}, + {String: "NuLL", Status: pgtype.Present}, + {String: `wow"quz\`, Status: pgtype.Present}, + {String: "", Status: pgtype.Present}, + {Status: pgtype.Null}, + {String: "null", Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}, {Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.TextArray{ + Elements: []pgtype.Text{ + {String: "bar", Status: pgtype.Present}, + {String: "baz", Status: pgtype.Present}, + {String: "quz", Status: pgtype.Present}, + {String: "foo", Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{ + {Length: 2, LowerBound: 4}, + {Length: 2, LowerBound: 2}, + }, + Status: pgtype.Present, + }, + }) +} + +func TestTextArraySet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.TextArray + }{ + { + source: []string{"foo"}, + result: pgtype.TextArray{ + Elements: []pgtype.Text{{String: "foo", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: (([]string)(nil)), + result: pgtype.TextArray{Status: pgtype.Null}, + }, + { + source: [][]string{{"foo"}, {"bar"}}, + result: pgtype.TextArray{ + Elements: []pgtype.Text{{String: "foo", Status: pgtype.Present}, {String: "bar", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [][][][]string{{{{"foo", "bar", "baz"}}}, {{{"wibble", "wobble", "wubble"}}}}, + result: pgtype.TextArray{ + Elements: []pgtype.Text{ + {String: "foo", Status: pgtype.Present}, + {String: "bar", Status: pgtype.Present}, + {String: "baz", Status: pgtype.Present}, + {String: "wibble", Status: pgtype.Present}, + {String: "wobble", Status: pgtype.Present}, + {String: "wubble", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + { + source: [2][1]string{{"foo"}, {"bar"}}, + result: pgtype.TextArray{ + Elements: []pgtype.Text{{String: "foo", Status: pgtype.Present}, {String: "bar", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [2][1][1][3]string{{{{"foo", "bar", "baz"}}}, {{{"wibble", "wobble", "wubble"}}}}, + result: pgtype.TextArray{ + Elements: []pgtype.Text{ + {String: "foo", Status: pgtype.Present}, + {String: "bar", Status: pgtype.Present}, + {String: "baz", Status: pgtype.Present}, + {String: "wibble", Status: pgtype.Present}, + {String: "wobble", Status: pgtype.Present}, + {String: "wubble", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + } + + for i, tt := range successfulTests { + var r pgtype.TextArray + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !reflect.DeepEqual(r, tt.result) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestTextArrayAssignTo(t *testing.T) { + var stringSlice []string + type _stringSlice []string + var namedStringSlice _stringSlice + var stringSliceDim2 [][]string + var stringSliceDim4 [][][][]string + var stringArrayDim2 [2][1]string + var stringArrayDim4 [2][1][1][3]string + + simpleTests := []struct { + src pgtype.TextArray + dst interface{} + expected interface{} + }{ + { + src: pgtype.TextArray{ + Elements: []pgtype.Text{{String: "foo", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &stringSlice, + expected: []string{"foo"}, + }, + { + src: pgtype.TextArray{ + Elements: []pgtype.Text{{String: "bar", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &namedStringSlice, + expected: _stringSlice{"bar"}, + }, + { + src: pgtype.TextArray{Status: pgtype.Null}, + dst: &stringSlice, + expected: (([]string)(nil)), + }, + { + src: pgtype.TextArray{Status: pgtype.Present}, + dst: &stringSlice, + expected: []string{}, + }, + { + src: pgtype.TextArray{ + Elements: []pgtype.Text{{String: "foo", Status: pgtype.Present}, {String: "bar", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &stringSliceDim2, + expected: [][]string{{"foo"}, {"bar"}}, + }, + { + src: pgtype.TextArray{ + Elements: []pgtype.Text{ + {String: "foo", Status: pgtype.Present}, + {String: "bar", Status: pgtype.Present}, + {String: "baz", Status: pgtype.Present}, + {String: "wibble", Status: pgtype.Present}, + {String: "wobble", Status: pgtype.Present}, + {String: "wubble", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + dst: &stringSliceDim4, + expected: [][][][]string{{{{"foo", "bar", "baz"}}}, {{{"wibble", "wobble", "wubble"}}}}, + }, + { + src: pgtype.TextArray{ + Elements: []pgtype.Text{{String: "foo", Status: pgtype.Present}, {String: "bar", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &stringArrayDim2, + expected: [2][1]string{{"foo"}, {"bar"}}, + }, + { + src: pgtype.TextArray{ + Elements: []pgtype.Text{ + {String: "foo", Status: pgtype.Present}, + {String: "bar", Status: pgtype.Present}, + {String: "baz", Status: pgtype.Present}, + {String: "wibble", Status: pgtype.Present}, + {String: "wobble", Status: pgtype.Present}, + {String: "wubble", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + dst: &stringArrayDim4, + expected: [2][1][1][3]string{{{{"foo", "bar", "baz"}}}, {{{"wibble", "wobble", "wubble"}}}}, + }, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.TextArray + dst interface{} + }{ + { + src: pgtype.TextArray{ + Elements: []pgtype.Text{{Status: pgtype.Null}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &stringSlice, + }, + { + src: pgtype.TextArray{ + Elements: []pgtype.Text{{String: "foo", Status: pgtype.Present}, {String: "bar", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &stringArrayDim2, + }, + { + src: pgtype.TextArray{ + Elements: []pgtype.Text{{String: "foo", Status: pgtype.Present}, {String: "bar", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &stringSlice, + }, + { + src: pgtype.TextArray{ + Elements: []pgtype.Text{{String: "foo", Status: pgtype.Present}, {String: "bar", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &stringArrayDim4, + }, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } +} diff --git a/text_test.go b/text_test.go new file mode 100644 index 00000000..cca3a05d --- /dev/null +++ b/text_test.go @@ -0,0 +1,164 @@ +package pgtype_test + +import ( + "bytes" + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestTextTranscode(t *testing.T) { + for _, pgTypeName := range []string{"text", "varchar"} { + testutil.TestSuccessfulTranscode(t, pgTypeName, []interface{}{ + &pgtype.Text{String: "", Status: pgtype.Present}, + &pgtype.Text{String: "foo", Status: pgtype.Present}, + &pgtype.Text{Status: pgtype.Null}, + }) + } +} + +func TestTextSet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.Text + }{ + {source: "foo", result: pgtype.Text{String: "foo", Status: pgtype.Present}}, + {source: _string("bar"), result: pgtype.Text{String: "bar", Status: pgtype.Present}}, + {source: (*string)(nil), result: pgtype.Text{Status: pgtype.Null}}, + } + + for i, tt := range successfulTests { + var d pgtype.Text + err := d.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if d != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, d) + } + } +} + +func TestTextAssignTo(t *testing.T) { + var s string + var ps *string + + stringTests := []struct { + src pgtype.Text + dst interface{} + expected interface{} + }{ + {src: pgtype.Text{String: "foo", Status: pgtype.Present}, dst: &s, expected: "foo"}, + {src: pgtype.Text{Status: pgtype.Null}, dst: &ps, expected: ((*string)(nil))}, + } + + for i, tt := range stringTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + var buf []byte + + bytesTests := []struct { + src pgtype.Text + dst *[]byte + expected []byte + }{ + {src: pgtype.Text{String: "foo", Status: pgtype.Present}, dst: &buf, expected: []byte("foo")}, + {src: pgtype.Text{Status: pgtype.Null}, dst: &buf, expected: nil}, + } + + for i, tt := range bytesTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if bytes.Compare(*tt.dst, tt.expected) != 0 { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, tt.dst) + } + } + + pointerAllocTests := []struct { + src pgtype.Text + dst interface{} + expected interface{} + }{ + {src: pgtype.Text{String: "foo", Status: pgtype.Present}, dst: &ps, expected: "foo"}, + } + + for i, tt := range pointerAllocTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.Text + dst interface{} + }{ + {src: pgtype.Text{Status: pgtype.Null}, dst: &s}, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } +} + +func TestTextMarshalJSON(t *testing.T) { + successfulTests := []struct { + source pgtype.Text + result string + }{ + {source: pgtype.Text{String: "", Status: pgtype.Null}, result: "null"}, + {source: pgtype.Text{String: "a", Status: pgtype.Present}, result: "\"a\""}, + } + for i, tt := range successfulTests { + r, err := tt.source.MarshalJSON() + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if string(r) != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, string(r)) + } + } +} + +func TestTextUnmarshalJSON(t *testing.T) { + successfulTests := []struct { + source string + result pgtype.Text + }{ + {source: "null", result: pgtype.Text{String: "", Status: pgtype.Null}}, + {source: "\"a\"", result: pgtype.Text{String: "a", Status: pgtype.Present}}, + } + for i, tt := range successfulTests { + var r pgtype.Text + err := r.UnmarshalJSON([]byte(tt.source)) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if r != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} diff --git a/tid_test.go b/tid_test.go new file mode 100644 index 00000000..818be8af --- /dev/null +++ b/tid_test.go @@ -0,0 +1,63 @@ +package pgtype_test + +import ( + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestTIDTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "tid", []interface{}{ + &pgtype.TID{BlockNumber: 42, OffsetNumber: 43, Status: pgtype.Present}, + &pgtype.TID{BlockNumber: 4294967295, OffsetNumber: 65535, Status: pgtype.Present}, + &pgtype.TID{Status: pgtype.Null}, + }) +} + +func TestTIDAssignTo(t *testing.T) { + var s string + var sp *string + + simpleTests := []struct { + src pgtype.TID + dst interface{} + expected interface{} + }{ + {src: pgtype.TID{BlockNumber: 42, OffsetNumber: 43, Status: pgtype.Present}, dst: &s, expected: "(42,43)"}, + {src: pgtype.TID{BlockNumber: 4294967295, OffsetNumber: 65535, Status: pgtype.Present}, dst: &s, expected: "(4294967295,65535)"}, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + pointerAllocTests := []struct { + src pgtype.TID + dst interface{} + expected interface{} + }{ + {src: pgtype.TID{BlockNumber: 42, OffsetNumber: 43, Status: pgtype.Present}, dst: &sp, expected: "(42,43)"}, + {src: pgtype.TID{BlockNumber: 4294967295, OffsetNumber: 65535, Status: pgtype.Present}, dst: &sp, expected: "(4294967295,65535)"}, + } + + for i, tt := range pointerAllocTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } +} + diff --git a/time_test.go b/time_test.go new file mode 100644 index 00000000..0af42b1e --- /dev/null +++ b/time_test.go @@ -0,0 +1,131 @@ +package pgtype_test + +import ( + "reflect" + "testing" + "time" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestTimeTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "time", []interface{}{ + &pgtype.Time{Microseconds: 0, Status: pgtype.Present}, + &pgtype.Time{Microseconds: 1, Status: pgtype.Present}, + &pgtype.Time{Microseconds: 86399999999, Status: pgtype.Present}, + &pgtype.Time{Status: pgtype.Null}, + }) +} + +// Test for transcoding 24:00:00 separately as github.com/lib/pq doesn't seem to support it. +func TestTimeTranscode24HH(t *testing.T) { + pgTypeName := "time" + values := []interface{}{ + &pgtype.Time{Microseconds: 86400000000, Status: pgtype.Present}, + } + + eqFunc := func(a, b interface{}) bool { + return reflect.DeepEqual(a, b) + } + + testutil.TestPgxSuccessfulTranscodeEqFunc(t, pgTypeName, values, eqFunc) + testutil.TestDatabaseSQLSuccessfulTranscodeEqFunc(t, "github.com/jackc/pgx/stdlib", pgTypeName, values, eqFunc) +} + +func TestTimeSet(t *testing.T) { + type _time time.Time + + successfulTests := []struct { + source interface{} + result pgtype.Time + }{ + {source: time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC), result: pgtype.Time{Microseconds: 0, Status: pgtype.Present}}, + {source: time.Date(1900, 1, 1, 1, 0, 0, 0, time.UTC), result: pgtype.Time{Microseconds: 3600000000, Status: pgtype.Present}}, + {source: time.Date(1900, 1, 1, 0, 1, 0, 0, time.UTC), result: pgtype.Time{Microseconds: 60000000, Status: pgtype.Present}}, + {source: time.Date(1900, 1, 1, 0, 0, 1, 0, time.UTC), result: pgtype.Time{Microseconds: 1000000, Status: pgtype.Present}}, + {source: time.Date(1970, 1, 1, 0, 0, 0, 1, time.UTC), result: pgtype.Time{Microseconds: 0, Status: pgtype.Present}}, + {source: time.Date(1970, 1, 1, 0, 0, 0, 1000, time.UTC), result: pgtype.Time{Microseconds: 1, Status: pgtype.Present}}, + {source: time.Date(1999, 12, 31, 23, 59, 59, 999999999, time.UTC), result: pgtype.Time{Microseconds: 86399999999, Status: pgtype.Present}}, + {source: time.Date(2015, 1, 1, 0, 0, 0, 2000, time.Local), result: pgtype.Time{Microseconds: 2, Status: pgtype.Present}}, + {source: func(t time.Time) *time.Time { return &t }(time.Date(2015, 1, 1, 0, 0, 0, 2000, time.Local)), result: pgtype.Time{Microseconds: 2, Status: pgtype.Present}}, + {source: nil, result: pgtype.Time{Status: pgtype.Null}}, + {source: (*time.Time)(nil), result: pgtype.Time{Status: pgtype.Null}}, + {source: _time(time.Date(1970, 1, 1, 0, 0, 0, 3000, time.UTC)), result: pgtype.Time{Microseconds: 3, Status: pgtype.Present}}, + } + + for i, tt := range successfulTests { + var r pgtype.Time + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if r != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestTimeAssignTo(t *testing.T) { + var tim time.Time + var ptim *time.Time + + simpleTests := []struct { + src pgtype.Time + dst interface{} + expected interface{} + }{ + {src: pgtype.Time{Microseconds: 0, Status: pgtype.Present}, dst: &tim, expected: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)}, + {src: pgtype.Time{Microseconds: 3600000000, Status: pgtype.Present}, dst: &tim, expected: time.Date(2000, 1, 1, 1, 0, 0, 0, time.UTC)}, + {src: pgtype.Time{Microseconds: 60000000, Status: pgtype.Present}, dst: &tim, expected: time.Date(2000, 1, 1, 0, 1, 0, 0, time.UTC)}, + {src: pgtype.Time{Microseconds: 1000000, Status: pgtype.Present}, dst: &tim, expected: time.Date(2000, 1, 1, 0, 0, 1, 0, time.UTC)}, + {src: pgtype.Time{Microseconds: 1, Status: pgtype.Present}, dst: &tim, expected: time.Date(2000, 1, 1, 0, 0, 0, 1000, time.UTC)}, + {src: pgtype.Time{Microseconds: 86399999999, Status: pgtype.Present}, dst: &tim, expected: time.Date(2000, 1, 1, 23, 59, 59, 999999000, time.UTC)}, + {src: pgtype.Time{Microseconds: 0, Status: pgtype.Null}, dst: &ptim, expected: ((*time.Time)(nil))}, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + pointerAllocTests := []struct { + src pgtype.Time + dst interface{} + expected interface{} + }{ + {src: pgtype.Time{Microseconds: 0, Status: pgtype.Present}, dst: &ptim, expected: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)}, + } + + for i, tt := range pointerAllocTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.Time + dst interface{} + }{ + {src: pgtype.Time{Microseconds: 86400000000, Status: pgtype.Present}, dst: &tim}, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } +} diff --git a/timestamp_array_test.go b/timestamp_array_test.go new file mode 100644 index 00000000..54d15b24 --- /dev/null +++ b/timestamp_array_test.go @@ -0,0 +1,307 @@ +package pgtype_test + +import ( + "reflect" + "testing" + "time" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestTimestampArrayTranscode(t *testing.T) { + testutil.TestSuccessfulTranscodeEqFunc(t, "timestamp[]", []interface{}{ + &pgtype.TimestampArray{ + Elements: nil, + Dimensions: nil, + Status: pgtype.Present, + }, + &pgtype.TimestampArray{ + Elements: []pgtype.Timestamp{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Status: pgtype.Null}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.TimestampArray{Status: pgtype.Null}, + &pgtype.TimestampArray{ + Elements: []pgtype.Timestamp{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2017, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Status: pgtype.Null}, + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}, {Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.TimestampArray{ + Elements: []pgtype.Timestamp{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2015, 2, 2, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2015, 2, 3, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2015, 2, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{ + {Length: 2, LowerBound: 4}, + {Length: 2, LowerBound: 2}, + }, + Status: pgtype.Present, + }, + }, func(a, b interface{}) bool { + ata := a.(pgtype.TimestampArray) + bta := b.(pgtype.TimestampArray) + + if len(ata.Elements) != len(bta.Elements) || ata.Status != bta.Status { + return false + } + + for i := range ata.Elements { + ae, be := ata.Elements[i], bta.Elements[i] + if !(ae.Time.Equal(be.Time) && ae.Status == be.Status && ae.InfinityModifier == be.InfinityModifier) { + return false + } + } + + return true + }) +} + +func TestTimestampArraySet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.TimestampArray + }{ + { + source: []time.Time{time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC)}, + result: pgtype.TimestampArray{ + Elements: []pgtype.Timestamp{{Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: (([]time.Time)(nil)), + result: pgtype.TimestampArray{Status: pgtype.Null}, + }, + { + source: [][]time.Time{ + {time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC)}, + {time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC)}}, + result: pgtype.TimestampArray{ + Elements: []pgtype.Timestamp{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [][][][]time.Time{ + {{{ + time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), + time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), + time.Date(2017, 5, 6, 0, 0, 0, 0, time.UTC)}}}, + {{{ + time.Date(2018, 7, 8, 0, 0, 0, 0, time.UTC), + time.Date(2019, 9, 10, 0, 0, 0, 0, time.UTC), + time.Date(2020, 11, 12, 0, 0, 0, 0, time.UTC)}}}}, + result: pgtype.TimestampArray{ + Elements: []pgtype.Timestamp{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2017, 5, 6, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2018, 7, 8, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2019, 9, 10, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2020, 11, 12, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + } + + for i, tt := range successfulTests { + var r pgtype.TimestampArray + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !reflect.DeepEqual(r, tt.result) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestTimestampArrayAssignTo(t *testing.T) { + var timeSlice []time.Time + var timeSliceDim2 [][]time.Time + var timeSliceDim4 [][][][]time.Time + var timeArrayDim2 [2][1]time.Time + var timeArrayDim4 [2][1][1][3]time.Time + + simpleTests := []struct { + src pgtype.TimestampArray + dst interface{} + expected interface{} + }{ + { + src: pgtype.TimestampArray{ + Elements: []pgtype.Timestamp{{Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &timeSlice, + expected: []time.Time{time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC)}, + }, + { + src: pgtype.TimestampArray{Status: pgtype.Null}, + dst: &timeSlice, + expected: (([]time.Time)(nil)), + }, + { + src: pgtype.TimestampArray{Status: pgtype.Present}, + dst: &timeSlice, + expected: []time.Time{}, + }, + { + src: pgtype.TimestampArray{ + Elements: []pgtype.Timestamp{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &timeSliceDim2, + expected: [][]time.Time{ + {time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC)}, + {time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC)}}, + }, + { + src: pgtype.TimestampArray{ + Elements: []pgtype.Timestamp{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2017, 5, 6, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2018, 7, 8, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2019, 9, 10, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2020, 11, 12, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + dst: &timeSliceDim4, + expected: [][][][]time.Time{ + {{{ + time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), + time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), + time.Date(2017, 5, 6, 0, 0, 0, 0, time.UTC)}}}, + {{{ + time.Date(2018, 7, 8, 0, 0, 0, 0, time.UTC), + time.Date(2019, 9, 10, 0, 0, 0, 0, time.UTC), + time.Date(2020, 11, 12, 0, 0, 0, 0, time.UTC)}}}}, + }, + { + src: pgtype.TimestampArray{ + Elements: []pgtype.Timestamp{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &timeArrayDim2, + expected: [2][1]time.Time{ + {time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC)}, + {time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC)}}, + }, + { + src: pgtype.TimestampArray{ + Elements: []pgtype.Timestamp{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2017, 5, 6, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2018, 7, 8, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2019, 9, 10, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2020, 11, 12, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + dst: &timeArrayDim4, + expected: [2][1][1][3]time.Time{ + {{{ + time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), + time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), + time.Date(2017, 5, 6, 0, 0, 0, 0, time.UTC)}}}, + {{{ + time.Date(2018, 7, 8, 0, 0, 0, 0, time.UTC), + time.Date(2019, 9, 10, 0, 0, 0, 0, time.UTC), + time.Date(2020, 11, 12, 0, 0, 0, 0, time.UTC)}}}}, + }, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.TimestampArray + dst interface{} + }{ + { + src: pgtype.TimestampArray{ + Elements: []pgtype.Timestamp{{Status: pgtype.Null}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &timeSlice, + }, + { + src: pgtype.TimestampArray{ + Elements: []pgtype.Timestamp{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &timeArrayDim2, + }, + { + src: pgtype.TimestampArray{ + Elements: []pgtype.Timestamp{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &timeSlice, + }, + { + src: pgtype.TimestampArray{ + Elements: []pgtype.Timestamp{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &timeArrayDim4, + }, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } + +} diff --git a/timestamp_test.go b/timestamp_test.go new file mode 100644 index 00000000..74cb1221 --- /dev/null +++ b/timestamp_test.go @@ -0,0 +1,178 @@ +package pgtype_test + +import ( + "reflect" + "testing" + "time" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" + "github.com/stretchr/testify/require" +) + +func TestTimestampTranscode(t *testing.T) { + testutil.TestSuccessfulTranscodeEqFunc(t, "timestamp", []interface{}{ + &pgtype.Timestamp{Time: time.Date(1800, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + &pgtype.Timestamp{Time: time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + &pgtype.Timestamp{Time: time.Date(1905, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + &pgtype.Timestamp{Time: time.Date(1940, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + &pgtype.Timestamp{Time: time.Date(1960, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + &pgtype.Timestamp{Time: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + &pgtype.Timestamp{Time: time.Date(1999, 12, 31, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + &pgtype.Timestamp{Time: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + &pgtype.Timestamp{Time: time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + &pgtype.Timestamp{Time: time.Date(2200, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + &pgtype.Timestamp{Status: pgtype.Null}, + &pgtype.Timestamp{Status: pgtype.Present, InfinityModifier: pgtype.Infinity}, + &pgtype.Timestamp{Status: pgtype.Present, InfinityModifier: -pgtype.Infinity}, + }, func(a, b interface{}) bool { + at := a.(pgtype.Timestamp) + bt := b.(pgtype.Timestamp) + + return at.Time.Equal(bt.Time) && at.Status == bt.Status && at.InfinityModifier == bt.InfinityModifier + }) +} + +func TestTimestampNanosecondsTruncated(t *testing.T) { + tests := []struct { + input time.Time + expected time.Time + }{ + {time.Date(2020, 1, 1, 0, 0, 0, 999999999, time.UTC), time.Date(2020, 1, 1, 0, 0, 0, 999999000, time.UTC)}, + {time.Date(2020, 1, 1, 0, 0, 0, 999999001, time.UTC), time.Date(2020, 1, 1, 0, 0, 0, 999999000, time.UTC)}, + } + for i, tt := range tests { + { + ts := pgtype.Timestamp{Time: tt.input, Status: pgtype.Present} + buf, err := ts.EncodeText(nil, nil) + if err != nil { + t.Errorf("%d. EncodeText failed - %v", i, err) + } + + ts.DecodeText(nil, buf) + if err != nil { + t.Errorf("%d. DecodeText failed - %v", i, err) + } + + if !(ts.Status == pgtype.Present && ts.Time.Equal(tt.expected)) { + t.Errorf("%d. EncodeText did not truncate nanoseconds", i) + } + } + + { + ts := pgtype.Timestamp{Time: tt.input, Status: pgtype.Present} + buf, err := ts.EncodeBinary(nil, nil) + if err != nil { + t.Errorf("%d. EncodeBinary failed - %v", i, err) + } + + ts.DecodeBinary(nil, buf) + if err != nil { + t.Errorf("%d. DecodeBinary failed - %v", i, err) + } + + if !(ts.Status == pgtype.Present && ts.Time.Equal(tt.expected)) { + t.Errorf("%d. EncodeBinary did not truncate nanoseconds", i) + } + } + } +} + +// https://github.com/jackc/pgtype/issues/74 +func TestTimestampDecodeTextInvalid(t *testing.T) { + tstz := &pgtype.Timestamp{} + err := tstz.DecodeText(nil, []byte(`eeeee`)) + require.Error(t, err) +} + +func TestTimestampSet(t *testing.T) { + type _time time.Time + + successfulTests := []struct { + source interface{} + result pgtype.Timestamp + }{ + {source: time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC), result: pgtype.Timestamp{Time: time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + {source: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC), result: pgtype.Timestamp{Time: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + {source: time.Date(1999, 12, 31, 12, 59, 59, 0, time.UTC), result: pgtype.Timestamp{Time: time.Date(1999, 12, 31, 12, 59, 59, 0, time.UTC), Status: pgtype.Present}}, + {source: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), result: pgtype.Timestamp{Time: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + {source: time.Date(2000, 1, 1, 0, 0, 1, 0, time.UTC), result: pgtype.Timestamp{Time: time.Date(2000, 1, 1, 0, 0, 1, 0, time.UTC), Status: pgtype.Present}}, + {source: time.Date(2200, 1, 1, 0, 0, 0, 0, time.UTC), result: pgtype.Timestamp{Time: time.Date(2200, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + {source: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), result: pgtype.Timestamp{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + {source: _time(time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)), result: pgtype.Timestamp{Time: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + {source: pgtype.Infinity, result: pgtype.Timestamp{InfinityModifier: pgtype.Infinity, Status: pgtype.Present}}, + {source: pgtype.NegativeInfinity, result: pgtype.Timestamp{InfinityModifier: pgtype.NegativeInfinity, Status: pgtype.Present}}, + } + + for i, tt := range successfulTests { + var r pgtype.Timestamp + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if r != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestTimestampAssignTo(t *testing.T) { + var tim time.Time + var ptim *time.Time + + simpleTests := []struct { + src pgtype.Timestamp + dst interface{} + expected interface{} + }{ + {src: pgtype.Timestamp{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, dst: &tim, expected: time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)}, + {src: pgtype.Timestamp{Time: time.Time{}, Status: pgtype.Null}, dst: &ptim, expected: ((*time.Time)(nil))}, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + pointerAllocTests := []struct { + src pgtype.Timestamp + dst interface{} + expected interface{} + }{ + {src: pgtype.Timestamp{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}, dst: &ptim, expected: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local)}, + } + + for i, tt := range pointerAllocTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.Timestamp + dst interface{} + }{ + {src: pgtype.Timestamp{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), InfinityModifier: pgtype.Infinity, Status: pgtype.Present}, dst: &tim}, + {src: pgtype.Timestamp{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), InfinityModifier: pgtype.NegativeInfinity, Status: pgtype.Present}, dst: &tim}, + {src: pgtype.Timestamp{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Null}, dst: &tim}, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } +} diff --git a/timestamptz_array_test.go b/timestamptz_array_test.go new file mode 100644 index 00000000..9856e4e7 --- /dev/null +++ b/timestamptz_array_test.go @@ -0,0 +1,343 @@ +package pgtype_test + +import ( + "reflect" + "testing" + "time" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestTimestamptzArrayTranscode(t *testing.T) { + testutil.TestSuccessfulTranscodeEqFunc(t, "timestamptz[]", []interface{}{ + &pgtype.TimestamptzArray{ + Elements: nil, + Dimensions: nil, + Status: pgtype.Present, + }, + &pgtype.TimestamptzArray{ + Elements: []pgtype.Timestamptz{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Status: pgtype.Null}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.TimestamptzArray{Status: pgtype.Null}, + &pgtype.TimestamptzArray{ + Elements: []pgtype.Timestamptz{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2017, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Status: pgtype.Null}, + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}, {Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.TimestamptzArray{ + Elements: []pgtype.Timestamptz{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2015, 2, 2, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2015, 2, 3, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2015, 2, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{ + {Length: 2, LowerBound: 4}, + {Length: 2, LowerBound: 2}, + }, + Status: pgtype.Present, + }, + }, func(a, b interface{}) bool { + ata := a.(pgtype.TimestamptzArray) + bta := b.(pgtype.TimestamptzArray) + + if len(ata.Elements) != len(bta.Elements) || ata.Status != bta.Status { + return false + } + + for i := range ata.Elements { + ae, be := ata.Elements[i], bta.Elements[i] + if !(ae.Time.Equal(be.Time) && ae.Status == be.Status && ae.InfinityModifier == be.InfinityModifier) { + return false + } + } + + return true + }) +} + +func TestTimestamptzArraySet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.TimestamptzArray + }{ + { + source: []time.Time{time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC)}, + result: pgtype.TimestamptzArray{ + Elements: []pgtype.Timestamptz{{Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: (([]time.Time)(nil)), + result: pgtype.TimestamptzArray{Status: pgtype.Null}, + }, + { + source: [][]time.Time{ + {time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC)}, + {time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC)}}, + result: pgtype.TimestamptzArray{ + Elements: []pgtype.Timestamptz{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [][][][]time.Time{ + {{{ + time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), + time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), + time.Date(2017, 5, 6, 0, 0, 0, 0, time.UTC)}}}, + {{{ + time.Date(2018, 7, 8, 0, 0, 0, 0, time.UTC), + time.Date(2019, 9, 10, 0, 0, 0, 0, time.UTC), + time.Date(2020, 11, 12, 0, 0, 0, 0, time.UTC)}}}}, + result: pgtype.TimestamptzArray{ + Elements: []pgtype.Timestamptz{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2017, 5, 6, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2018, 7, 8, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2019, 9, 10, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2020, 11, 12, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + { + source: [2][1]time.Time{ + {time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC)}, + {time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC)}}, + result: pgtype.TimestamptzArray{ + Elements: []pgtype.Timestamptz{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [2][1][1][3]time.Time{ + {{{ + time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), + time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), + time.Date(2017, 5, 6, 0, 0, 0, 0, time.UTC)}}}, + {{{ + time.Date(2018, 7, 8, 0, 0, 0, 0, time.UTC), + time.Date(2019, 9, 10, 0, 0, 0, 0, time.UTC), + time.Date(2020, 11, 12, 0, 0, 0, 0, time.UTC)}}}}, + result: pgtype.TimestamptzArray{ + Elements: []pgtype.Timestamptz{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2017, 5, 6, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2018, 7, 8, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2019, 9, 10, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2020, 11, 12, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + } + + for i, tt := range successfulTests { + var r pgtype.TimestamptzArray + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !reflect.DeepEqual(r, tt.result) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestTimestamptzArrayAssignTo(t *testing.T) { + var timeSlice []time.Time + var timeSliceDim2 [][]time.Time + var timeSliceDim4 [][][][]time.Time + var timeArrayDim2 [2][1]time.Time + var timeArrayDim4 [2][1][1][3]time.Time + + simpleTests := []struct { + src pgtype.TimestamptzArray + dst interface{} + expected interface{} + }{ + { + src: pgtype.TimestamptzArray{ + Elements: []pgtype.Timestamptz{{Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &timeSlice, + expected: []time.Time{time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC)}, + }, + { + src: pgtype.TimestamptzArray{Status: pgtype.Null}, + dst: &timeSlice, + expected: (([]time.Time)(nil)), + }, + { + src: pgtype.TimestamptzArray{Status: pgtype.Present}, + dst: &timeSlice, + expected: []time.Time{}, + }, + { + src: pgtype.TimestamptzArray{ + Elements: []pgtype.Timestamptz{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &timeSliceDim2, + expected: [][]time.Time{ + {time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC)}, + {time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC)}}, + }, + { + src: pgtype.TimestamptzArray{ + Elements: []pgtype.Timestamptz{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2017, 5, 6, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2018, 7, 8, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2019, 9, 10, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2020, 11, 12, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + dst: &timeSliceDim4, + expected: [][][][]time.Time{ + {{{ + time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), + time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), + time.Date(2017, 5, 6, 0, 0, 0, 0, time.UTC)}}}, + {{{ + time.Date(2018, 7, 8, 0, 0, 0, 0, time.UTC), + time.Date(2019, 9, 10, 0, 0, 0, 0, time.UTC), + time.Date(2020, 11, 12, 0, 0, 0, 0, time.UTC)}}}}, + }, + { + src: pgtype.TimestamptzArray{ + Elements: []pgtype.Timestamptz{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &timeArrayDim2, + expected: [2][1]time.Time{ + {time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC)}, + {time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC)}}, + }, + { + src: pgtype.TimestamptzArray{ + Elements: []pgtype.Timestamptz{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2017, 5, 6, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2018, 7, 8, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2019, 9, 10, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2020, 11, 12, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + dst: &timeArrayDim4, + expected: [2][1][1][3]time.Time{ + {{{ + time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), + time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), + time.Date(2017, 5, 6, 0, 0, 0, 0, time.UTC)}}}, + {{{ + time.Date(2018, 7, 8, 0, 0, 0, 0, time.UTC), + time.Date(2019, 9, 10, 0, 0, 0, 0, time.UTC), + time.Date(2020, 11, 12, 0, 0, 0, 0, time.UTC)}}}}, + }, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.TimestamptzArray + dst interface{} + }{ + { + src: pgtype.TimestamptzArray{ + Elements: []pgtype.Timestamptz{{Status: pgtype.Null}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &timeSlice, + }, + { + src: pgtype.TimestamptzArray{ + Elements: []pgtype.Timestamptz{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &timeArrayDim2, + }, + { + src: pgtype.TimestamptzArray{ + Elements: []pgtype.Timestamptz{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &timeSlice, + }, + { + src: pgtype.TimestamptzArray{ + Elements: []pgtype.Timestamptz{ + {Time: time.Date(2015, 2, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + {Time: time.Date(2016, 3, 4, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &timeArrayDim4, + }, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } + +} diff --git a/timestamptz_test.go b/timestamptz_test.go new file mode 100644 index 00000000..769c9239 --- /dev/null +++ b/timestamptz_test.go @@ -0,0 +1,224 @@ +package pgtype_test + +import ( + "reflect" + "testing" + "time" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" + "github.com/stretchr/testify/require" +) + +func TestTimestamptzTranscode(t *testing.T) { + testutil.TestSuccessfulTranscodeEqFunc(t, "timestamptz", []interface{}{ + &pgtype.Timestamptz{Time: time.Date(1800, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}, + &pgtype.Timestamptz{Time: time.Date(1900, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}, + &pgtype.Timestamptz{Time: time.Date(1905, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}, + &pgtype.Timestamptz{Time: time.Date(1940, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}, + &pgtype.Timestamptz{Time: time.Date(1960, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}, + &pgtype.Timestamptz{Time: time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}, + &pgtype.Timestamptz{Time: time.Date(1999, 12, 31, 0, 0, 0, 0, time.Local), Status: pgtype.Present}, + &pgtype.Timestamptz{Time: time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}, + &pgtype.Timestamptz{Time: time.Date(2000, 1, 2, 0, 0, 0, 0, time.Local), Status: pgtype.Present}, + &pgtype.Timestamptz{Time: time.Date(2200, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}, + &pgtype.Timestamptz{Status: pgtype.Null}, + &pgtype.Timestamptz{Status: pgtype.Present, InfinityModifier: pgtype.Infinity}, + &pgtype.Timestamptz{Status: pgtype.Present, InfinityModifier: -pgtype.Infinity}, + }, func(a, b interface{}) bool { + at := a.(pgtype.Timestamptz) + bt := b.(pgtype.Timestamptz) + + return at.Time.Equal(bt.Time) && at.Status == bt.Status && at.InfinityModifier == bt.InfinityModifier + }) +} + +func TestTimestamptzNanosecondsTruncated(t *testing.T) { + tests := []struct { + input time.Time + expected time.Time + }{ + {time.Date(2020, 1, 1, 0, 0, 0, 999999999, time.Local), time.Date(2020, 1, 1, 0, 0, 0, 999999000, time.Local)}, + {time.Date(2020, 1, 1, 0, 0, 0, 999999001, time.Local), time.Date(2020, 1, 1, 0, 0, 0, 999999000, time.Local)}, + } + for i, tt := range tests { + { + tstz := pgtype.Timestamptz{Time: tt.input, Status: pgtype.Present} + buf, err := tstz.EncodeText(nil, nil) + if err != nil { + t.Errorf("%d. EncodeText failed - %v", i, err) + } + + tstz.DecodeText(nil, buf) + if err != nil { + t.Errorf("%d. DecodeText failed - %v", i, err) + } + + if !(tstz.Status == pgtype.Present && tstz.Time.Equal(tt.expected)) { + t.Errorf("%d. EncodeText did not truncate nanoseconds", i) + } + } + + { + tstz := pgtype.Timestamptz{Time: tt.input, Status: pgtype.Present} + buf, err := tstz.EncodeBinary(nil, nil) + if err != nil { + t.Errorf("%d. EncodeBinary failed - %v", i, err) + } + + tstz.DecodeBinary(nil, buf) + if err != nil { + t.Errorf("%d. DecodeBinary failed - %v", i, err) + } + + if !(tstz.Status == pgtype.Present && tstz.Time.Equal(tt.expected)) { + t.Errorf("%d. EncodeBinary did not truncate nanoseconds", i) + } + } + } +} + +// https://github.com/jackc/pgtype/issues/74 +func TestTimestamptzDecodeTextInvalid(t *testing.T) { + tstz := &pgtype.Timestamptz{} + err := tstz.DecodeText(nil, []byte(`eeeee`)) + require.Error(t, err) +} + +func TestTimestamptzSet(t *testing.T) { + type _time time.Time + + successfulTests := []struct { + source interface{} + result pgtype.Timestamptz + }{ + {source: time.Date(1900, 1, 1, 0, 0, 0, 0, time.Local), result: pgtype.Timestamptz{Time: time.Date(1900, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}}, + {source: time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local), result: pgtype.Timestamptz{Time: time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}}, + {source: time.Date(1999, 12, 31, 12, 59, 59, 0, time.Local), result: pgtype.Timestamptz{Time: time.Date(1999, 12, 31, 12, 59, 59, 0, time.Local), Status: pgtype.Present}}, + {source: time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local), result: pgtype.Timestamptz{Time: time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}}, + {source: time.Date(2000, 1, 1, 0, 0, 1, 0, time.Local), result: pgtype.Timestamptz{Time: time.Date(2000, 1, 1, 0, 0, 1, 0, time.Local), Status: pgtype.Present}}, + {source: time.Date(2200, 1, 1, 0, 0, 0, 0, time.Local), result: pgtype.Timestamptz{Time: time.Date(2200, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}}, + {source: _time(time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local)), result: pgtype.Timestamptz{Time: time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}}, + {source: pgtype.Infinity, result: pgtype.Timestamptz{InfinityModifier: pgtype.Infinity, Status: pgtype.Present}}, + {source: pgtype.NegativeInfinity, result: pgtype.Timestamptz{InfinityModifier: pgtype.NegativeInfinity, Status: pgtype.Present}}, + } + + for i, tt := range successfulTests { + var r pgtype.Timestamptz + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if r != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestTimestamptzAssignTo(t *testing.T) { + var tim time.Time + var ptim *time.Time + + simpleTests := []struct { + src pgtype.Timestamptz + dst interface{} + expected interface{} + }{ + {src: pgtype.Timestamptz{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}, dst: &tim, expected: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local)}, + {src: pgtype.Timestamptz{Time: time.Time{}, Status: pgtype.Null}, dst: &ptim, expected: ((*time.Time)(nil))}, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + pointerAllocTests := []struct { + src pgtype.Timestamptz + dst interface{} + expected interface{} + }{ + {src: pgtype.Timestamptz{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}, dst: &ptim, expected: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local)}, + } + + for i, tt := range pointerAllocTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.Timestamptz + dst interface{} + }{ + {src: pgtype.Timestamptz{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), InfinityModifier: pgtype.Infinity, Status: pgtype.Present}, dst: &tim}, + {src: pgtype.Timestamptz{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), InfinityModifier: pgtype.NegativeInfinity, Status: pgtype.Present}, dst: &tim}, + {src: pgtype.Timestamptz{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Null}, dst: &tim}, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } +} + +func TestTimestamptzMarshalJSON(t *testing.T) { + successfulTests := []struct { + source pgtype.Timestamptz + result string + }{ + {source: pgtype.Timestamptz{Status: pgtype.Null}, result: "null"}, + {source: pgtype.Timestamptz{Time: time.Date(2012, 3, 29, 10, 5, 45, 0, time.FixedZone("", -6*60*60)), Status: pgtype.Present}, result: "\"2012-03-29T10:05:45-06:00\""}, + {source: pgtype.Timestamptz{Time: time.Date(2012, 3, 29, 10, 5, 45, 555*1000*1000, time.FixedZone("", -6*60*60)), Status: pgtype.Present}, result: "\"2012-03-29T10:05:45.555-06:00\""}, + {source: pgtype.Timestamptz{InfinityModifier: pgtype.Infinity, Status: pgtype.Present}, result: "\"infinity\""}, + {source: pgtype.Timestamptz{InfinityModifier: pgtype.NegativeInfinity, Status: pgtype.Present}, result: "\"-infinity\""}, + } + for i, tt := range successfulTests { + r, err := tt.source.MarshalJSON() + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if string(r) != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, string(r)) + } + } +} + +func TestTimestamptzUnmarshalJSON(t *testing.T) { + successfulTests := []struct { + source string + result pgtype.Timestamptz + }{ + {source: "null", result: pgtype.Timestamptz{Status: pgtype.Null}}, + {source: "\"2012-03-29T10:05:45-06:00\"", result: pgtype.Timestamptz{Time: time.Date(2012, 3, 29, 10, 5, 45, 0, time.FixedZone("", -6*60*60)), Status: pgtype.Present}}, + {source: "\"2012-03-29T10:05:45.555-06:00\"", result: pgtype.Timestamptz{Time: time.Date(2012, 3, 29, 10, 5, 45, 555*1000*1000, time.FixedZone("", -6*60*60)), Status: pgtype.Present}}, + {source: "\"infinity\"", result: pgtype.Timestamptz{InfinityModifier: pgtype.Infinity, Status: pgtype.Present}}, + {source: "\"-infinity\"", result: pgtype.Timestamptz{InfinityModifier: pgtype.NegativeInfinity, Status: pgtype.Present}}, + } + for i, tt := range successfulTests { + var r pgtype.Timestamptz + err := r.UnmarshalJSON([]byte(tt.source)) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !r.Time.Equal(tt.result.Time) || r.Status != tt.result.Status || r.InfinityModifier != tt.result.InfinityModifier { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} diff --git a/tsrange_test.go b/tsrange_test.go new file mode 100644 index 00000000..1be0c7d2 --- /dev/null +++ b/tsrange_test.go @@ -0,0 +1,41 @@ +package pgtype_test + +import ( + "testing" + "time" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestTsrangeTranscode(t *testing.T) { + testutil.TestSuccessfulTranscodeEqFunc(t, "tsrange", []interface{}{ + &pgtype.Tsrange{LowerType: pgtype.Empty, UpperType: pgtype.Empty, Status: pgtype.Present}, + &pgtype.Tsrange{ + Lower: pgtype.Timestamp{Time: time.Date(1990, 12, 31, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + Upper: pgtype.Timestamp{Time: time.Date(2028, 1, 1, 0, 23, 12, 0, time.UTC), Status: pgtype.Present}, + LowerType: pgtype.Inclusive, + UpperType: pgtype.Exclusive, + Status: pgtype.Present, + }, + &pgtype.Tsrange{ + Lower: pgtype.Timestamp{Time: time.Date(1800, 12, 31, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + Upper: pgtype.Timestamp{Time: time.Date(2200, 1, 1, 0, 23, 12, 0, time.UTC), Status: pgtype.Present}, + LowerType: pgtype.Inclusive, + UpperType: pgtype.Exclusive, + Status: pgtype.Present, + }, + &pgtype.Tsrange{Status: pgtype.Null}, + }, func(aa, bb interface{}) bool { + a := aa.(pgtype.Tsrange) + b := bb.(pgtype.Tsrange) + + return a.Status == b.Status && + a.Lower.Time.Equal(b.Lower.Time) && + a.Lower.Status == b.Lower.Status && + a.Lower.InfinityModifier == b.Lower.InfinityModifier && + a.Upper.Time.Equal(b.Upper.Time) && + a.Upper.Status == b.Upper.Status && + a.Upper.InfinityModifier == b.Upper.InfinityModifier + }) +} diff --git a/tstzrange_test.go b/tstzrange_test.go new file mode 100644 index 00000000..f8e2c2c5 --- /dev/null +++ b/tstzrange_test.go @@ -0,0 +1,49 @@ +package pgtype_test + +import ( + "testing" + "time" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" + "github.com/stretchr/testify/require" +) + +func TestTstzrangeTranscode(t *testing.T) { + testutil.TestSuccessfulTranscodeEqFunc(t, "tstzrange", []interface{}{ + &pgtype.Tstzrange{LowerType: pgtype.Empty, UpperType: pgtype.Empty, Status: pgtype.Present}, + &pgtype.Tstzrange{ + Lower: pgtype.Timestamptz{Time: time.Date(1990, 12, 31, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + Upper: pgtype.Timestamptz{Time: time.Date(2028, 1, 1, 0, 23, 12, 0, time.UTC), Status: pgtype.Present}, + LowerType: pgtype.Inclusive, + UpperType: pgtype.Exclusive, + Status: pgtype.Present, + }, + &pgtype.Tstzrange{ + Lower: pgtype.Timestamptz{Time: time.Date(1800, 12, 31, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, + Upper: pgtype.Timestamptz{Time: time.Date(2200, 1, 1, 0, 23, 12, 0, time.UTC), Status: pgtype.Present}, + LowerType: pgtype.Inclusive, + UpperType: pgtype.Exclusive, + Status: pgtype.Present, + }, + &pgtype.Tstzrange{Status: pgtype.Null}, + }, func(aa, bb interface{}) bool { + a := aa.(pgtype.Tstzrange) + b := bb.(pgtype.Tstzrange) + + return a.Status == b.Status && + a.Lower.Time.Equal(b.Lower.Time) && + a.Lower.Status == b.Lower.Status && + a.Lower.InfinityModifier == b.Lower.InfinityModifier && + a.Upper.Time.Equal(b.Upper.Time) && + a.Upper.Status == b.Upper.Status && + a.Upper.InfinityModifier == b.Upper.InfinityModifier + }) +} + +// https://github.com/jackc/pgtype/issues/74 +func TestTstzRangeDecodeTextInvalid(t *testing.T) { + tstzrange := &pgtype.Tstzrange{} + err := tstzrange.DecodeText(nil, []byte(`[eeee,)`)) + require.Error(t, err) +} diff --git a/uuid_array_test.go b/uuid_array_test.go new file mode 100644 index 00000000..7d822e7a --- /dev/null +++ b/uuid_array_test.go @@ -0,0 +1,368 @@ +package pgtype_test + +import ( + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestUUIDArrayTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "uuid[]", []interface{}{ + &pgtype.UUIDArray{ + Elements: nil, + Dimensions: nil, + Status: pgtype.Present, + }, + &pgtype.UUIDArray{ + Elements: []pgtype.UUID{ + {Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}, + {Status: pgtype.Null}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.UUIDArray{Status: pgtype.Null}, + &pgtype.UUIDArray{ + Elements: []pgtype.UUID{ + {Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}, + {Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Status: pgtype.Present}, + {Bytes: [16]byte{32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}, Status: pgtype.Present}, + {Bytes: [16]byte{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63}, Status: pgtype.Present}, + {Status: pgtype.Null}, + {Bytes: [16]byte{64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79}, Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}, {Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.UUIDArray{ + Elements: []pgtype.UUID{ + {Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}, + {Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Status: pgtype.Present}, + {Bytes: [16]byte{32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}, Status: pgtype.Present}, + {Bytes: [16]byte{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63}, Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{ + {Length: 2, LowerBound: 4}, + {Length: 2, LowerBound: 2}, + }, + Status: pgtype.Present, + }, + }) +} + +func TestUUIDArraySet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.UUIDArray + }{ + { + source: nil, + result: pgtype.UUIDArray{Status: pgtype.Null}, + }, + { + source: [][16]byte{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}}, + result: pgtype.UUIDArray{ + Elements: []pgtype.UUID{{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [][16]byte{}, + result: pgtype.UUIDArray{Status: pgtype.Present}, + }, + { + source: ([][16]byte)(nil), + result: pgtype.UUIDArray{Status: pgtype.Null}, + }, + { + source: [][]byte{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}}, + result: pgtype.UUIDArray{ + Elements: []pgtype.UUID{{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [][]byte{ + {0, 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}, + nil, + {32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}, + }, + result: pgtype.UUIDArray{ + Elements: []pgtype.UUID{ + {Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}, + {Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Status: pgtype.Present}, + {Status: pgtype.Null}, + {Bytes: [16]byte{32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}, Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 4}}, + Status: pgtype.Present}, + }, + { + source: [][]byte{}, + result: pgtype.UUIDArray{Status: pgtype.Present}, + }, + { + source: ([][]byte)(nil), + result: pgtype.UUIDArray{Status: pgtype.Null}, + }, + { + source: []string{"00010203-0405-0607-0809-0a0b0c0d0e0f"}, + result: pgtype.UUIDArray{ + Elements: []pgtype.UUID{{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: []string{}, + result: pgtype.UUIDArray{Status: pgtype.Present}, + }, + { + source: ([]string)(nil), + result: pgtype.UUIDArray{Status: pgtype.Null}, + }, + { + source: [][][16]byte{{ + {0, 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}}}, + result: pgtype.UUIDArray{ + Elements: []pgtype.UUID{ + {Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}, + {Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [][][][]string{ + {{{ + "00010203-0405-0607-0809-0a0b0c0d0e0f", + "10111213-1415-1617-1819-1a1b1c1d1e1f", + "20212223-2425-2627-2829-2a2b2c2d2e2f"}}}, + {{{ + "30313233-3435-3637-3839-3a3b3c3d3e3f", + "40414243-4445-4647-4849-4a4b4c4d4e4f", + "50515253-5455-5657-5859-5a5b5c5d5e5f"}}}}, + result: pgtype.UUIDArray{ + Elements: []pgtype.UUID{ + {Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}, + {Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Status: pgtype.Present}, + {Bytes: [16]byte{32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}, Status: pgtype.Present}, + {Bytes: [16]byte{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63}, Status: pgtype.Present}, + {Bytes: [16]byte{64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79}, Status: pgtype.Present}, + {Bytes: [16]byte{80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95}, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + { + source: [2][1][16]byte{{ + {0, 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}}}, + result: pgtype.UUIDArray{ + Elements: []pgtype.UUID{ + {Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}, + {Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [2][1][1][3]string{ + {{{ + "00010203-0405-0607-0809-0a0b0c0d0e0f", + "10111213-1415-1617-1819-1a1b1c1d1e1f", + "20212223-2425-2627-2829-2a2b2c2d2e2f"}}}, + {{{ + "30313233-3435-3637-3839-3a3b3c3d3e3f", + "40414243-4445-4647-4849-4a4b4c4d4e4f", + "50515253-5455-5657-5859-5a5b5c5d5e5f"}}}}, + result: pgtype.UUIDArray{ + Elements: []pgtype.UUID{ + {Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}, + {Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Status: pgtype.Present}, + {Bytes: [16]byte{32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}, Status: pgtype.Present}, + {Bytes: [16]byte{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63}, Status: pgtype.Present}, + {Bytes: [16]byte{64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79}, Status: pgtype.Present}, + {Bytes: [16]byte{80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95}, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + } + + for i, tt := range successfulTests { + var r pgtype.UUIDArray + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !reflect.DeepEqual(r, tt.result) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestUUIDArrayAssignTo(t *testing.T) { + var byteArraySlice [][16]byte + var byteSliceSlice [][]byte + var stringSlice []string + var byteSlice []byte + var byteArraySliceDim2 [][][16]byte + var stringSliceDim4 [][][][]string + var byteArrayDim2 [2][1][16]byte + var stringArrayDim4 [2][1][1][3]string + + simpleTests := []struct { + src pgtype.UUIDArray + dst interface{} + expected interface{} + }{ + { + src: pgtype.UUIDArray{ + Elements: []pgtype.UUID{{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &byteArraySlice, + expected: [][16]byte{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}}, + }, + { + src: pgtype.UUIDArray{Status: pgtype.Null}, + dst: &byteArraySlice, + expected: ([][16]byte)(nil), + }, + { + src: pgtype.UUIDArray{ + Elements: []pgtype.UUID{{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &byteSliceSlice, + expected: [][]byte{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}}, + }, + { + src: pgtype.UUIDArray{Status: pgtype.Null}, + dst: &byteSliceSlice, + expected: ([][]byte)(nil), + }, + { + src: pgtype.UUIDArray{Status: pgtype.Present}, + dst: &byteSlice, + expected: []byte{}, + }, + { + src: pgtype.UUIDArray{Status: pgtype.Present}, + dst: &stringSlice, + expected: []string{}, + }, + { + src: pgtype.UUIDArray{ + Elements: []pgtype.UUID{{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &stringSlice, + expected: []string{"00010203-0405-0607-0809-0a0b0c0d0e0f"}, + }, + { + src: pgtype.UUIDArray{Status: pgtype.Null}, + dst: &stringSlice, + expected: ([]string)(nil), + }, + { + src: pgtype.UUIDArray{ + Elements: []pgtype.UUID{ + {Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}, + {Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &byteArraySliceDim2, + expected: [][][16]byte{{ + {0, 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}}}, + }, + { + src: pgtype.UUIDArray{ + Elements: []pgtype.UUID{ + {Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}, + {Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Status: pgtype.Present}, + {Bytes: [16]byte{32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}, Status: pgtype.Present}, + {Bytes: [16]byte{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63}, Status: pgtype.Present}, + {Bytes: [16]byte{64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79}, Status: pgtype.Present}, + {Bytes: [16]byte{80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95}, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + dst: &stringSliceDim4, + expected: [][][][]string{ + {{{ + "00010203-0405-0607-0809-0a0b0c0d0e0f", + "10111213-1415-1617-1819-1a1b1c1d1e1f", + "20212223-2425-2627-2829-2a2b2c2d2e2f"}}}, + {{{ + "30313233-3435-3637-3839-3a3b3c3d3e3f", + "40414243-4445-4647-4849-4a4b4c4d4e4f", + "50515253-5455-5657-5859-5a5b5c5d5e5f"}}}}, + }, + { + src: pgtype.UUIDArray{ + Elements: []pgtype.UUID{ + {Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}, + {Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &byteArrayDim2, + expected: [2][1][16]byte{{ + {0, 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}}}, + }, + { + src: pgtype.UUIDArray{ + Elements: []pgtype.UUID{ + {Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}, + {Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Status: pgtype.Present}, + {Bytes: [16]byte{32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}, Status: pgtype.Present}, + {Bytes: [16]byte{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63}, Status: pgtype.Present}, + {Bytes: [16]byte{64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79}, Status: pgtype.Present}, + {Bytes: [16]byte{80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95}, Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + dst: &stringArrayDim4, + expected: [2][1][1][3]string{ + {{{ + "00010203-0405-0607-0809-0a0b0c0d0e0f", + "10111213-1415-1617-1819-1a1b1c1d1e1f", + "20212223-2425-2627-2829-2a2b2c2d2e2f"}}}, + {{{ + "30313233-3435-3637-3839-3a3b3c3d3e3f", + "40414243-4445-4647-4849-4a4b4c4d4e4f", + "50515253-5455-5657-5859-5a5b5c5d5e5f"}}}}, + }, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } +} diff --git a/uuid_test.go b/uuid_test.go new file mode 100644 index 00000000..5a93ea8d --- /dev/null +++ b/uuid_test.go @@ -0,0 +1,245 @@ +package pgtype_test + +import ( + "bytes" + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestUUIDTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "uuid", []interface{}{ + &pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}, + &pgtype.UUID{Status: pgtype.Null}, + }) +} + +type SomeUUIDWrapper struct { + SomeUUIDType +} + +type SomeUUIDType [16]byte + +func TestUUIDSet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.UUID + }{ + { + source: nil, + result: pgtype.UUID{Status: pgtype.Null}, + }, + { + source: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, + result: pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}, + }, + { + source: []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, + result: pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}, + }, + { + source: SomeUUIDType{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, + result: pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}, + }, + { + source: ([]byte)(nil), + result: pgtype.UUID{Status: pgtype.Null}, + }, + { + source: "00010203-0405-0607-0809-0a0b0c0d0e0f", + result: pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}, + }, + { + source: "000102030405060708090a0b0c0d0e0f", + result: pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}, + }, + } + + for i, tt := range successfulTests { + var r pgtype.UUID + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if r != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestUUIDAssignTo(t *testing.T) { + { + src := pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present} + var dst [16]byte + expected := [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} + + err := src.AssignTo(&dst) + if err != nil { + t.Error(err) + } + + if dst != expected { + t.Errorf("expected %v to assign %v, but result was %v", src, expected, dst) + } + } + + { + src := pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present} + var dst []byte + expected := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} + + err := src.AssignTo(&dst) + if err != nil { + t.Error(err) + } + + if bytes.Compare(dst, expected) != 0 { + t.Errorf("expected %v to assign %v, but result was %v", src, expected, dst) + } + } + + { + src := pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present} + var dst SomeUUIDType + expected := [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} + + err := src.AssignTo(&dst) + if err != nil { + t.Error(err) + } + + if dst != expected { + t.Errorf("expected %v to assign %v, but result was %v", src, expected, dst) + } + } + + { + src := pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present} + var dst string + expected := "00010203-0405-0607-0809-0a0b0c0d0e0f" + + err := src.AssignTo(&dst) + if err != nil { + t.Error(err) + } + + if dst != expected { + t.Errorf("expected %v to assign %v, but result was %v", src, expected, dst) + } + } + + { + src := pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present} + var dst SomeUUIDWrapper + expected := [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} + + err := src.AssignTo(&dst) + if err != nil { + t.Error(err) + } + + if dst.SomeUUIDType != expected { + t.Errorf("expected %v to assign %v, but result was %v", src, expected, dst) + } + } +} + +func TestUUID_MarshalJSON(t *testing.T) { + tests := []struct { + name string + src pgtype.UUID + want []byte + wantErr bool + }{ + { + name: "first", + src: pgtype.UUID{ + Bytes: [16]byte{29, 72, 90, 122, 109, 24, 69, 153, 140, 108, 52, 66, 86, 22, 136, 122}, + Status: pgtype.Present, + }, + want: []byte(`"1d485a7a-6d18-4599-8c6c-34425616887a"`), + wantErr: false, + }, + { + name: "second", + src: pgtype.UUID{ + Bytes: [16]byte{}, + Status: pgtype.Undefined, + }, + want: nil, + wantErr: true, + }, + { + name: "third", + src: pgtype.UUID{ + Bytes: [16]byte{}, + Status: pgtype.Null, + }, + want: []byte("null"), + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := tt.src.MarshalJSON() + if (err != nil) != tt.wantErr { + t.Errorf("MarshalJSON() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("MarshalJSON() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestUUID_UnmarshalJSON(t *testing.T) { + tests := []struct { + name string + want *pgtype.UUID + src []byte + wantErr bool + }{ + { + name: "first", + want: &pgtype.UUID{ + Bytes: [16]byte{29, 72, 90, 122, 109, 24, 69, 153, 140, 108, 52, 66, 86, 22, 136, 122}, + Status: pgtype.Present, + }, + src: []byte(`"1d485a7a-6d18-4599-8c6c-34425616887a"`), + wantErr: false, + }, + { + name: "second", + want: &pgtype.UUID{ + Bytes: [16]byte{}, + Status: pgtype.Null, + }, + src: []byte("null"), + wantErr: false, + }, + { + name: "third", + want: &pgtype.UUID{ + Bytes: [16]byte{}, + Status: pgtype.Undefined, + }, + src: []byte("1d485a7a-6d18-4599-8c6c-34425616887a"), + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := &pgtype.UUID{} + if err := got.UnmarshalJSON(tt.src); (err != nil) != tt.wantErr { + t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("UnmarshalJSON() got = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/varbit_test.go b/varbit_test.go new file mode 100644 index 00000000..3c5aea1e --- /dev/null +++ b/varbit_test.go @@ -0,0 +1,26 @@ +package pgtype_test + +import ( + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestVarbitTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "varbit", []interface{}{ + &pgtype.Varbit{Bytes: []byte{}, Len: 0, Status: pgtype.Present}, + &pgtype.Varbit{Bytes: []byte{0, 1, 128, 254, 255}, Len: 40, Status: pgtype.Present}, + &pgtype.Varbit{Bytes: []byte{0, 1, 128, 254, 128}, Len: 33, Status: pgtype.Present}, + &pgtype.Varbit{Status: pgtype.Null}, + }) +} + +func TestVarbitNormalize(t *testing.T) { + testutil.TestSuccessfulNormalize(t, []testutil.NormalizeTest{ + { + SQL: "select B'111111111'", + Value: &pgtype.Varbit{Bytes: []byte{255, 128}, Len: 9, Status: pgtype.Present}, + }, + }) +} diff --git a/varchar_array_test.go b/varchar_array_test.go new file mode 100644 index 00000000..5fb7326d --- /dev/null +++ b/varchar_array_test.go @@ -0,0 +1,282 @@ +package pgtype_test + +import ( + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestVarcharArrayTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "varchar[]", []interface{}{ + &pgtype.VarcharArray{ + Elements: nil, + Dimensions: nil, + Status: pgtype.Present, + }, + &pgtype.VarcharArray{ + Elements: []pgtype.Varchar{ + {String: "foo", Status: pgtype.Present}, + {Status: pgtype.Null}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.VarcharArray{Status: pgtype.Null}, + &pgtype.VarcharArray{ + Elements: []pgtype.Varchar{ + {String: "bar ", Status: pgtype.Present}, + {String: "NuLL", Status: pgtype.Present}, + {String: `wow"quz\`, Status: pgtype.Present}, + {String: "", Status: pgtype.Present}, + {Status: pgtype.Null}, + {String: "null", Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}, {Length: 2, LowerBound: 1}}, + Status: pgtype.Present, + }, + &pgtype.VarcharArray{ + Elements: []pgtype.Varchar{ + {String: "bar", Status: pgtype.Present}, + {String: "baz", Status: pgtype.Present}, + {String: "quz", Status: pgtype.Present}, + {String: "foo", Status: pgtype.Present}, + }, + Dimensions: []pgtype.ArrayDimension{ + {Length: 2, LowerBound: 4}, + {Length: 2, LowerBound: 2}, + }, + Status: pgtype.Present, + }, + }) +} + +func TestVarcharArraySet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.VarcharArray + }{ + { + source: []string{"foo"}, + result: pgtype.VarcharArray{ + Elements: []pgtype.Varchar{{String: "foo", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: (([]string)(nil)), + result: pgtype.VarcharArray{Status: pgtype.Null}, + }, + { + source: [][]string{{"foo"}, {"bar"}}, + result: pgtype.VarcharArray{ + Elements: []pgtype.Varchar{{String: "foo", Status: pgtype.Present}, {String: "bar", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [][][][]string{{{{"foo", "bar", "baz"}}}, {{{"wibble", "wobble", "wubble"}}}}, + result: pgtype.VarcharArray{ + Elements: []pgtype.Varchar{ + {String: "foo", Status: pgtype.Present}, + {String: "bar", Status: pgtype.Present}, + {String: "baz", Status: pgtype.Present}, + {String: "wibble", Status: pgtype.Present}, + {String: "wobble", Status: pgtype.Present}, + {String: "wubble", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + { + source: [2][1]string{{"foo"}, {"bar"}}, + result: pgtype.VarcharArray{ + Elements: []pgtype.Varchar{{String: "foo", Status: pgtype.Present}, {String: "bar", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + }, + { + source: [2][1][1][3]string{{{{"foo", "bar", "baz"}}}, {{{"wibble", "wobble", "wubble"}}}}, + result: pgtype.VarcharArray{ + Elements: []pgtype.Varchar{ + {String: "foo", Status: pgtype.Present}, + {String: "bar", Status: pgtype.Present}, + {String: "baz", Status: pgtype.Present}, + {String: "wibble", Status: pgtype.Present}, + {String: "wobble", Status: pgtype.Present}, + {String: "wubble", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + }, + } + + for i, tt := range successfulTests { + var r pgtype.VarcharArray + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if !reflect.DeepEqual(r, tt.result) { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestVarcharArrayAssignTo(t *testing.T) { + var stringSlice []string + type _stringSlice []string + var namedStringSlice _stringSlice + var stringSliceDim2 [][]string + var stringSliceDim4 [][][][]string + var stringArrayDim2 [2][1]string + var stringArrayDim4 [2][1][1][3]string + + simpleTests := []struct { + src pgtype.VarcharArray + dst interface{} + expected interface{} + }{ + { + src: pgtype.VarcharArray{ + Elements: []pgtype.Varchar{{String: "foo", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &stringSlice, + expected: []string{"foo"}, + }, + { + src: pgtype.VarcharArray{ + Elements: []pgtype.Varchar{{String: "bar", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &namedStringSlice, + expected: _stringSlice{"bar"}, + }, + { + src: pgtype.VarcharArray{Status: pgtype.Null}, + dst: &stringSlice, + expected: (([]string)(nil)), + }, + { + src: pgtype.VarcharArray{Status: pgtype.Present}, + dst: &stringSlice, + expected: []string{}, + }, + { + src: pgtype.VarcharArray{ + Elements: []pgtype.Varchar{{String: "foo", Status: pgtype.Present}, {String: "bar", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &stringSliceDim2, + expected: [][]string{{"foo"}, {"bar"}}, + }, + { + src: pgtype.VarcharArray{ + Elements: []pgtype.Varchar{ + {String: "foo", Status: pgtype.Present}, + {String: "bar", Status: pgtype.Present}, + {String: "baz", Status: pgtype.Present}, + {String: "wibble", Status: pgtype.Present}, + {String: "wobble", Status: pgtype.Present}, + {String: "wubble", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + dst: &stringSliceDim4, + expected: [][][][]string{{{{"foo", "bar", "baz"}}}, {{{"wibble", "wobble", "wubble"}}}}, + }, + { + src: pgtype.VarcharArray{ + Elements: []pgtype.Varchar{{String: "foo", Status: pgtype.Present}, {String: "bar", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &stringArrayDim2, + expected: [2][1]string{{"foo"}, {"bar"}}, + }, + { + src: pgtype.VarcharArray{ + Elements: []pgtype.Varchar{ + {String: "foo", Status: pgtype.Present}, + {String: "bar", Status: pgtype.Present}, + {String: "baz", Status: pgtype.Present}, + {String: "wibble", Status: pgtype.Present}, + {String: "wobble", Status: pgtype.Present}, + {String: "wubble", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{ + {LowerBound: 1, Length: 2}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 1}, + {LowerBound: 1, Length: 3}}, + Status: pgtype.Present}, + dst: &stringArrayDim4, + expected: [2][1][1][3]string{{{{"foo", "bar", "baz"}}}, {{{"wibble", "wobble", "wubble"}}}}, + }, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.VarcharArray + dst interface{} + }{ + { + src: pgtype.VarcharArray{ + Elements: []pgtype.Varchar{{Status: pgtype.Null}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}}, + Status: pgtype.Present, + }, + dst: &stringSlice, + }, + { + src: pgtype.VarcharArray{ + Elements: []pgtype.Varchar{{String: "foo", Status: pgtype.Present}, {String: "bar", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &stringArrayDim2, + }, + { + src: pgtype.VarcharArray{ + Elements: []pgtype.Varchar{{String: "foo", Status: pgtype.Present}, {String: "bar", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}}, + Status: pgtype.Present}, + dst: &stringSlice, + }, + { + src: pgtype.VarcharArray{ + Elements: []pgtype.Varchar{{String: "foo", Status: pgtype.Present}, {String: "bar", Status: pgtype.Present}}, + Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}}, + Status: pgtype.Present}, + dst: &stringArrayDim4, + }, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } +} diff --git a/xid_test.go b/xid_test.go new file mode 100644 index 00000000..563ce96e --- /dev/null +++ b/xid_test.go @@ -0,0 +1,105 @@ +package pgtype_test + +import ( + "reflect" + "testing" + + "github.com/jackc/pgtype" + "github.com/jackc/pgtype/testutil" +) + +func TestXIDTranscode(t *testing.T) { + pgTypeName := "xid" + values := []interface{}{ + &pgtype.XID{Uint: 42, Status: pgtype.Present}, + &pgtype.XID{Status: pgtype.Null}, + } + eqFunc := func(a, b interface{}) bool { + return reflect.DeepEqual(a, b) + } + + testutil.TestPgxSuccessfulTranscodeEqFunc(t, pgTypeName, values, eqFunc) + + for _, driverName := range []string{"github.com/lib/pq", "github.com/jackc/pgx/stdlib"} { + testutil.TestDatabaseSQLSuccessfulTranscodeEqFunc(t, driverName, pgTypeName, values, eqFunc) + } +} + +func TestXIDSet(t *testing.T) { + successfulTests := []struct { + source interface{} + result pgtype.XID + }{ + {source: uint32(1), result: pgtype.XID{Uint: 1, Status: pgtype.Present}}, + } + + for i, tt := range successfulTests { + var r pgtype.XID + err := r.Set(tt.source) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if r != tt.result { + t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r) + } + } +} + +func TestXIDAssignTo(t *testing.T) { + var ui32 uint32 + var pui32 *uint32 + + simpleTests := []struct { + src pgtype.XID + dst interface{} + expected interface{} + }{ + {src: pgtype.XID{Uint: 42, Status: pgtype.Present}, dst: &ui32, expected: uint32(42)}, + {src: pgtype.XID{Status: pgtype.Null}, dst: &pui32, expected: ((*uint32)(nil))}, + } + + for i, tt := range simpleTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + pointerAllocTests := []struct { + src pgtype.XID + dst interface{} + expected interface{} + }{ + {src: pgtype.XID{Uint: 42, Status: pgtype.Present}, dst: &pui32, expected: uint32(42)}, + } + + for i, tt := range pointerAllocTests { + err := tt.src.AssignTo(tt.dst) + if err != nil { + t.Errorf("%d: %v", i, err) + } + + if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected { + t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst) + } + } + + errorTests := []struct { + src pgtype.XID + dst interface{} + }{ + {src: pgtype.XID{Status: pgtype.Null}, dst: &ui32}, + } + + for i, tt := range errorTests { + err := tt.src.AssignTo(tt.dst) + if err == nil { + t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst) + } + } +} diff --git a/zeronull/int2_test.go b/zeronull/int2_test.go new file mode 100644 index 00000000..2dcb4e79 --- /dev/null +++ b/zeronull/int2_test.go @@ -0,0 +1,23 @@ +package zeronull_test + +import ( + "testing" + + "github.com/jackc/pgtype/testutil" + "github.com/jackc/pgtype/zeronull" +) + +func TestInt2Transcode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "int2", []interface{}{ + (zeronull.Int2)(1), + (zeronull.Int2)(0), + }) +} + +func TestInt2ConvertsGoZeroToNull(t *testing.T) { + testutil.TestGoZeroToNullConversion(t, "int2", (zeronull.Int2)(0)) +} + +func TestInt2ConvertsNullToGoZero(t *testing.T) { + testutil.TestNullToGoZeroConversion(t, "int2", (zeronull.Int2)(0)) +} diff --git a/zeronull/int4_test.go b/zeronull/int4_test.go new file mode 100644 index 00000000..309e4125 --- /dev/null +++ b/zeronull/int4_test.go @@ -0,0 +1,23 @@ +package zeronull_test + +import ( + "testing" + + "github.com/jackc/pgtype/testutil" + "github.com/jackc/pgtype/zeronull" +) + +func TestInt4Transcode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "int4", []interface{}{ + (zeronull.Int4)(1), + (zeronull.Int4)(0), + }) +} + +func TestInt4ConvertsGoZeroToNull(t *testing.T) { + testutil.TestGoZeroToNullConversion(t, "int4", (zeronull.Int4)(0)) +} + +func TestInt4ConvertsNullToGoZero(t *testing.T) { + testutil.TestNullToGoZeroConversion(t, "int4", (zeronull.Int4)(0)) +} diff --git a/zeronull/int8_test.go b/zeronull/int8_test.go new file mode 100644 index 00000000..ae80bc0a --- /dev/null +++ b/zeronull/int8_test.go @@ -0,0 +1,23 @@ +package zeronull_test + +import ( + "testing" + + "github.com/jackc/pgtype/testutil" + "github.com/jackc/pgtype/zeronull" +) + +func TestInt8Transcode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "int8", []interface{}{ + (zeronull.Int8)(1), + (zeronull.Int8)(0), + }) +} + +func TestInt8ConvertsGoZeroToNull(t *testing.T) { + testutil.TestGoZeroToNullConversion(t, "int8", (zeronull.Int8)(0)) +} + +func TestInt8ConvertsNullToGoZero(t *testing.T) { + testutil.TestNullToGoZeroConversion(t, "int8", (zeronull.Int8)(0)) +} diff --git a/zeronull/text_test.go b/zeronull/text_test.go new file mode 100644 index 00000000..f08a0d2a --- /dev/null +++ b/zeronull/text_test.go @@ -0,0 +1,23 @@ +package zeronull_test + +import ( + "testing" + + "github.com/jackc/pgtype/testutil" + "github.com/jackc/pgtype/zeronull" +) + +func TestTextTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "text", []interface{}{ + (zeronull.Text)("foo"), + (zeronull.Text)(""), + }) +} + +func TestTextConvertsGoZeroToNull(t *testing.T) { + testutil.TestGoZeroToNullConversion(t, "text", (zeronull.Text)("")) +} + +func TestTextConvertsNullToGoZero(t *testing.T) { + testutil.TestNullToGoZeroConversion(t, "text", (zeronull.Text)("")) +} diff --git a/zeronull/timestamp_test.go b/zeronull/timestamp_test.go new file mode 100644 index 00000000..ec96ff07 --- /dev/null +++ b/zeronull/timestamp_test.go @@ -0,0 +1,29 @@ +package zeronull_test + +import ( + "testing" + "time" + + "github.com/jackc/pgtype/testutil" + "github.com/jackc/pgtype/zeronull" +) + +func TestTimestampTranscode(t *testing.T) { + testutil.TestSuccessfulTranscodeEqFunc(t, "timestamp", []interface{}{ + (zeronull.Timestamp)(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)), + (zeronull.Timestamp)(time.Time{}), + }, func(a, b interface{}) bool { + at := a.(zeronull.Timestamp) + bt := b.(zeronull.Timestamp) + + return time.Time(at).Equal(time.Time(bt)) + }) +} + +func TestTimestampConvertsGoZeroToNull(t *testing.T) { + testutil.TestGoZeroToNullConversion(t, "timestamp", (zeronull.Timestamp)(time.Time{})) +} + +func TestTimestampConvertsNullToGoZero(t *testing.T) { + testutil.TestNullToGoZeroConversion(t, "timestamp", (zeronull.Timestamp)(time.Time{})) +} diff --git a/zeronull/timestamptz_test.go b/zeronull/timestamptz_test.go new file mode 100644 index 00000000..3a401c49 --- /dev/null +++ b/zeronull/timestamptz_test.go @@ -0,0 +1,29 @@ +package zeronull_test + +import ( + "testing" + "time" + + "github.com/jackc/pgtype/testutil" + "github.com/jackc/pgtype/zeronull" +) + +func TestTimestamptzTranscode(t *testing.T) { + testutil.TestSuccessfulTranscodeEqFunc(t, "timestamptz", []interface{}{ + (zeronull.Timestamptz)(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)), + (zeronull.Timestamptz)(time.Time{}), + }, func(a, b interface{}) bool { + at := a.(zeronull.Timestamptz) + bt := b.(zeronull.Timestamptz) + + return time.Time(at).Equal(time.Time(bt)) + }) +} + +func TestTimestamptzConvertsGoZeroToNull(t *testing.T) { + testutil.TestGoZeroToNullConversion(t, "timestamptz", (zeronull.Timestamptz)(time.Time{})) +} + +func TestTimestamptzConvertsNullToGoZero(t *testing.T) { + testutil.TestNullToGoZeroConversion(t, "timestamptz", (zeronull.Timestamptz)(time.Time{})) +} diff --git a/zeronull/uuid_test.go b/zeronull/uuid_test.go new file mode 100644 index 00000000..162bdf1f --- /dev/null +++ b/zeronull/uuid_test.go @@ -0,0 +1,23 @@ +package zeronull_test + +import ( + "testing" + + "github.com/jackc/pgtype/testutil" + "github.com/jackc/pgtype/zeronull" +) + +func TestUUIDTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "uuid", []interface{}{ + (*zeronull.UUID)(&[16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}), + (*zeronull.UUID)(&[16]byte{}), + }) +} + +func TestUUIDConvertsGoZeroToNull(t *testing.T) { + testutil.TestGoZeroToNullConversion(t, "uuid", (*zeronull.UUID)(&[16]byte{})) +} + +func TestUUIDConvertsNullToGoZero(t *testing.T) { + testutil.TestNullToGoZeroConversion(t, "uuid", (*zeronull.UUID)(&[16]byte{})) +}