forked from kellydunn/golang-geo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_geocoder_test.go
92 lines (74 loc) · 2.29 KB
/
google_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
package geo
import (
"fmt"
"io/ioutil"
"os"
"path"
"testing"
)
/// TODO Test extracting Address from Google Reverse Geocoding Response
func TestExtractAddressFromResponse(t *testing.T) {
g := &GoogleGeocoder{}
data, err := GetMockResponse("test/data/google_reverse_geocode_success.json")
if err != nil {
t.Error("%v\n", err)
}
address, err := g.extractAddressFromResponse(data)
if address != "285 Bedford Avenue, Brooklyn, NY 11211, USA" {
t.Error(fmt.Sprintf("Expected: 285 Bedford Avenue, Brooklyn, NY 11211 USA. Got: %s", address))
}
}
func TestExtractAddressFromResponseZeroResults(t *testing.T) {
g := &GoogleGeocoder{}
data, err := GetMockResponse("test/data/google_geocode_zero_results.json")
if err != nil {
t.Error("%v\n", err)
}
address, err := g.extractAddressFromResponse(data)
if address != "" && err != nil {
t.Error(fmt.Sprintf("Expected: '' response and 'ERROR: ZERO_RESULTS'. Got: '%s' response and '%s'", address, err.Error()))
}
}
// TODO Test extracting LatLng from Google Geocoding Response
func TestExtractLatLngFromRequest(t *testing.T) {
g := &GoogleGeocoder{}
data, err := GetMockResponse("test/data/google_geocode_success.json")
if err != nil {
t.Error("%v\n", err)
}
point, err := g.extractLatLngFromResponse(data)
if err != nil {
t.Error("%v\n", err)
}
if point.lat != 37.615223 || point.lng != -122.389979 {
t.Error(fmt.Sprintf("Expected: [37.615223, -122.389979], Got: [%f, %f]", point.lat, point.lng))
}
}
// TODO Test extracting LatLng from Google Geocoding Response when no results are returned
func TestExtractLatLngFromRequestZeroResults(t *testing.T) {
g := &GoogleGeocoder{}
data, err := GetMockResponse("test/data/google_geocode_zero_results.json")
if err != nil {
t.Error("%v\n", err)
}
_, err = g.extractLatLngFromResponse(data)
if err != googleZeroResultsError {
t.Error(fmt.Sprintf("Expected error: %v, Got: %v"), googleZeroResultsError, err)
}
}
func GetMockResponse(s string) ([]byte, error) {
dataPath := path.Join(s)
_, readErr := os.Stat(dataPath)
if readErr != nil && os.IsNotExist(readErr) {
return nil, readErr
}
handler, handlerErr := os.Open(dataPath)
if handlerErr != nil {
return nil, handlerErr
}
data, readErr := ioutil.ReadAll(handler)
if readErr != nil {
return nil, readErr
}
return data, nil
}