forked from twpayne/pgx-geom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgxgeom_test.go
132 lines (112 loc) · 3.59 KB
/
pgxgeom_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
package pgxgeom_test
import (
"context"
"encoding/binary"
"strconv"
"testing"
"github.com/alecthomas/assert/v2"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxtest"
"github.com/twpayne/go-geom"
"github.com/twpayne/go-geom/encoding/ewkb"
"github.com/twpayne/go-geom/encoding/wkt"
pgxgeom "github.com/twpayne/pgx-geom"
)
var defaultConnTestRunner pgxtest.ConnTestRunner
func init() {
defaultConnTestRunner = pgxtest.DefaultConnTestRunner()
defaultConnTestRunner.AfterConnect = func(ctx context.Context, tb testing.TB, conn *pgx.Conn) {
tb.Helper()
_, err := conn.Exec(ctx, "create extension if not exists postgis")
assert.NoError(tb, err)
assert.NoError(tb, pgxgeom.Register(ctx, conn))
}
}
func TestCodecDecodeValue(t *testing.T) {
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, tb testing.TB, conn *pgx.Conn) {
tb.Helper()
for _, format := range []int16{
pgx.BinaryFormatCode,
pgx.TextFormatCode,
} {
tb.(*testing.T).Run(strconv.Itoa(int(format)), func(t *testing.T) {
original := mustNewGeomFromWKT(t, "POINT(1 2)", 4326)
rows, err := conn.Query(ctx, "select $1::geometry", pgx.QueryResultFormats{format}, original)
assert.NoError(t, err)
for rows.Next() {
values, err := rows.Values()
assert.NoError(t, err)
assert.Equal(t, 1, len(values))
v0, ok := values[0].(geom.T)
assert.True(t, ok)
assert.Equal(t, mustEWKB(t, original), mustEWKB(t, v0))
}
assert.NoError(t, rows.Err())
})
}
})
}
func TestCodecDecodeNullValue(t *testing.T) {
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, tb testing.TB, conn *pgx.Conn) {
tb.Helper()
type s struct {
Geom geom.T `db:"geom"`
}
for _, format := range []int16{
pgx.BinaryFormatCode,
pgx.TextFormatCode,
} {
tb.(*testing.T).Run(strconv.Itoa(int(format)), func(t *testing.T) {
tb.Helper()
rows, err := conn.Query(ctx, "select NULL::geometry AS geom", pgx.QueryResultFormats{format})
assert.NoError(tb, err)
value, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[s])
assert.NoError(t, err)
assert.Zero(t, value)
})
}
})
}
func TestCodecDecodeNullGeometry(t *testing.T) {
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, tb testing.TB, conn *pgx.Conn) {
tb.Helper()
rows, err := conn.Query(ctx, "select NULL::geometry", pgx.QueryResultFormats{pgx.BinaryFormatCode})
assert.NoError(tb, err)
for rows.Next() {
values, err := rows.Values()
assert.NoError(tb, err)
assert.Equal(tb, []any{nil}, values)
}
assert.NoError(tb, rows.Err())
})
}
func TestCodecScanValue(t *testing.T) {
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, tb testing.TB, conn *pgx.Conn) {
tb.Helper()
for _, format := range []int16{
pgx.BinaryFormatCode,
pgx.TextFormatCode,
} {
tb.(*testing.T).Run(strconv.Itoa(int(format)), func(t *testing.T) {
var geom geom.T
err := conn.QueryRow(ctx, "select ST_SetSRID('POINT(1 2)'::geometry, 4326)", pgx.QueryResultFormats{format}).Scan(&geom)
assert.NoError(t, err)
// assert.Equal(t, mustNewGeomFromWKT(t, "POINT(1 2)", 4326).SetSRID(4326).ToEWKBWithSRID(), geom.ToEWKBWithSRID())
})
}
})
}
func mustEWKB(tb testing.TB, g geom.T) []byte {
tb.Helper()
data, err := ewkb.Marshal(g, binary.LittleEndian)
assert.NoError(tb, err)
return data
}
func mustNewGeomFromWKT(tb testing.TB, s string, srid int) geom.T {
tb.Helper()
g, err := wkt.Unmarshal(s)
assert.NoError(tb, err)
g, err = geom.SetSRID(g, srid)
assert.NoError(tb, err)
return g
}