1package pgtype_test
2
3import (
4	"reflect"
5	"testing"
6
7	"github.com/jackc/pgtype"
8	"github.com/jackc/pgtype/testutil"
9)
10
11func TestUUIDArrayTranscode(t *testing.T) {
12	testutil.TestSuccessfulTranscode(t, "uuid[]", []interface{}{
13		&pgtype.UUIDArray{
14			Elements:   nil,
15			Dimensions: nil,
16			Status:     pgtype.Present,
17		},
18		&pgtype.UUIDArray{
19			Elements: []pgtype.UUID{
20				{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
21				{Status: pgtype.Null},
22			},
23			Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}},
24			Status:     pgtype.Present,
25		},
26		&pgtype.UUIDArray{Status: pgtype.Null},
27		&pgtype.UUIDArray{
28			Elements: []pgtype.UUID{
29				{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
30				{Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Status: pgtype.Present},
31				{Bytes: [16]byte{32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}, Status: pgtype.Present},
32				{Bytes: [16]byte{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63}, Status: pgtype.Present},
33				{Status: pgtype.Null},
34				{Bytes: [16]byte{64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79}, Status: pgtype.Present},
35			},
36			Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}, {Length: 2, LowerBound: 1}},
37			Status:     pgtype.Present,
38		},
39		&pgtype.UUIDArray{
40			Elements: []pgtype.UUID{
41				{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
42				{Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Status: pgtype.Present},
43				{Bytes: [16]byte{32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}, Status: pgtype.Present},
44				{Bytes: [16]byte{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63}, Status: pgtype.Present},
45			},
46			Dimensions: []pgtype.ArrayDimension{
47				{Length: 2, LowerBound: 4},
48				{Length: 2, LowerBound: 2},
49			},
50			Status: pgtype.Present,
51		},
52	})
53}
54
55func TestUUIDArraySet(t *testing.T) {
56	successfulTests := []struct {
57		source interface{}
58		result pgtype.UUIDArray
59	}{
60		{
61			source: nil,
62			result: pgtype.UUIDArray{Status: pgtype.Null},
63		},
64		{
65			source: [][16]byte{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
66			result: pgtype.UUIDArray{
67				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}},
68				Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}},
69				Status:     pgtype.Present},
70		},
71		{
72			source: [][16]byte{},
73			result: pgtype.UUIDArray{Status: pgtype.Present},
74		},
75		{
76			source: ([][16]byte)(nil),
77			result: pgtype.UUIDArray{Status: pgtype.Null},
78		},
79		{
80			source: [][]byte{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
81			result: pgtype.UUIDArray{
82				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}},
83				Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}},
84				Status:     pgtype.Present},
85		},
86		{
87			source: [][]byte{
88				{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
89				{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31},
90				nil,
91				{32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47},
92			},
93			result: pgtype.UUIDArray{
94				Elements: []pgtype.UUID{
95					{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
96					{Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Status: pgtype.Present},
97					{Status: pgtype.Null},
98					{Bytes: [16]byte{32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}, Status: pgtype.Present},
99				},
100				Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 4}},
101				Status:     pgtype.Present},
102		},
103		{
104			source: [][]byte{},
105			result: pgtype.UUIDArray{Status: pgtype.Present},
106		},
107		{
108			source: ([][]byte)(nil),
109			result: pgtype.UUIDArray{Status: pgtype.Null},
110		},
111		{
112			source: []string{"00010203-0405-0607-0809-0a0b0c0d0e0f"},
113			result: pgtype.UUIDArray{
114				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}},
115				Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}},
116				Status:     pgtype.Present},
117		},
118		{
119			source: []string{},
120			result: pgtype.UUIDArray{Status: pgtype.Present},
121		},
122		{
123			source: ([]string)(nil),
124			result: pgtype.UUIDArray{Status: pgtype.Null},
125		},
126		{
127			source: [][][16]byte{{
128				{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
129				{{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}}},
130			result: pgtype.UUIDArray{
131				Elements: []pgtype.UUID{
132					{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
133					{Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Status: pgtype.Present}},
134				Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}},
135				Status:     pgtype.Present},
136		},
137		{
138			source: [][][][]string{
139				{{{
140					"00010203-0405-0607-0809-0a0b0c0d0e0f",
141					"10111213-1415-1617-1819-1a1b1c1d1e1f",
142					"20212223-2425-2627-2829-2a2b2c2d2e2f"}}},
143				{{{
144					"30313233-3435-3637-3839-3a3b3c3d3e3f",
145					"40414243-4445-4647-4849-4a4b4c4d4e4f",
146					"50515253-5455-5657-5859-5a5b5c5d5e5f"}}}},
147			result: pgtype.UUIDArray{
148				Elements: []pgtype.UUID{
149					{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
150					{Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Status: pgtype.Present},
151					{Bytes: [16]byte{32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}, Status: pgtype.Present},
152					{Bytes: [16]byte{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63}, Status: pgtype.Present},
153					{Bytes: [16]byte{64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79}, Status: pgtype.Present},
154					{Bytes: [16]byte{80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95}, Status: pgtype.Present}},
155				Dimensions: []pgtype.ArrayDimension{
156					{LowerBound: 1, Length: 2},
157					{LowerBound: 1, Length: 1},
158					{LowerBound: 1, Length: 1},
159					{LowerBound: 1, Length: 3}},
160				Status: pgtype.Present},
161		},
162		{
163			source: [2][1][16]byte{{
164				{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
165				{{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}}},
166			result: pgtype.UUIDArray{
167				Elements: []pgtype.UUID{
168					{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
169					{Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Status: pgtype.Present}},
170				Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}},
171				Status:     pgtype.Present},
172		},
173		{
174			source: [2][1][1][3]string{
175				{{{
176					"00010203-0405-0607-0809-0a0b0c0d0e0f",
177					"10111213-1415-1617-1819-1a1b1c1d1e1f",
178					"20212223-2425-2627-2829-2a2b2c2d2e2f"}}},
179				{{{
180					"30313233-3435-3637-3839-3a3b3c3d3e3f",
181					"40414243-4445-4647-4849-4a4b4c4d4e4f",
182					"50515253-5455-5657-5859-5a5b5c5d5e5f"}}}},
183			result: pgtype.UUIDArray{
184				Elements: []pgtype.UUID{
185					{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
186					{Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Status: pgtype.Present},
187					{Bytes: [16]byte{32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}, Status: pgtype.Present},
188					{Bytes: [16]byte{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63}, Status: pgtype.Present},
189					{Bytes: [16]byte{64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79}, Status: pgtype.Present},
190					{Bytes: [16]byte{80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95}, Status: pgtype.Present}},
191				Dimensions: []pgtype.ArrayDimension{
192					{LowerBound: 1, Length: 2},
193					{LowerBound: 1, Length: 1},
194					{LowerBound: 1, Length: 1},
195					{LowerBound: 1, Length: 3}},
196				Status: pgtype.Present},
197		},
198	}
199
200	for i, tt := range successfulTests {
201		var r pgtype.UUIDArray
202		err := r.Set(tt.source)
203		if err != nil {
204			t.Errorf("%d: %v", i, err)
205		}
206
207		if !reflect.DeepEqual(r, tt.result) {
208			t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
209		}
210	}
211}
212
213func TestUUIDArrayAssignTo(t *testing.T) {
214	var byteArraySlice [][16]byte
215	var byteSliceSlice [][]byte
216	var stringSlice []string
217	var byteSlice []byte
218	var byteArraySliceDim2 [][][16]byte
219	var stringSliceDim4 [][][][]string
220	var byteArrayDim2 [2][1][16]byte
221	var stringArrayDim4 [2][1][1][3]string
222
223	simpleTests := []struct {
224		src      pgtype.UUIDArray
225		dst      interface{}
226		expected interface{}
227	}{
228		{
229			src: pgtype.UUIDArray{
230				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}},
231				Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}},
232				Status:     pgtype.Present,
233			},
234			dst:      &byteArraySlice,
235			expected: [][16]byte{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
236		},
237		{
238			src:      pgtype.UUIDArray{Status: pgtype.Null},
239			dst:      &byteArraySlice,
240			expected: ([][16]byte)(nil),
241		},
242		{
243			src: pgtype.UUIDArray{
244				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}},
245				Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}},
246				Status:     pgtype.Present,
247			},
248			dst:      &byteSliceSlice,
249			expected: [][]byte{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
250		},
251		{
252			src:      pgtype.UUIDArray{Status: pgtype.Null},
253			dst:      &byteSliceSlice,
254			expected: ([][]byte)(nil),
255		},
256		{
257			src:      pgtype.UUIDArray{Status: pgtype.Present},
258			dst:      &byteSlice,
259			expected: []byte{},
260		},
261		{
262			src:      pgtype.UUIDArray{Status: pgtype.Present},
263			dst:      &stringSlice,
264			expected: []string{},
265		},
266		{
267			src: pgtype.UUIDArray{
268				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}},
269				Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}},
270				Status:     pgtype.Present,
271			},
272			dst:      &stringSlice,
273			expected: []string{"00010203-0405-0607-0809-0a0b0c0d0e0f"},
274		},
275		{
276			src:      pgtype.UUIDArray{Status: pgtype.Null},
277			dst:      &stringSlice,
278			expected: ([]string)(nil),
279		},
280		{
281			src: pgtype.UUIDArray{
282				Elements: []pgtype.UUID{
283					{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
284					{Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Status: pgtype.Present}},
285				Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}},
286				Status:     pgtype.Present},
287			dst: &byteArraySliceDim2,
288			expected: [][][16]byte{{
289				{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
290				{{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}}},
291		},
292		{
293			src: pgtype.UUIDArray{
294				Elements: []pgtype.UUID{
295					{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
296					{Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Status: pgtype.Present},
297					{Bytes: [16]byte{32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}, Status: pgtype.Present},
298					{Bytes: [16]byte{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63}, Status: pgtype.Present},
299					{Bytes: [16]byte{64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79}, Status: pgtype.Present},
300					{Bytes: [16]byte{80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95}, Status: pgtype.Present}},
301				Dimensions: []pgtype.ArrayDimension{
302					{LowerBound: 1, Length: 2},
303					{LowerBound: 1, Length: 1},
304					{LowerBound: 1, Length: 1},
305					{LowerBound: 1, Length: 3}},
306				Status: pgtype.Present},
307			dst: &stringSliceDim4,
308			expected: [][][][]string{
309				{{{
310					"00010203-0405-0607-0809-0a0b0c0d0e0f",
311					"10111213-1415-1617-1819-1a1b1c1d1e1f",
312					"20212223-2425-2627-2829-2a2b2c2d2e2f"}}},
313				{{{
314					"30313233-3435-3637-3839-3a3b3c3d3e3f",
315					"40414243-4445-4647-4849-4a4b4c4d4e4f",
316					"50515253-5455-5657-5859-5a5b5c5d5e5f"}}}},
317		},
318		{
319			src: pgtype.UUIDArray{
320				Elements: []pgtype.UUID{
321					{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
322					{Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Status: pgtype.Present}},
323				Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}},
324				Status:     pgtype.Present},
325			dst: &byteArrayDim2,
326			expected: [2][1][16]byte{{
327				{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
328				{{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}}},
329		},
330		{
331			src: pgtype.UUIDArray{
332				Elements: []pgtype.UUID{
333					{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
334					{Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Status: pgtype.Present},
335					{Bytes: [16]byte{32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}, Status: pgtype.Present},
336					{Bytes: [16]byte{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63}, Status: pgtype.Present},
337					{Bytes: [16]byte{64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79}, Status: pgtype.Present},
338					{Bytes: [16]byte{80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95}, Status: pgtype.Present}},
339				Dimensions: []pgtype.ArrayDimension{
340					{LowerBound: 1, Length: 2},
341					{LowerBound: 1, Length: 1},
342					{LowerBound: 1, Length: 1},
343					{LowerBound: 1, Length: 3}},
344				Status: pgtype.Present},
345			dst: &stringArrayDim4,
346			expected: [2][1][1][3]string{
347				{{{
348					"00010203-0405-0607-0809-0a0b0c0d0e0f",
349					"10111213-1415-1617-1819-1a1b1c1d1e1f",
350					"20212223-2425-2627-2829-2a2b2c2d2e2f"}}},
351				{{{
352					"30313233-3435-3637-3839-3a3b3c3d3e3f",
353					"40414243-4445-4647-4849-4a4b4c4d4e4f",
354					"50515253-5455-5657-5859-5a5b5c5d5e5f"}}}},
355		},
356	}
357
358	for i, tt := range simpleTests {
359		err := tt.src.AssignTo(tt.dst)
360		if err != nil {
361			t.Errorf("%d: %v", i, err)
362		}
363
364		if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) {
365			t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst)
366		}
367	}
368}
369