forked from alexreisner/geocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlookup_test.rb
More file actions
184 lines (162 loc) · 6.16 KB
/
lookup_test.rb
File metadata and controls
184 lines (162 loc) · 6.16 KB
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
# encoding: utf-8
require 'test_helper'
class LookupTest < GeocoderTestCase
def test_responds_to_name_method
Geocoder::Lookup.all_services.each do |l|
lookup = Geocoder::Lookup.get(l)
assert lookup.respond_to?(:name),
"Lookup #{l} does not respond to #name method."
end
end
def test_search_returns_empty_array_when_no_results
Geocoder::Lookup.all_services_except_test.each do |l|
next if [:ipgeolocation, :nationaal_georegister_nl].include?(l) # lookups that always return a result
lookup = Geocoder::Lookup.get(l)
set_api_key!(l)
silence_warnings do
assert_equal [], lookup.send(:results, Geocoder::Query.new("no results")),
"Lookup #{l} does not return empty array when no results."
end
end
end
def test_query_url_contains_values_in_params_hash
Geocoder::Lookup.all_services_except_test.each do |l|
next if [:freegeoip, :maxmind_local, :telize, :pointpin, :geoip2, :maxmind_geoip2, :mapbox, :ipdata_co, :ipinfo_io, :ipapi_com, :ipregistry, :ipstack, :postcodes_io, :uk_ordnance_survey_names].include? l # does not use query string
set_api_key!(l)
url = Geocoder::Lookup.get(l).query_url(Geocoder::Query.new(
"test", :params => {:one_in_the_hand => "two in the bush"}
))
assert_match(/one_in_the_hand=two\+in\+the\+bush/, url,
"Lookup #{l} does not appear to support arbitrary params in URL")
end
end
{
:esri => :l,
:bing => :key,
:geocoder_ca => :auth,
:google => :language,
:google_premier => :language,
:mapquest => :key,
:maxmind => :l,
:nominatim => :"accept-language",
:yandex => :lang
}.each do |l,p|
define_method "test_passing_param_to_#{l}_query_overrides_configuration_value" do
set_api_key!(l)
url = Geocoder::Lookup.get(l).query_url(Geocoder::Query.new(
"test", :params => {p => "xxxx"}
))
assert_match(/#{p}=xxxx/, url,
"Param passed to #{l} lookup does not override configuration value")
end
end
{
:bing => :language,
:google => :language,
:google_premier => :language,
:here => :language,
:nominatim => :"accept-language",
:yandex => :lang
}.each do |l,p|
define_method "test_passing_language_to_#{l}_query_overrides_configuration_value" do
set_api_key!(l)
url = Geocoder::Lookup.get(l).query_url(Geocoder::Query.new(
"test", :language => 'xxxx'
))
assert_match(/#{p}=xxxx/, url,
"Param passed to #{l} lookup does not override configuration value")
end
end
def test_raises_exception_on_invalid_key
Geocoder.configure(:always_raise => [Geocoder::InvalidApiKey])
#Geocoder::Lookup.all_services_except_test.each do |l|
[:bing, :yandex, :maxmind, :baidu, :baidu_ip, :amap].each do |l|
lookup = Geocoder::Lookup.get(l)
assert_raises Geocoder::InvalidApiKey do
lookup.send(:results, Geocoder::Query.new("invalid key"))
end
end
end
def test_returns_empty_array_on_invalid_key
silence_warnings do
#Geocoder::Lookup.all_services_except_test.each do |l|
[:bing, :yandex, :maxmind, :baidu, :baidu_ip, :amap].each do |l|
Geocoder.configure(:lookup => l)
set_api_key!(l)
assert_equal [], Geocoder.search("invalid key")
end
end
end
def test_does_not_choke_on_nil_address
Geocoder::Lookup.all_services.each do |l|
Geocoder.configure(:lookup => l)
assert_nothing_raised { Place.new("Place", nil).geocode }
end
end
def test_hash_to_query
g = Geocoder::Lookup::Google.new
assert_equal "a=1&b=2", g.send(:hash_to_query, {:a => 1, :b => 2})
end
def test_baidu_api_key
Geocoder.configure(:api_key => "MY_KEY")
g = Geocoder::Lookup::BaiduIp.new
assert_match "ak=MY_KEY", g.query_url(Geocoder::Query.new("232.65.123.94"))
end
def test_baidu_ip_api_key
Geocoder.configure(:api_key => "MY_KEY")
g = Geocoder::Lookup::Baidu.new
assert_match "ak=MY_KEY", g.query_url(Geocoder::Query.new("Madison Square Garden, New York, NY 10001, United States"))
end
def test_db_ip_com_api_key
Geocoder.configure(:api_key => "MY_KEY")
g = Geocoder::Lookup::DbIpCom.new
assert_match "\/MY_KEY\/", g.query_url(Geocoder::Query.new("232.65.123.94"))
end
def test_pointpin_api_key
Geocoder.configure(:api_key => "MY_KEY")
g = Geocoder::Lookup::Pointpin.new
assert_match "/MY_KEY/", g.query_url(Geocoder::Query.new("232.65.123.94"))
end
def test_google_api_key
Geocoder.configure(:api_key => "MY_KEY")
g = Geocoder::Lookup::Google.new
assert_match "key=MY_KEY", g.query_url(Geocoder::Query.new("Madison Square Garden, New York, NY 10001, United States"))
end
def test_geocoder_ca_showpostal
Geocoder.configure(:api_key => "MY_KEY")
g = Geocoder::Lookup::GeocoderCa.new
assert_match "showpostal=1", g.query_url(Geocoder::Query.new("Madison Square Garden, New York, NY 10001, United States"))
end
def test_telize_api_key
Geocoder.configure(:api_key => "MY_KEY")
g = Geocoder::Lookup::Telize.new
assert_match "rapidapi-key=MY_KEY", g.query_url(Geocoder::Query.new("232.65.123.94"))
end
def test_ipinfo_io_api_key
Geocoder.configure(:api_key => "MY_KEY")
g = Geocoder::Lookup::IpinfoIo.new
assert_match "token=MY_KEY", g.query_url(Geocoder::Query.new("232.65.123.94"))
end
def test_ipregistry_api_key
Geocoder.configure(:api_key => "MY_KEY")
g = Geocoder::Lookup::Ipregistry.new
assert_match "key=MY_KEY", g.query_url(Geocoder::Query.new("232.65.123.94"))
end
def test_amap_api_key
Geocoder.configure(:api_key => "MY_KEY")
g = Geocoder::Lookup::Amap.new
assert_match "key=MY_KEY", g.query_url(Geocoder::Query.new("202.198.16.3"))
end
def test_raises_configuration_error_on_missing_key
[:bing, :baidu, :amap].each do |l|
assert_raises Geocoder::ConfigurationError do
Geocoder.configure(:lookup => l, :api_key => nil)
Geocoder.search("Madison Square Garden, New York, NY 10001, United States")
end
end
end
def test_handle
assert_equal :google, Geocoder::Lookup::Google.new.handle
assert_equal :geocoder_ca, Geocoder::Lookup::GeocoderCa.new.handle
end
end