-
-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathpostgis_internal_test.go
393 lines (361 loc) · 10.5 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package postgis
import (
"context"
"reflect"
"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{}
var DefaultConfig map[string]interface{} = map[string]interface{}{
ConfigKeyURI: "postgres://postgres:postgres@localhost:5432/tegola?sslmode=disable",
ConfigKeySSLMode: "disable",
ConfigKeySSLKey: "",
ConfigKeySSLCert: "",
ConfigKeySSLRootCert: "",
}
func getConfigFromEnv() map[string]interface{} {
return map[string]interface{}{
ConfigKeyURI: ttools.GetEnvDefault("PGURI", "postgres://postgres:postgres@localhost:5432/tegola?sslmode=disable"),
ConfigKeySSLMode: ttools.GetEnvDefault("PGSSLMODE", "disable"),
ConfigKeySSLKey: ttools.GetEnvDefault("PGSSLKEY", ""),
ConfigKeySSLCert: ttools.GetEnvDefault("PGSSLCERT", ""),
ConfigKeySSLRootCert: ttools.GetEnvDefault("PGSSLROOTCERT", ""),
}
}
func init() {
DefaultEnvConfig = getConfigFromEnv()
}
type TCConfig struct {
BaseConfig map[string]interface{}
ConfigOverride map[string]interface{}
LayerConfig []map[string]interface{}
}
func (cfg TCConfig) Config(mConfig map[string]interface{}) dict.Dict {
var config map[string]interface{}
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(DefaultEnvConfig)
config[ConfigKeyName] = "provider_name"
prvd, err := NewMVTTileProvider(config, nil)
// 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, nil, 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": {
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(DefaultEnvConfig)
config[ConfigKeyName] = "provider_name"
provider, err := NewTileProvider(config, nil)
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]interface{}{
ConfigKeyURI: ttools.GetEnvDefault("PGURI_NO_ACCESS", "postgres://tegola_no_access:postgres@localhost:5432/tegola"),
},
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)",
},
"configure from postgreql URI": {
TCConfig: TCConfig{
ConfigOverride: map[string]interface{}{
ConfigKeyURI: DefaultEnvConfig["uri"],
},
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{},
},
}
for name, tc := range tests {
t.Run(name, fn(tc))
}
}
func TestBuildUri(t *testing.T) {
type tcase struct {
TCConfig
expectedUri string
err string
}
tests := map[string]tcase{
"add sslmode to uri if missing": {
TCConfig: TCConfig{
ConfigOverride: map[string]interface{}{
ConfigKeyURI: "postgres://postgres:postgres@localhost:5432/tegola",
},
},
expectedUri: "postgres://postgres:postgres@localhost:5432/tegola?sslmode=disable",
},
"add sslmode of uri and dont overwrite with default": {
TCConfig: TCConfig{
ConfigOverride: map[string]interface{}{
ConfigKeyURI: "postgres://postgres:postgres@localhost:5432/tegola?sslmode=prefer",
},
},
expectedUri: "postgres://postgres:postgres@localhost:5432/tegola?sslmode=prefer",
},
"invalid uri": {
TCConfig: TCConfig{
ConfigOverride: map[string]interface{}{
ConfigKeyURI: false,
},
},
err: "config: value mapped to \"uri\" is bool not string",
},
"invalid uri scheme": {
TCConfig: TCConfig{
ConfigOverride: map[string]interface{}{
ConfigKeyURI: "http://hi.de",
},
},
err: "postgis: invalid uri (invalid connection scheme (http))",
},
"invalid uri missing user": {
TCConfig: TCConfig{
ConfigOverride: map[string]interface{}{
ConfigKeyURI: "postgres://hi.de",
},
},
err: "postgis: invalid uri (auth credentials missing)",
},
"invalid uri missing port": {
TCConfig: TCConfig{
ConfigOverride: map[string]interface{}{
ConfigKeyURI: "postgres://postgres:postgres@localhost/bla",
},
},
err: "postgis: splitting host port error: address localhost: missing port in address",
},
"invalid uri missing host": {
TCConfig: TCConfig{
ConfigOverride: map[string]interface{}{
ConfigKeyURI: "postgres://postgres:postgres@:5432/bla",
},
},
err: "postgis: invalid uri (address :5432: missing host in address)",
},
"invalid uri missing database": {
TCConfig: TCConfig{
ConfigOverride: map[string]interface{}{
ConfigKeyURI: "postgres://postgres:postgres@localhost:5432",
},
},
err: "postgis: invalid uri (missing database)",
},
"invalid sslmode": {
TCConfig: TCConfig{
ConfigOverride: map[string]interface{}{
ConfigKeySSLMode: false,
},
},
err: "config: value mapped to \"ssl_mode\" is bool not string",
},
}
fn := func(tc tcase) func(t *testing.T) {
return func(t *testing.T) {
config := tc.Config(DefaultConfig)
uri, _, err := BuildURI(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 BuildURI, got: %v", tc.err, err)
}
return
}
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
if uri.String() != tc.expectedUri {
t.Errorf("expected: %v, got: %v", tc.expectedUri, uri)
}
}
}
for name, tc := range tests {
t.Run(name, fn(tc))
}
}