Skip to content
Snippets Groups Projects
Commit fdd58c0b authored by Fernando Morgenstern's avatar Fernando Morgenstern
Browse files

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.
parent 6b03f839
No related branches found
No related tags found
No related merge requests found
...@@ -24,7 +24,7 @@ module Geocoder::Lookup ...@@ -24,7 +24,7 @@ module Geocoder::Lookup
) )
end end
[GeoIP.new(configuration[:database]).city(query.to_s)] [GeoIP.new(configuration[:database]).city(query.to_s).to_hash]
end end
end end
end end
\ No newline at end of file
...@@ -3,32 +3,32 @@ require 'geocoder/results/base' ...@@ -3,32 +3,32 @@ require 'geocoder/results/base'
module Geocoder::Result module Geocoder::Result
class MaxmindLocal < Base class MaxmindLocal < Base
def address(format = :full) def address(format = :full)
s = state_code.to_s == "" ? "" : ", #{state_code}" s = state.to_s == "" ? "" : ", #{state}"
"#{city}#{s} #{postal_code}, #{country}".sub(/^[ ,]*/, "") "#{city}#{s} #{postal_code}, #{country}".sub(/^[ ,]*/, "")
end end
def city def city
@data.city_name @data[:city_name]
end end
def state def state
@data.region_name @data[:region_name]
end end
def state_code def state_code
@data.region_code "" # Not available in Maxmind's database
end end
def country def country
@data.country_name @data[:country_name]
end end
def country_code def country_code
@data.country_code3 @data[:country_code3]
end end
def postal_code def postal_code
@data.postal_code @data[:postal_code]
end end
def self.response_attributes def self.response_attributes
......
...@@ -6,6 +6,7 @@ class CacheTest < Test::Unit::TestCase ...@@ -6,6 +6,7 @@ class CacheTest < Test::Unit::TestCase
def test_second_occurrence_of_request_is_cache_hit def test_second_occurrence_of_request_is_cache_hit
Geocoder.configure(:cache => {}) Geocoder.configure(:cache => {})
Geocoder::Lookup.all_services_except_test.each do |l| Geocoder::Lookup.all_services_except_test.each do |l|
next if l == :maxmind_local # local, does not use cache
Geocoder.configure(:lookup => l) Geocoder.configure(:lookup => l)
set_api_key!(l) set_api_key!(l)
results = Geocoder.search("Madison Square Garden") results = Geocoder.search("Madison Square Garden")
......
...@@ -22,6 +22,7 @@ class ErrorHandlingTest < Test::Unit::TestCase ...@@ -22,6 +22,7 @@ class ErrorHandlingTest < Test::Unit::TestCase
def test_always_raise_timeout_error def test_always_raise_timeout_error
Geocoder.configure(:always_raise => [TimeoutError]) Geocoder.configure(:always_raise => [TimeoutError])
Geocoder::Lookup.all_services_except_test.each do |l| Geocoder::Lookup.all_services_except_test.each do |l|
next if l == :maxmind_local # local, does not raise timeout
lookup = Geocoder::Lookup.get(l) lookup = Geocoder::Lookup.get(l)
set_api_key!(l) set_api_key!(l)
assert_raises TimeoutError do assert_raises TimeoutError do
...@@ -33,6 +34,7 @@ class ErrorHandlingTest < Test::Unit::TestCase ...@@ -33,6 +34,7 @@ class ErrorHandlingTest < Test::Unit::TestCase
def test_always_raise_socket_error def test_always_raise_socket_error
Geocoder.configure(:always_raise => [SocketError]) Geocoder.configure(:always_raise => [SocketError])
Geocoder::Lookup.all_services_except_test.each do |l| Geocoder::Lookup.all_services_except_test.each do |l|
next if l == :maxmind_local # local, does not raise timeout
lookup = Geocoder::Lookup.get(l) lookup = Geocoder::Lookup.get(l)
set_api_key!(l) set_api_key!(l)
assert_raises SocketError do assert_raises SocketError do
......
...@@ -22,7 +22,7 @@ class LookupTest < Test::Unit::TestCase ...@@ -22,7 +22,7 @@ class LookupTest < Test::Unit::TestCase
def test_query_url_contains_values_in_params_hash def test_query_url_contains_values_in_params_hash
Geocoder::Lookup.all_services_except_test.each do |l| 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) set_api_key!(l)
url = Geocoder::Lookup.get(l).query_url(Geocoder::Query.new( url = Geocoder::Lookup.get(l).query_url(Geocoder::Query.new(
"test", :params => {:one_in_the_hand => "two in the bush"} "test", :params => {:one_in_the_hand => "two in the bush"}
......
...@@ -2,11 +2,16 @@ ...@@ -2,11 +2,16 @@
require 'test_helper' require 'test_helper'
class MaxmindLocalTest < Test::Unit::TestCase class MaxmindLocalTest < Test::Unit::TestCase
def test_it_requires_database_path def test_it_returns_the_correct_results
g = Geocoder::Lookup::MaxmindLocal.new g = Geocoder::Lookup::MaxmindLocal.new
assert_raise Geocoder::ConfigurationError do result = g.search(Geocoder::Query.new('8.8.8.8')).first
g.search(Geocoder::Query.new('8.8.8.8')).first
end 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
end end
\ No newline at end of file
...@@ -140,6 +140,15 @@ module Geocoder ...@@ -140,6 +140,15 @@ module Geocoder
end end
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
end end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment