Skip to content
Snippets Groups Projects
Commit 00d6322f authored by Michael Guterl's avatar Michael Guterl
Browse files

Initial implementation of test mode for easier stubbing

parent 47edf4e3
No related branches found
No related tags found
No related merge requests found
...@@ -56,7 +56,7 @@ module Geocoder ...@@ -56,7 +56,7 @@ module Geocoder
# All street address lookups, default first. # All street address lookups, default first.
# #
def street_lookups def street_lookups
[:google, :google_premier, :yahoo, :bing, :geocoder_ca, :yandex, :nominatim] [:google, :google_premier, :yahoo, :bing, :geocoder_ca, :yandex, :nominatim, :test]
end end
## ##
......
...@@ -54,5 +54,30 @@ module Geocoder ...@@ -54,5 +54,30 @@ module Geocoder
END END
end end
def self.test_mode=(test_mode)
if test_mode
@original_lookup = lookup
self.lookup = :test
else
self.lookup = @original_lookup
end
end
def self.add_stub(query, results)
stubs[query] = results
end
def self.read_stub(query)
stubs.fetch(query) { raise ArgumentError, "unknown stub request #{query}" }
end
def self.stubs
@stubs ||= {}
end
def self.reset_stubs
@stubs = {}
end
end end
end end
require 'geocoder/lookups/base'
require 'geocoder/results/test'
module Geocoder
module Lookup
class Test < Base
private
def results(query, reverse = false)
Geocoder::Configuration.read_stub(query)
end
end
end
end
require 'geocoder/results/base'
module Geocoder
module Result
class Test < Base
def address
@data['address']
end
def state
@data['state']
end
def state_code
@data['state_code']
end
def country
@data['country']
end
def country_code
@data['country_code']
end
end
end
end
require 'test_helper'
class TestModeTest < Test::Unit::TestCase
def teardown
Geocoder::Configuration.reset_stubs
end
def test_search_with_known_stub
Geocoder::Configuration.test_mode = true
attributes = {
'latitude' => 40.7143528,
'longitude' => -74.0059731,
'address' => 'New York, NY, USA',
'state' => 'New York',
'state_code' => 'NY',
'country' => 'United States',
'country_code' => 'US',
}
coordinates = [attributes['latitude'], attributes['longitude']]
Geocoder::Configuration.add_stub("New York, NY", [attributes])
results = Geocoder.search("New York, NY")
assert_equal 1, results.size
result = results.first
assert_equal coordinates, result.coordinates
assert_equal attributes['latitude'], result.latitude
assert_equal attributes['longitude'], result.longitude
assert_equal attributes['address'], result.address
assert_equal attributes['state'], result.state
assert_equal attributes['state_code'], result.state_code
assert_equal attributes['country'], result.country
assert_equal attributes['country_code'], result.country_code
end
def test_search_with_unknown_stub
Geocoder::Configuration.test_mode = true
assert_raise ArgumentError do
Geocoder.search("New York, NY")
end
end
def test_turning_off_test_mode_restores_lookup
original_lookup = Geocoder::Configuration.lookup
Geocoder::Configuration.test_mode = true
Geocoder::Configuration.test_mode = false
assert_equal original_lookup, Geocoder::Configuration.lookup
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