Skip to content

Commit

Permalink
[UK] Stop nearest request with scientific notation
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
dracos committed May 18, 2017
1 parent 61eb613 commit 6757441
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 18 deletions.
4 changes: 3 additions & 1 deletion perllib/FixMyStreet/Cobrand/UK.pm
Expand Up @@ -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' ] }
Expand Down Expand Up @@ -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);
Expand Down
37 changes: 37 additions & 0 deletions 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;
3 changes: 2 additions & 1 deletion t/Mock/MapIt.pm
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 },
});
}
}
Expand Down
31 changes: 15 additions & 16 deletions t/cobrand/closest.t
Expand Up @@ -2,6 +2,7 @@ use strict;
use warnings;

use Test::More;
use t::Mock::Bing;

use mySociety::Locale;
use FixMyStreet::DB;
Expand Down Expand Up @@ -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',
Expand All @@ -47,30 +48,27 @@ 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,
}
);
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);

Expand All @@ -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();
}

0 comments on commit 6757441

Please sign in to comment.