-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrange_codec_test.go
163 lines (143 loc) · 4.96 KB
/
range_codec_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package pgtype_test
import (
"context"
"testing"
pgx "github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxtest"
"github.com/stretchr/testify/require"
)
func TestRangeCodecTranscode(t *testing.T) {
skipCockroachDB(t, "Server does not support range types (see https://github.com/cockroachdb/cockroach/issues/27791)")
pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "int4range", []pgxtest.ValueRoundTripTest{
{
pgtype.Range[pgtype.Int4]{LowerType: pgtype.Empty, UpperType: pgtype.Empty, Valid: true},
new(pgtype.Range[pgtype.Int4]),
isExpectedEq(pgtype.Range[pgtype.Int4]{LowerType: pgtype.Empty, UpperType: pgtype.Empty, Valid: true}),
},
{
pgtype.Range[pgtype.Int4]{
LowerType: pgtype.Inclusive,
Lower: pgtype.Int4{Int32: 1, Valid: true},
Upper: pgtype.Int4{Int32: 5, Valid: true},
UpperType: pgtype.Exclusive, Valid: true,
},
new(pgtype.Range[pgtype.Int4]),
isExpectedEq(pgtype.Range[pgtype.Int4]{
LowerType: pgtype.Inclusive,
Lower: pgtype.Int4{Int32: 1, Valid: true},
Upper: pgtype.Int4{Int32: 5, Valid: true},
UpperType: pgtype.Exclusive, Valid: true,
}),
},
{pgtype.Range[pgtype.Int4]{}, new(pgtype.Range[pgtype.Int4]), isExpectedEq(pgtype.Range[pgtype.Int4]{})},
{nil, new(pgtype.Range[pgtype.Int4]), isExpectedEq(pgtype.Range[pgtype.Int4]{})},
})
}
func TestRangeCodecTranscodeCompatibleRangeElementTypes(t *testing.T) {
ctr := defaultConnTestRunner
ctr.AfterConnect = func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
pgxtest.SkipCockroachDB(t, conn, "Server does not support range types (see https://github.com/cockroachdb/cockroach/issues/27791)")
}
pgxtest.RunValueRoundTripTests(context.Background(), t, ctr, nil, "numrange", []pgxtest.ValueRoundTripTest{
{
pgtype.Range[pgtype.Float8]{LowerType: pgtype.Empty, UpperType: pgtype.Empty, Valid: true},
new(pgtype.Range[pgtype.Float8]),
isExpectedEq(pgtype.Range[pgtype.Float8]{LowerType: pgtype.Empty, UpperType: pgtype.Empty, Valid: true}),
},
{
pgtype.Range[pgtype.Float8]{
LowerType: pgtype.Inclusive,
Lower: pgtype.Float8{Float64: 1, Valid: true},
Upper: pgtype.Float8{Float64: 5, Valid: true},
UpperType: pgtype.Exclusive, Valid: true,
},
new(pgtype.Range[pgtype.Float8]),
isExpectedEq(pgtype.Range[pgtype.Float8]{
LowerType: pgtype.Inclusive,
Lower: pgtype.Float8{Float64: 1, Valid: true},
Upper: pgtype.Float8{Float64: 5, Valid: true},
UpperType: pgtype.Exclusive, Valid: true,
}),
},
{pgtype.Range[pgtype.Float8]{}, new(pgtype.Range[pgtype.Float8]), isExpectedEq(pgtype.Range[pgtype.Float8]{})},
{nil, new(pgtype.Range[pgtype.Float8]), isExpectedEq(pgtype.Range[pgtype.Float8]{})},
})
}
func TestRangeCodecScanRangeTwiceWithUnbounded(t *testing.T) {
skipCockroachDB(t, "Server does not support range types (see https://github.com/cockroachdb/cockroach/issues/27791)")
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
var r pgtype.Range[pgtype.Int4]
err := conn.QueryRow(context.Background(), `select '[1,5)'::int4range`).Scan(&r)
require.NoError(t, err)
require.Equal(
t,
pgtype.Range[pgtype.Int4]{
Lower: pgtype.Int4{Int32: 1, Valid: true},
Upper: pgtype.Int4{Int32: 5, Valid: true},
LowerType: pgtype.Inclusive,
UpperType: pgtype.Exclusive,
Valid: true,
},
r,
)
err = conn.QueryRow(ctx, `select '[1,)'::int4range`).Scan(&r)
require.NoError(t, err)
require.Equal(
t,
pgtype.Range[pgtype.Int4]{
Lower: pgtype.Int4{Int32: 1, Valid: true},
Upper: pgtype.Int4{},
LowerType: pgtype.Inclusive,
UpperType: pgtype.Unbounded,
Valid: true,
},
r,
)
err = conn.QueryRow(ctx, `select 'empty'::int4range`).Scan(&r)
require.NoError(t, err)
require.Equal(
t,
pgtype.Range[pgtype.Int4]{
Lower: pgtype.Int4{},
Upper: pgtype.Int4{},
LowerType: pgtype.Empty,
UpperType: pgtype.Empty,
Valid: true,
},
r,
)
})
}
func TestRangeCodecDecodeValue(t *testing.T) {
skipCockroachDB(t, "Server does not support range types (see https://github.com/cockroachdb/cockroach/issues/27791)")
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, _ testing.TB, conn *pgx.Conn) {
for _, tt := range []struct {
sql string
expected any
}{
{
sql: `select '[1,5)'::int4range`,
expected: pgtype.Range[any]{
Lower: int32(1),
Upper: int32(5),
LowerType: pgtype.Inclusive,
UpperType: pgtype.Exclusive,
Valid: true,
},
},
} {
t.Run(tt.sql, func(t *testing.T) {
rows, err := conn.Query(ctx, tt.sql)
require.NoError(t, err)
for rows.Next() {
values, err := rows.Values()
require.NoError(t, err)
require.Len(t, values, 1)
require.Equal(t, tt.expected, values[0])
}
require.NoError(t, rows.Err())
})
}
})
}