-
Notifications
You must be signed in to change notification settings - Fork 5
/
geocode_test.go
119 lines (97 loc) · 3.2 KB
/
geocode_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
package geolocate
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func ExampleGeocode() {
client := NewGoogleGeo("")
res, _ := client.Geocode("New York City")
fmt.Println(res)
// Output: &{40.7127837 -74.0059413 New York, NY, USA APPROXIMATE}
}
func ExampleGeocodeWithRegion() {
client := NewGoogleGeo("")
res, _ := client.Geocode("Toledo")
fmt.Println(res)
res, _ = client.GeocodeWithRegion("Toledo", "es")
fmt.Println(res)
// Output: &{41.6639383 -83.55521200000001 Toledo, OH, USA APPROXIMATE}
// &{39.8628316 -4.027323099999999 Toledo, Spain APPROXIMATE}
}
func ExampleReverseGeocode() {
client := NewGoogleGeo("")
p := Point{Lat: 40.7127837, Lng: -74.0059413}
res, _ := client.ReverseGeocode(&p)
fmt.Println(res)
// Output: New York City Hall, New York, NY 10007, USA
}
func ExampleReverseGeocodeDetailed() {
client := NewGoogleGeo("")
p := Point{Lat: 40.7127837, Lng: -74.0059413}
res, _ := client.ReverseGeocodeDetailed(&p)
address := DetailsToAddress(&res.Results[0])
fmt.Printf("%#v", address)
// Output: &geolocate.Address{StreetNumber:"", Locality:"New York", Sublocality:"Manhattan", Neighborhood:"Lower Manhattan", Route:"", PostalCode:"10007", Country:"United States", AdministrativeAreaLevel1:"New York", AdministrativeAreaLevel2:"New York County"}
}
func TestGoogleGeocoder(t *testing.T) {
// Empty API Key
c := NewGoogleGeo("")
res, err := c.Geocode("Mora, Sweden")
assert.Nil(t, err)
assert.Equal(t, "Mora, Sweden", res.Address)
}
func TestGoogleGeocoderQueryStr(t *testing.T) {
// Empty API Key
c := NewGoogleGeo("")
address := "123 fake st"
res, err := c.geocodeQueryStr(address, "")
if err != nil {
t.Errorf("Error creating query string: %v", err)
}
expected := "address=123+fake+st&key="
if res != expected {
t.Errorf(fmt.Sprintf("Mismatched query string. Expected: %s. Actual: %s", expected, res))
}
res, err = c.geocodeQueryStr(address, "se")
if err != nil {
t.Errorf("Error creating query string: %v", err)
}
expected = "address=123+fake+st®ion=se&key="
if res != expected {
t.Errorf(fmt.Sprintf("Mismatched query string. Expected: %s. Actual: %s", expected, res))
}
// Set api key to some value
c.SetGoogleAPIKey("foo")
res, err = c.geocodeQueryStr(address, "")
if err != nil {
t.Errorf("Error creating query string: %v", err)
}
expected = "address=123+fake+st&key=foo"
if res != expected {
t.Errorf(fmt.Sprintf("Mismatched query string. Expected: %s. Actual: %s", expected, res))
}
}
func TestGoogleReverseGeocoderQueryStr(t *testing.T) {
// Empty API Key
c := NewGoogleGeo("")
p := &Point{Lat: 123.45, Lng: 56.78}
res, err := c.reverseGeocodeQueryStr(p)
if err != nil {
t.Errorf("Error creating query string: %v", err)
}
expected := "latlng=123.450000,56.780000&key="
if res != expected {
t.Errorf(fmt.Sprintf("Mismatched query string. Expected: %s. Actual: %s", expected, res))
}
// Set api key to some value
c.SetGoogleAPIKey("foo")
res, err = c.reverseGeocodeQueryStr(p)
if err != nil {
t.Errorf("Error creating query string: %v", err)
}
expected = "latlng=123.450000,56.780000&key=foo"
if res != expected {
t.Errorf(fmt.Sprintf("Mismatched query string. Expected: %s. Actual: %s", expected, res))
}
}