forked from go-spatial/tegola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_internal_test.go
222 lines (193 loc) · 6.16 KB
/
util_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
package postgis
import (
"context"
"fmt"
"os"
"testing"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/go-spatial/tegola"
"github.com/go-spatial/tegola/internal/ttools"
"github.com/go-spatial/tegola/provider"
)
func TestReplaceTokens(t *testing.T) {
type tcase struct {
sql string
tile provider.Tile
expected string
layer Layer
}
fn := func(tc tcase) func(t *testing.T) {
return func(t *testing.T) {
sql, err := replaceTokens(tc.sql, &tc.layer, tc.tile, true)
if err != nil {
t.Errorf("unexpected error, Expected nil Got %v", err)
return
}
if sql != tc.expected {
t.Errorf("incorrect sql,\n Expected \n \t%v\n Got \n \t%v", tc.expected, sql)
return
}
}
}
tests := map[string]tcase{
"replace BBOX": {
sql: "SELECT * FROM foo WHERE geom && !BBOX!",
layer: Layer{srid: tegola.WebMercator},
tile: provider.NewTile(2, 1, 1, 64, tegola.WebMercator),
expected: "SELECT * FROM foo WHERE geom && ST_MakeEnvelope(-1.017529720390625e+07,-156543.03390625,156543.03390625,1.017529720390625e+07,3857)",
},
"replace BBOX with != in query": {
sql: "SELECT * FROM foo WHERE geom && !BBOX! AND bar != 42",
layer: Layer{srid: tegola.WebMercator},
tile: provider.NewTile(2, 1, 1, 64, tegola.WebMercator),
expected: "SELECT * FROM foo WHERE geom && ST_MakeEnvelope(-1.017529720390625e+07,-156543.03390625,156543.03390625,1.017529720390625e+07,3857) AND bar != 42",
},
"replace BBOX and ZOOM 1": {
sql: "SELECT id, scalerank=!ZOOM! FROM foo WHERE geom && !BBOX!",
layer: Layer{srid: tegola.WebMercator},
tile: provider.NewTile(2, 1, 1, 64, tegola.WebMercator),
expected: "SELECT id, scalerank=2 FROM foo WHERE geom && ST_MakeEnvelope(-1.017529720390625e+07,-156543.03390625,156543.03390625,1.017529720390625e+07,3857)",
},
"replace BBOX and ZOOM 2": {
sql: "SELECT id, scalerank=!ZOOM! FROM foo WHERE geom && !BBOX!",
layer: Layer{srid: tegola.WebMercator},
tile: provider.NewTile(16, 11241, 26168, 64, tegola.WebMercator),
expected: "SELECT id, scalerank=16 FROM foo WHERE geom && ST_MakeEnvelope(-1.3163688815956049e+07,4.0352540420407765e+06,-1.3163058210472783e+07,4.035884647524042e+06,3857)",
},
"replace pixel_width/height and scale_denominator": {
sql: "SELECT id, !pixel_width! as width, !pixel_height! as height, !scale_denominator! as scale_denom FROM foo WHERE geom && !BBOX!",
layer: Layer{srid: tegola.WebMercator},
tile: provider.NewTile(11, 1070, 676, 64, tegola.WebMercator),
expected: "SELECT id, 76.43702827453671 as width, 76.43702827453671 as height, 272989.38669477403 as scale_denom FROM foo WHERE geom && ST_MakeEnvelope(899816.6968478388,6.789748347570495e+06,919996.0723123164,6.809927723034973e+06,3857)",
},
}
for name, tc := range tests {
t.Run(name, fn(tc))
}
}
func TestUppercaseTokens(t *testing.T) {
type tcase struct {
str string
expected string
}
fn := func(tc tcase) func(t *testing.T) {
return func(t *testing.T) {
out := uppercaseTokens(tc.str)
if out != tc.expected {
t.Errorf("expected \n \t%v\n out \n \t%v", tc.expected, out)
return
}
}
}
tests := map[string]tcase{
"uppercase tokens": {
str: "this !lower! case !STrInG! should uppercase !TOKENS!",
expected: "this !LOWER! case !STRING! should uppercase !TOKENS!",
},
"no tokens": {
str: "no token",
expected: "no token",
},
"empty string": {
str: "",
expected: "",
},
"unclosed token": {
str: "unclosed !token",
expected: "unclosed !token",
},
}
for name, tc := range tests {
t.Run(name, fn(tc))
}
}
func TestDecipherFields(t *testing.T) {
ttools.ShouldSkip(t, TESTENV)
type tcase struct {
sql string
expectedRowCount int
expectedTags map[string]interface{}
}
host := os.Getenv("PGHOST")
port := 5432
db := os.Getenv("PGDATABASE")
user := os.Getenv("PGUSER")
password := os.Getenv("PGPASSWORD")
cs := fmt.Sprintf("postgres://%v:%v@%v:%v/%v", user, password, host, port, db)
dbconfig, err := BuildDBConfig(cs)
if err != nil {
t.Fatalf("unable to build db config: %v", err)
}
conn, err := pgxpool.ConnectConfig(context.Background(), dbconfig)
if err != nil {
t.Fatalf("unable to connect to database: %v", err)
}
defer conn.Close()
fn := func(tc tcase) func(t *testing.T) {
return func(t *testing.T) {
rows, err := conn.Query(context.Background(), tc.sql)
if err != nil {
t.Errorf("Error performing query: %v", err)
return
}
defer rows.Close()
var rowCount int
for rows.Next() {
geoFieldname := "geom"
idFieldname := "id"
descriptions := rows.FieldDescriptions()
vals, err := rows.Values()
if err != nil {
t.Errorf("unexepcted error reading row Values: %v", err)
return
}
_, _, tags, err := decipherFields(context.TODO(), geoFieldname, idFieldname, descriptions, vals)
if err != nil {
t.Errorf("unexepcted error running decipherFileds: %v", err)
return
}
if len(tags) != len(tc.expectedTags) {
t.Errorf("got %v tags, expecting %v: %#v, %#v", len(tags), len(tc.expectedTags), tags, tc.expectedTags)
return
}
for k, v := range tags {
if tc.expectedTags[k] != v {
t.Errorf("missing or bad value for tag %v: %v (%T) != %v (%T)", k, v, v, tc.expectedTags[k], tc.expectedTags[k])
return
}
}
rowCount++
}
if rows.Err() != nil {
t.Errorf("unexpected err: %v", rows.Err())
return
}
if rowCount != tc.expectedRowCount {
t.Errorf("invalid row count. expected %v. got %v", tc.expectedRowCount, rowCount)
return
}
}
}
tests := map[string]tcase{
"hstore 1": {
sql: "SELECT id, tags, int8_test FROM hstore_test WHERE id = 1;",
expectedRowCount: 1,
expectedTags: map[string]interface{}{
"height": "9",
"int8_test": int64(1000888),
},
},
"hstore 2": {
sql: "SELECT id, tags, int8_test FROM hstore_test WHERE id = 2;",
expectedRowCount: 1,
expectedTags: map[string]interface{}{
"hello": "there",
"good": "day",
"int8_test": int64(8880001),
},
},
}
for name, tc := range tests {
t.Run(name, fn(tc))
}
}