-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
esri_test.rb
200 lines (172 loc) · 7.39 KB
/
esri_test.rb
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# encoding: utf-8
require 'test_helper'
require 'geocoder/esri_token'
class EsriTest < GeocoderTestCase
def setup
Geocoder.configure(lookup: :esri)
end
def test_query_for_geocode
query = Geocoder::Query.new("Bluffton, SC")
lookup = Geocoder::Lookup.get(:esri)
res = lookup.query_url(query)
assert_equal "http:https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/find?f=pjson&outFields=%2A&text=Bluffton%2C+SC",
res
end
def test_query_for_geocode_with_source_country
Geocoder.configure(esri: {source_country: 'USA'})
query = Geocoder::Query.new("Bluffton, SC")
lookup = Geocoder::Lookup.get(:esri)
url = lookup.query_url(query)
assert_match %r{sourceCountry=USA}, url
end
def test_query_for_geocode_with_preferred_label_values
Geocoder.configure(esri: {preferred_label_values: 'localCity'})
query = Geocoder::Query.new("Bluffton, SC")
lookup = Geocoder::Lookup.get(:esri)
url = lookup.query_url(query)
assert_match %r{preferredLabelValues=localCity}, url
end
def test_query_for_geocode_with_token_and_for_storage
token = Geocoder::EsriToken.new('xxxxx', Time.now + 60*60*24)
Geocoder.configure(esri: {token: token, for_storage: true})
query = Geocoder::Query.new("Bluffton, SC")
lookup = Geocoder::Lookup.get(:esri)
url = lookup.query_url(query)
assert_match %r{forStorage=true}, url
assert_match %r{token=xxxxx}, url
end
def test_token_from_options
options_token = Geocoder::EsriToken.new('options_token', Time.now + 60*60*24)
query = Geocoder::Query.new("Bluffton, SC", token: options_token)
lookup = Geocoder::Lookup.get(:esri)
url = lookup.query_url(query)
assert_match %r{token=options_token}, url
end
def test_token_from_options_overrides_configuration
config_token = Geocoder::EsriToken.new('config_token', Time.now + 60*60*24)
options_token = Geocoder::EsriToken.new('options_token', Time.now + 60*60*24)
Geocoder.configure(esri: { token: config_token })
query = Geocoder::Query.new("Bluffton, SC", token: options_token)
lookup = Geocoder::Lookup.get(:esri)
url = lookup.query_url(query)
assert_match %r{token=options_token}, url
end
def test_query_for_geocode_with_config_for_storage_false
Geocoder.configure(esri: {for_storage: false})
query = Geocoder::Query.new("Bluffton, SC", for_storage: true)
lookup = Geocoder::Lookup.get(:esri)
url = lookup.query_url(query)
assert_match %r{forStorage=true}, url
query = Geocoder::Query.new("Bluffton, SC", for_storage: false)
lookup = Geocoder::Lookup.get(:esri)
url = lookup.query_url(query)
assert_no_match %r{forStorage}, url
end
def test_query_for_geocode_with_config_for_storage_true
Geocoder.configure(esri: {for_storage: true})
query = Geocoder::Query.new("Bluffton, SC", for_storage: true)
lookup = Geocoder::Lookup.get(:esri)
url = lookup.query_url(query)
assert_match %r{forStorage=true}, url
query = Geocoder::Query.new("Bluffton, SC", for_storage: false)
lookup = Geocoder::Lookup.get(:esri)
url = lookup.query_url(query)
assert_no_match %r{forStorage}, url
end
def test_token_generation_doesnt_overwrite_existing_config
Geocoder.configure(esri: {api_key: ['id','secret'], for_storage: true})
query = Geocoder::Query.new("Bluffton, SC")
lookup = Geocoder::Lookup.get(:esri)
lookup.instance_eval do
# redefine `create_token` to return a manually-created token
def create_token
"xxxxx"
end
end
url = lookup.query_url(query)
assert_match %r{forStorage=true}, url
assert_match %r{token=xxxxx}, url
end
def test_query_for_reverse_geocode
query = Geocoder::Query.new([45.423733, -75.676333])
lookup = Geocoder::Lookup.get(:esri)
res = lookup.query_url(query)
assert_equal "http:https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/reverseGeocode?f=pjson&location=-75.676333%2C45.423733&outFields=%2A",
res
end
def test_results_component
result = Geocoder.search("Madison Square Garden, New York, NY").first
assert_equal "10001", result.postal_code
assert_equal "USA", result.country
assert_equal "Madison Square Garden", result.address
assert_equal "New York", result.city
assert_equal "New York", result.state
assert_equal "Madison Square Garden", result.place_name
assert_equal "Sports Complex", result.place_type
assert_equal(40.75004981300049, result.coordinates[0])
assert_equal(-73.99423889799965, result.coordinates[1])
end
def test_results_component_when_location_type_is_national_capital
result = Geocoder.search("washington dc").first
assert_equal "Washington", result.city
assert_equal "District of Columbia", result.state
assert_equal "USA", result.country
assert_equal "Washington, D. C., District of Columbia, United States", result.address
assert_equal "Washington", result.place_name
assert_equal "National Capital", result.place_type
assert_equal(38.895107833000452, result.coordinates[0])
assert_equal(-77.036365517999627, result.coordinates[1])
end
def test_results_component_when_location_type_is_state_capital
result = Geocoder.search("austin tx").first
assert_equal "Austin", result.city
assert_equal "Texas", result.state
assert_equal "USA", result.country
assert_equal "Austin, Texas, United States", result.address
assert_equal "Austin", result.place_name
assert_equal "State Capital", result.place_type
assert_equal(30.267145960000448, result.coordinates[0])
assert_equal(-97.743055550999657, result.coordinates[1])
end
def test_results_component_when_location_type_is_city
result = Geocoder.search("new york ny").first
assert_equal "New York City", result.city
assert_equal "New York", result.state
assert_equal "USA", result.country
assert_equal "New York City, New York, United States", result.address
assert_equal "New York City", result.place_name
assert_equal "City", result.place_type
assert_equal(40.714269404000447, result.coordinates[0])
assert_equal(-74.005969928999662, result.coordinates[1])
end
def test_results_component_when_reverse_geocoding
result = Geocoder.search([45.423733, -75.676333]).first
assert_equal "75007", result.postal_code
assert_equal "FRA", result.country
assert_equal "4 Avenue Gustave Eiffel", result.address
assert_equal "Paris", result.city
assert_equal "Île-de-France", result.state
assert_equal "4 Avenue Gustave Eiffel", result.place_name
assert_equal "Address", result.place_type
assert_equal(48.858129997357558, result.coordinates[0])
assert_equal(2.2956200048981574, result.coordinates[1])
end
def test_results_viewport
result = Geocoder.search("Madison Square Garden, New York, NY").first
assert_equal [40.744050000000001, -74.000241000000003, 40.756050000000002, -73.988241000000002],
result.viewport
end
def test_cache_key_doesnt_include_api_key_or_token
token = Geocoder::EsriToken.new('xxxxx', Time.now + 60)
Geocoder.configure(esri: {token: token, api_key: "xxxxx", for_storage: true})
query = Geocoder::Query.new("Bluffton, SC")
lookup = Geocoder::Lookup.get(:esri)
key = lookup.send(:cache_key, query)
assert_match %r{forStorage}, key
assert_no_match %r{token}, key
assert_no_match %r{api_key}, key
end
def teardown
Geocoder.configure(esri: {token: nil, for_storage: nil})
end
end