-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeom_test.go
152 lines (140 loc) · 4.71 KB
/
geom_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
package pgxgeos_test
import (
"context"
"strconv"
"testing"
"github.com/alecthomas/assert/v2"
"github.com/jackc/pgx/v5"
"github.com/twpayne/go-geos"
)
var geomTypes = []string{"geography", "geometry"}
func TestGeometryCodecNull(t *testing.T) {
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, tb testing.TB, conn *pgx.Conn) {
tb.Helper()
for _, geomType := range geomTypes {
t.Run(geomType, func(t *testing.T) {
for _, format := range []int16{
pgx.BinaryFormatCode,
pgx.TextFormatCode,
} {
tb.(*testing.T).Run(strconv.Itoa(int(format)), func(t *testing.T) {
var actual *geos.Geom
assert.NoError(t, conn.QueryRow(ctx, "select NULL::"+geomType, pgx.QueryResultFormats{format}).Scan(&actual))
assert.Zero(t, actual)
})
}
})
}
})
}
func TestGeometryCodecPointer(t *testing.T) {
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, tb testing.TB, conn *pgx.Conn) {
tb.Helper()
for _, geomType := range geomTypes {
t.Run(geomType, func(t *testing.T) {
for _, format := range []int16{
pgx.BinaryFormatCode,
pgx.TextFormatCode,
} {
tb.(*testing.T).Run(strconv.Itoa(int(format)), func(t *testing.T) {
geom := mustNewGeomFromWKT(t, "POINT(1 2)").SetSRID(4326)
var actual *geos.Geom
assert.NoError(t, conn.QueryRow(ctx, "select $1::"+geomType, pgx.QueryResultFormats{format}, geom).Scan(&actual))
assert.Equal(t, geom.ToEWKBWithSRID(), actual.ToEWKBWithSRID())
})
}
})
}
})
}
func TestGeometryCodecEncode(t *testing.T) {
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, tb testing.TB, conn *pgx.Conn) {
tb.Helper()
for _, geomType := range geomTypes {
t.Run(geomType, func(t *testing.T) {
for _, format := range []int16{
pgx.BinaryFormatCode,
pgx.TextFormatCode,
} {
tb.(*testing.T).Run(strconv.Itoa(int(format)), func(t *testing.T) {
geom := mustNewGeomFromWKT(t, "POINT(1 2)").SetSRID(4326)
var actual *geos.Geom
assert.NoError(t, conn.QueryRow(ctx, "select $1::"+geomType, pgx.QueryResultFormats{format}, geom).Scan(&actual))
assert.Equal(t, geom.ToEWKBWithSRID(), actual.ToEWKBWithSRID())
})
}
})
}
})
}
func TestGeometryCodecEncodeNull(t *testing.T) {
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, tb testing.TB, conn *pgx.Conn) {
tb.Helper()
for _, geomType := range geomTypes {
t.Run(geomType, func(t *testing.T) {
for _, format := range []int16{
pgx.BinaryFormatCode,
pgx.TextFormatCode,
} {
tb.(*testing.T).Run(strconv.Itoa(int(format)), func(t *testing.T) {
var nullGeom, actual *geos.Geom
assert.NoError(t, conn.QueryRow(ctx, "select $1::"+geomType, pgx.QueryResultFormats{format}, nullGeom).Scan(&actual))
assert.Zero(t, actual)
})
}
})
}
})
}
func TestGeometryCodecScan(t *testing.T) {
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, tb testing.TB, conn *pgx.Conn) {
tb.Helper()
for _, geomType := range geomTypes {
t.Run(geomType, func(t *testing.T) {
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)").SetSRID(4326)
rows, err := conn.Query(ctx, "select $1::"+geomType, pgx.QueryResultFormats{format}, original)
assert.NoError(t, err)
assert.True(t, rows.Next())
values, err := rows.Values()
assert.NoError(t, err)
assert.Equal(t, 1, len(values))
assert.Equal(t, original.ToEWKBWithSRID(), values[0].(*geos.Geom).ToEWKBWithSRID())
assert.False(t, rows.Next())
assert.NoError(t, rows.Err())
})
}
})
}
})
}
func TestGeometryCodecValue(t *testing.T) {
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, tb testing.TB, conn *pgx.Conn) {
tb.Helper()
for _, geomType := range geomTypes {
t.Run(geomType, func(t *testing.T) {
for _, format := range []int16{
pgx.BinaryFormatCode,
pgx.TextFormatCode,
} {
tb.(*testing.T).Run(strconv.Itoa(int(format)), func(t *testing.T) {
var actual *geos.Geom
assert.NoError(t, conn.QueryRow(ctx, "select ST_SetSRID('POINT(3 4)'::"+geomType+", 4326)", pgx.QueryResultFormats{format}).Scan(&actual))
expected := mustNewGeomFromWKT(t, "POINT(3 4)").SetSRID(4326)
assert.Equal(t, expected.ToEWKBWithSRID(), actual.ToEWKBWithSRID())
})
}
})
}
})
}
func mustNewGeomFromWKT(tb testing.TB, wkt string) *geos.Geom {
tb.Helper()
geom, err := geos.NewGeomFromWKT(wkt)
assert.NoError(tb, err)
return geom
}