PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/test/services_test.rb

https://gitlab.com/meetly/geocoder
Ruby | 423 lines | 339 code | 70 blank | 14 comment | 0 complexity | cac9e303c4ea07932abc1b745a12a219 MD5 | raw file
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class ServicesTest < Test::Unit::TestCase
  4. # --- Google ---
  5. def test_google_result_components
  6. result = Geocoder.search("Madison Square Garden, New York, NY").first
  7. assert_equal "Manhattan",
  8. result.address_components_of_type(:sublocality).first['long_name']
  9. end
  10. def test_google_result_components_contains_route
  11. result = Geocoder.search("Madison Square Garden, New York, NY").first
  12. assert_equal "Penn Plaza",
  13. result.address_components_of_type(:route).first['long_name']
  14. end
  15. def test_google_result_components_contains_street_number
  16. result = Geocoder.search("Madison Square Garden, New York, NY").first
  17. assert_equal "4",
  18. result.address_components_of_type(:street_number).first['long_name']
  19. end
  20. def test_google_returns_city_when_no_locality_in_result
  21. result = Geocoder.search("no locality").first
  22. assert_equal "Haram", result.city
  23. end
  24. def test_google_city_results_returns_nil_if_no_matching_component_types
  25. result = Geocoder.search("no city data").first
  26. assert_equal nil, result.city
  27. end
  28. def test_google_street_address_returns_formatted_street_address
  29. result = Geocoder.search("Madison Square Garden, New York, NY").first
  30. assert_equal "4 Penn Plaza", result.street_address
  31. end
  32. def test_google_precision
  33. result = Geocoder.search("Madison Square Garden, New York, NY").first
  34. assert_equal "ROOFTOP",
  35. result.precision
  36. end
  37. def test_google_query_url_contains_bounds
  38. lookup = Geocoder::Lookup::Google.new
  39. url = lookup.query_url(Geocoder::Query.new(
  40. "Some Intersection",
  41. :bounds => [[40.0, -120.0], [39.0, -121.0]]
  42. ))
  43. assert_match /bounds=40.0+%2C-120.0+%7C39.0+%2C-121.0+/, url
  44. end
  45. def test_google_query_url_contains_region
  46. lookup = Geocoder::Lookup::Google.new
  47. url = lookup.query_url(Geocoder::Query.new(
  48. "Some Intersection",
  49. :region => "gb"
  50. ))
  51. assert_match /region=gb/, url
  52. end
  53. def test_google_query_url_contains_components_when_given_as_string
  54. lookup = Geocoder::Lookup::Google.new
  55. url = lookup.query_url(Geocoder::Query.new(
  56. "Some Intersection",
  57. :components => "locality:ES"
  58. ))
  59. formatted = "components=" + CGI.escape("locality:ES")
  60. assert url.include?(formatted), "Expected #{formatted} to be included in #{url}"
  61. end
  62. def test_google_query_url_contains_components_when_given_as_array
  63. lookup = Geocoder::Lookup::Google.new
  64. url = lookup.query_url(Geocoder::Query.new(
  65. "Some Intersection",
  66. :components => ["country:ES", "locality:ES"]
  67. ))
  68. formatted = "components=" + CGI.escape("country:ES|locality:ES")
  69. assert url.include?(formatted), "Expected #{formatted} to be included in #{url}"
  70. end
  71. # --- Google Premier ---
  72. def test_google_premier_result_components
  73. Geocoder.configure(:lookup => :google_premier)
  74. set_api_key!(:google_premier)
  75. result = Geocoder.search("Madison Square Garden, New York, NY").first
  76. assert_equal "Manhattan",
  77. result.address_components_of_type(:sublocality).first['long_name']
  78. end
  79. def test_google_premier_query_url
  80. Geocoder.configure(:api_key => ["deadbeef", "gme-test", "test-dev"])
  81. assert_equal "http://maps.googleapis.com/maps/api/geocode/json?address=Madison+Square+Garden%2C+New+York%2C+NY&channel=test-dev&client=gme-test&language=en&sensor=false&signature=doJvJqX7YJzgV9rJ0DnVkTGZqTg=",
  82. Geocoder::Lookup::GooglePremier.new.query_url(Geocoder::Query.new("Madison Square Garden, New York, NY"))
  83. end
  84. # --- DSTK (Data Science Toolkit) ---
  85. def test_dstk_result_components
  86. Geocoder.configure(:lookup => :dstk, :dstk => { :host => 'NOT_AN_ACTUAL_HOST' })
  87. result = Geocoder.search("Madison Square Garden, New York, NY").first
  88. assert_equal "Manhattan",
  89. result.address_components_of_type(:sublocality).first['long_name']
  90. end
  91. def test_dstk_query_url
  92. Geocoder.configure(:lookup => :dstk, :dstk => { :host => 'NOT_AN_ACTUAL_HOST' })
  93. assert_equal "http://NOT_AN_ACTUAL_HOST/maps/api/geocode/json?address=Madison+Square+Garden%2C+New+York%2C+NY&language=en&sensor=false",
  94. Geocoder::Lookup::Dstk.new.query_url(Geocoder::Query.new("Madison Square Garden, New York, NY"))
  95. end
  96. def test_dstk_default_query_url
  97. Geocoder.configure(:lookup => :dstk)
  98. assert_equal "http://www.datasciencetoolkit.org/maps/api/geocode/json?address=Madison+Square+Garden%2C+New+York%2C+NY&language=en&sensor=false",
  99. Geocoder::Lookup::Dstk.new.query_url(Geocoder::Query.new("Madison Square Garden, New York, NY"))
  100. end
  101. # --- Yahoo ---
  102. def test_yahoo_no_results
  103. Geocoder.configure(:lookup => :yahoo)
  104. set_api_key!(:yahoo)
  105. assert_equal [], Geocoder.search("no results")
  106. end
  107. def test_yahoo_error
  108. Geocoder.configure(:lookup => :yahoo)
  109. set_api_key!(:yahoo)
  110. # keep test output clean: suppress timeout warning
  111. orig = $VERBOSE; $VERBOSE = nil
  112. assert_equal [], Geocoder.search("error")
  113. ensure
  114. $VERBOSE = orig
  115. end
  116. def test_yahoo_result_components
  117. Geocoder.configure(:lookup => :yahoo)
  118. set_api_key!(:yahoo)
  119. result = Geocoder.search("madison square garden").first
  120. assert_equal "10001", result.postal_code
  121. end
  122. def test_yahoo_address_formatting
  123. Geocoder.configure(:lookup => :yahoo)
  124. set_api_key!(:yahoo)
  125. result = Geocoder.search("madison square garden").first
  126. assert_equal "Madison Square Garden, New York, NY 10001, United States", result.address
  127. end
  128. def test_yahoo_raises_exception_when_over_query_limit
  129. Geocoder.configure(:always_raise => [Geocoder::OverQueryLimitError])
  130. l = Geocoder::Lookup.get(:yahoo)
  131. assert_raises Geocoder::OverQueryLimitError do
  132. l.send(:results, Geocoder::Query.new("over limit"))
  133. end
  134. end
  135. # --- Geocoder.ca ---
  136. def test_geocoder_ca_result_components
  137. Geocoder.configure(:lookup => :geocoder_ca)
  138. set_api_key!(:geocoder_ca)
  139. result = Geocoder.search([45.423733, -75.676333]).first
  140. assert_equal "CA", result.country_code
  141. assert_equal "289 Somerset ST E, Ottawa, ON K1N6W1, Canada", result.address
  142. end
  143. # --- FreeGeoIp ---
  144. def test_freegeoip_result_on_ip_address_search
  145. result = Geocoder.search("74.200.247.59").first
  146. assert result.is_a?(Geocoder::Result::Freegeoip)
  147. end
  148. def test_freegeoip_result_components
  149. result = Geocoder.search("74.200.247.59").first
  150. assert_equal "Plano, TX 75093, United States", result.address
  151. end
  152. def test_freegeoip_host_config
  153. Geocoder.configure(:lookup => :freegeoip, :freegeoip => {:host => "local.com"})
  154. lookup = Geocoder::Lookup::Freegeoip.new
  155. query = Geocoder::Query.new("24.24.24.23")
  156. assert_match %r(http://local\.com), lookup.query_url(query)
  157. end
  158. # --- MaxMind ---
  159. def test_maxmind_result_on_ip_address_search
  160. Geocoder.configure(:ip_lookup => :maxmind, :maxmind => {:service => :city_isp_org})
  161. result = Geocoder.search("74.200.247.59").first
  162. assert result.is_a?(Geocoder::Result::Maxmind)
  163. end
  164. def test_maxmind_result_knows_country_service_name
  165. Geocoder.configure(:ip_lookup => :maxmind, :maxmind => {:service => :country})
  166. assert_equal :country, Geocoder.search("24.24.24.21").first.service_name
  167. end
  168. def test_maxmind_result_knows_city_service_name
  169. Geocoder.configure(:ip_lookup => :maxmind, :maxmind => {:service => :city})
  170. assert_equal :city, Geocoder.search("24.24.24.22").first.service_name
  171. end
  172. def test_maxmind_result_knows_city_isp_org_service_name
  173. Geocoder.configure(:ip_lookup => :maxmind, :maxmind => {:service => :city_isp_org})
  174. assert_equal :city_isp_org, Geocoder.search("24.24.24.23").first.service_name
  175. end
  176. def test_maxmind_result_knows_omni_service_name
  177. Geocoder.configure(:ip_lookup => :maxmind, :maxmind => {:service => :omni})
  178. assert_equal :omni, Geocoder.search("24.24.24.24").first.service_name
  179. end
  180. def test_maxmind_special_result_components
  181. Geocoder.configure(:ip_lookup => :maxmind, :maxmind => {:service => :omni})
  182. result = Geocoder.search("24.24.24.24").first
  183. assert_equal "Road Runner", result.isp_name
  184. assert_equal "Cable/DSL", result.netspeed
  185. assert_equal "rr.com", result.domain
  186. end
  187. def test_maxmind_raises_exception_when_service_not_configured
  188. Geocoder.configure(:ip_lookup => :maxmind)
  189. Geocoder.configure(:maxmind => {:service => nil})
  190. assert_raises Geocoder::ConfigurationError do
  191. Geocoder::Query.new("24.24.24.24").url
  192. end
  193. end
  194. def test_maxmind_works_when_loopback_address_on_omni
  195. Geocoder.configure(:ip_lookup => :maxmind, :maxmind => { :service => :omni })
  196. result = Geocoder.search("127.0.0.1").first
  197. assert_equal "", result.country_code
  198. end
  199. def test_maxmind_works_when_loopback_address_on_country
  200. Geocoder.configure(:ip_lookup => :maxmind, :maxmind => { :service => :country })
  201. result = Geocoder.search("127.0.0.1").first
  202. assert_equal "", result.country_code
  203. end
  204. # --- Bing ---
  205. def test_bing_result_components
  206. Geocoder.configure(:lookup => :bing)
  207. set_api_key!(:bing)
  208. result = Geocoder.search("Madison Square Garden, New York, NY").first
  209. assert_equal "Madison Square Garden, NY", result.address
  210. assert_equal "NY", result.state
  211. assert_equal "New York", result.city
  212. end
  213. def test_bing_no_results
  214. Geocoder.configure(:lookup => :bing)
  215. set_api_key!(:bing)
  216. results = Geocoder.search("no results")
  217. assert_equal 0, results.length
  218. end
  219. # --- Nominatim ---
  220. def test_nominatim_result_components
  221. Geocoder.configure(:lookup => :nominatim)
  222. set_api_key!(:nominatim)
  223. result = Geocoder.search("Madison Square Garden, New York, NY").first
  224. assert_equal "10001", result.postal_code
  225. end
  226. def test_nominatim_address_formatting
  227. Geocoder.configure(:lookup => :nominatim)
  228. set_api_key!(:nominatim)
  229. result = Geocoder.search("Madison Square Garden, New York, NY").first
  230. assert_equal "Madison Square Garden, West 31st Street, Long Island City, New York City, New York, 10001, United States of America",
  231. result.address
  232. end
  233. def test_nominatim_host_config
  234. Geocoder.configure(:lookup => :nominatim, :nominatim => {:host => "local.com"})
  235. lookup = Geocoder::Lookup::Nominatim.new
  236. query = Geocoder::Query.new("Bluffton, SC")
  237. assert_match %r(http://local\.com), lookup.query_url(query)
  238. end
  239. # --- MapQuest ---
  240. def test_api_route
  241. Geocoder.configure(:lookup => :mapquest, :api_key => "abc123")
  242. lookup = Geocoder::Lookup::Mapquest.new
  243. query = Geocoder::Query.new("Bluffton, SC")
  244. res = lookup.query_url(query)
  245. assert_equal "http://open.mapquestapi.com/geocoding/v1/address?key=abc123&location=Bluffton%2C+SC",
  246. res
  247. end
  248. def test_api_route_licensed
  249. Geocoder.configure(:lookup => :mapquest, :api_key => "abc123", :mapquest => {:licensed => true, :version => 2})
  250. lookup = Geocoder::Lookup::Mapquest.new
  251. query = Geocoder::Query.new("Bluffton, SC")
  252. res = lookup.query_url(query)
  253. assert_equal "http://www.mapquestapi.com/geocoding/v2/address?key=abc123&location=Bluffton%2C+SC",
  254. res
  255. end
  256. def test_mapquest_result_components
  257. Geocoder.configure(:lookup => :mapquest)
  258. set_api_key!(:mapquest)
  259. result = Geocoder.search("Madison Square Garden, New York, NY").first
  260. assert_equal "10001", result.postal_code
  261. end
  262. def test_mapquest_address_formatting
  263. Geocoder.configure(:lookup => :mapquest)
  264. set_api_key!(:mapquest)
  265. result = Geocoder.search("Madison Square Garden, New York, NY").first
  266. assert_equal "46 West 31st Street, New York, NY, 10001, US",
  267. result.address
  268. end
  269. def test_mapquest_no_results
  270. Geocoder.configure(:lookup => :mapquest)
  271. set_api_key!(:mapquest)
  272. assert_equal [], Geocoder.search("no results")
  273. end
  274. def test_mapquest_raises_exception_when_invalid_request
  275. Geocoder.configure(:always_raise => [Geocoder::InvalidRequest])
  276. l = Geocoder::Lookup.get(:mapquest)
  277. assert_raises Geocoder::InvalidRequest do
  278. l.send(:results, Geocoder::Query.new("invalid request"))
  279. end
  280. end
  281. def test_mapquest_raises_exception_when_invalid_api_key
  282. Geocoder.configure(:always_raise => [Geocoder::InvalidApiKey])
  283. l = Geocoder::Lookup.get(:mapquest)
  284. assert_raises Geocoder::InvalidApiKey do
  285. l.send(:results, Geocoder::Query.new("invalid api key"))
  286. end
  287. end
  288. def test_mapquest_raises_exception_when_error
  289. Geocoder.configure(:always_raise => [Geocoder::Error])
  290. l = Geocoder::Lookup.get(:mapquest)
  291. assert_raises Geocoder::Error do
  292. l.send(:results, Geocoder::Query.new("error"))
  293. end
  294. end
  295. # --- Esri ---
  296. def test_esri_query_for_geocode
  297. query = Geocoder::Query.new("Bluffton, SC")
  298. lookup = Geocoder::Lookup.get(:esri)
  299. res = lookup.query_url(query)
  300. assert_equal "http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/find?f=pjson&outFields=%2A&text=Bluffton%2C+SC",
  301. res
  302. end
  303. def test_esri_query_for_reverse_geocode
  304. query = Geocoder::Query.new([45.423733, -75.676333])
  305. lookup = Geocoder::Lookup.get(:esri)
  306. res = lookup.query_url(query)
  307. assert_equal "http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/reverseGeocode?f=pjson&location=-75.676333%2C45.423733&outFields=%2A",
  308. res
  309. end
  310. def test_esri_results_component
  311. Geocoder.configure(:lookup => :esri)
  312. result = Geocoder.search("Madison Square Garden, New York, NY").first
  313. assert_equal "10001", result.postal_code
  314. assert_equal "USA", result.country
  315. assert_equal "Madison Square Garden", result.address
  316. assert_equal "New York", result.city
  317. assert_equal "New York", result.state
  318. assert_equal 40.75004981300049, result.coordinates[0]
  319. assert_equal -73.99423889799965, result.coordinates[1]
  320. end
  321. def test_esri_results_component_when_reverse_geocoding
  322. Geocoder.configure(:lookup => :esri)
  323. result = Geocoder.search([45.423733, -75.676333]).first
  324. assert_equal "75007", result.postal_code
  325. assert_equal "FRA", result.country
  326. assert_equal "4 Avenue Gustave Eiffel", result.address
  327. assert_equal "Paris", result.city
  328. assert_equal "Île-de-France", result.state
  329. assert_equal 48.858129997357558, result.coordinates[0]
  330. assert_equal 2.2956200048981574, result.coordinates[1]
  331. end
  332. # --- Geocodio ---
  333. def test_geocodio_result_components
  334. Geocoder.configure(:lookup => :geocodio)
  335. set_api_key!(:geocodio)
  336. result = Geocoder.search("1101 Pennsylvania Ave NW, Washington DC").first
  337. assert_equal 1.0, result.accuracy
  338. assert_equal "1101", result.number
  339. assert_equal "Ave", result.suffix
  340. assert_equal "DC", result.state
  341. assert_equal "20004", result.zip
  342. assert_equal "NW", result.postdirectional
  343. assert_equal "Washington", result.city
  344. assert_equal "1101 Pennsylvania Ave NW, Washington DC, 20004", result.formatted_address
  345. assert_equal({ "lat" => 38.895019, "lng" => -77.028095 }, result.location)
  346. end
  347. def test_geocodio_no_results
  348. Geocoder.configure(:lookup => :geocodio)
  349. set_api_key!(:geocodio)
  350. results = Geocoder.search("no results")
  351. assert_equal 0, results.length
  352. end
  353. end