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 TestACLItemArrayTranscode(t *testing.T) {
12 testutil.TestSuccessfulTranscode(t, "aclitem[]", []interface{}{
13 &pgtype.ACLItemArray{
14 Elements: nil,
15 Dimensions: nil,
16 Status: pgtype.Present,
17 },
18 &pgtype.ACLItemArray{
19 Elements: []pgtype.ACLItem{
20 {String: "=r/postgres", Status: pgtype.Present},
21 {Status: pgtype.Null},
22 },
23 Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}},
24 Status: pgtype.Present,
25 },
26 &pgtype.ACLItemArray{Status: pgtype.Null},
27 &pgtype.ACLItemArray{
28 Elements: []pgtype.ACLItem{
29 {String: "=r/postgres", Status: pgtype.Present},
30 {String: "postgres=arwdDxt/postgres", Status: pgtype.Present},
31
32 {String: `postgres=arwdDxt/postgres`, Status: pgtype.Present},
33 {String: "=r/postgres", Status: pgtype.Present},
34 {Status: pgtype.Null},
35 {String: "=r/postgres", Status: pgtype.Present},
36 },
37 Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}, {Length: 2, LowerBound: 1}},
38 Status: pgtype.Present,
39 },
40 &pgtype.ACLItemArray{
41 Elements: []pgtype.ACLItem{
42 {String: "=r/postgres", Status: pgtype.Present},
43 {String: "postgres=arwdDxt/postgres", Status: pgtype.Present},
44 {String: "=r/postgres", Status: pgtype.Present},
45 {String: "postgres=arwdDxt/postgres", Status: pgtype.Present},
46 },
47 Dimensions: []pgtype.ArrayDimension{
48 {Length: 2, LowerBound: 4},
49 {Length: 2, LowerBound: 2},
50 },
51 Status: pgtype.Present,
52 },
53 })
54 }
55
56 func TestACLItemArraySet(t *testing.T) {
57 successfulTests := []struct {
58 source interface{}
59 result pgtype.ACLItemArray
60 }{
61 {
62 source: []string{"=r/postgres"},
63 result: pgtype.ACLItemArray{
64 Elements: []pgtype.ACLItem{{String: "=r/postgres", Status: pgtype.Present}},
65 Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}},
66 Status: pgtype.Present},
67 },
68 {
69 source: (([]string)(nil)),
70 result: pgtype.ACLItemArray{Status: pgtype.Null},
71 },
72 {
73 source: [][]string{{"=r/postgres"}, {"postgres=arwdDxt/postgres"}},
74 result: pgtype.ACLItemArray{
75 Elements: []pgtype.ACLItem{
76 {String: "=r/postgres", Status: pgtype.Present},
77 {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}},
78 Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}},
79 Status: pgtype.Present},
80 },
81 {
82 source: [][][][]string{
83 {{{
84 "=r/postgres",
85 "postgres=arwdDxt/postgres",
86 "=r/postgres"}}},
87 {{{
88 "postgres=arwdDxt/postgres",
89 "=r/postgres",
90 "postgres=arwdDxt/postgres"}}}},
91 result: pgtype.ACLItemArray{
92 Elements: []pgtype.ACLItem{
93 {String: "=r/postgres", Status: pgtype.Present},
94 {String: "postgres=arwdDxt/postgres", Status: pgtype.Present},
95 {String: "=r/postgres", Status: pgtype.Present},
96 {String: "postgres=arwdDxt/postgres", Status: pgtype.Present},
97 {String: "=r/postgres", Status: pgtype.Present},
98 {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}},
99 Dimensions: []pgtype.ArrayDimension{
100 {LowerBound: 1, Length: 2},
101 {LowerBound: 1, Length: 1},
102 {LowerBound: 1, Length: 1},
103 {LowerBound: 1, Length: 3}},
104 Status: pgtype.Present},
105 },
106 {
107 source: [2][1]string{{"=r/postgres"}, {"postgres=arwdDxt/postgres"}},
108 result: pgtype.ACLItemArray{
109 Elements: []pgtype.ACLItem{
110 {String: "=r/postgres", Status: pgtype.Present},
111 {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}},
112 Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}},
113 Status: pgtype.Present},
114 },
115 {
116 source: [2][1][1][3]string{
117 {{{
118 "=r/postgres",
119 "postgres=arwdDxt/postgres",
120 "=r/postgres"}}},
121 {{{
122 "postgres=arwdDxt/postgres",
123 "=r/postgres",
124 "postgres=arwdDxt/postgres"}}}},
125 result: pgtype.ACLItemArray{
126 Elements: []pgtype.ACLItem{
127 {String: "=r/postgres", Status: pgtype.Present},
128 {String: "postgres=arwdDxt/postgres", Status: pgtype.Present},
129 {String: "=r/postgres", Status: pgtype.Present},
130 {String: "postgres=arwdDxt/postgres", Status: pgtype.Present},
131 {String: "=r/postgres", Status: pgtype.Present},
132 {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}},
133 Dimensions: []pgtype.ArrayDimension{
134 {LowerBound: 1, Length: 2},
135 {LowerBound: 1, Length: 1},
136 {LowerBound: 1, Length: 1},
137 {LowerBound: 1, Length: 3}},
138 Status: pgtype.Present},
139 },
140 }
141
142 for i, tt := range successfulTests {
143 var r pgtype.ACLItemArray
144 err := r.Set(tt.source)
145 if err != nil {
146 t.Errorf("%d: %v", i, err)
147 }
148
149 if !reflect.DeepEqual(r, tt.result) {
150 t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
151 }
152 }
153 }
154
155 func TestACLItemArrayAssignTo(t *testing.T) {
156 var stringSlice []string
157 type _stringSlice []string
158 var namedStringSlice _stringSlice
159 var stringSliceDim2 [][]string
160 var stringSliceDim4 [][][][]string
161 var stringArrayDim2 [2][1]string
162 var stringArrayDim4 [2][1][1][3]string
163
164 simpleTests := []struct {
165 src pgtype.ACLItemArray
166 dst interface{}
167 expected interface{}
168 }{
169 {
170 src: pgtype.ACLItemArray{
171 Elements: []pgtype.ACLItem{{String: "=r/postgres", Status: pgtype.Present}},
172 Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}},
173 Status: pgtype.Present,
174 },
175 dst: &stringSlice,
176 expected: []string{"=r/postgres"},
177 },
178 {
179 src: pgtype.ACLItemArray{
180 Elements: []pgtype.ACLItem{{String: "=r/postgres", Status: pgtype.Present}},
181 Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}},
182 Status: pgtype.Present,
183 },
184 dst: &namedStringSlice,
185 expected: _stringSlice{"=r/postgres"},
186 },
187 {
188 src: pgtype.ACLItemArray{Status: pgtype.Null},
189 dst: &stringSlice,
190 expected: (([]string)(nil)),
191 },
192 {
193 src: pgtype.ACLItemArray{Status: pgtype.Present},
194 dst: &stringSlice,
195 expected: []string{},
196 },
197 {
198 src: pgtype.ACLItemArray{
199 Elements: []pgtype.ACLItem{
200 {String: "=r/postgres", Status: pgtype.Present},
201 {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}},
202 Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}},
203 Status: pgtype.Present},
204 dst: &stringSliceDim2,
205 expected: [][]string{{"=r/postgres"}, {"postgres=arwdDxt/postgres"}},
206 },
207 {
208 src: pgtype.ACLItemArray{
209 Elements: []pgtype.ACLItem{
210 {String: "=r/postgres", Status: pgtype.Present},
211 {String: "postgres=arwdDxt/postgres", Status: pgtype.Present},
212 {String: "=r/postgres", Status: pgtype.Present},
213 {String: "postgres=arwdDxt/postgres", Status: pgtype.Present},
214 {String: "=r/postgres", Status: pgtype.Present},
215 {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}},
216 Dimensions: []pgtype.ArrayDimension{
217 {LowerBound: 1, Length: 2},
218 {LowerBound: 1, Length: 1},
219 {LowerBound: 1, Length: 1},
220 {LowerBound: 1, Length: 3}},
221 Status: pgtype.Present},
222 dst: &stringSliceDim4,
223 expected: [][][][]string{
224 {{{
225 "=r/postgres",
226 "postgres=arwdDxt/postgres",
227 "=r/postgres"}}},
228 {{{
229 "postgres=arwdDxt/postgres",
230 "=r/postgres",
231 "postgres=arwdDxt/postgres"}}}},
232 },
233 {
234 src: pgtype.ACLItemArray{
235 Elements: []pgtype.ACLItem{
236 {String: "=r/postgres", Status: pgtype.Present},
237 {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}},
238 Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}},
239 Status: pgtype.Present},
240 dst: &stringArrayDim2,
241 expected: [2][1]string{{"=r/postgres"}, {"postgres=arwdDxt/postgres"}},
242 },
243 {
244 src: pgtype.ACLItemArray{
245 Elements: []pgtype.ACLItem{
246 {String: "=r/postgres", Status: pgtype.Present},
247 {String: "postgres=arwdDxt/postgres", Status: pgtype.Present},
248 {String: "=r/postgres", Status: pgtype.Present},
249 {String: "postgres=arwdDxt/postgres", Status: pgtype.Present},
250 {String: "=r/postgres", Status: pgtype.Present},
251 {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}},
252 Dimensions: []pgtype.ArrayDimension{
253 {LowerBound: 1, Length: 2},
254 {LowerBound: 1, Length: 1},
255 {LowerBound: 1, Length: 1},
256 {LowerBound: 1, Length: 3}},
257 Status: pgtype.Present},
258 dst: &stringArrayDim4,
259 expected: [2][1][1][3]string{
260 {{{
261 "=r/postgres",
262 "postgres=arwdDxt/postgres",
263 "=r/postgres"}}},
264 {{{
265 "postgres=arwdDxt/postgres",
266 "=r/postgres",
267 "postgres=arwdDxt/postgres"}}}},
268 },
269 }
270
271 for i, tt := range simpleTests {
272 err := tt.src.AssignTo(tt.dst)
273 if err != nil {
274 t.Errorf("%d: %v", i, err)
275 }
276
277 if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) {
278 t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst)
279 }
280 }
281
282 errorTests := []struct {
283 src pgtype.ACLItemArray
284 dst interface{}
285 }{
286 {
287 src: pgtype.ACLItemArray{
288 Elements: []pgtype.ACLItem{{Status: pgtype.Null}},
289 Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}},
290 Status: pgtype.Present,
291 },
292 dst: &stringSlice,
293 },
294 {
295 src: pgtype.ACLItemArray{
296 Elements: []pgtype.ACLItem{
297 {String: "=r/postgres", Status: pgtype.Present},
298 {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}},
299 Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}},
300 Status: pgtype.Present},
301 dst: &stringArrayDim2,
302 },
303 {
304 src: pgtype.ACLItemArray{
305 Elements: []pgtype.ACLItem{
306 {String: "=r/postgres", Status: pgtype.Present},
307 {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}},
308 Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}, {LowerBound: 1, Length: 2}},
309 Status: pgtype.Present},
310 dst: &stringSlice,
311 },
312 {
313 src: pgtype.ACLItemArray{
314 Elements: []pgtype.ACLItem{
315 {String: "=r/postgres", Status: pgtype.Present},
316 {String: "postgres=arwdDxt/postgres", Status: pgtype.Present}},
317 Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}},
318 Status: pgtype.Present},
319 dst: &stringArrayDim4,
320 },
321 }
322
323 for i, tt := range errorTests {
324 err := tt.src.AssignTo(tt.dst)
325 if err == nil {
326 t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst)
327 }
328 }
329 }
330
View as plain text