-
Notifications
You must be signed in to change notification settings - Fork 125
/
mapquest_geocoder_test.go
99 lines (83 loc) · 2.67 KB
/
mapquest_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
package geo
import (
"encoding/json"
"fmt"
"testing"
)
func TestSetMapquestAPIKey(t *testing.T) {
SetMapquestAPIKey("foo")
if MapquestAPIKey != "foo" {
t.Errorf("Mismatched value for MapQuestAPIKey. Expected: 'foo', Actual: %s", MapquestAPIKey)
}
}
func TestSetMapquestGeocodeURL(t *testing.T) {
SetMapquestGeocodeURL("foo")
if mapquestGeocodeURL != "foo" {
t.Errorf("Mismatched value for MapQuestGeocoeURL. Expected: 'foo', Actual: %s", mapquestGeocodeURL)
}
}
func TestMapquestGeocoderQueryStr(t *testing.T) {
// Empty API Key
SetMapquestAPIKey("")
address := "123 fake st"
res, err := mapquestGeocodeQueryStr(address)
if err != nil {
t.Errorf("Error creating query string: %v", err)
}
expected := "search.php?q=123+fake+st&format=json"
if res != expected {
t.Errorf(fmt.Sprintf("Mismatched query string. Expected: %s. Actual: %s", expected, res))
}
// Set api key to some value
SetMapquestAPIKey("foo")
res, err = mapquestGeocodeQueryStr(address)
if err != nil {
t.Errorf("Error creating query string: %v", err)
}
expected = "search.php?q=123+fake+st&key=foo&format=json"
if res != expected {
t.Errorf(fmt.Sprintf("Mismatched query string. Expected: %s. Actual: %s", expected, res))
}
}
func TestMapquestReverseGeocoderQueryStr(t *testing.T) {
// Empty API Key
SetMapquestAPIKey("")
p := &Point{lat: 123.45, lng: 56.78}
res, err := mapquestReverseGeocodeQueryStr(p)
if err != nil {
t.Errorf("Error creating query string: %v", err)
}
expected := "reverse.php?lat=123.450000&lng=56.780000&format=json"
if res != expected {
t.Errorf(fmt.Sprintf("Mismatched query string. Expected: %s. Actual: %s", expected, res))
}
// Set api key to some value
SetMapquestAPIKey("foo")
res, err = mapquestReverseGeocodeQueryStr(p)
if err != nil {
t.Errorf("Error creating query string: %v", err)
}
expected = "reverse.php?lat=123.450000&lng=56.780000&key=foo&format=json"
if res != expected {
t.Errorf(fmt.Sprintf("Mismatched query string. Expected: %s. Actual: %s", expected, res))
}
}
// Ensures that the Data Transfer Object used
// to get data from the Mapquest Geocoding API is well formed.
func TestMapQuestGeocodeFromRequest(t *testing.T) {
data, err := GetMockResponse("test/data/mapquest_geocode_success.json")
if err != nil {
t.Error("%v\n", err)
}
res := []*mapQuestGeocodeResponse{}
err = json.Unmarshal(data, &res)
if err != nil {
t.Error("%v\n", err)
}
if len(res) <= 0 {
t.Error("Unexecpected amount of results for mapquest mock response")
}
if res[0].Lat != "37.62181845" || res[0].Lng != "-122.383992092462" {
t.Error(fmt.Sprintf("Expected: [37.62181845, -122.383992092462], Got: [%s, %s]", res[0].Lat, res[0].Lng))
}
}