From fdd58c0b5b26f9c8ebe8b1c658425606392a5d95 Mon Sep 17 00:00:00 2001 From: Fernando Morgenstern <contato@fernandomarcelo.com> Date: Sat, 18 May 2013 10:33:17 -0300 Subject: [PATCH] Update tests to handle maxmind_local specifics. The result of the lookup in maxmind_local is stored at test/test_helper.rb to avoid the inclusion of maxmind's database in repository. --- lib/geocoder/lookups/maxmind_local.rb | 4 ++-- lib/geocoder/results/maxmind_local.rb | 14 +++++++------- test/cache_test.rb | 1 + test/error_handling_test.rb | 2 ++ test/lookup_test.rb | 2 +- test/maxmind_local.rb | 13 +++++++++---- test/test_helper.rb | 9 +++++++++ 7 files changed, 31 insertions(+), 14 deletions(-) diff --git a/lib/geocoder/lookups/maxmind_local.rb b/lib/geocoder/lookups/maxmind_local.rb index ae1578f7..0f84014d 100644 --- a/lib/geocoder/lookups/maxmind_local.rb +++ b/lib/geocoder/lookups/maxmind_local.rb @@ -23,8 +23,8 @@ module Geocoder::Lookup "Geocoder.configure(:maxmind_local => {:database => ...}), " ) end - - [GeoIP.new(configuration[:database]).city(query.to_s)] + + [GeoIP.new(configuration[:database]).city(query.to_s).to_hash] end end end \ No newline at end of file diff --git a/lib/geocoder/results/maxmind_local.rb b/lib/geocoder/results/maxmind_local.rb index 7b8b9d09..82dd1d7f 100644 --- a/lib/geocoder/results/maxmind_local.rb +++ b/lib/geocoder/results/maxmind_local.rb @@ -3,32 +3,32 @@ require 'geocoder/results/base' module Geocoder::Result class MaxmindLocal < Base def address(format = :full) - s = state_code.to_s == "" ? "" : ", #{state_code}" + s = state.to_s == "" ? "" : ", #{state}" "#{city}#{s} #{postal_code}, #{country}".sub(/^[ ,]*/, "") end def city - @data.city_name + @data[:city_name] end def state - @data.region_name + @data[:region_name] end def state_code - @data.region_code + "" # Not available in Maxmind's database end def country - @data.country_name + @data[:country_name] end def country_code - @data.country_code3 + @data[:country_code3] end def postal_code - @data.postal_code + @data[:postal_code] end def self.response_attributes diff --git a/test/cache_test.rb b/test/cache_test.rb index edd45bbf..7e9a6239 100644 --- a/test/cache_test.rb +++ b/test/cache_test.rb @@ -6,6 +6,7 @@ class CacheTest < Test::Unit::TestCase def test_second_occurrence_of_request_is_cache_hit Geocoder.configure(:cache => {}) Geocoder::Lookup.all_services_except_test.each do |l| + next if l == :maxmind_local # local, does not use cache Geocoder.configure(:lookup => l) set_api_key!(l) results = Geocoder.search("Madison Square Garden") diff --git a/test/error_handling_test.rb b/test/error_handling_test.rb index b4f28774..cd83c3cd 100644 --- a/test/error_handling_test.rb +++ b/test/error_handling_test.rb @@ -22,6 +22,7 @@ class ErrorHandlingTest < Test::Unit::TestCase def test_always_raise_timeout_error Geocoder.configure(:always_raise => [TimeoutError]) Geocoder::Lookup.all_services_except_test.each do |l| + next if l == :maxmind_local # local, does not raise timeout lookup = Geocoder::Lookup.get(l) set_api_key!(l) assert_raises TimeoutError do @@ -33,6 +34,7 @@ class ErrorHandlingTest < Test::Unit::TestCase def test_always_raise_socket_error Geocoder.configure(:always_raise => [SocketError]) Geocoder::Lookup.all_services_except_test.each do |l| + next if l == :maxmind_local # local, does not raise timeout lookup = Geocoder::Lookup.get(l) set_api_key!(l) assert_raises SocketError do diff --git a/test/lookup_test.rb b/test/lookup_test.rb index 185249c6..4303eb6f 100644 --- a/test/lookup_test.rb +++ b/test/lookup_test.rb @@ -22,7 +22,7 @@ class LookupTest < Test::Unit::TestCase def test_query_url_contains_values_in_params_hash Geocoder::Lookup.all_services_except_test.each do |l| - next if l == :freegeoip # does not use query string + next if l == :freegeoip || l == :maxmind_local # 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"} diff --git a/test/maxmind_local.rb b/test/maxmind_local.rb index acc91291..5c0e8fc5 100644 --- a/test/maxmind_local.rb +++ b/test/maxmind_local.rb @@ -2,11 +2,16 @@ require 'test_helper' class MaxmindLocalTest < Test::Unit::TestCase - def test_it_requires_database_path + def test_it_returns_the_correct_results g = Geocoder::Lookup::MaxmindLocal.new - assert_raise Geocoder::ConfigurationError do - g.search(Geocoder::Query.new('8.8.8.8')).first - end + result = g.search(Geocoder::Query.new('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' + assert_equal result.country, 'United States' + assert_equal result.country_code, 'USA' + assert_equal result.postal_code, '94043' end end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index b74017d4..8b1facaf 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -140,6 +140,15 @@ module Geocoder end end + class MaxmindLocal + private + def results query + return [] if query.to_s == "no results" + + [{:request=>"8.8.8.8", :ip=>"8.8.8.8", :country_code2=>"US", :country_code3=>"USA", :country_name=>"United States", :continent_code=>"NA", :region_name=>"CA", :city_name=>"Mountain View", :postal_code=>"94043", :latitude=>37.41919999999999, :longitude=>-122.0574, :dma_code=>807, :area_code=>650, :timezone=>"America/Los_Angeles"}] + end + end + end end -- GitLab