diff --git a/test/cache_test.rb b/test/cache_test.rb new file mode 100644 index 0000000000000000000000000000000000000000..f505b805db0863bbc9c6fc8168b7cb6457b0785a --- /dev/null +++ b/test/cache_test.rb @@ -0,0 +1,19 @@ +# encoding: utf-8 +require 'test_helper' + +class CacheTest < Test::Unit::TestCase + + def test_second_occurrence_of_request_is_cache_hit + Geocoder::Configuration.cache = {} + Geocoder::Lookup.all_services_except_test.each do |l| + Geocoder::Configuration.lookup = l + set_api_key!(l) + results = Geocoder.search("Madison Square Garden") + assert !results.first.cache_hit, + "Lookup #{l} returned erroneously cached result." + results = Geocoder.search("Madison Square Garden") + assert results.first.cache_hit, + "Lookup #{l} did not return cached result." + end + end +end diff --git a/test/error_handling_test.rb b/test/error_handling_test.rb index 002425b63c182433487607d7aab20846671666fb..01f5f73305a22c39954bd2e984d427496229adc5 100644 --- a/test/error_handling_test.rb +++ b/test/error_handling_test.rb @@ -12,6 +12,7 @@ class ErrorHandlingTest < Test::Unit::TestCase orig = $VERBOSE; $VERBOSE = nil Geocoder::Lookup.all_services_except_test.each do |l| Geocoder::Configuration.lookup = l + set_api_key!(l) assert_nothing_raised { Geocoder.search("timeout") } end ensure @@ -22,6 +23,7 @@ class ErrorHandlingTest < Test::Unit::TestCase Geocoder::Configuration.always_raise = [TimeoutError] Geocoder::Lookup.all_services_except_test.each do |l| lookup = Geocoder::Lookup.get(l) + set_api_key!(l) assert_raises TimeoutError do lookup.send(:results, Geocoder::Query.new("timeout")) end @@ -32,6 +34,7 @@ class ErrorHandlingTest < Test::Unit::TestCase Geocoder::Configuration.always_raise = [SocketError] Geocoder::Lookup.all_services_except_test.each do |l| lookup = Geocoder::Lookup.get(l) + set_api_key!(l) assert_raises SocketError do lookup.send(:results, Geocoder::Query.new("socket_error")) end diff --git a/test/lookup_test.rb b/test/lookup_test.rb index a0c8cd22b61efdb429d1ae0f273981d030d5cd52..b721e4a1b51bfc826b77a9f86886e51bdd1a87fc 100644 --- a/test/lookup_test.rb +++ b/test/lookup_test.rb @@ -6,6 +6,7 @@ class LookupTest < Test::Unit::TestCase def test_search_returns_empty_array_when_no_results Geocoder::Lookup.all_services_except_test.each do |l| lookup = Geocoder::Lookup.get(l) + set_api_key!(l) assert_equal [], lookup.send(:results, Geocoder::Query.new("no results")), "Lookup #{l} does not return empty array when no results." end diff --git a/test/result_test.rb b/test/result_test.rb index ab207c5fb6f0bd4834cf88b3764708d2db91ebee..f826648dd743cde17c0c2a8d29d87f0d39406029 100644 --- a/test/result_test.rb +++ b/test/result_test.rb @@ -6,6 +6,7 @@ class ResultTest < Test::Unit::TestCase def test_result_has_required_attributes Geocoder::Lookup.all_services_except_test.each do |l| Geocoder::Configuration.lookup = l + set_api_key!(l) result = Geocoder.search([45.423733, -75.676333]).first assert_result_has_required_attributes(result) end diff --git a/test/services_test.rb b/test/services_test.rb index 2e6cd3aff6231763efc43a6e6ae81a269758d4aa..540f71c3f4ab1f2d4540f09dfcc3a83a2e6d35b3 100644 --- a/test/services_test.rb +++ b/test/services_test.rb @@ -54,6 +54,7 @@ class ServicesTest < Test::Unit::TestCase def test_google_premier_result_components Geocoder::Configuration.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'] @@ -185,23 +186,4 @@ class ServicesTest < Test::Unit::TestCase assert_equal "46 West 31st Street, New York, NY, 10001, US", result.address end - - private # ------------------------------------------------------------------ - - def set_api_key!(lookup_name) - if lookup_name == :google_premier - Geocoder::Configuration.api_key = [ - 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', - 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', - 'cccccccccccccccccccccccccccccc' - ] - elsif lookup_name == :yahoo - Geocoder::Configuration.api_key = [ - 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', - 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' - ] - else - Geocoder::Configuration.api_key = nil - end - end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 8285233e5411f014a7d53772550403fbe399fbf6..81deb6f3f269fb9bec6809fe2948d909694378ab 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -75,13 +75,19 @@ module Geocoder class Base private #----------------------------------------------------------------- def read_fixture(file) - File.read(File.join("test", "fixtures", file)).strip.gsub(/\n\s*/, "") + filepath = File.join("test", "fixtures", file) + s = File.read(filepath).strip.gsub(/\n\s*/, "") + s.instance_eval do + def body; self; end + def code; "200"; end + end + s end end class Google < Base private #----------------------------------------------------------------- - def fetch_raw_data(query) + def make_api_request(query) raise TimeoutError if query.text == "timeout" raise SocketError if query.text == "socket_error" file = case query.text @@ -99,7 +105,7 @@ module Geocoder class Yahoo < Base private #----------------------------------------------------------------- - def fetch_raw_data(query) + def make_api_request(query) raise TimeoutError if query.text == "timeout" raise SocketError if query.text == "socket_error" file = case query.text @@ -113,7 +119,7 @@ module Geocoder class Yandex < Base private #----------------------------------------------------------------- - def fetch_raw_data(query) + def make_api_request(query) raise TimeoutError if query.text == "timeout" raise SocketError if query.text == "socket_error" file = case query.text @@ -127,7 +133,7 @@ module Geocoder class GeocoderCa < Base private #----------------------------------------------------------------- - def fetch_raw_data(query) + def make_api_request(query) raise TimeoutError if query.text == "timeout" raise SocketError if query.text == "socket_error" if query.reverse_geocode? @@ -144,7 +150,7 @@ module Geocoder class Freegeoip < Base private #----------------------------------------------------------------- - def fetch_raw_data(query) + def make_api_request(query) raise TimeoutError if query.text == "timeout" raise SocketError if query.text == "socket_error" file = case query.text @@ -157,7 +163,7 @@ module Geocoder class Bing < Base private #----------------------------------------------------------------- - def fetch_raw_data(query) + def make_api_request(query) raise TimeoutError if query.text == "timeout" raise SocketError if query.text == "socket_error" if query.reverse_geocode? @@ -174,7 +180,7 @@ module Geocoder class Nominatim < Base private #----------------------------------------------------------------- - def fetch_raw_data(query) + def make_api_request(query) raise TimeoutError if query.text == "timeout" raise SocketError if query.text == "socket_error" file = case query.text @@ -187,7 +193,7 @@ module Geocoder class Mapquest < Base private #----------------------------------------------------------------- - def fetch_raw_data(query) + def make_api_request(query) raise TimeoutError if query.text == "timeout" raise SocketError if query.text == "socket_error" file = case query.text @@ -318,5 +324,21 @@ class Test::Unit::TestCase return false unless coordinates.size == 2 # Should have dimension 2 coordinates[0].nan? && coordinates[1].nan? # Both coordinates should be NaN end -end + def set_api_key!(lookup_name) + if lookup_name == :google_premier + Geocoder::Configuration.api_key = [ + 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', + 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', + 'cccccccccccccccccccccccccccccc' + ] + elsif lookup_name == :yahoo + Geocoder::Configuration.api_key = [ + 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', + 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' + ] + else + Geocoder::Configuration.api_key = nil + end + end +end