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
......@@ -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
......@@ -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
......
......@@ -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")
......
......@@ -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
......
......@@ -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"}
......
......@@ -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
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment