aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--perllib/FixMyStreet/Cobrand/UK.pm4
-rw-r--r--t/Mock/Bing.pm37
-rw-r--r--t/Mock/MapIt.pm3
-rw-r--r--t/cobrand/closest.t31
4 files changed, 57 insertions, 18 deletions
diff --git a/perllib/FixMyStreet/Cobrand/UK.pm b/perllib/FixMyStreet/Cobrand/UK.pm
index 46891f906..1a5682dad 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 000000000..3dfb8fbe0
--- /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 0b355983b..24e7a8084 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 0d6a772ba..8bb2d649b 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();
+}