1 package pgtype_test
2
3 import (
4 "reflect"
5 "testing"
6
7 "github.com/jackc/pgtype"
8 "github.com/jackc/pgtype/testutil"
9 )
10
11 func TestACLItemTranscode(t *testing.T) {
12 testutil.TestSuccessfulTranscode(t, "aclitem", []interface{}{
13 &pgtype.ACLItem{String: "postgres=arwdDxt/postgres", Status: pgtype.Present},
14
15 &pgtype.ACLItem{Status: pgtype.Null},
16 })
17 }
18
19 func TestACLItemSet(t *testing.T) {
20 successfulTests := []struct {
21 source interface{}
22 result pgtype.ACLItem
23 }{
24 {source: "postgres=arwdDxt/postgres", result: pgtype.ACLItem{String: "postgres=arwdDxt/postgres", Status: pgtype.Present}},
25 {source: (*string)(nil), result: pgtype.ACLItem{Status: pgtype.Null}},
26 }
27
28 for i, tt := range successfulTests {
29 var d pgtype.ACLItem
30 err := d.Set(tt.source)
31 if err != nil {
32 t.Errorf("%d: %v", i, err)
33 }
34
35 if d != tt.result {
36 t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, d)
37 }
38 }
39 }
40
41 func TestACLItemAssignTo(t *testing.T) {
42 var s string
43 var ps *string
44
45 simpleTests := []struct {
46 src pgtype.ACLItem
47 dst interface{}
48 expected interface{}
49 }{
50 {src: pgtype.ACLItem{String: "postgres=arwdDxt/postgres", Status: pgtype.Present}, dst: &s, expected: "postgres=arwdDxt/postgres"},
51 {src: pgtype.ACLItem{Status: pgtype.Null}, dst: &ps, expected: ((*string)(nil))},
52 }
53
54 for i, tt := range simpleTests {
55 err := tt.src.AssignTo(tt.dst)
56 if err != nil {
57 t.Errorf("%d: %v", i, err)
58 }
59
60 if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected {
61 t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst)
62 }
63 }
64
65 pointerAllocTests := []struct {
66 src pgtype.ACLItem
67 dst interface{}
68 expected interface{}
69 }{
70 {src: pgtype.ACLItem{String: "postgres=arwdDxt/postgres", Status: pgtype.Present}, dst: &ps, expected: "postgres=arwdDxt/postgres"},
71 }
72
73 for i, tt := range pointerAllocTests {
74 err := tt.src.AssignTo(tt.dst)
75 if err != nil {
76 t.Errorf("%d: %v", i, err)
77 }
78
79 if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected {
80 t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst)
81 }
82 }
83
84 errorTests := []struct {
85 src pgtype.ACLItem
86 dst interface{}
87 }{
88 {src: pgtype.ACLItem{Status: pgtype.Null}, dst: &s},
89 }
90
91 for i, tt := range errorTests {
92 err := tt.src.AssignTo(tt.dst)
93 if err == nil {
94 t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst)
95 }
96 }
97 }
98
View as plain text