Skip to content

Commit 97c660d

Browse files
committed
Start on some testing infra
1 parent e214c84 commit 97c660d

File tree

6 files changed

+151
-7
lines changed

6 files changed

+151
-7
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ Snapshot builds of the latest code:
1919
## Basic Operation
2020

2121
The simplest start-up uses just a [database connection string](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING) in the `DATABASE_URL` environment variable, and reads all other information from the database.
22+
```
23+
Usage: pg_tileserv [-dh] [-c value] [parameters ...]
24+
-c, --config=value
25+
config file name
26+
-d, --debug log debugging information
27+
-h, --help display help output
28+
```
2229

2330
### Linux/OSX
2431

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ require (
1313
github.com/pborman/getopt v0.0.0-20190409184431-ee0cd42419d3
1414
github.com/sirupsen/logrus v1.4.2
1515
github.com/spf13/viper v1.6.1
16+
github.com/stretchr/testify v1.4.0
1617
github.com/theckman/httpforwarded v0.4.0
1718
)

main.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ var globalDb *pgxpool.Pool = nil
3838

3939
/******************************************************************************/
4040

41-
func Calculate(i int) int {
42-
return 3
43-
}
44-
4541
func main() {
4642

4743
viper.SetDefault("DbConnection", "sslmode=disable")
@@ -300,16 +296,25 @@ func (fn tileAppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
300296

301297
/******************************************************************************/
302298

303-
func handleRequests() {
304-
299+
func TileRouter() *mux.Router {
305300
// creates a new instance of a mux router
306301
r := mux.NewRouter().StrictSlash(true)
302+
// Front page and layer list
307303
r.Handle("/", tileAppHandler(requestListHtml))
308304
r.Handle("/index.html", tileAppHandler(requestListHtml))
309-
r.Handle("/{name}.html", tileAppHandler(requestPreview))
310305
r.Handle("/index.json", tileAppHandler(requestListJson))
306+
// Layer detail and demo pages
307+
r.Handle("/{name}.html", tileAppHandler(requestPreview))
311308
r.Handle("/{name}.json", tileAppHandler(requestDetailJson))
309+
// Tile requests
312310
r.Handle("/{name}/{z:[0-9]+}/{x:[0-9]+}/{y:[0-9]+}.{ext}", tileAppHandler(requestTile))
311+
return r
312+
}
313+
314+
func handleRequests() {
315+
316+
// Get a configured router
317+
r := TileRouter()
313318

314319
// Allow CORS from anywhere
315320
corsOpt := handlers.AllowedOrigins([]string{"*"})

main_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"net/http/httptest"
7+
"os"
8+
"strings"
9+
"testing"
10+
11+
"github.com/jackc/pgx/v4/pgxpool"
12+
"github.com/spf13/viper"
13+
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/suite"
15+
)
16+
17+
type dbSuite struct {
18+
suite.Suite
19+
setup bool
20+
}
21+
22+
var db *pgxpool.Pool
23+
24+
func (suite *dbSuite) SetupSuite() {
25+
viper.Set("DbConnection", os.Getenv("TEST_DATABASE_URL"))
26+
db, err := DbConnect()
27+
if err != nil {
28+
suite.setup = false
29+
return
30+
}
31+
sql := "CREATE EXTENSION IF NOT EXISTS postgis"
32+
_, err = db.Exec(context.Background(), sql)
33+
if err != nil {
34+
suite.T().Skip("DB integration test suite setup failed, skipping")
35+
suite.setup = false
36+
return
37+
}
38+
suite.setup = true
39+
}
40+
41+
func (suite *dbSuite) TearDownSuite() {
42+
}
43+
44+
func (suite *dbSuite) SetupTest() {
45+
}
46+
47+
func (suite *dbSuite) TearDownTest() {
48+
}
49+
50+
func (suite *dbSuite) TestDBNoTables() {
51+
if !suite.setup {
52+
suite.T().Skip("DB integration test suite setup failed, skipping")
53+
}
54+
r := TileRouter()
55+
request, _ := http.NewRequest("GET", "/index.json", nil)
56+
response := httptest.NewRecorder()
57+
r.ServeHTTP(response, request)
58+
assert.Equal(suite.T(), 200, response.Code, "OK response is expected")
59+
60+
json_result := response.Body.String()
61+
json_result = strings.TrimSpace(json_result)
62+
json_expect := "{}"
63+
assert.Equal(suite.T(), json_expect, json_result, "empty json response is expected")
64+
}
65+
66+
func TestDatabaseSuite(t *testing.T) {
67+
tests := new(dbSuite)
68+
suite.Run(t, tests)
69+
}

testdata.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CREATE EXTENSION postgis;
2+
3+
CREATE TABLE public.no_geometry (id integer PRIMARY KEY, name text);
4+
5+
CREATE TABLE public.geometry_no_srid (id integer PRIMARY KEY, geom Geometry(Point));
6+
7+
CREATE TABLE public.geometry_only (geom Geometry(Point, 4326));
8+
9+
CREATE TABLE public.geometry_no_type (id integer PRIMARY KEY, geom Geometry(Geometry, 4326));
10+
11+
CREATE TABLE public.geometry_no_data (id integer PRIMARY KEY, geom Geometry(Geometry, 4326));

tile_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
// "github.com/stretchr/testify/assert"
6+
)
7+
8+
func Test_makeTile(t *testing.T) {
9+
// func makeTile(vars map[string]string) (Tile, error) {
10+
11+
var tests = []struct {
12+
input map[string]string
13+
outtile Tile
14+
outerr error
15+
}{
16+
{
17+
map[string]string{"x": "0", "y": "0", "z": "0", "ext": "pbf"},
18+
Tile{X: 0, Y: 0, Zoom: 0, Ext: "pbf"},
19+
nil,
20+
},
21+
}
22+
23+
for _, test := range tests {
24+
if output, err := makeTile(test.input); test.outtile != output || test.outerr != err {
25+
t.Error("Test Failed: {} inputted, {} expected, recieved: {}, {}", test.input, test.outtile, output, err)
26+
}
27+
}
28+
29+
}
30+
31+
func Test_tileIsValid(t *testing.T) {
32+
// func makeTile(vars map[string]string) (Tile, error) {
33+
34+
var tests = []struct {
35+
input Tile
36+
output bool
37+
}{
38+
{Tile{X: 0, Y: 0, Zoom: 0, Ext: "pbf"}, true},
39+
{Tile{X: 0, Y: 1, Zoom: 0, Ext: "pbf"}, false},
40+
{Tile{X: 1, Y: 1, Zoom: 1, Ext: "pbf"}, true},
41+
{Tile{X: -1, Y: 0, Zoom: 0, Ext: "pbf"}, false},
42+
{Tile{X: 0, Y: 0, Zoom: -1, Ext: "pbf"}, false},
43+
}
44+
45+
for _, test := range tests {
46+
if output := test.input.IsValid(); output != test.output {
47+
t.Error("Test Failed: {} inputted, {} expected, recieved: {}", test.input, test.output, output)
48+
}
49+
}
50+
51+
}

0 commit comments

Comments
 (0)