Skip to content
Snippets Groups Projects
Commit 87421b31 authored by Christian Bøtker Høj's avatar Christian Bøtker Høj
Browse files

Add tests and make them pass

parent b2b33dd8
No related branches found
No related tags found
No related merge requests found
require 'ipaddr'
require 'geocoder/lookups/base'
require 'geocoder/results/geolite2'
......@@ -11,9 +10,7 @@ module Geocoder
gem_name = 'hive_geoip2'
require gem_name
rescue LoadError
raise "Could not load maxminddb dependency. To use MaxMind2 Local \
lookup you must add the #{gem_name} gem to your Gemfile or \
have it installed in your system."
raise "Could not load maxminddb dependency. To use GeoLite2 lookup you must add the #{gem_name} gem to your Gemfile or have it installed in your system."
end
end
super
......
......@@ -4,7 +4,7 @@ module Geocoder
module Result
class Geolite2 < Base
def address(format = :full)
s = state.to_s == '' ? '' : ", #{state}"
s = state.to_s == '' ? '' : ", #{state_code}"
"#{city}#{s} #{postal_code}, #{country}".sub(/^[ ,]*/, '')
end
......
......@@ -150,6 +150,25 @@ module Geocoder
end
end
class Geolite2
private
remove_method(:results)
def results(query)
return [] if query.to_s == 'no results'
return [] if query.to_s == '127.0.0.1'
[{'city'=>{'names'=>{'en'=>'Mountain View'}},'country'=>{'iso_code'=>'US','names'=>
{'en'=>'United States'}},'location'=>{'latitude'=>37.41919999999999,
'longitude'=>-122.0574},'postal'=>{'code'=>'94043'},'subdivisions'=>[{
'iso_code'=>'CA','names'=>{'en'=>'California'}}]}]
end
def default_fixture_filename
'geolite2_74_200_247_59'
end
end
class Telize
private
def default_fixture_filename
......
......@@ -7,7 +7,7 @@ class CacheTest < GeocoderTestCase
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
next if l == :maxmind_local || l == :geolite2 # local, does not use cache
Geocoder.configure(:lookup => l)
set_api_key!(l)
results = Geocoder.search("Madison Square Garden")
......
......@@ -21,7 +21,7 @@ class ErrorHandlingTest < GeocoderTestCase
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
next if l == :maxmind_local || l == :geolite2 # local, does not use cache
lookup = Geocoder::Lookup.get(l)
set_api_key!(l)
assert_raises TimeoutError do
......@@ -33,7 +33,7 @@ class ErrorHandlingTest < GeocoderTestCase
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
next if l == :maxmind_local || l == :geolite2 # local, does not use cache
lookup = Geocoder::Lookup.get(l)
set_api_key!(l)
assert_raises SocketError do
......@@ -45,7 +45,7 @@ class ErrorHandlingTest < GeocoderTestCase
def test_always_raise_connection_refused_error
Geocoder.configure(:always_raise => [Errno::ECONNREFUSED])
Geocoder::Lookup.all_services_except_test.each do |l|
next if l == :maxmind_local # local, does not raise timeout
next if l == :maxmind_local || l == :geolite2 # local, does not use cache
lookup = Geocoder::Lookup.get(l)
set_api_key!(l)
assert_raises Errno::ECONNREFUSED do
......
......@@ -23,7 +23,7 @@ class LookupTest < GeocoderTestCase
def test_query_url_contains_values_in_params_hash
Geocoder::Lookup.all_services_except_test.each do |l|
next if [:freegeoip, :maxmind_local, :telize, :pointpin].include? l # does not use query string
next if [:freegeoip, :maxmind_local, :telize, :pointpin, :geolite2].include? l # 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"}
......
# encoding: utf-8
require 'test_helper'
class Geolite2Test < GeocoderTestCase
def setup
Geocoder.configure(ip_lookup: :geolite2, file: 'test_file')
end
def test_result_attributes
result = Geocoder.search('8.8.8.8').first
assert_equal 'Mountain View, CA 94043, United States', result.address
assert_equal 'Mountain View', result.city
assert_equal 'CA', result.state_code
assert_equal 'California', result.state
assert_equal 'United States', result.country
assert_equal 'US', result.country_code
assert_equal '94043', result.postal_code
assert_equal 37.41919999999999, result.latitude
assert_equal -122.0574, result.longitude
assert_equal [37.41919999999999, -122.0574], result.coordinates
end
def test_loopback
results = Geocoder.search('127.0.0.1')
assert_equal [], results
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