forked from codingsince1985/geo-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeocoder_test.go
103 lines (93 loc) · 2.69 KB
/
geocoder_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
package pickpoint_test
import (
"github.com/codingsince1985/geo-golang"
"github.com/codingsince1985/geo-golang/pickpoint"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
var key = os.Getenv("PICKPOINT_API_KEY")
func TestGeocode(t *testing.T) {
ts := testServer(response1)
defer ts.Close()
geocoder := pickpoint.Geocoder(key, ts.URL "/")
location, err := geocoder.Geocode("60 Collins St, Melbourne VIC 3000")
assert.Nil(t, err)
assert.Equal(t, geo.Location{Lat: -37.8157915, Lng: 144.9656171}, *location)
}
func TestReverseGeocode(t *testing.T) {
ts := testServer(response2)
defer ts.Close()
geocoder := pickpoint.Geocoder(key, ts.URL "/")
address, err := geocoder.ReverseGeocode(-37.8157915, 144.9656171)
assert.Nil(t, err)
assert.True(t, strings.Index(address.FormattedAddress, "Collins St") > 0)
}
func TestReverseGeocodeWithNoResult(t *testing.T) {
ts := testServer(response3)
defer ts.Close()
geocoder := pickpoint.Geocoder(key, ts.URL "/")
addr, err := geocoder.ReverseGeocode(-37.8157915, 164.9656171)
assert.Nil(t, addr)
assert.NotNil(t, err)
}
func testServer(response string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
resp.Write([]byte(response))
}))
}
const (
response1 = `[
{
"place_id":"133372311",
"licence":"Data © OpenStreetMap contributors, ODbL 1.0. http:\/\/www.openstreetmap.org\/copyright",
"osm_type":"way",
"osm_id":"316166613",
"boundingbox":[
"-37.8162553",
"-37.815533",
"144.9640149",
"144.9665099"
],
"lat":"-37.8157915",
"lon":"144.9656171",
"display_name":"Collins Street, Melbourne, City of Melbourne, Greater Melbourne, Victoria, 3000, Australia",
"class":"highway",
"type":"tertiary",
"importance":0.51
}
]`
response2 = `{
"place_id":"5122082",
"licence":"Data © OpenStreetMap contributors, ODbL 1.0. http:\/\/www.openstreetmap.org\/copyright",
"osm_type":"node",
"osm_id":"863206614",
"lat":"-37.8158091",
"lon":"144.9656492",
"display_name":"Telstra, Collins Street, Melbourne, City of Melbourne, Greater Melbourne, Victoria, 3000, Australia",
"address":{
"telephone":"Telstra",
"road":"Collins Street",
"suburb":"Melbourne",
"city":"Melbourne",
"county":"City of Melbourne",
"region":"Greater Melbourne",
"state":"Victoria",
"postcode":"3000",
"country":"Australia",
"country_code":"au"
},
"boundingbox":[
"-37.8159091",
"-37.8157091",
"144.9655492",
"144.9657492"
]
}`
response3 = `{
"error":"Unable to geocode"
}`
)