Skip to content
Snippets Groups Projects
Commit 627134d6 authored by Alex Reisner's avatar Alex Reisner
Browse files

Merge branch 'mguterl-test_mode'

Conflicts:
	lib/geocoder.rb
parents 44a6212d f2434e54
No related branches found
No related tags found
No related merge requests found
......@@ -57,7 +57,7 @@ module Geocoder
# All street address lookups, default first.
#
def street_lookups
[:google, :google_premier, :yahoo, :bing, :geocoder_ca, :yandex, :nominatim,:mapquest]
[:google, :google_premier, :yahoo, :bing, :geocoder_ca, :yandex, :nominatim, :mapquest, :test]
end
##
......
......@@ -99,5 +99,6 @@ module Geocoder
instance.set_defaults
end
end
end
end
require 'geocoder/lookups/base'
require 'geocoder/results/test'
module Geocoder
module Lookup
class Test < Base
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 = {}
end
private
def results(query, reverse = false)
Geocoder::Lookup::Test.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'
require 'geocoder/lookups/test'
class TestModeTest < Test::Unit::TestCase
def setup
@_original_lookup = Geocoder::Configuration.lookup
end
def teardown
Geocoder::Lookup::Test.reset
Geocoder::Configuration.lookup = @_original_lookup
end
def test_search_with_known_stub
Geocoder::Configuration.lookup = :test
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::Lookup::Test.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.lookup = :test
assert_raise ArgumentError do
Geocoder.search("New York, NY")
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