-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgis_internal_test.go
275 lines (251 loc) · 7.1 KB
/
postgis_internal_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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package postgis
import (
"context"
"fmt"
"os"
"reflect"
"strconv"
"strings"
"testing"
"github.com/go-spatial/geom"
"github.com/go-spatial/tegola/dict"
"github.com/go-spatial/tegola/internal/ttools"
"github.com/go-spatial/tegola/provider"
)
// TESTENV is the environment variable that must be set to "yes" to run postgis tests.
const TESTENV = "RUN_POSTGIS_TESTS"
var defaultEnvConfig map[string]interface{}
func GetTestPort() int {
port, err := strconv.ParseInt(os.Getenv("PGPORT"), 10, 32)
if err != nil {
// Since this is happening at init time, have a sane default
fmt.Fprintf(os.Stderr, "err parsing PGPORT: '%v' using default port: 5433", err)
return 5433
}
return int(port)
}
func getConfigFromEnv() map[string]interface{} {
port := GetTestPort()
return map[string]interface{}{
ConfigKeyHost: os.Getenv("PGHOST"),
ConfigKeyPort: port,
ConfigKeyDB: os.Getenv("PGDATABASE"),
ConfigKeyUser: os.Getenv("PGUSER"),
ConfigKeyPassword: os.Getenv("PGPASSWORD"),
ConfigKeySSLMode: os.Getenv("PGSSLMODE"),
ConfigKeySSLKey: os.Getenv("PGSSLKEY"),
ConfigKeySSLCert: os.Getenv("PGSSLCERT"),
ConfigKeySSLRootCert: os.Getenv("PGSSLROOTCERT"),
}
}
func init() {
defaultEnvConfig = getConfigFromEnv()
}
type TCConfig struct {
BaseConfig map[string]interface{}
ConfigOverride map[string]string
LayerConfig []map[string]interface{}
}
func (cfg TCConfig) Config() dict.Dict {
var config map[string]interface{}
mConfig := defaultEnvConfig
if cfg.BaseConfig != nil {
mConfig = cfg.BaseConfig
}
config = make(map[string]interface{}, len(mConfig))
for k, v := range mConfig {
config[k] = v
}
// set the config overrides
for k, v := range cfg.ConfigOverride {
config[k] = v
}
if len(cfg.LayerConfig) > 0 {
layerConfig, _ := config[ConfigKeyLayers].([]map[string]interface{})
layerConfig = append(layerConfig, cfg.LayerConfig...)
config[ConfigKeyLayers] = layerConfig
}
return dict.Dict(config)
}
func TestMVTProviders(t *testing.T) {
ttools.ShouldSkip(t, TESTENV)
type tcase struct {
TCConfig
layerNames []string
mvtTile []byte
err string
tile provider.Tile
}
fn := func(tc tcase) func(t *testing.T) {
return func(t *testing.T) {
config := tc.Config()
prvd, err := NewMVTTileProvider(config)
// for now we will just check the length of the bytes.
if tc.err != "" {
if err == nil || !strings.Contains(err.Error(), tc.err) {
t.Logf("error %#v", err)
t.Errorf("expected error with %v in NewMVTTileProvider, got: %v", tc.err, err)
}
return
}
if err != nil {
t.Errorf("NewMVTTileProvider unexpected error: %v", err)
return
}
layers := make([]provider.Layer, len(tc.layerNames))
for i := range tc.layerNames {
layers[i] = provider.Layer{
Name: tc.layerNames[i],
MVTName: tc.layerNames[i],
}
}
mvtTile, err := prvd.MVTForLayers(context.Background(), tc.tile, layers)
if err != nil {
t.Errorf("NewProvider unexpected error: %v", err)
return
}
if len(tc.mvtTile) != len(mvtTile) {
t.Errorf("tile byte length, exected %v got %v", len(tc.mvtTile), len(mvtTile))
}
}
}
tests := map[string]tcase{
"1": tcase{
TCConfig: TCConfig{
LayerConfig: []map[string]interface{}{
{
ConfigKeyGeomIDField: "gid",
ConfigKeyGeomType: "multipolygon",
ConfigKeyGeomField: "geom",
ConfigKeyLayerName: "land",
ConfigKeySQL: "SELECT ST_AsMVTGeom(geom,!BBOX!) as geom, gid, scalerank FROM ne_10m_land_scale_rank WHERE geom && !BBOX!",
ConfigKeySRID: 4326,
},
},
},
layerNames: []string{"land"},
mvtTile: make([]byte, 174689),
tile: provider.NewTile(0, 0, 0, 16, 4326),
},
}
for name, tc := range tests {
t.Run(name, fn(tc))
}
}
func TestLayerGeomType(t *testing.T) {
ttools.ShouldSkip(t, TESTENV)
type tcase struct {
TCConfig
layerName string
geom geom.Geometry
err string
}
fn := func(tc tcase) func(t *testing.T) {
return func(t *testing.T) {
config := tc.Config()
provider, err := NewTileProvider(config)
if tc.err != "" {
if err == nil || !strings.Contains(err.Error(), tc.err) {
t.Logf("error %#v", err)
t.Errorf("expected error with %v in NewProvider, got: %v", tc.err, err)
}
return
}
if err != nil {
t.Errorf("NewProvider unexpected error: %v", err)
return
}
p := provider.(*Provider)
layer := p.layers[tc.layerName]
if !reflect.DeepEqual(tc.geom, layer.geomType) {
t.Errorf("geom type, expected %v got %v", tc.geom, layer.geomType)
return
}
}
}
tests := map[string]tcase{
"1": {
TCConfig: TCConfig{
LayerConfig: []map[string]interface{}{
{
ConfigKeyLayerName: "land",
ConfigKeySQL: "SELECT gid, ST_AsBinary(geom) FROM ne_10m_land_scale_rank WHERE geom && !BBOX!",
},
},
},
layerName: "land",
geom: geom.MultiPolygon{},
},
"zoom token replacement": {
TCConfig: TCConfig{
LayerConfig: []map[string]interface{}{
{
ConfigKeyLayerName: "land",
ConfigKeySQL: "SELECT gid, ST_AsBinary(geom) FROM ne_10m_land_scale_rank WHERE gid = !ZOOM! AND geom && !BBOX!",
},
},
},
layerName: "land",
geom: geom.MultiPolygon{},
},
"configured geometry_type": {
TCConfig: TCConfig{
LayerConfig: []map[string]interface{}{
{
ConfigKeyLayerName: "land",
ConfigKeyGeomType: "multipolygon",
ConfigKeySQL: "SELECT gid, ST_AsBinary(geom) FROM invalid_table_to_check_query_table_was_not_inspected WHERE geom && !BBOX!",
},
},
},
layerName: "land",
geom: geom.MultiPolygon{},
},
"configured geometry_type (case insensitive)": {
TCConfig: TCConfig{
LayerConfig: []map[string]interface{}{
{
ConfigKeyLayerName: "land",
ConfigKeyGeomType: "MultiPolyGOn",
ConfigKeySQL: "SELECT gid, ST_AsBinary(geom) FROM invalid_table_to_check_query_table_was_not_inspected WHERE geom && !BBOX!",
},
},
},
layerName: "land",
geom: geom.MultiPolygon{},
},
"invalid configured geometry_type": {
TCConfig: TCConfig{
LayerConfig: []map[string]interface{}{
{
ConfigKeyLayerName: "land",
ConfigKeyGeomType: "invalid",
ConfigKeySQL: "SELECT gid, ST_AsBinary(geom) FROM invalid_table_to_check_query_table_was_not_inspected WHERE geom && !BBOX!",
},
},
},
layerName: "land",
geom: geom.MultiPolygon{},
err: "unsupported geometry_type",
},
"role no access to table": {
TCConfig: TCConfig{
ConfigOverride: map[string]string{
ConfigKeyUser: os.Getenv("PGUSER_NO_ACCESS"),
},
LayerConfig: []map[string]interface{}{
{
ConfigKeyLayerName: "land",
ConfigKeySQL: "SELECT gid, ST_AsBinary(geom) FROM ne_10m_land_scale_rank WHERE geom && !BBOX!",
},
},
},
layerName: "land",
geom: geom.MultiPolygon{},
err: "error fetching geometry type for layer (land): ERROR: permission denied for table ne_10m_land_scale_rank (SQLSTATE 42501)",
},
}
for name, tc := range tests {
t.Run(name, fn(tc))
}
}