Skip to content

Commit e0594ba

Browse files
move common type structs to test_common.go
1 parent 6f71973 commit e0594ba

File tree

3 files changed

+41
-46
lines changed

3 files changed

+41
-46
lines changed

internal/service/db_test/handler_db_test.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,12 @@ import (
1616
"github.com/CrunchyData/pg_featureserv/internal/service"
1717
"github.com/CrunchyData/pg_featureserv/util"
1818
"github.com/jackc/pgx/v4/pgxpool"
19-
"github.com/paulmach/orb/geojson"
2019
)
2120

2221
var hTest util.HttpTesting
2322
var db *pgxpool.Pool
2423
var cat data.Catalog
2524

26-
// extracted from catalog_db.go
27-
// TODO should be imported from catalog.go
28-
type geojsonFeatureData struct {
29-
Type string `json:"type"`
30-
ID string `json:"id,omitempty"`
31-
Geom *geojson.Geometry `json:"geometry"`
32-
Props map[string]interface{} `json:"properties"`
33-
}
34-
3525
func TestMain(m *testing.M) {
3626
db = util.CreateTestDb()
3727
defer util.CloseTestDb(db)
@@ -50,10 +40,10 @@ func TestProperDbInit(t *testing.T) {
5040
util.Equals(t, 2, len(tables), "# table in DB")
5141
}
5242

53-
func TestTestPropertiesAllFromDb(t *testing.T) {
54-
/*rr := hTest.DoRequest(t, "/collections/mock_a/items?limit=2")
43+
func TestPropertiesAllFromDb(t *testing.T) {
44+
rr := hTest.DoRequest(t, "/collections/mock_a/items?limit=2")
5545

56-
var v FeatureCollection
46+
var v util.FeatureCollection
5747
errUnMarsh := json.Unmarshal(hTest.ReadBody(rr), &v)
5848
util.Assert(t, errUnMarsh == nil, fmt.Sprintf("%v", errUnMarsh))
5949

@@ -64,7 +54,7 @@ func TestTestPropertiesAllFromDb(t *testing.T) {
6454
util.Equals(t, "propA", v.Features[0].Props["prop_a"], "feature 1 # property A")
6555
util.Equals(t, 1.0, v.Features[0].Props["prop_b"], "feature 1 # property B")
6656
util.Equals(t, "propC", v.Features[0].Props["prop_c"], "feature 1 # property C")
67-
util.Equals(t, 1.0, v.Features[0].Props["prop_d"], "feature 1 # property D")*/
57+
util.Equals(t, 1.0, v.Features[0].Props["prop_d"], "feature 1 # property D")
6858
}
6959

7060
func TestCreateFeatureWithBadGeojsonInputDb(t *testing.T) {
@@ -138,7 +128,7 @@ func checkItem(t *testing.T, id int) {
138128
resp := hTest.DoRequest(t, path)
139129
body, _ := ioutil.ReadAll(resp.Body)
140130

141-
var v geojsonFeatureData
131+
var v util.GeojsonFeatureData
142132
errUnMarsh := json.Unmarshal(body, &v)
143133
util.Assert(t, errUnMarsh == nil, fmt.Sprintf("%v", errUnMarsh))
144134

internal/service/handler_test.go

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,6 @@ import (
2828
"github.com/CrunchyData/pg_featureserv/util"
2929
)
3030

31-
// Define a FeatureCollection structure for parsing test data
32-
33-
type Feature struct {
34-
Type string `json:"type"`
35-
ID string `json:"id,omitempty"`
36-
Geom *json.RawMessage `json:"geometry"`
37-
Props map[string]interface{} `json:"properties"`
38-
}
39-
40-
type FeatureCollection struct {
41-
Type string `json:"type"`
42-
Features []*Feature `json:"features"`
43-
NumberMatched uint `json:"numberMatched,omitempty"`
44-
NumberReturned uint `json:"numberReturned"`
45-
TimeStamp string `json:"timeStamp,omitempty"`
46-
Links []*api.Link `json:"links"`
47-
}
48-
4931
var hTest util.HttpTesting
5032

5133
var catalogMock *data.CatalogMock
@@ -149,7 +131,7 @@ func TestCollectionItem(t *testing.T) {
149131
func TestFilterB(t *testing.T) {
150132
rr := hTest.DoRequest(t, "/collections/mock_a/items?prop_b=1")
151133

152-
var v FeatureCollection
134+
var v util.FeatureCollection
153135
errUnMarsh := json.Unmarshal(hTest.ReadBody(rr), &v)
154136
util.Assert(t, errUnMarsh == nil, fmt.Sprintf("%v", errUnMarsh))
155137

@@ -159,7 +141,7 @@ func TestFilterB(t *testing.T) {
159141
func TestFilterD(t *testing.T) {
160142
rr := hTest.DoRequest(t, "/collections/mock_c/items?prop_d=1")
161143

162-
var v FeatureCollection
144+
var v util.FeatureCollection
163145
errUnMarsh := json.Unmarshal(hTest.ReadBody(rr), &v)
164146
util.Assert(t, errUnMarsh == nil, fmt.Sprintf("%v", errUnMarsh))
165147

@@ -169,7 +151,7 @@ func TestFilterD(t *testing.T) {
169151
func TestFilterBD(t *testing.T) {
170152
rr := hTest.DoRequest(t, "/collections/mock_c/items?prop_b=2&prop_d=2")
171153

172-
var v FeatureCollection
154+
var v util.FeatureCollection
173155
errUnMarsh := json.Unmarshal(hTest.ReadBody(rr), &v)
174156
util.Assert(t, errUnMarsh == nil, fmt.Sprintf("%v", errUnMarsh))
175157

@@ -179,7 +161,7 @@ func TestFilterBD(t *testing.T) {
179161
func TestFilterBDNone(t *testing.T) {
180162
rr := hTest.DoRequest(t, "/collections/mock_c/items?prop_b=1&prop_d=2")
181163

182-
var v FeatureCollection
164+
var v util.FeatureCollection
183165
errUnMarsh := json.Unmarshal(hTest.ReadBody(rr), &v)
184166
util.Assert(t, errUnMarsh == nil, fmt.Sprintf("%v", errUnMarsh))
185167

@@ -189,7 +171,7 @@ func TestFilterBDNone(t *testing.T) {
189171
func TestSortBy(t *testing.T) {
190172
rr := hTest.DoRequest(t, "/collections/mock_a/items?sortby=prop_b")
191173

192-
var v FeatureCollection
174+
var v util.FeatureCollection
193175
errUnMarsh := json.Unmarshal(hTest.ReadBody(rr), &v)
194176
util.Assert(t, errUnMarsh == nil, fmt.Sprintf("%v", errUnMarsh))
195177

@@ -199,7 +181,7 @@ func TestSortBy(t *testing.T) {
199181
func TestSortByDesc(t *testing.T) {
200182
rr := hTest.DoRequest(t, "/collections/mock_a/items?sortby=-prop_b")
201183

202-
var v FeatureCollection
184+
var v util.FeatureCollection
203185
errUnMarsh := json.Unmarshal(hTest.ReadBody(rr), &v)
204186
util.Assert(t, errUnMarsh == nil, fmt.Sprintf("%v", errUnMarsh))
205187

@@ -209,7 +191,7 @@ func TestSortByDesc(t *testing.T) {
209191
func TestSortByAsc(t *testing.T) {
210192
rr := hTest.DoRequest(t, "/collections/mock_a/items?sortby=+prop_b")
211193

212-
var v FeatureCollection
194+
var v util.FeatureCollection
213195
errUnMarsh := json.Unmarshal(hTest.ReadBody(rr), &v)
214196
util.Assert(t, errUnMarsh == nil, fmt.Sprintf("%v", errUnMarsh))
215197

@@ -219,7 +201,7 @@ func TestSortByAsc(t *testing.T) {
219201
func TestLimit(t *testing.T) {
220202
rr := hTest.DoRequest(t, "/collections/mock_a/items?limit=3")
221203

222-
var v FeatureCollection
204+
var v util.FeatureCollection
223205
errUnMarsh := json.Unmarshal(hTest.ReadBody(rr), &v)
224206
util.Assert(t, errUnMarsh == nil, fmt.Sprintf("%v", errUnMarsh))
225207

@@ -232,7 +214,7 @@ func TestLimit(t *testing.T) {
232214
func TestLimitZero(t *testing.T) {
233215
rr := hTest.DoRequest(t, "/collections/mock_a/items?limit=0")
234216

235-
var v FeatureCollection
217+
var v util.FeatureCollection
236218
errUnMarsh := json.Unmarshal(hTest.ReadBody(rr), &v)
237219
util.Assert(t, errUnMarsh == nil, fmt.Sprintf("%v", errUnMarsh))
238220

@@ -247,7 +229,7 @@ func TestLimitInvalid(t *testing.T) {
247229
func TestQueryParamCase(t *testing.T) {
248230
rr := hTest.DoRequest(t, "/collections/mock_a/items?LIMIT=2&Offset=4")
249231

250-
var v FeatureCollection
232+
var v util.FeatureCollection
251233
errUnMarsh := json.Unmarshal(hTest.ReadBody(rr), &v)
252234
util.Assert(t, errUnMarsh == nil, fmt.Sprintf("%v", errUnMarsh))
253235

@@ -259,7 +241,7 @@ func TestQueryParamCase(t *testing.T) {
259241
func TestOffset(t *testing.T) {
260242
rr := hTest.DoRequest(t, "/collections/mock_a/items?limit=2&offset=4")
261243

262-
var v FeatureCollection
244+
var v util.FeatureCollection
263245
errUnMarsh := json.Unmarshal(hTest.ReadBody(rr), &v)
264246
util.Assert(t, errUnMarsh == nil, fmt.Sprintf("%v", errUnMarsh))
265247

@@ -302,7 +284,7 @@ func TestProperties(t *testing.T) {
302284
// - non-existing names are ignored
303285
rr := hTest.DoRequest(t, "/collections/mock_a/items?limit=2&properties=PROP_A,prop_C,prop_a,not_prop")
304286

305-
var v FeatureCollection
287+
var v util.FeatureCollection
306288
errUnMarsh := json.Unmarshal(hTest.ReadBody(rr), &v)
307289
util.Assert(t, errUnMarsh == nil, fmt.Sprintf("%v", errUnMarsh))
308290

@@ -316,7 +298,7 @@ func TestProperties(t *testing.T) {
316298
func TestPropertiesAll(t *testing.T) {
317299
rr := hTest.DoRequest(t, "/collections/mock_a/items?limit=2")
318300

319-
var v FeatureCollection
301+
var v util.FeatureCollection
320302
errUnMarsh := json.Unmarshal(hTest.ReadBody(rr), &v)
321303
util.Assert(t, errUnMarsh == nil, fmt.Sprintf("%v", errUnMarsh))
322304

util/test_common.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,31 @@ import (
1919
"reflect"
2020
"runtime"
2121
"testing"
22+
23+
"github.com/CrunchyData/pg_featureserv/internal/api"
24+
"github.com/paulmach/orb/geojson"
2225
)
2326

27+
// extracted from catalog_db.go
28+
// TODO should be imported from catalog.go
29+
type GeojsonFeatureData struct {
30+
Type string `json:"type"`
31+
ID string `json:"id,omitempty"`
32+
Geom *geojson.Geometry `json:"geometry"`
33+
Props map[string]interface{} `json:"properties"`
34+
}
35+
36+
// Define a FeatureCollection structure for parsing test data
37+
// TODO should be move to and imported from catalog.go
38+
type FeatureCollection struct {
39+
Type string `json:"type"`
40+
Features []*GeojsonFeatureData `json:"features"`
41+
NumberMatched uint `json:"numberMatched,omitempty"`
42+
NumberReturned uint `json:"numberReturned"`
43+
TimeStamp string `json:"timeStamp,omitempty"`
44+
Links []*api.Link `json:"links"`
45+
}
46+
2447
//---- testing utilities from https://github.com/benbjohnson/testing
2548

2649
// assert fails the test if the condition is false.

0 commit comments

Comments
 (0)