-
Notifications
You must be signed in to change notification settings - Fork 112
/
array_type_test.go
84 lines (62 loc) · 2.29 KB
/
array_type_test.go
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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)
}