Initial commit

This commit is contained in:
zhongjin
2020-06-15 10:58:47 +08:00
commit 4f1dfe7564
8590 changed files with 1516878 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
recid,searchtext
"Fredericton, Canada","Fredericton, Canada"
1 recid searchtext
2 Fredericton, Canada Fredericton, Canada
+1
View File
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns2:SearchBatch xmlns:ns2="http://www.navteq.com/lbsp/Search-Batch/1"><Response><MetaInfo><RequestId>0TK8XRhsIAxMi0KNbEu58MkApMVWctFE</RequestId></MetaInfo><Status>cancelled</Status><JobStarted>2013-09-15T18:19:49.000Z</JobStarted><JobFinished>2013-09-15T18:19:54.000Z</JobFinished><TotalCount>3</TotalCount><ValidCount>2</ValidCount><InvalidCount>1</InvalidCount><ProcessedCount>2</ProcessedCount><PendingCount>0</PendingCount><SuccessCount>2</SuccessCount><ErrorCount>0</ErrorCount></Response></ns2:SearchBatch>
+1
View File
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns2:SearchBatch xmlns:ns2="http://www.navteq.com/lbsp/Search-Batch/1"><Response><MetaInfo><RequestId>K8DmCWzsZGh4gbawxOuMv2BUcZsIkt7v</RequestId></MetaInfo><Status>submitted</Status><TotalCount>0</TotalCount><ValidCount>0</ValidCount><InvalidCount>0</InvalidCount><ProcessedCount>0</ProcessedCount><PendingCount>0</PendingCount><SuccessCount>0</SuccessCount><ErrorCount>0</ErrorCount></Response></ns2:SearchBatch>
@@ -0,0 +1 @@
{"response":{"metaInfo":{"timestamp":"2014-02-19T12:29:49.723+0000"},"view":[{"result":[{"relevance":1.0,"matchLevel":"country","matchQuality":{"country":1.0},"location":{"locationId":"AREA_21000001","locationType":"point","displayPosition":{"latitude":38.89037,"longitude":-77.03196},"navigationPosition":[{"latitude":38.89037,"longitude":-77.03196}],"mapView":{"topLeft":{"latitude":49.3845,"longitude":-124.749},"bottomRight":{"latitude":24.5018,"longitude":-66.9406}},"address":{"label":"United States","country":"USA","additionalData":[{"value":"United States","key":"CountryName"}]}}}],"viewId":0}]}}
+1
View File
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns2:Error xmlns:ns2="http://www.navteq.com/lbsp/Errors/1" type="ApplicationError" subtype="InvalidInputData"><Details>Input parameter validation failed. JobId: 9rFyj7kbGMmpF50ZUFAkRnroEiOpDOEZ Email Address is missing!</Details><AdditionalData key="mailto"/></ns2:Error>
+1
View File
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns2:SearchBatch xmlns:ns2="http://www.navteq.com/lbsp/Search-Batch/1"><Response><MetaInfo><RequestId>0TK8XRhsIAxMi0KNbEu58MkApMVWctFE</RequestId></MetaInfo><Status>completed</Status><JobStarted>2013-09-15T18:19:49.000Z</JobStarted><JobFinished>2013-09-15T18:19:54.000Z</JobFinished><TotalCount>3</TotalCount><ValidCount>2</ValidCount><InvalidCount>1</InvalidCount><ProcessedCount>2</ProcessedCount><PendingCount>0</PendingCount><SuccessCount>2</SuccessCount><ErrorCount>0</ErrorCount></Response></ns2:SearchBatch>
+4
View File
@@ -0,0 +1,4 @@
recId,searchText,country
1,425 W Randolph St, Chicago Illinois 60606,USA
2,31 St James Ave Boston MA 02116,USA
3,10115 Berlin Invalidenstrasse 117,DEU
1 recId,searchText,country
2 1,425 W Randolph St, Chicago Illinois 60606,USA
3 2,31 St James Ave Boston MA 02116,USA
4 3,10115 Berlin Invalidenstrasse 117,DEU
+4
View File
@@ -0,0 +1,4 @@
recId,searchText
1,425 W Randolph St, Chicago Illinois 60606
2,31 St James Ave Boston MA 02116
3,10115 Berlin Invalidenstrasse 117
1 recId,searchText
2 1,425 W Randolph St, Chicago Illinois 60606
3 2,31 St James Ave Boston MA 02116
4 3,10115 Berlin Invalidenstrasse 117
+171
View File
@@ -0,0 +1,171 @@
require_relative '../../../spec/spec_helper'
require_relative '../../../spec/rspec_configuration.rb'
require_relative '../lib/hires_batch_geocoder'
# TODO rename to hires_batch_geocoder_spec.rb or split into batch/non-batch
describe CartoDB::HiresBatchGeocoder do
before(:each) do
@log = mock
@log.stubs(:append)
@log.stubs(:append_and_store)
CartoDB::HiresBatchGeocoder.any_instance.stubs(:config).returns({
'base_url' => 'http://wadus.nokia.com',
'app_id' => '',
'token' => '',
'mailto' => ''
})
@working_dir = Dir.mktmpdir
@geocoding_model = FactoryGirl.create(:geocoding, kind: 'high-resolution', formatter: '{street}',
remote_id: 'wadus')
end
after(:each) do
FileUtils.remove_entry_secure @working_dir
end
describe '#upload' do
it 'returns rec_id on success' do
stub_api_request 200, 'response_example.xml'
filepath = path_to 'without_country.csv'
rec_id = CartoDB::HiresBatchGeocoder.new(filepath, @working_dir, @log, @geocoding_model).upload
rec_id.should eq "K8DmCWzsZGh4gbawxOuMv2BUcZsIkt7v"
end
it 'raises error on failure' do
stub_api_request 400, 'response_failure.xml'
filepath = path_to 'without_country.csv'
expect {
CartoDB::HiresBatchGeocoder.new(filepath, @working_dir, @log, @geocoding_model).upload
}.to raise_error('Geocoding API communication failure: Input parameter validation failed. JobId: 9rFyj7kbGMmpF50ZUFAkRnroEiOpDOEZ Email Address is missing!')
end
end
describe '#update_status' do
before {
stub_api_request(200, 'response_status.xml')
CartoDB::HiresBatchGeocoder.any_instance.stubs(:request_id).returns('wadus')
}
let(:geocoder) { CartoDB::HiresBatchGeocoder.new('/tmp/dummy_input_file.csv', @working_dir, @log, @geocoding_model) }
it "updates status" do
expect { geocoder.update_status }.to change(geocoder, :status).from(nil).to('completed')
end
it "updates processed rows" do
expect { geocoder.update_status }.to change(geocoder, :processed_rows).from(nil).to(2)
end
it "updates total rows" do
expect { geocoder.update_status }.to change(geocoder, :total_rows).from(nil).to(3)
end
end
describe '#result' do
it "saves result file on working directory" do
pending 'move to non-batch suite' # TODO
filepath = path_to 'without_country.csv'
stub_api_request 200, 'response_example_non_batch.json'
geocoder = CartoDB::Geocoder.new(default_params.merge(input_file: filepath, force_batch: false))
geocoder.upload
geocoder.status.should eq 'completed'
result_file = geocoder.result
File.file?(result_file).should be true
File.dirname(result_file).should eq geocoder.dir
end
end
describe '#cancel' do
before {
stub_api_request(200, 'response_cancel.xml')
@geocoding_model.remote_id = 'wadus'
@geocoding_model.save
CartoDB::HiresBatchGeocoder.any_instance.stubs(:request_id).returns('wadus')
}
let(:geocoder) { CartoDB::HiresBatchGeocoder.new('dummy_input_file.csv', @working_dir, @log, @geocoding_model) }
it "updates the status" do
geocoder.cancel
@geocoding_model.state.should eq 'cancelled'
end
end
describe '#extract_response_field' do
let(:geocoder) { CartoDB::HiresBatchGeocoder.new('dummy_input.csv', @working_dir, @log, @geocoding_model) }
let(:response) { File.open(path_to('response_example.xml')).read }
it 'returns specified element value' do
geocoder.send(:extract_response_field, response, '//Response/Status').should == 'submitted'
end
it 'returns nil for missing elements' do
CartoDB.expects(:notify_exception).once
geocoder.send(:extract_response_field, response, 'MissingField').should == nil
end
end
describe '#api_url' do
# TODO move to common place for both geocoders
before(:each) {
CartoDB::HiresBatchGeocoder.any_instance.stubs(:config).returns({
'base_url' => '',
'app_id' => 'a',
'token' => 'b',
'mailto' => 'c'
})
@geocoder = CartoDB::HiresBatchGeocoder.new('dummy_input.csv', @working_dir, @log, @geocoding_model)
}
it 'returns base url by default' do
@geocoder.send(:api_url, {}).should == "/wadus/?app_id=a&token=b&mailto=c"
end
it 'allows for api method specification' do
@geocoder.send(:api_url, {}, 'all').should == "/wadus/all/?app_id=a&token=b&mailto=c"
end
it 'allows for api attributes specification' do
@geocoder.send(:api_url, {attr: 'wadus'}, 'all').should == "/wadus/all/?attr=wadus&app_id=a&token=b&mailto=c"
end
end
describe '#geocode_text' do
it 'returns lat/lon on success' do
pending 'move to non-batched suite' # TODO
stub_api_request 200, 'response_example_non_batch.json'
g = CartoDB::Geocoder.new(default_params)
g.geocode_text("United States").should eq [38.89037, -77.03196]
end
end
describe '#used_batch_request?' do
it 'returns true if sent a request to hi-res batch api' do
pending 'move these to the factory tests' # TODO
stub_api_request 200, 'response_example.xml'
filepath = path_to 'without_country.csv'
geocoder = CartoDB::Geocoder.new(default_params.merge(input_file: filepath))
geocoder.used_batch_request?.should eq true
end
it 'returns false if sent the request was non-batched' do
pending 'move these to the factory tests' # TODO
stub_api_request 200, 'response_example_non_batch.json'
filepath = path_to 'without_country.csv'
g = CartoDB::Geocoder.new(default_params.merge(force_batch: false, input_file: filepath))
g.upload
g.used_batch_request?.should eq false
end
end
def path_to(filepath)
File.expand_path(
File.join(File.dirname(__FILE__), "../spec/fixtures/#{filepath}")
)
end #path_to
def stub_api_request(code, response_file)
response = File.open(path_to(response_file)).read
Typhoeus.stub(/.*nokia.com/).and_return(
Typhoeus::Response.new(code: code, body: response)
)
end
end # CartoDB::Geocoder
@@ -0,0 +1,240 @@
require 'tmpdir'
require 'fileutils'
require_relative '../../../spec/rspec_configuration.rb'
require_relative '../../../spec/spec_helper.rb'
require_relative '../lib/hires_batch_geocoder'
describe CartoDB::HiresBatchGeocoder do
RSpec.configure do |config|
config.before :each do
Typhoeus::Expectation.clear
end
end
before(:each) do
@log = mock
@log.stubs(:append)
@log.stubs(:append_and_store)
@working_dir = Dir.mktmpdir
@input_csv_file = path_to '../../table-geocoder/spec/fixtures/nokia_input.csv'
CartoDB::HiresBatchGeocoder.any_instance.stubs(:config).returns({
'base_url' => 'batch.example.com',
'app_id' => '',
'token' => '',
'mailto' => ''
})
@geocoding_model = FactoryGirl.create(:geocoding, kind: 'high-resolution', formatter: '{street}' )
@batch_geocoder = CartoDB::HiresBatchGeocoder.new(@input_csv_file, @working_dir, @log, @geocoding_model)
end
after(:each) do
FileUtils.rm_f @working_dir
end
describe '#run' do
it 'uploads a file to the batch server' do
mock_complete_response
@batch_geocoder.expects(:upload).once
@batch_geocoder.run
@geocoding_model.state.should == 'completed'
end
it 'times out if not finished before the DEFAULT_TIMEOUT' do
mock_complete_response('running')
@batch_geocoder.expects(:upload).once
@batch_geocoder.expects(:cancel).once
@batch_geocoder.stubs(:default_timeout).returns(-10) # make sure it times out
@batch_geocoder.run
@geocoding_model.state.should == 'timeout'
end
end
describe '#upload' do
it 'uploads a file to the batch service' do
url = @batch_geocoder.send(:api_url, CartoDB::HiresBatchGeocoder::UPLOAD_OPTIONS)
expected_request_id = 'dummy_id'
xml_response_body = "<Response><MetaInfo><RequestId>#{expected_request_id}</RequestId></MetaInfo></Response>"
response = Typhoeus::Response.new(code: 200, body: xml_response_body)
Typhoeus.stub(url, method: :post).and_return(response)
@batch_geocoder.upload
@batch_geocoder.request_id.should == expected_request_id
@batch_geocoder.used_batch_request?.should == true
end
it 'raises an exception if the api returns != 200' do
url = @batch_geocoder.send(:api_url, CartoDB::HiresBatchGeocoder::UPLOAD_OPTIONS)
response = Typhoeus::Response.new(code: 401)
Typhoeus.stub(url, method: :post).and_return(response)
CartoDB.expects(:notify_exception).once
expect {
@batch_geocoder.upload
}.to raise_error(RuntimeError, /Geocoding API communication failure/)
end
it 'raises an exception if the api does not return a RequestId' do
url = @batch_geocoder.send(:api_url, CartoDB::HiresBatchGeocoder::UPLOAD_OPTIONS)
response = Typhoeus::Response.new(code: 200)
Typhoeus.stub(url, method: :post).and_return(response)
CartoDB.expects(:notify_exception).once
expect {
@batch_geocoder.upload
}.to raise_error(RuntimeError, /Could not get the request ID/)
end
end
describe '#cancel' do
it 'sends a cancel put request and gets the status, processed and total rows' do
request_id = 'dummy_request_id'
@geocoding_model.remote_id = request_id
@geocoding_model.save
@batch_geocoder.stubs(:request_id).returns(request_id)
url = @batch_geocoder.send(:api_url, action: 'cancel')
url.should match(%r'/#{request_id}/')
url.should match(%r'action=cancel')
expected_status = 'cancelled'
expected_processed_rows = 20
expected_success_rows = 17
expected_failed_rows = 0
expected_empty_rows = 3
expected_total_rows = 30
response_body = <<END_XML
<Response>
<Status>#{expected_status}</Status>
<ProcessedCount>#{expected_processed_rows}</ProcessedCount>
<SuccessCount>#{expected_success_rows}</SuccessCount>
<ErrorCount>#{expected_failed_rows}</ErrorCount>
<InvalidCount>#{expected_empty_rows}</InvalidCount>
<TotalCount>#{expected_total_rows}</TotalCount>
</Response>
END_XML
response = Typhoeus::Response.new(code: 200, body: response_body)
Typhoeus.stub(url, method: :put).and_return(response)
@batch_geocoder.cancel
@batch_geocoder.status.should == expected_status
@batch_geocoder.processed_rows.should == expected_processed_rows
@batch_geocoder.total_rows.should == expected_total_rows
end
end
describe '#update' do
it 'gets the status, processed and total rows by sending a get request' do
request_id = 'dummy_request_id'
@geocoding_model.remote_id = request_id
@geocoding_model.save
@batch_geocoder.stubs(:request_id).returns(request_id)
url = @batch_geocoder.send(:api_url, action: 'status')
url.should match(%r'/#{request_id}/')
url.should match(%r'action=status')
expected_status = 'running'
expected_processed_rows = 20
expected_success_rows = 17
expected_failed_rows = 0
expected_empty_rows = 3
expected_total_rows = 30
response_body = <<END_XML
<Response>
<Status>#{expected_status}</Status>
<ProcessedCount>#{expected_processed_rows}</ProcessedCount>
<SuccessCount>#{expected_success_rows}</SuccessCount>
<ErrorCount>#{expected_failed_rows}</ErrorCount>
<InvalidCount>#{expected_empty_rows}</InvalidCount>
<TotalCount>#{expected_total_rows}</TotalCount>
</Response>
END_XML
response = Typhoeus::Response.new(code: 200, body: response_body)
Typhoeus.stub(url, method: :get).and_return(response)
@batch_geocoder.update_status
@batch_geocoder.status.should == expected_status
@batch_geocoder.processed_rows.should == expected_processed_rows
@batch_geocoder.total_rows.should == expected_total_rows
end
end
describe '#result' do
it "raises an exception if there's no request_id from a previous upload" do
expect {
@batch_geocoder.result
}.to raise_error(RuntimeError, /No request_id provided/)
end
it 'downloads the result file from the remote server' do
request_id = 'dummy_request_id'
@geocoding_model.remote_id = request_id
@geocoding_model.save
@batch_geocoder.stubs(:request_id).returns(request_id)
expected_response_body = 'dummy result file contents'
url = @batch_geocoder.send(:api_url, {}, 'result')
response = Typhoeus::Response.new(code: 200, body: expected_response_body)
Typhoeus.stub(url, method: :get).and_return(response)
result_file = @batch_geocoder.result
File.open(result_file).read.should == expected_response_body
# it also "memoizes" the result file and avoids further downloads
@batch_geocoder.expects(:http_client).never
@batch_geocoder.result.should == result_file
end
it 'raises an exception if cannot get a result file' do
request_id = 'dummy_request_id'
@geocoding_model.remote_id = request_id
@geocoding_model.save
@batch_geocoder.stubs(:request_id).returns(request_id)
expected_response_body = 'dummy result file contents'
url = @batch_geocoder.send(:api_url, {}, 'result')
response = Typhoeus::Response.new(code: 400)
Typhoeus.stub(url, method: :get).and_return(response)
expect {
@batch_geocoder.result
}.to raise_error(RuntimeError, /Download request failed/)
end
end
def path_to(filepath = '')
File.expand_path(
File.join(File.dirname(__FILE__), "../fixtures/#{filepath}")
)
end
def mock_complete_response(state='completed')
@geocoding_model.remote_id = 'dummy_id'
@geocoding_model.save.reload
url = @batch_geocoder.send(:api_url, {action: 'status'})
xml_response_body = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:SearchBatch xmlns:ns2="http://www.navteq.com/lbsp/Search-Batch/1">
<Response>
<MetaInfo>
<RequestId>dummy_id</RequestId>
</MetaInfo>
<Status>'+state+'</Status>
<JobStarted>2016-04-08T08:24:05.000Z</JobStarted>
<JobFinished>2016-04-08T08:24:39.000Z</JobFinished>
<TotalCount>1</TotalCount>
<ValidCount>1</ValidCount>
<InvalidCount>0</InvalidCount>
<ProcessedCount>1</ProcessedCount>
<PendingCount>0</PendingCount>
<SuccessCount>1</SuccessCount>
<ErrorCount>0</ErrorCount>
</Response>
</ns2:SearchBatch>'
response = Typhoeus::Response.new(code: 200, body: xml_response_body)
Typhoeus.stub(url, method: :get).and_return(response)
end
end
@@ -0,0 +1,68 @@
require_relative '../../../spec/rspec_configuration'
require_relative '../../../spec/spec_helper'
require_relative '../lib/hires_geocoder_factory'
require_relative '../lib/geocoder_config'
describe CartoDB::HiresGeocoderFactory do
after(:all) do
# reset config
CartoDB::GeocoderConfig.instance.set(nil)
end
before(:each) do
@log = mock
@log.stubs(:append)
@log.stubs(:append_and_store)
@geocoding_model = FactoryGirl.create(:geocoding, kind: 'high-resolution', formatter: '{street}')
end
describe '#get' do
it 'returns a HiresGeocoder instance if the input file has less than N rows' do
CartoDB::GeocoderConfig.instance.set({
'non_batch_base_url' => 'http://api.example.com',
'app_id' => 'dummy_app_id',
'token' => 'dummy_token',
'mailto' => 'dummy_mail_addr'
})
dummy_input_file = 'dummy_input_file.csv'
working_dir = '/tmp/any_dir'
input_rows = CartoDB::HiresGeocoderFactory::BATCH_FILES_OVER - 1
CartoDB::HiresGeocoderFactory.expects(:input_rows).once.with(dummy_input_file).returns(input_rows)
CartoDB::HiresGeocoderFactory.get(dummy_input_file, working_dir, @log, @geocoding_model).class.should == CartoDB::HiresGeocoder
end
it 'returns a HiresBatchGeocoder instance if the input file is above N rows' do
CartoDB::GeocoderConfig.instance.set({
'base_url' => 'http://api.example.com',
'app_id' => 'dummy_app_id',
'token' => 'dummy_token',
'mailto' => 'dummy_mail_addr'
})
dummy_input_file = 'dummy_input_file.csv'
working_dir = '/tmp/any_dir'
input_rows = CartoDB::HiresGeocoderFactory::BATCH_FILES_OVER + 1
CartoDB::HiresGeocoderFactory.expects(:input_rows).once.with(dummy_input_file).returns(input_rows)
CartoDB::HiresGeocoderFactory.get(dummy_input_file, working_dir, @log, @geocoding_model).class.should == CartoDB::HiresBatchGeocoder
end
it 'returns a batch geocoder if config has force_batch set to true' do
CartoDB::GeocoderConfig.instance.set({
'force_batch' => true,
'base_url' => 'http://api.example.com',
'app_id' => 'dummy_app_id',
'token' => 'dummy_token',
'mailto' => 'dummy_mail_addr'
})
dummy_input_file = 'dummy_input_file.csv'
working_dir = '/tmp/any_dir'
CartoDB::HiresGeocoderFactory.expects(:input_rows).never
CartoDB::HiresGeocoderFactory.get(dummy_input_file, working_dir, @log, @geocoding_model).class.should == CartoDB::HiresBatchGeocoder
end
end
end
@@ -0,0 +1,135 @@
require 'tmpdir'
require 'fileutils'
require 'csv'
require_relative '../../../spec/rspec_configuration'
require_relative '../../../spec/spec_helper'
require_relative '../lib/hires_geocoder'
describe CartoDB::HiresGeocoder do
MOCK_COORDINATES = [38.89037, -77.03196]
RSpec.configure do |config|
config.before :each do
Typhoeus::Expectation.clear
end
end
before(:each) do
@working_dir = Dir.mktmpdir
@input_csv_file = path_to '../../table-geocoder/spec/fixtures/nokia_input.csv'
@log = mock
@log.stubs(:append)
@log.stubs(:append_and_store)
CartoDB::HiresGeocoder.any_instance.stubs(:config).returns({
'non_batch_base_url' => 'batch.example.com',
'app_id' => '',
'token' => '',
'mailto' => ''
})
@geocoding_model = FactoryGirl.create(:geocoding, kind: 'high-resolution', formatter: '{street}')
@geocoder = CartoDB::HiresGeocoder.new(@input_csv_file, @working_dir, @log, @geocoding_model)
end
after(:each) do
FileUtils.rm_f @working_dir
end
describe '#run' do
it 'takes every row from input and calls geocode_text on them' do
rows_to_geocode = ::CSV.read(@input_csv_file, headers: true).length
@geocoder.expects(:geocode_text).times(rows_to_geocode).returns(MOCK_COORDINATES)
@geocoder.run
@geocoder.status.should == 'completed'
end
end
describe '#process_row' do
it 'increments the number of processed rows by one when called' do
output_csv_mock = mock
output_csv_mock.expects(:add_row).once
input_row = {'searchtext' => 'olakase'}
@geocoder.expects(:geocode_text).once.returns(MOCK_COORDINATES)
@geocoder.processed_rows.should == 0
@geocoder.send(:process_row, input_row, output_csv_mock)
@geocoder.processed_rows.should == 1
end
it 'adds a row with the expected format when success' do
output_csv_mock = mock
output_csv_mock.expects(:add_row).once.with ['olakase', 1, 1, MOCK_COORDINATES[0], MOCK_COORDINATES[1]]
input_row = {'searchtext' => 'olakase'}
@geocoder.expects(:geocode_text).once.returns(MOCK_COORDINATES)
@geocoder.send(:process_row, input_row, output_csv_mock)
end
it 'does not add any row when it when geolocation fails' do
output_csv_mock = mock
output_csv_mock.expects(:add_row).never
input_row = {'searchtext' => 'olakase'}
@geocoder.expects(:geocode_text).once.returns([nil, nil])
@geocoder.send(:process_row, input_row, output_csv_mock)
end
end
describe '#geocode_text' do
it 'sends a request to the non-batched geocoder service and gets a couple of coordinates' do
json_response_body = {
response: {
view: [
result: [
location: {
displayPosition: {
latitude: MOCK_COORDINATES[0],
longitude: MOCK_COORDINATES[1]
}
}
]
]
}
}.to_json
mocked_response = Typhoeus::Response.new(code: 200, body: json_response_body)
Typhoeus.stub(//, method: :get).and_return(mocked_response)
@geocoder.send(:geocode_text, 'Dummy address').should == MOCK_COORDINATES
end
it "returns nil coordinates if the http request doesn't succeed" do
mocked_response = Typhoeus::Response.new(code: 500)
Typhoeus.stub(//, method: :get).and_return(mocked_response)
CartoDB.expects(:notify_debug).with('Non-batched geocoder failed request', mocked_response).once
@geocoder.send(:geocode_text, 'Dummy address').should == [nil, nil]
end
it 'returns nil coordinates and log a trace if it is not able to parse the response' do
input_text = 'Dummy address'
json_response_body = {
unexpected: 'this response body has unexpected format for whatever reason'
}.to_json
mocked_response = Typhoeus::Response.new(code: 200, body: json_response_body)
Typhoeus.stub(//, method: :get).and_return(mocked_response)
CartoDB.expects(:notify_debug).with("Non-batched geocoder couldn't parse response", anything()).once
@geocoder.send(:geocode_text, input_text).should == [nil, nil]
end
it 'returns nil coordinates and stops there if the response does not contain any location' do
input_text = 'Dummy address'
json_response_body = '{"response":{"metaInfo":{"timestamp":"2015-07-14T15:33:35.023+0000"},"view":[]}}'
mocked_response = Typhoeus::Response.new(code: 200, body: json_response_body)
Typhoeus.stub(//, method: :get).and_return(mocked_response)
@geocoder.send(:geocode_text, input_text).should == [nil, nil]
end
end
def path_to(filepath = '')
File.expand_path(
File.join(File.dirname(__FILE__), "../fixtures/#{filepath}")
)
end
end