From 675744156c56d7b5ea5c8866fdd59a1c3cbcee1b Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Thu, 18 May 2017 20:12:16 +0100 Subject: [PATCH] [UK] Stop nearest request with scientific notation If the longitude was very close to 0, it was being sent as e.g. 1e-6 in the request to MapIt. Mock out the Bing query so that this can be tested (the closest.t tests were previously always being skipped). --- perllib/FixMyStreet/Cobrand/UK.pm | 4 +++- t/Mock/Bing.pm | 37 +++++++++++++++++++++++++++++++ t/Mock/MapIt.pm | 3 ++- t/cobrand/closest.t | 31 +++++++++++++------------- 4 files changed, 57 insertions(+), 18 deletions(-) create mode 100644 t/Mock/Bing.pm diff --git a/perllib/FixMyStreet/Cobrand/UK.pm b/perllib/FixMyStreet/Cobrand/UK.pm index 46891f90675..1a5682dada4 100644 --- a/perllib/FixMyStreet/Cobrand/UK.pm +++ b/perllib/FixMyStreet/Cobrand/UK.pm @@ -5,6 +5,7 @@ use strict; use JSON::MaybeXS; use mySociety::MaPit; use mySociety::VotingArea; +use Utils; sub country { return 'GB'; } sub area_types { [ 'DIS', 'LBO', 'MTD', 'UTA', 'CTY', 'COI', 'LGD' ] } @@ -122,7 +123,8 @@ sub find_closest { my $data = $self->SUPER::find_closest($problem, $as_data); my $mapit_url = FixMyStreet->config('MAPIT_URL'); - my $url = $mapit_url . "nearest/4326/" . $problem->longitude . ',' . $problem->latitude; + my ($lat, $lon) = map { Utils::truncate_coordinate($_) } $problem->latitude, $problem->longitude; + my $url = $mapit_url . "nearest/4326/$lon,$lat"; my $j = LWP::Simple::get($url); if ($j) { $j = JSON->new->utf8->allow_nonref->decode($j); diff --git a/t/Mock/Bing.pm b/t/Mock/Bing.pm new file mode 100644 index 00000000000..3dfb8fbe018 --- /dev/null +++ b/t/Mock/Bing.pm @@ -0,0 +1,37 @@ +package t::Mock::Bing; + +use JSON::MaybeXS; +use Web::Simple; +use LWP::Protocol::PSGI; + +has json => ( + is => 'lazy', + default => sub { + JSON->new->pretty->allow_blessed->convert_blessed; + }, +); + +sub dispatch_request { + my $self = shift; + + sub (GET + /REST/v1/Locations/* + ?*) { + my ($self, $location, $query) = @_; + my $data = { + resourceSets => [ { + resources => [ { + name => 'Constitution Hill, London, SW1A', + address => { + addressLine => 'Constitution Hill', + locality => 'London', + } + } ], + } ], + }; + my $json = $self->json->encode($data); + return [ 200, [ 'Content-Type' => 'application/json' ], [ $json ] ]; + }, +} + +LWP::Protocol::PSGI->register(t::Mock::Bing->to_psgi_app, host => 'dev.virtualearth.net'); + +__PACKAGE__->run_if_script; diff --git a/t/Mock/MapIt.pm b/t/Mock/MapIt.pm index 0b355983b70..24e7a808412 100644 --- a/t/Mock/MapIt.pm +++ b/t/Mock/MapIt.pm @@ -32,6 +32,7 @@ my @PLACES = ( [ 'GU51 4AE', 51.279456, -0.846216, 2333, 'Hart District Council', 'DIS', 2227, 'Hampshire County Council', 'CTY' ], [ 'WS1 4NH', 52.563074, -1.991032, 2535, 'Sandwell Borough Council', 'MTD' ], [ 'OX28 4DS', 51.784721, -1.494453 ], + [ 'E14 2DN', 51.508536, '0.000001' ], ); sub dispatch_request { @@ -112,7 +113,7 @@ sub dispatch_request { foreach (@PLACES) { if ($point eq "4326/$_->[2],$_->[1]") { return $self->output({ - postcode => { wgs84_lat => $_->[1], wgs84_lon => $_->[2], postcode => $_->[0] }, + postcode => { wgs84_lat => $_->[1], wgs84_lon => $_->[2], postcode => $_->[0], distance => 93 }, }); } } diff --git a/t/cobrand/closest.t b/t/cobrand/closest.t index 0d6a772bafb..8bb2d649b26 100644 --- a/t/cobrand/closest.t +++ b/t/cobrand/closest.t @@ -2,6 +2,7 @@ use strict; use warnings; use Test::More; +use t::Mock::Bing; use mySociety::Locale; use FixMyStreet::DB; @@ -31,7 +32,7 @@ my $dt = DateTime->new( my $report = FixMyStreet::DB->resultset('Problem')->find_or_create( { - postcode => 'SW1A 1AA', + postcode => 'E142DN', bodies_str => '2504', areas => ',105255,11806,11828,2247,2504,', category => 'Other', @@ -47,8 +48,8 @@ my $report = FixMyStreet::DB->resultset('Problem')->find_or_create( cobrand => 'default', cobrand_data => '', send_questionnaire => 't', - latitude => '51.5016605453401', - longitude => '-0.142497580865087', + latitude => 51.508536, + longitude => 0.000001, user_id => $user->id, } ); @@ -56,21 +57,18 @@ my $report_id = $report->id; ok $report, "created test report - $report_id"; $report->geocode( undef ); - ok !$report->geocode, 'no geocode entry for report'; -my $near = $c->find_closest($report); - -SKIP: { - if (!FixMyStreet->config('BING_MAPS_API_KEY')) { - skip 'No Bing Maps key', 0; - } - +FixMyStreet::override_config { + MAPIT_URL => 'http://mapit.uk/', + BING_MAPS_API_KEY => 'test', +}, sub { + my $near = $c->find_closest($report); ok $report->geocode, 'geocode entry added to report'; ok $report->geocode->{resourceSets}, 'geocode entry looks like right sort of thing'; like $near, qr/Constitution Hill/i, 'nearest street looks right'; - like $near, qr/Nearest postcode .*: SW1A 1AA/i, 'nearest postcode looks right'; + like $near, qr/Nearest postcode .*: E14 2DN/i, 'nearest postcode looks right'; $near = $c->find_closest_address_for_rss($report); @@ -81,8 +79,9 @@ SKIP: { $near = $c->find_closest_address_for_rss($report); ok !$near, 'no closest address for RSS if not cached'; -} +}; -# all done -$mech->delete_user( $user ); -done_testing(); +END { + $mech->delete_user( $user ); + done_testing(); +}