diff options
Diffstat (limited to 'bin/fixmystreet.com/fixture')
-rwxr-xr-x | bin/fixmystreet.com/fixture | 155 |
1 files changed, 133 insertions, 22 deletions
diff --git a/bin/fixmystreet.com/fixture b/bin/fixmystreet.com/fixture index 93982af8a..d0b23ede5 100755 --- a/bin/fixmystreet.com/fixture +++ b/bin/fixmystreet.com/fixture @@ -1,11 +1,12 @@ #!/usr/bin/env perl # -# This script will create a test body and its categories, covering the area of -# Westminster, and a user associated with that body, which should help testing +# This script will create a test body and its categories, covering the area +# provided, and users associated with that body, which should help testing # of report interactions. use strict; use warnings; +use v5.14; BEGIN { use File::Basename qw(dirname); @@ -14,26 +15,136 @@ BEGIN { require "$d/../../setenv.pl"; } -use FixMyStreet::DB; - -my $body = FixMyStreet::DB->resultset("Body")->find_or_create({ name => 'Test City Council' }); -$body->body_areas->find_or_create({ area_id => 2504 }); -foreach ("Potholes", "Street lighting", "Graffiti") { - (my $email = lc $_) =~ s/ /-/g; - $body->contacts->find_or_create({ - category => $_, - email => $email . '@example.net', - confirmed => 't', - deleted => 'f', - whenedited => \'current_timestamp', - editor => 'fixture', - note => 'Created by fixture' - }); +use FixMyStreet::Cobrand; +use FixMyStreet::DB::Factories; +use DateTime::Format::Pg; +use Getopt::Long::Descriptive; +use Path::Tiny 'path'; + +my ($opt, $usage) = describe_options( + '%c %o', + [ 'area-id=i', "MapIt area ID to create body for", { required => 1 } ], + [ 'name:s', "Name of body to use (defaults to MapIt area name)" ], + [ 'empty', "Empty all tables of the database first" ], + [ 'commit', "Actually commit changes to the database" ], + [ 'help', "print usage message and exit", { shortcircuit => 1 } ], +); +print($usage->text), exit if $opt->help; + +my $db = FixMyStreet::DB->storage; +$db->txn_begin; +END { + if ($db) { + $opt->commit ? $db->txn_commit : $db->txn_rollback; + } +} +if (!$opt->commit) { + say "NOT COMMITTING TO DATABASE"; +} + +if ($opt->empty) { + $db->dbh->do(q{ +DO +$func$ +BEGIN + EXECUTE + (SELECT 'TRUNCATE TABLE ' || string_agg(quote_ident(tablename), ', ') || ' RESTART IDENTITY CASCADE ' + FROM pg_tables WHERE schemaname='public'); +END +$func$; +}) or die $!; + $db->dbh->do( path('db/alert_types.sql')->slurp ) or die $!; + $db->dbh->do( path('db/generate_secret.sql')->slurp ) or die $!; + say "Emptied database"; } -FixMyStreet::DB->resultset("User")->find_or_create({ - email => 'council@example.net', - name => 'Test City Council User', - from_body => $body, - password => 'password', +# Body + categories +my $categories = ['Potholes', 'Street lighting', 'Graffiti', 'Other']; +my $body = FixMyStreet::DB::Factory::Body->find_or_create({ + area_id => $opt->area_id, + categories => $categories, + $opt->name ? (name => $opt->name) : (), }); +say "Created body " . $body->name . " for MapIt area ID " . $opt->area_id . ', categories ' . join(', ', @$categories); + +# Users +say "Created users, all with password 'password':"; +my $user; # Will store the final (normal) user of the loop for later user +my $perms_inspector = ['report_inspect', 'planned_reports']; +my $perms_cs = [ + 'contribute_as_body', 'contribute_as_another_user', + 'moderate', 'view_body_contribute_details', +]; +foreach ( + { name => 'Inspector Gadget', email => 'inspector@example.org', body => $body, permissions => $perms_inspector }, + { name => 'Harriet Helpful', email => 'cs@example.org', body => $body, permissions => $perms_cs }, + { name => 'Super User', email => 'super@example.org', body => $body, permissions => [ + @$perms_cs, @$perms_inspector, 'report_edit', + 'category_edit', 'template_edit', 'responsepriority_edit', + 'user_assign_body', 'user_manage_permissions', 'user_edit', + ] }, + { name => 'Wizard of Oz', email => 'admin@example.org', is_superuser => 't' }, + { name => "Norma User", email => 'user@example.org' }, +) { + $user = FixMyStreet::DB::Factory::User->find_or_create($_); + my $perms = $_->{permissions} ? " (" . join(', ', @{$_->{permissions} || []}) . ")" : ""; + my $su = $_->{is_superuser} ? " (superuser)" : ""; + say "* $_->{email}$perms$su"; +} + +# Problems + +my %titles = ( + 'Potholes' => ['Deep pothole', 'Small pothole', 'Pothole in cycle lane', 'Pothole on busy pavement', 'Large pothole', 'Sinking manhole'], + 'Street lighting' => ['Faulty light', 'Street light not working', 'Lights out in tunnel', 'Light not coming on', 'Light not going off'], + 'Graffiti' => ['Graffiti', 'Graffiti', 'Offensive graffiti', 'Graffiti on the bridge', 'Remove graffiti'], + 'Other' => ['Loose drain cover', 'Flytipping on country lane', 'Vehicle blocking footpath', 'Hedge encroaching pavement', 'Full litter bins'], +); +my $postcode = mySociety::MaPit::call('area/example_postcode', $opt->area_id); +$postcode = mySociety::MaPit::call('postcode', $postcode); + +my $cobrand = 'default'; +foreach (FixMyStreet::Cobrand->available_cobrand_classes) { + my $sub = $_->{class} && $_->{class}->can('council_id'); + if ($sub && &$sub == $opt->area_id) { + $cobrand = $_->{class}->moniker; + last; + } +} + +my $num = 10; +say "Created $num problems around '$postcode->{postcode}' in cobrand '$cobrand'"; +my $inaccurate_km = 0.01; +my $confirmed = DateTime->today->subtract(days => 1)->add(hours => 8); +my $problems = []; +for (1..$num) { + $confirmed->add(seconds => rand(7000)); + my $category = $categories->[int(rand(@$categories))]; + my $titles = $titles{$category}; + push @$problems, FixMyStreet::DB::Factory::Problem->create({ + body => $body, + user => $user, + postcode => $postcode->{postcode}, + latitude => $postcode->{wgs84_lat} + rand($inaccurate_km) - $inaccurate_km / 2, + longitude => $postcode->{wgs84_lon} + rand($inaccurate_km) - $inaccurate_km / 2, + category => $category, + cobrand => $cobrand, + title => $titles->[int(rand(@$titles))], + detail => 'The details of the report would be here, provided by the user, including exact location and how long it has been here.', + confirmed => DateTime::Format::Pg->format_datetime($confirmed), + }); +} + +# Comment + +my $updates = []; +for (1..$num) { + $confirmed->add(seconds => rand(1000)); + push @$updates, FixMyStreet::DB::Factory::Comment->create({ + problem => $problems->[int(rand(@$problems))], + user => $user, + text => 'This is an update on this problem, perhaps providing further relevant information or letting everyone know it is being worked on or fixed.', + confirmed => DateTime::Format::Pg->format_datetime($confirmed), + }); +} +say "Created $num updates on problems " . join(', ', map { $_->problem_id } @$updates); |