aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Somerville <matthew-github@dracos.co.uk>2017-10-16 17:02:52 +0100
committerMatthew Somerville <matthew-github@dracos.co.uk>2017-10-16 17:02:52 +0100
commitdf06903e0c0f3e7f66af787b0acb6fd0a3d3e523 (patch)
treebf841e459d1b68d85aecfebc0aa1bb06752ef041
parent6e7a34d79217f71076f65b3017eba313b78854cb (diff)
Allow testing override of /reports data.
-rwxr-xr-xbin/update-all-reports14
-rw-r--r--perllib/FixMyStreet/App/Controller/Reports.pm15
-rwxr-xr-xperllib/FixMyStreet/Script/UpdateAllReports.pm14
-rw-r--r--t/app/controller/reports.t19
4 files changed, 35 insertions, 27 deletions
diff --git a/bin/update-all-reports b/bin/update-all-reports
index 4c0e96d31..24192d5be 100755
--- a/bin/update-all-reports
+++ b/bin/update-all-reports
@@ -17,7 +17,10 @@ BEGIN {
}
use FixMyStreet::Script::UpdateAllReports;
+use File::Path ();
+use File::Slurp;
use Getopt::Long::Descriptive;
+use JSON::MaybeXS;
my ($opt, $usage) = describe_options(
'%c %o',
@@ -27,8 +30,15 @@ my ($opt, $usage) = describe_options(
);
print($usage->text), exit if $opt->help;
+my ($data, $filename);
if ($opt->table) {
- FixMyStreet::Script::UpdateAllReports::generate($opt->areas);
+ $data = FixMyStreet::Script::UpdateAllReports::generate($opt->areas);
+ $filename = 'all-reports.json';
} else {
- FixMyStreet::Script::UpdateAllReports::generate_dashboard();
+ $data = FixMyStreet::Script::UpdateAllReports::generate_dashboard();
+ $filename = 'all-reports-dashboard.json';
}
+
+my $json = encode_json($data);
+File::Path::mkpath(FixMyStreet->path_to('../data/')->stringify);
+File::Slurp::write_file(FixMyStreet->path_to("../data/$filename")->stringify, \$json);
diff --git a/perllib/FixMyStreet/App/Controller/Reports.pm b/perllib/FixMyStreet/App/Controller/Reports.pm
index 187dfb299..c30c113ec 100644
--- a/perllib/FixMyStreet/App/Controller/Reports.pm
+++ b/perllib/FixMyStreet/App/Controller/Reports.pm
@@ -2,9 +2,9 @@ package FixMyStreet::App::Controller::Reports;
use Moose;
use namespace::autoclean;
-use File::Slurp;
use JSON::MaybeXS;
use List::MoreUtils qw(any);
+use Path::Tiny;
use POSIX qw(strcoll);
use RABX;
use mySociety::MaPit;
@@ -83,16 +83,15 @@ sub index : Path : Args(0) {
$c->stash->{any_empty_bodies} = any { $_->get_column('area_count') == 0 } @bodies;
my $dashboard = eval {
- my $data = File::Slurp::read_file(
- FixMyStreet->path_to( '../data/all-reports-dashboard.json' )->stringify
- );
- $c->stash(decode_json($data));
+ my $data = FixMyStreet->config('TEST_DASHBOARD_DATA');
+ unless ($data) {
+ $data = decode_json(path(FixMyStreet->path_to('../data/all-reports-dashboard.json'))->slurp_utf8);
+ }
+ $c->stash($data);
return 1;
};
my $table = eval {
- my $data = File::Slurp::read_file(
- FixMyStreet->path_to( '../data/all-reports.json' )->stringify
- );
+ my $data = path(FixMyStreet->path_to('../data/all-reports.json'))->slurp_utf8;
$c->stash(decode_json($data));
return 1;
};
diff --git a/perllib/FixMyStreet/Script/UpdateAllReports.pm b/perllib/FixMyStreet/Script/UpdateAllReports.pm
index 1bd069ee8..f459b7b12 100755
--- a/perllib/FixMyStreet/Script/UpdateAllReports.pm
+++ b/perllib/FixMyStreet/Script/UpdateAllReports.pm
@@ -6,9 +6,6 @@ use warnings;
use FixMyStreet;
use FixMyStreet::DB;
-use File::Path ();
-use File::Slurp;
-use JSON::MaybeXS;
use List::MoreUtils qw(zip);
use List::Util qw(sum);
@@ -81,13 +78,10 @@ sub generate {
}
}
- my $body = encode_json( {
+ return {
fixed => \%fixed,
open => \%open,
- } );
-
- File::Path::mkpath( FixMyStreet->path_to( '../data/' )->stringify );
- File::Slurp::write_file( FixMyStreet->path_to( '../data/all-reports.json' )->stringify, \$body );
+ };
}
sub end_period {
@@ -247,9 +241,7 @@ sub generate_dashboard {
}
$data{other_categories} = $last_seven_days;
- my $body = encode_json( \%data );
- File::Path::mkpath( FixMyStreet->path_to( '../data/' )->stringify );
- File::Slurp::write_file( FixMyStreet->path_to( '../data/all-reports-dashboard.json' )->stringify, \$body );
+ return \%data;
}
sub stuff_by_day_or_year {
diff --git a/t/app/controller/reports.t b/t/app/controller/reports.t
index 7773223dd..76c920562 100644
--- a/t/app/controller/reports.t
+++ b/t/app/controller/reports.t
@@ -12,9 +12,6 @@ END {
ok( my $mech = FixMyStreet::TestMech->new, 'Created mech object' );
-# Run the cron script with empty database
-FixMyStreet::Script::UpdateAllReports::generate_dashboard();
-
$mech->create_body_ok(2514, 'Birmingham City Council');
my $body_edin_id = $mech->create_body_ok(2651, 'City of Edinburgh Council')->id;
my $body_west_id = $mech->create_body_ok(2504, 'Westminster City Council')->id;
@@ -99,10 +96,14 @@ $fife_problems[10]->update( {
});
# Run the cron script that makes the data for /reports so we don't get an error.
-FixMyStreet::Script::UpdateAllReports::generate_dashboard();
+my $data = FixMyStreet::Script::UpdateAllReports::generate_dashboard();
# check that we can get the page
-$mech->get_ok('/reports');
+FixMyStreet::override_config {
+ TEST_DASHBOARD_DATA => $data,
+}, sub {
+ $mech->get_ok('/reports');
+};
$mech->title_like(qr{Dashboard});
$mech->content_contains('Birmingham');
@@ -138,6 +139,7 @@ is scalar @$problems, 5, 'correct number of problems displayed';
FixMyStreet::override_config {
MAPIT_URL => 'http://mapit.uk/',
+ TEST_DASHBOARD_DATA => $data,
}, sub {
$mech->get_ok('/reports');
$mech->submit_form_ok({ with_fields => { body => $body_slash_id } }, 'Submitted dropdown okay');
@@ -199,13 +201,18 @@ is scalar @$problems, 4, 'only public problems are displayed';
$mech->content_lacks('All reports Test 3 for ' . $body_west_id, 'non public problem is not visible');
# No change to numbers if report is non-public
-$mech->get_ok('/reports');
+FixMyStreet::override_config {
+ TEST_DASHBOARD_DATA => $data,
+}, sub {
+ $mech->get_ok('/reports');
+};
$mech->content_contains('&quot;Apr&quot;,&quot;May&quot;,&quot;Jun&quot;,&quot;Jul&quot;');
$mech->content_contains('5,9,10,22');
subtest "test fiksgatami all reports page" => sub {
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'fiksgatami' ],
+ TEST_DASHBOARD_DATA => $data, # Not relevant to what we're testing, just so page loads
}, sub {
$mech->create_body_ok(3, 'Oslo');
ok $mech->host("fiksgatami.no"), 'change host to fiksgatami';