-
Notifications
You must be signed in to change notification settings - Fork 125
/
google_geocoder_test.go
97 lines (82 loc) · 2.27 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
93
94
95
96
97
package geo
import (
"fmt"
"io/ioutil"
"os"
"path"
"testing"
)
func TestSetGoogleAPIKey(t *testing.T) {
SetGoogleAPIKey("foo")
if GoogleAPIKey != "foo" {
t.Errorf("Mismatched value for GoogleAPIKey. Expected: 'foo', Actual: %s", GoogleAPIKey)
}
}
func TestSetGoogleGeocodeURL(t *testing.T) {
SetGoogleGeocodeURL("foo")
if googleGeocodeURL != "foo" {
t.Errorf("Mismatched value for googleGeocoeURL. Expected: 'foo', Actual: %s", googleGeocodeURL)
}
}
func TestGoogleGeocoderQueryStr(t *testing.T) {
// Empty API Key
SetGoogleAPIKey("")
address := "123 fake st"
res, err := googleGeocodeQueryStr(address)
if err != nil {
t.Errorf("Error creating query string: %v", err)
}
expected := "address=123+fake+st"
if res != expected {
t.Errorf(fmt.Sprintf("Mismatched query string. Expected: %s. Actual: %s", expected, res))
}
// Set api key to some value
SetGoogleAPIKey("foo")
res, err = googleGeocodeQueryStr(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
SetGoogleAPIKey("")
p := &Point{lat: 123.45, lng: 56.78}
res, err := googleReverseGeocodeQueryStr(p)
if err != nil {
t.Errorf("Error creating query string: %v", err)
}
expected := "latlng=123.450000,56.780000"
if res != expected {
t.Errorf(fmt.Sprintf("Mismatched query string. Expected: %s. Actual: %s", expected, res))
}
// Set api key to some value
SetGoogleAPIKey("foo")
res, err = googleReverseGeocodeQueryStr(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))
}
}
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
}