From 4e25ac281183f9c379a5824b60603fadc447e57e Mon Sep 17 00:00:00 2001 From: Alex Reisner <alex@alexreisner.com> Date: Fri, 7 Feb 2014 20:14:48 -0500 Subject: [PATCH] Restructure and clean up lookup tests. --- Rakefile | 2 +- test/services_test.rb | 468 ------------------ test/{ => unit}/active_record_test.rb | 0 test/{ => unit}/cache_test.rb | 0 test/{ => unit}/calculations_test.rb | 0 test/{ => unit}/configuration_test.rb | 0 test/{ => unit}/custom_block_test.rb | 0 test/{ => unit}/error_handling_test.rb | 0 test/{ => unit}/geocoder_test.rb | 0 test/{ => unit}/https_test.rb | 0 test/{ => unit}/ip_address_test.rb | 0 test/{ => unit}/lookup_test.rb | 0 test/unit/lookups/bing_test.rb | 67 +++ test/unit/lookups/dstk_test.rb | 25 + test/unit/lookups/esri_test.rb | 47 ++ test/unit/lookups/freegeoip_test.rb | 26 + test/unit/lookups/geocoder_ca_test.rb | 16 + test/unit/lookups/geocodio_test.rb | 28 ++ test/unit/lookups/google_premier_test.rb | 21 + test/unit/lookups/google_test.rb | 83 ++++ test/unit/lookups/mapquest_test.rb | 55 ++ test/{ => unit/lookups}/maxmind_local_test.rb | 19 +- test/unit/lookups/maxmind_test.rb | 62 +++ test/unit/lookups/nominatim_test.rb | 23 + test/unit/lookups/yahoo_test.rb | 36 ++ test/{ => unit}/method_aliases_test.rb | 0 test/{ => unit}/mongoid_test.rb | 0 test/{ => unit}/near_test.rb | 0 test/{ => unit}/oauth_util_test.rb | 0 test/{ => unit}/proxy_test.rb | 0 test/{ => unit}/query_test.rb | 0 test/{ => unit}/request_test.rb | 0 test/{ => unit}/result_test.rb | 0 test/{ => unit}/test_mode_test.rb | 0 34 files changed, 499 insertions(+), 479 deletions(-) delete mode 100644 test/services_test.rb rename test/{ => unit}/active_record_test.rb (100%) rename test/{ => unit}/cache_test.rb (100%) rename test/{ => unit}/calculations_test.rb (100%) rename test/{ => unit}/configuration_test.rb (100%) rename test/{ => unit}/custom_block_test.rb (100%) rename test/{ => unit}/error_handling_test.rb (100%) rename test/{ => unit}/geocoder_test.rb (100%) rename test/{ => unit}/https_test.rb (100%) rename test/{ => unit}/ip_address_test.rb (100%) rename test/{ => unit}/lookup_test.rb (100%) create mode 100644 test/unit/lookups/bing_test.rb create mode 100644 test/unit/lookups/dstk_test.rb create mode 100644 test/unit/lookups/esri_test.rb create mode 100644 test/unit/lookups/freegeoip_test.rb create mode 100644 test/unit/lookups/geocoder_ca_test.rb create mode 100644 test/unit/lookups/geocodio_test.rb create mode 100644 test/unit/lookups/google_premier_test.rb create mode 100644 test/unit/lookups/google_test.rb create mode 100644 test/unit/lookups/mapquest_test.rb rename test/{ => unit/lookups}/maxmind_local_test.rb (57%) create mode 100644 test/unit/lookups/maxmind_test.rb create mode 100644 test/unit/lookups/nominatim_test.rb create mode 100644 test/unit/lookups/yahoo_test.rb rename test/{ => unit}/method_aliases_test.rb (100%) rename test/{ => unit}/mongoid_test.rb (100%) rename test/{ => unit}/near_test.rb (100%) rename test/{ => unit}/oauth_util_test.rb (100%) rename test/{ => unit}/proxy_test.rb (100%) rename test/{ => unit}/query_test.rb (100%) rename test/{ => unit}/request_test.rb (100%) rename test/{ => unit}/result_test.rb (100%) rename test/{ => unit}/test_mode_test.rb (100%) diff --git a/Rakefile b/Rakefile index 55ce7fe7..f06367dc 100644 --- a/Rakefile +++ b/Rakefile @@ -4,7 +4,7 @@ Bundler::GemHelper.install_tasks require 'rake/testtask' Rake::TestTask.new(:test) do |test| test.libs << 'lib' << 'test' - test.pattern = 'test/*_test.rb' + test.pattern = 'test/unit/**/*_test.rb' test.verbose = true end diff --git a/test/services_test.rb b/test/services_test.rb deleted file mode 100644 index 04b7d010..00000000 --- a/test/services_test.rb +++ /dev/null @@ -1,468 +0,0 @@ -# encoding: utf-8 -require 'test_helper' - -class ServicesTest < Test::Unit::TestCase - - # --- Google --- - - def test_google_result_components - result = Geocoder.search("Madison Square Garden, New York, NY").first - assert_equal "Manhattan", - result.address_components_of_type(:sublocality).first['long_name'] - end - - def test_google_result_components_contains_route - result = Geocoder.search("Madison Square Garden, New York, NY").first - assert_equal "Penn Plaza", - result.address_components_of_type(:route).first['long_name'] - end - - def test_google_result_components_contains_street_number - result = Geocoder.search("Madison Square Garden, New York, NY").first - assert_equal "4", - result.address_components_of_type(:street_number).first['long_name'] - end - - def test_google_returns_city_when_no_locality_in_result - result = Geocoder.search("no locality").first - assert_equal "Haram", result.city - end - - def test_google_city_results_returns_nil_if_no_matching_component_types - result = Geocoder.search("no city data").first - assert_equal nil, result.city - end - - def test_google_street_address_returns_formatted_street_address - result = Geocoder.search("Madison Square Garden, New York, NY").first - assert_equal "4 Penn Plaza", result.street_address - end - - def test_google_precision - result = Geocoder.search("Madison Square Garden, New York, NY").first - assert_equal "ROOFTOP", - result.precision - end - - def test_google_query_url_contains_bounds - lookup = Geocoder::Lookup::Google.new - url = lookup.query_url(Geocoder::Query.new( - "Some Intersection", - :bounds => [[40.0, -120.0], [39.0, -121.0]] - )) - assert_match /bounds=40.0+%2C-120.0+%7C39.0+%2C-121.0+/, url - end - - def test_google_query_url_contains_region - lookup = Geocoder::Lookup::Google.new - url = lookup.query_url(Geocoder::Query.new( - "Some Intersection", - :region => "gb" - )) - assert_match /region=gb/, url - end - - def test_google_query_url_contains_components_when_given_as_string - lookup = Geocoder::Lookup::Google.new - url = lookup.query_url(Geocoder::Query.new( - "Some Intersection", - :components => "locality:ES" - )) - formatted = "components=" + CGI.escape("locality:ES") - assert url.include?(formatted), "Expected #{formatted} to be included in #{url}" - end - - def test_google_query_url_contains_components_when_given_as_array - lookup = Geocoder::Lookup::Google.new - url = lookup.query_url(Geocoder::Query.new( - "Some Intersection", - :components => ["country:ES", "locality:ES"] - )) - formatted = "components=" + CGI.escape("country:ES|locality:ES") - assert url.include?(formatted), "Expected #{formatted} to be included in #{url}" - end - - # --- Google Premier --- - - def test_google_premier_result_components - Geocoder.configure(:lookup => :google_premier) - set_api_key!(:google_premier) - result = Geocoder.search("Madison Square Garden, New York, NY").first - assert_equal "Manhattan", - result.address_components_of_type(:sublocality).first['long_name'] - end - - def test_google_premier_query_url - Geocoder.configure(:api_key => ["deadbeef", "gme-test", "test-dev"]) - 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=", - Geocoder::Lookup::GooglePremier.new.query_url(Geocoder::Query.new("Madison Square Garden, New York, NY")) - end - - # --- DSTK (Data Science Toolkit) --- - - def test_dstk_result_components - Geocoder.configure(:lookup => :dstk, :dstk => { :host => 'NOT_AN_ACTUAL_HOST' }) - result = Geocoder.search("Madison Square Garden, New York, NY").first - assert_equal "Manhattan", - result.address_components_of_type(:sublocality).first['long_name'] - end - - def test_dstk_query_url - Geocoder.configure(:lookup => :dstk, :dstk => { :host => 'NOT_AN_ACTUAL_HOST' }) - assert_equal "http://NOT_AN_ACTUAL_HOST/maps/api/geocode/json?address=Madison+Square+Garden%2C+New+York%2C+NY&language=en&sensor=false", - Geocoder::Lookup::Dstk.new.query_url(Geocoder::Query.new("Madison Square Garden, New York, NY")) - end - - def test_dstk_default_query_url - Geocoder.configure(:lookup => :dstk) - assert_equal "http://www.datasciencetoolkit.org/maps/api/geocode/json?address=Madison+Square+Garden%2C+New+York%2C+NY&language=en&sensor=false", - Geocoder::Lookup::Dstk.new.query_url(Geocoder::Query.new("Madison Square Garden, New York, NY")) - end - - # --- Yahoo --- - - def test_yahoo_no_results - Geocoder.configure(:lookup => :yahoo) - set_api_key!(:yahoo) - assert_equal [], Geocoder.search("no results") - end - - def test_yahoo_error - Geocoder.configure(:lookup => :yahoo) - set_api_key!(:yahoo) - # keep test output clean: suppress timeout warning - orig = $VERBOSE; $VERBOSE = nil - assert_equal [], Geocoder.search("error") - ensure - $VERBOSE = orig - end - - def test_yahoo_result_components - Geocoder.configure(:lookup => :yahoo) - set_api_key!(:yahoo) - result = Geocoder.search("madison square garden").first - assert_equal "10001", result.postal_code - end - - def test_yahoo_address_formatting - Geocoder.configure(:lookup => :yahoo) - set_api_key!(:yahoo) - result = Geocoder.search("madison square garden").first - assert_equal "Madison Square Garden, New York, NY 10001, United States", result.address - end - - def test_yahoo_raises_exception_when_over_query_limit - Geocoder.configure(:always_raise => [Geocoder::OverQueryLimitError]) - l = Geocoder::Lookup.get(:yahoo) - assert_raises Geocoder::OverQueryLimitError do - l.send(:results, Geocoder::Query.new("over limit")) - end - end - - # --- Geocoder.ca --- - - def test_geocoder_ca_result_components - Geocoder.configure(:lookup => :geocoder_ca) - set_api_key!(:geocoder_ca) - result = Geocoder.search([45.423733, -75.676333]).first - assert_equal "CA", result.country_code - assert_equal "289 Somerset ST E, Ottawa, ON K1N6W1, Canada", result.address - end - - - # --- FreeGeoIp --- - - def test_freegeoip_result_on_ip_address_search - result = Geocoder.search("74.200.247.59").first - assert result.is_a?(Geocoder::Result::Freegeoip) - end - - def test_freegeoip_result_components - result = Geocoder.search("74.200.247.59").first - assert_equal "Plano, TX 75093, United States", result.address - end - - def test_freegeoip_host_config - Geocoder.configure(:lookup => :freegeoip, :freegeoip => {:host => "local.com"}) - lookup = Geocoder::Lookup::Freegeoip.new - query = Geocoder::Query.new("24.24.24.23") - assert_match %r(http://local\.com), lookup.query_url(query) - end - - # --- MaxMind --- - - def test_maxmind_result_on_ip_address_search - Geocoder.configure(:ip_lookup => :maxmind, :maxmind => {:service => :city_isp_org}) - result = Geocoder.search("74.200.247.59").first - assert result.is_a?(Geocoder::Result::Maxmind) - end - - def test_maxmind_result_knows_country_service_name - Geocoder.configure(:ip_lookup => :maxmind, :maxmind => {:service => :country}) - assert_equal :country, Geocoder.search("24.24.24.21").first.service_name - end - - def test_maxmind_result_knows_city_service_name - Geocoder.configure(:ip_lookup => :maxmind, :maxmind => {:service => :city}) - assert_equal :city, Geocoder.search("24.24.24.22").first.service_name - end - - def test_maxmind_result_knows_city_isp_org_service_name - Geocoder.configure(:ip_lookup => :maxmind, :maxmind => {:service => :city_isp_org}) - assert_equal :city_isp_org, Geocoder.search("24.24.24.23").first.service_name - end - - def test_maxmind_result_knows_omni_service_name - Geocoder.configure(:ip_lookup => :maxmind, :maxmind => {:service => :omni}) - assert_equal :omni, Geocoder.search("24.24.24.24").first.service_name - end - - def test_maxmind_special_result_components - Geocoder.configure(:ip_lookup => :maxmind, :maxmind => {:service => :omni}) - result = Geocoder.search("24.24.24.24").first - assert_equal "Road Runner", result.isp_name - assert_equal "Cable/DSL", result.netspeed - assert_equal "rr.com", result.domain - end - - def test_maxmind_raises_exception_when_service_not_configured - Geocoder.configure(:ip_lookup => :maxmind) - Geocoder.configure(:maxmind => {:service => nil}) - assert_raises Geocoder::ConfigurationError do - Geocoder::Query.new("24.24.24.24").url - end - end - - def test_maxmind_works_when_loopback_address_on_omni - Geocoder.configure(:ip_lookup => :maxmind, :maxmind => { :service => :omni }) - result = Geocoder.search("127.0.0.1").first - assert_equal "", result.country_code - end - - def test_maxmind_works_when_loopback_address_on_country - Geocoder.configure(:ip_lookup => :maxmind, :maxmind => { :service => :country }) - result = Geocoder.search("127.0.0.1").first - assert_equal "", result.country_code - end - - - # --- Bing --- - - def test_bing_query_for_reverse_geocode - lookup = Geocoder::Lookup::Bing.new - url = lookup.query_url(Geocoder::Query.new([45.423733, -75.676333])) - assert_match /Locations\/45.423733/, url - end - - def test_bing_result_components - Geocoder.configure(:lookup => :bing) - set_api_key!(:bing) - result = Geocoder.search("Madison Square Garden, New York, NY").first - assert_equal "Madison Square Garden, NY", result.address - assert_equal "NY", result.state - assert_equal "New York", result.city - end - - def test_bing_no_results - Geocoder.configure(:lookup => :bing) - set_api_key!(:bing) - results = Geocoder.search("no results") - assert_equal 0, results.length - end - - def test_bing_query_url_contains_region - lookup = Geocoder::Lookup::Bing.new - url = lookup.query_url(Geocoder::Query.new( - "manchester", - :region => "uk" - )) - assert_match /Locations\/uk\/manchester/, url - assert_no_match /query/, url - end - - def test_bing_query_url_without_region - lookup = Geocoder::Lookup::Bing.new - url = lookup.query_url(Geocoder::Query.new( - "manchester" - )) - assert_match /Locations\/manchester/, url - assert_no_match /query/, url - end - - def test_bing_query_url_contains_address_with_spaces - lookup = Geocoder::Lookup::Bing.new - url = lookup.query_url(Geocoder::Query.new( - "manchester, lancashire", - :region => "uk" - )) - assert_match /Locations\/uk\/manchester,%20lancashire/, url - assert_no_match /query/, url - end - - def test_bing_query_url_contains_address_with_trailing_and_leading_spaces - lookup = Geocoder::Lookup::Bing.new - url = lookup.query_url(Geocoder::Query.new( - " manchester, lancashire ", - :region => "uk" - )) - assert_match /Locations\/uk\/manchester,%20lancashire/, url - assert_no_match /query/, url - end - - # --- Nominatim --- - - def test_nominatim_result_components - Geocoder.configure(:lookup => :nominatim) - set_api_key!(:nominatim) - result = Geocoder.search("Madison Square Garden, New York, NY").first - assert_equal "10001", result.postal_code - end - - def test_nominatim_address_formatting - Geocoder.configure(:lookup => :nominatim) - set_api_key!(:nominatim) - result = Geocoder.search("Madison Square Garden, New York, NY").first - assert_equal "Madison Square Garden, West 31st Street, Long Island City, New York City, New York, 10001, United States of America", - result.address - end - - def test_nominatim_host_config - Geocoder.configure(:lookup => :nominatim, :nominatim => {:host => "local.com"}) - lookup = Geocoder::Lookup::Nominatim.new - query = Geocoder::Query.new("Bluffton, SC") - assert_match %r(http://local\.com), lookup.query_url(query) - end - - # --- MapQuest --- - - def test_api_route - Geocoder.configure(:lookup => :mapquest, :api_key => "abc123") - lookup = Geocoder::Lookup::Mapquest.new - query = Geocoder::Query.new("Bluffton, SC") - res = lookup.query_url(query) - assert_equal "http://open.mapquestapi.com/geocoding/v1/address?key=abc123&location=Bluffton%2C+SC", - res - end - - def test_api_route_licensed - Geocoder.configure(:lookup => :mapquest, :api_key => "abc123", :mapquest => {:licensed => true, :version => 2}) - lookup = Geocoder::Lookup::Mapquest.new - query = Geocoder::Query.new("Bluffton, SC") - res = lookup.query_url(query) - assert_equal "http://www.mapquestapi.com/geocoding/v2/address?key=abc123&location=Bluffton%2C+SC", - res - end - - def test_mapquest_result_components - Geocoder.configure(:lookup => :mapquest) - set_api_key!(:mapquest) - result = Geocoder.search("Madison Square Garden, New York, NY").first - assert_equal "10001", result.postal_code - end - - def test_mapquest_address_formatting - Geocoder.configure(:lookup => :mapquest) - set_api_key!(:mapquest) - result = Geocoder.search("Madison Square Garden, New York, NY").first - assert_equal "46 West 31st Street, New York, NY, 10001, US", - result.address - end - - def test_mapquest_no_results - Geocoder.configure(:lookup => :mapquest) - set_api_key!(:mapquest) - assert_equal [], Geocoder.search("no results") - end - - def test_mapquest_raises_exception_when_invalid_request - Geocoder.configure(:always_raise => [Geocoder::InvalidRequest]) - l = Geocoder::Lookup.get(:mapquest) - assert_raises Geocoder::InvalidRequest do - l.send(:results, Geocoder::Query.new("invalid request")) - end - end - - def test_mapquest_raises_exception_when_invalid_api_key - Geocoder.configure(:always_raise => [Geocoder::InvalidApiKey]) - l = Geocoder::Lookup.get(:mapquest) - assert_raises Geocoder::InvalidApiKey do - l.send(:results, Geocoder::Query.new("invalid api key")) - end - end - - def test_mapquest_raises_exception_when_error - Geocoder.configure(:always_raise => [Geocoder::Error]) - l = Geocoder::Lookup.get(:mapquest) - assert_raises Geocoder::Error do - l.send(:results, Geocoder::Query.new("error")) - end - end - - - - # --- Esri --- - - def test_esri_query_for_geocode - query = Geocoder::Query.new("Bluffton, SC") - lookup = Geocoder::Lookup.get(:esri) - res = lookup.query_url(query) - assert_equal "http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/find?f=pjson&outFields=%2A&text=Bluffton%2C+SC", - res - end - - def test_esri_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://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/reverseGeocode?f=pjson&location=-75.676333%2C45.423733&outFields=%2A", - res - end - - def test_esri_results_component - Geocoder.configure(:lookup => :esri) - 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 40.75004981300049, result.coordinates[0] - assert_equal -73.99423889799965, result.coordinates[1] - end - - def test_esri_results_component_when_reverse_geocoding - Geocoder.configure(:lookup => :esri) - 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 48.858129997357558, result.coordinates[0] - assert_equal 2.2956200048981574, result.coordinates[1] - end - - # --- Geocodio --- - - def test_geocodio_result_components - Geocoder.configure(:lookup => :geocodio) - set_api_key!(:geocodio) - result = Geocoder.search("1101 Pennsylvania Ave NW, Washington DC").first - assert_equal 1.0, result.accuracy - assert_equal "1101", result.number - assert_equal "Ave", result.suffix - assert_equal "DC", result.state - assert_equal "20004", result.zip - assert_equal "NW", result.postdirectional - assert_equal "Washington", result.city - assert_equal "1101 Pennsylvania Ave NW, Washington DC, 20004", result.formatted_address - assert_equal({ "lat" => 38.895019, "lng" => -77.028095 }, result.location) - end - - def test_geocodio_no_results - Geocoder.configure(:lookup => :geocodio) - set_api_key!(:geocodio) - results = Geocoder.search("no results") - assert_equal 0, results.length - end -end diff --git a/test/active_record_test.rb b/test/unit/active_record_test.rb similarity index 100% rename from test/active_record_test.rb rename to test/unit/active_record_test.rb diff --git a/test/cache_test.rb b/test/unit/cache_test.rb similarity index 100% rename from test/cache_test.rb rename to test/unit/cache_test.rb diff --git a/test/calculations_test.rb b/test/unit/calculations_test.rb similarity index 100% rename from test/calculations_test.rb rename to test/unit/calculations_test.rb diff --git a/test/configuration_test.rb b/test/unit/configuration_test.rb similarity index 100% rename from test/configuration_test.rb rename to test/unit/configuration_test.rb diff --git a/test/custom_block_test.rb b/test/unit/custom_block_test.rb similarity index 100% rename from test/custom_block_test.rb rename to test/unit/custom_block_test.rb diff --git a/test/error_handling_test.rb b/test/unit/error_handling_test.rb similarity index 100% rename from test/error_handling_test.rb rename to test/unit/error_handling_test.rb diff --git a/test/geocoder_test.rb b/test/unit/geocoder_test.rb similarity index 100% rename from test/geocoder_test.rb rename to test/unit/geocoder_test.rb diff --git a/test/https_test.rb b/test/unit/https_test.rb similarity index 100% rename from test/https_test.rb rename to test/unit/https_test.rb diff --git a/test/ip_address_test.rb b/test/unit/ip_address_test.rb similarity index 100% rename from test/ip_address_test.rb rename to test/unit/ip_address_test.rb diff --git a/test/lookup_test.rb b/test/unit/lookup_test.rb similarity index 100% rename from test/lookup_test.rb rename to test/unit/lookup_test.rb diff --git a/test/unit/lookups/bing_test.rb b/test/unit/lookups/bing_test.rb new file mode 100644 index 00000000..7e886ae8 --- /dev/null +++ b/test/unit/lookups/bing_test.rb @@ -0,0 +1,67 @@ +# encoding: utf-8 +require 'test_helper' + +class BingTest < Test::Unit::TestCase + + def setup + Geocoder.configure(lookup: :bing) + set_api_key!(:bing) + end + + def test_query_for_reverse_geocode + lookup = Geocoder::Lookup::Bing.new + url = lookup.query_url(Geocoder::Query.new([45.423733, -75.676333])) + assert_match /Locations\/45.423733/, url + end + + def test_result_components + result = Geocoder.search("Madison Square Garden, New York, NY").first + assert_equal "Madison Square Garden, NY", result.address + assert_equal "NY", result.state + assert_equal "New York", result.city + end + + def test_no_results + results = Geocoder.search("no results") + assert_equal 0, results.length + end + + def test_query_url_contains_region + lookup = Geocoder::Lookup::Bing.new + url = lookup.query_url(Geocoder::Query.new( + "manchester", + :region => "uk" + )) + assert_match /Locations\/uk\/manchester/, url + assert_no_match /query/, url + end + + def test_query_url_without_region + lookup = Geocoder::Lookup::Bing.new + url = lookup.query_url(Geocoder::Query.new( + "manchester" + )) + assert_match /Locations\/manchester/, url + assert_no_match /query/, url + end + + def test_query_url_contains_address_with_spaces + lookup = Geocoder::Lookup::Bing.new + url = lookup.query_url(Geocoder::Query.new( + "manchester, lancashire", + :region => "uk" + )) + assert_match /Locations\/uk\/manchester,%20lancashire/, url + assert_no_match /query/, url + end + + def test_query_url_contains_address_with_trailing_and_leading_spaces + lookup = Geocoder::Lookup::Bing.new + url = lookup.query_url(Geocoder::Query.new( + " manchester, lancashire ", + :region => "uk" + )) + assert_match /Locations\/uk\/manchester,%20lancashire/, url + assert_no_match /query/, url + end +end diff --git a/test/unit/lookups/dstk_test.rb b/test/unit/lookups/dstk_test.rb new file mode 100644 index 00000000..e05cf32b --- /dev/null +++ b/test/unit/lookups/dstk_test.rb @@ -0,0 +1,25 @@ +# encoding: utf-8 +require 'test_helper' + +class DstkTest < Test::Unit::TestCase + + def setup + Geocoder.configure(lookup: :dstk) + end + + def test_dstk_result_components + result = Geocoder.search("Madison Square Garden, New York, NY").first + assert_equal "Manhattan", result.address_components_of_type(:sublocality).first['long_name'] + end + + def test_dstk_query_url + query = Geocoder::Query.new("Madison Square Garden, New York, NY") + assert_equal "http://www.datasciencetoolkit.org/maps/api/geocode/json?address=Madison+Square+Garden%2C+New+York%2C+NY&language=en&sensor=false", query.url + end + + def test_dstk_query_url_with_custom_host + Geocoder.configure(dstk: {host: 'NOT_AN_ACTUAL_HOST'}) + query = Geocoder::Query.new("Madison Square Garden, New York, NY") + assert_equal "http://NOT_AN_ACTUAL_HOST/maps/api/geocode/json?address=Madison+Square+Garden%2C+New+York%2C+NY&language=en&sensor=false", query.url + end +end diff --git a/test/unit/lookups/esri_test.rb b/test/unit/lookups/esri_test.rb new file mode 100644 index 00000000..da92592e --- /dev/null +++ b/test/unit/lookups/esri_test.rb @@ -0,0 +1,47 @@ +# encoding: utf-8 +require 'test_helper' + +class EsriTest < Test::Unit::TestCase + + 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://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/find?f=pjson&outFields=%2A&text=Bluffton%2C+SC", + res + 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://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 40.75004981300049, result.coordinates[0] + assert_equal -73.99423889799965, 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 48.858129997357558, result.coordinates[0] + assert_equal 2.2956200048981574, result.coordinates[1] + end +end diff --git a/test/unit/lookups/freegeoip_test.rb b/test/unit/lookups/freegeoip_test.rb new file mode 100644 index 00000000..e0f31acb --- /dev/null +++ b/test/unit/lookups/freegeoip_test.rb @@ -0,0 +1,26 @@ +# encoding: utf-8 +require 'test_helper' + +class FreegeoipTest < Test::Unit::TestCase + + def setup + Geocoder.configure(ip_lookup: :freegeoip) + end + + def test_result_on_ip_address_search + result = Geocoder.search("74.200.247.59").first + assert result.is_a?(Geocoder::Result::Freegeoip) + end + + def test_result_components + result = Geocoder.search("74.200.247.59").first + assert_equal "Plano, TX 75093, United States", result.address + end + + def test_host_config + Geocoder.configure(freegeoip: {host: "local.com"}) + lookup = Geocoder::Lookup::Freegeoip.new + query = Geocoder::Query.new("24.24.24.23") + assert_match %r(http://local\.com), lookup.query_url(query) + end +end diff --git a/test/unit/lookups/geocoder_ca_test.rb b/test/unit/lookups/geocoder_ca_test.rb new file mode 100644 index 00000000..d9dffe20 --- /dev/null +++ b/test/unit/lookups/geocoder_ca_test.rb @@ -0,0 +1,16 @@ +# encoding: utf-8 +require 'test_helper' + +class GeocoderCaTest < Test::Unit::TestCase + + def setup + Geocoder.configure(lookup: :geocoder_ca) + set_api_key!(:geocoder_ca) + end + + def test_result_components + result = Geocoder.search([45.423733, -75.676333]).first + assert_equal "CA", result.country_code + assert_equal "289 Somerset ST E, Ottawa, ON K1N6W1, Canada", result.address + end +end diff --git a/test/unit/lookups/geocodio_test.rb b/test/unit/lookups/geocodio_test.rb new file mode 100644 index 00000000..2237f5ea --- /dev/null +++ b/test/unit/lookups/geocodio_test.rb @@ -0,0 +1,28 @@ +# encoding: utf-8 +require 'test_helper' + +class GeocodioTest < Test::Unit::TestCase + + def setup + Geocoder.configure(lookup: :geocodio) + set_api_key!(:geocodio) + end + + def test_result_components + result = Geocoder.search("1101 Pennsylvania Ave NW, Washington DC").first + assert_equal 1.0, result.accuracy + assert_equal "1101", result.number + assert_equal "Ave", result.suffix + assert_equal "DC", result.state + assert_equal "20004", result.zip + assert_equal "NW", result.postdirectional + assert_equal "Washington", result.city + assert_equal "1101 Pennsylvania Ave NW, Washington DC, 20004", result.formatted_address + assert_equal({ "lat" => 38.895019, "lng" => -77.028095 }, result.location) + end + + def test_no_results + results = Geocoder.search("no results") + assert_equal 0, results.length + end +end diff --git a/test/unit/lookups/google_premier_test.rb b/test/unit/lookups/google_premier_test.rb new file mode 100644 index 00000000..b46b3f32 --- /dev/null +++ b/test/unit/lookups/google_premier_test.rb @@ -0,0 +1,21 @@ +# encoding: utf-8 +require 'test_helper' + +class GooglePremierTest < Test::Unit::TestCase + + def setup + Geocoder.configure(lookup: :google_premier) + set_api_key!(:google_premier) + end + + def test_result_components + result = Geocoder.search("Madison Square Garden, New York, NY").first + assert_equal "Manhattan", result.address_components_of_type(:sublocality).first['long_name'] + end + + def test_query_url + Geocoder.configure(google_premier: {api_key: ["deadbeef", "gme-test", "test-dev"]}) + query = Geocoder::Query.new("Madison Square Garden, New York, NY") + 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=", query.url + end +end diff --git a/test/unit/lookups/google_test.rb b/test/unit/lookups/google_test.rb new file mode 100644 index 00000000..967f7d04 --- /dev/null +++ b/test/unit/lookups/google_test.rb @@ -0,0 +1,83 @@ +# encoding: utf-8 +require 'test_helper' + +class GoogleTest < Test::Unit::TestCase + + def test_google_result_components + result = Geocoder.search("Madison Square Garden, New York, NY").first + assert_equal "Manhattan", + result.address_components_of_type(:sublocality).first['long_name'] + end + + def test_google_result_components_contains_route + result = Geocoder.search("Madison Square Garden, New York, NY").first + assert_equal "Penn Plaza", + result.address_components_of_type(:route).first['long_name'] + end + + def test_google_result_components_contains_street_number + result = Geocoder.search("Madison Square Garden, New York, NY").first + assert_equal "4", + result.address_components_of_type(:street_number).first['long_name'] + end + + def test_google_returns_city_when_no_locality_in_result + result = Geocoder.search("no locality").first + assert_equal "Haram", result.city + end + + def test_google_city_results_returns_nil_if_no_matching_component_types + result = Geocoder.search("no city data").first + assert_equal nil, result.city + end + + def test_google_street_address_returns_formatted_street_address + result = Geocoder.search("Madison Square Garden, New York, NY").first + assert_equal "4 Penn Plaza", result.street_address + end + + def test_google_precision + result = Geocoder.search("Madison Square Garden, New York, NY").first + assert_equal "ROOFTOP", + result.precision + end + + def test_google_query_url_contains_bounds + lookup = Geocoder::Lookup::Google.new + url = lookup.query_url(Geocoder::Query.new( + "Some Intersection", + :bounds => [[40.0, -120.0], [39.0, -121.0]] + )) + assert_match /bounds=40.0+%2C-120.0+%7C39.0+%2C-121.0+/, url + end + + def test_google_query_url_contains_region + lookup = Geocoder::Lookup::Google.new + url = lookup.query_url(Geocoder::Query.new( + "Some Intersection", + :region => "gb" + )) + assert_match /region=gb/, url + end + + def test_google_query_url_contains_components_when_given_as_string + lookup = Geocoder::Lookup::Google.new + url = lookup.query_url(Geocoder::Query.new( + "Some Intersection", + :components => "locality:ES" + )) + formatted = "components=" + CGI.escape("locality:ES") + assert url.include?(formatted), "Expected #{formatted} to be included in #{url}" + end + + def test_google_query_url_contains_components_when_given_as_array + lookup = Geocoder::Lookup::Google.new + url = lookup.query_url(Geocoder::Query.new( + "Some Intersection", + :components => ["country:ES", "locality:ES"] + )) + formatted = "components=" + CGI.escape("country:ES|locality:ES") + assert url.include?(formatted), "Expected #{formatted} to be included in #{url}" + end + +end diff --git a/test/unit/lookups/mapquest_test.rb b/test/unit/lookups/mapquest_test.rb new file mode 100644 index 00000000..a6f1e391 --- /dev/null +++ b/test/unit/lookups/mapquest_test.rb @@ -0,0 +1,55 @@ +# encoding: utf-8 +require 'test_helper' + +class MapquestTest < Test::Unit::TestCase + + def setup + Geocoder.configure(lookup: :mapquest) + set_api_key!(:mapquest) + end + + def test_url_contains_api_key + Geocoder.configure(mapquest: {api_key: "abc123"}) + lookup = Geocoder::Lookup::Mapquest.new + query = Geocoder::Query.new("Bluffton, SC") + assert_equal "http://open.mapquestapi.com/geocoding/v1/address?key=abc123&location=Bluffton%2C+SC", query.url + end + + def test_url_for_version_2 + Geocoder.configure(mapquest: {api_key: "abc123", licensed: true, version: 2}) + lookup = Geocoder::Lookup::Mapquest.new + query = Geocoder::Query.new("Bluffton, SC") + assert_equal "http://www.mapquestapi.com/geocoding/v2/address?key=abc123&location=Bluffton%2C+SC", query.url + end + + def test_result_components + result = Geocoder.search("Madison Square Garden, New York, NY").first + assert_equal "10001", result.postal_code + assert_equal "46 West 31st Street, New York, NY, 10001, US", result.address + end + + def test_no_results + assert_equal [], Geocoder.search("no results") + end + + def test_raises_exception_when_invalid_request + Geocoder.configure(always_raise: [Geocoder::InvalidRequest]) + assert_raises Geocoder::InvalidRequest do + Geocoder.search("invalid request") + end + end + + def test_raises_exception_when_invalid_api_key + Geocoder.configure(always_raise: [Geocoder::InvalidApiKey]) + assert_raises Geocoder::InvalidApiKey do + Geocoder.search("invalid api key") + end + end + + def test_raises_exception_when_error + Geocoder.configure(always_raise: [Geocoder::Error]) + assert_raises Geocoder::Error do + Geocoder.search("error") + end + end +end diff --git a/test/maxmind_local_test.rb b/test/unit/lookups/maxmind_local_test.rb similarity index 57% rename from test/maxmind_local_test.rb rename to test/unit/lookups/maxmind_local_test.rb index cf72b778..b7de9a94 100644 --- a/test/maxmind_local_test.rb +++ b/test/unit/lookups/maxmind_local_test.rb @@ -2,11 +2,13 @@ require 'test_helper' class MaxmindLocalTest < Test::Unit::TestCase - def test_it_returns_the_correct_results - g = Geocoder::Lookup::MaxmindLocal.new - result = g.search(Geocoder::Query.new('8.8.8.8')).first + def setup + Geocoder.configure(ip_lookup: :maxmind_local) + end + def test_result_attributes + result = Geocoder.search('8.8.8.8').first assert_equal result.address, 'Mountain View, CA 94043, United States' assert_equal result.city, 'Mountain View' assert_equal result.state, 'CA' @@ -17,11 +19,8 @@ class MaxmindLocalTest < Test::Unit::TestCase assert_equal result.longitude, -122.0574 end - def test_it_returns_empty_results_when_nothing_is_found - g = Geocoder::Lookup::MaxmindLocal.new - - result = g.search(Geocoder::Query.new('127.0.0.1')) - - assert result.empty?, "Result wasn't empty." + def test_loopback + results = Geocoder.search('127.0.0.1') + assert_equal [], results end -end \ No newline at end of file +end diff --git a/test/unit/lookups/maxmind_test.rb b/test/unit/lookups/maxmind_test.rb new file mode 100644 index 00000000..41386bcd --- /dev/null +++ b/test/unit/lookups/maxmind_test.rb @@ -0,0 +1,62 @@ +# encoding: utf-8 +require 'test_helper' + +class MaxmindTest < Test::Unit::TestCase + + def setup + Geocoder.configure(ip_lookup: :maxmind) + end + + def test_maxmind_result_on_ip_address_search + Geocoder.configure(maxmind: {service: :city_isp_org}) + result = Geocoder.search("74.200.247.59").first + assert result.is_a?(Geocoder::Result::Maxmind) + end + + def test_maxmind_result_knows_country_service_name + Geocoder.configure(maxmind: {service: :country}) + assert_equal :country, Geocoder.search("24.24.24.21").first.service_name + end + + def test_maxmind_result_knows_city_service_name + Geocoder.configure(maxmind: {service: :city}) + assert_equal :city, Geocoder.search("24.24.24.22").first.service_name + end + + def test_maxmind_result_knows_city_isp_org_service_name + Geocoder.configure(maxmind: {service: :city_isp_org}) + assert_equal :city_isp_org, Geocoder.search("24.24.24.23").first.service_name + end + + def test_maxmind_result_knows_omni_service_name + Geocoder.configure(maxmind: {service: :omni}) + assert_equal :omni, Geocoder.search("24.24.24.24").first.service_name + end + + def test_maxmind_special_result_components + Geocoder.configure(maxmind: {service: :omni}) + result = Geocoder.search("24.24.24.24").first + assert_equal "Road Runner", result.isp_name + assert_equal "Cable/DSL", result.netspeed + assert_equal "rr.com", result.domain + end + + def test_maxmind_raises_exception_when_service_not_configured + Geocoder.configure(maxmind: {service: nil}) + assert_raises Geocoder::ConfigurationError do + Geocoder::Query.new("24.24.24.24").url + end + end + + def test_maxmind_works_when_loopback_address_on_omni + Geocoder.configure(maxmind: {service: :omni}) + result = Geocoder.search("127.0.0.1").first + assert_equal "", result.country_code + end + + def test_maxmind_works_when_loopback_address_on_country + Geocoder.configure(maxmind: {service: :country}) + result = Geocoder.search("127.0.0.1").first + assert_equal "", result.country_code + end +end diff --git a/test/unit/lookups/nominatim_test.rb b/test/unit/lookups/nominatim_test.rb new file mode 100644 index 00000000..ddab3d1f --- /dev/null +++ b/test/unit/lookups/nominatim_test.rb @@ -0,0 +1,23 @@ +# encoding: utf-8 +require 'test_helper' + +class NominatimTest < Test::Unit::TestCase + + def setup + Geocoder.configure(lookup: :nominatim) + set_api_key!(:nominatim) + end + + def test_result_components + result = Geocoder.search("Madison Square Garden, New York, NY").first + assert_equal "10001", result.postal_code + assert_equal "Madison Square Garden, West 31st Street, Long Island City, New York City, New York, 10001, United States of America", result.address + end + + def test_host_configuration + Geocoder.configure(nominatim: {host: "local.com"}) + lookup = Geocoder::Lookup::Nominatim.new + query = Geocoder::Query.new("Bluffton, SC") + assert_match %r(http://local\.com), query.url + end +end diff --git a/test/unit/lookups/yahoo_test.rb b/test/unit/lookups/yahoo_test.rb new file mode 100644 index 00000000..579f5b85 --- /dev/null +++ b/test/unit/lookups/yahoo_test.rb @@ -0,0 +1,36 @@ +# encoding: utf-8 +require 'test_helper' + +class YahooTest < Test::Unit::TestCase + + def setup + Geocoder.configure(lookup: :yahoo) + set_api_key!(:yahoo) + end + + def test_no_results + assert_equal [], Geocoder.search("no results") + end + + def test_error + # keep test output clean: suppress timeout warning + orig = $VERBOSE; $VERBOSE = nil + assert_equal [], Geocoder.search("error") + ensure + $VERBOSE = orig + end + + def test_result_components + result = Geocoder.search("madison square garden").first + assert_equal "10001", result.postal_code + assert_equal "Madison Square Garden, New York, NY 10001, United States", result.address + end + + def test_raises_exception_when_over_query_limit + Geocoder.configure(:always_raise => [Geocoder::OverQueryLimitError]) + l = Geocoder::Lookup.get(:yahoo) + assert_raises Geocoder::OverQueryLimitError do + l.send(:results, Geocoder::Query.new("over limit")) + end + end +end diff --git a/test/method_aliases_test.rb b/test/unit/method_aliases_test.rb similarity index 100% rename from test/method_aliases_test.rb rename to test/unit/method_aliases_test.rb diff --git a/test/mongoid_test.rb b/test/unit/mongoid_test.rb similarity index 100% rename from test/mongoid_test.rb rename to test/unit/mongoid_test.rb diff --git a/test/near_test.rb b/test/unit/near_test.rb similarity index 100% rename from test/near_test.rb rename to test/unit/near_test.rb diff --git a/test/oauth_util_test.rb b/test/unit/oauth_util_test.rb similarity index 100% rename from test/oauth_util_test.rb rename to test/unit/oauth_util_test.rb diff --git a/test/proxy_test.rb b/test/unit/proxy_test.rb similarity index 100% rename from test/proxy_test.rb rename to test/unit/proxy_test.rb diff --git a/test/query_test.rb b/test/unit/query_test.rb similarity index 100% rename from test/query_test.rb rename to test/unit/query_test.rb diff --git a/test/request_test.rb b/test/unit/request_test.rb similarity index 100% rename from test/request_test.rb rename to test/unit/request_test.rb diff --git a/test/result_test.rb b/test/unit/result_test.rb similarity index 100% rename from test/result_test.rb rename to test/unit/result_test.rb diff --git a/test/test_mode_test.rb b/test/unit/test_mode_test.rb similarity index 100% rename from test/test_mode_test.rb rename to test/unit/test_mode_test.rb -- GitLab