diff options
author | Matthew Somerville <matthew-github@dracos.co.uk> | 2017-06-14 13:59:43 +0100 |
---|---|---|
committer | Matthew Somerville <matthew-github@dracos.co.uk> | 2017-06-20 15:02:12 +0100 |
commit | 3b72526db1cdb4695f61a188261351ddd2aea395 (patch) | |
tree | 6817af03285d6695e11e59dd90cad5dfeb39a599 /perllib | |
parent | e57cbf483790bedfef6496be3c7ffa42882c3ffc (diff) |
Run each test file in a transaction.
This means that the tests can be run in parallel.
Diffstat (limited to 'perllib')
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report.pm | 2 | ||||
-rwxr-xr-x | perllib/FixMyStreet/Script/UpdateAllReports.pm | 80 | ||||
-rw-r--r-- | perllib/FixMyStreet/Test.pm | 30 | ||||
-rw-r--r-- | perllib/FixMyStreet/TestMech.pm | 9 |
4 files changed, 112 insertions, 9 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Report.pm b/perllib/FixMyStreet/App/Controller/Report.pm index b0a98c9d0..eefb4fc99 100644 --- a/perllib/FixMyStreet/App/Controller/Report.pm +++ b/perllib/FixMyStreet/App/Controller/Report.pm @@ -159,7 +159,7 @@ sub load_updates : Private { my $updates = $c->model('DB::Comment')->search( { problem_id => $c->stash->{problem}->id, state => 'confirmed' }, - { order_by => 'confirmed' } + { order_by => [ 'confirmed', 'id' ] } ); my $questionnaires = $c->model('DB::Questionnaire')->search( diff --git a/perllib/FixMyStreet/Script/UpdateAllReports.pm b/perllib/FixMyStreet/Script/UpdateAllReports.pm new file mode 100755 index 000000000..7b5c34290 --- /dev/null +++ b/perllib/FixMyStreet/Script/UpdateAllReports.pm @@ -0,0 +1,80 @@ +package FixMyStreet::Script::UpdateAllReports; + +use strict; +use warnings; + +use FixMyStreet; +use FixMyStreet::DB; + +use File::Path (); +use File::Slurp; +use JSON::MaybeXS; +use List::MoreUtils qw(zip); + +my $fourweeks = 4*7*24*60*60; + +# Age problems from when they're confirmed, except on Zurich +# where they appear as soon as they're created. +my $age_column = 'confirmed'; +if ( FixMyStreet->config('BASE_URL') =~ /zurich|zueri/ ) { + $age_column = 'created'; +} + +sub generate { + my $problems = FixMyStreet::DB->resultset('Problem')->search( + { + state => [ FixMyStreet::DB::Result::Problem->visible_states() ], + }, + { + columns => [ + 'id', 'bodies_str', 'state', 'areas', 'cobrand', + { duration => { extract => "epoch from current_timestamp-lastupdate" } }, + { age => { extract => "epoch from current_timestamp-$age_column" } }, + ] + } + ); + $problems = $problems->cursor; # Raw DB cursor for speed + + my ( %fixed, %open ); + my @cols = ( 'id', 'bodies_str', 'state', 'areas', 'cobrand', 'duration', 'age' ); + while ( my @problem = $problems->next ) { + my %problem = zip @cols, @problem; + my @bodies; + my $cobrand = $problem{cobrand}; + + if ( !$problem{bodies_str} ) { + # Problem was not sent to any bodies, add to all areas + @bodies = grep { $_ } split( /,/, $problem{areas} ); + $problem{bodies} = 0; + } else { + # Add to bodies it was sent to + @bodies = split( /,/, $problem{bodies_str} ); + $problem{bodies} = scalar @bodies; + } + foreach my $body ( @bodies ) { + my $duration_str = ( $problem{duration} > 2 * $fourweeks ) ? 'old' : 'new'; + my $type = ( $problem{duration} > 2 * $fourweeks ) + ? 'unknown' + : ($problem{age} > $fourweeks ? 'older' : 'new'); + if (FixMyStreet::DB::Result::Problem->fixed_states()->{$problem{state}} || FixMyStreet::DB::Result::Problem->closed_states()->{$problem{state}}) { + # Fixed problems are either old or new + $fixed{$body}{$duration_str}++; + $fixed{$cobrand}{$body}{$duration_str}++; + } else { + # Open problems are either unknown, older, or new + $open{$body}{$type}++; + $open{$cobrand}{$body}{$type}++; + } + } + } + + my $body = encode_json( { + 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 ); +} + +1; diff --git a/perllib/FixMyStreet/Test.pm b/perllib/FixMyStreet/Test.pm new file mode 100644 index 000000000..add67dfd9 --- /dev/null +++ b/perllib/FixMyStreet/Test.pm @@ -0,0 +1,30 @@ +package FixMyStreet::Test; + +use parent qw(Exporter); + +use strict; +use warnings FATAL => 'all'; +use utf8; +use Test::More; +use FixMyStreet::DB; + +my $db = FixMyStreet::DB->storage; + +sub import { + strict->import; + warnings->import(FATAL => 'all'); + utf8->import; + Test::More->export_to_level(1); + $db->txn_begin; +} + +BEGIN { + use FixMyStreet; + FixMyStreet->test_mode(1); +} + +END { + $db->txn_rollback if $db; +} + +1; diff --git a/perllib/FixMyStreet/TestMech.pm b/perllib/FixMyStreet/TestMech.pm index a698261cc..92588a598 100644 --- a/perllib/FixMyStreet/TestMech.pm +++ b/perllib/FixMyStreet/TestMech.pm @@ -1,9 +1,7 @@ package FixMyStreet::TestMech; use parent qw(Test::WWW::Mechanize::Catalyst Test::Builder::Module); -use strict; -use warnings FATAL => 'all'; -use utf8; +use FixMyStreet::Test; sub import { strict->import; @@ -12,11 +10,6 @@ sub import { Test::More->export_to_level(1); } -BEGIN { - use FixMyStreet; - FixMyStreet->test_mode(1); -} - use Test::WWW::Mechanize::Catalyst 'FixMyStreet::App'; use t::Mock::MapIt; use Test::More; |