aboutsummaryrefslogtreecommitdiffstats
path: root/perllib
diff options
context:
space:
mode:
authorMatthew Somerville <matthew-github@dracos.co.uk>2017-06-08 23:44:03 +0100
committerMatthew Somerville <matthew-github@dracos.co.uk>2017-06-20 12:18:06 +0100
commit0c9b61f25d37c95f3398b540e7ad664b2f57f9ac (patch)
tree007e3ad24152440f7fb3289ae49e941926f336b1 /perllib
parentf4a7250eccdc70e3413c18f80b01aec7042a17da (diff)
[fixmystreet.com] Improve fixture loading script.
Given a MapIt area ID, the script will create a body and categories, users with different levels of permissions, and some random problems and updates. It can optionally empty out the database first.
Diffstat (limited to 'perllib')
-rw-r--r--perllib/FixMyStreet/DB/Factories.pm155
1 files changed, 155 insertions, 0 deletions
diff --git a/perllib/FixMyStreet/DB/Factories.pm b/perllib/FixMyStreet/DB/Factories.pm
new file mode 100644
index 000000000..e877ffd9f
--- /dev/null
+++ b/perllib/FixMyStreet/DB/Factories.pm
@@ -0,0 +1,155 @@
+use FixMyStreet::DB;
+
+package FixMyStreet::DB::Factory::Base;
+
+use parent "DBIx::Class::Factory";
+
+sub find_or_create {
+ my ($class, $fields) = @_;
+ my $key_field = $class->key_field;
+ my $id = $class->get_fields($fields)->{$key_field};
+ my $rs = $class->_class_data->{resultset};
+ my $obj = $rs->find({ $key_field => $id });
+ return $obj if $obj;
+ return $class->create($fields);
+}
+
+#######################
+
+package FixMyStreet::DB::Factory::Problem;
+
+use parent "DBIx::Class::Factory";
+
+__PACKAGE__->resultset(FixMyStreet::DB->resultset("Problem"));
+
+__PACKAGE__->exclude(['body']);
+
+__PACKAGE__->fields({
+ postcode => '',
+ title => __PACKAGE__->seq(sub { 'Title #' . (shift()+1) }),
+ detail => __PACKAGE__->seq(sub { 'Detail #' . (shift()+1) }),
+ name => __PACKAGE__->callback(sub { shift->get('user')->name }),
+ bodies_str => __PACKAGE__->callback(sub { shift->get('body')->id }),
+ confirmed => \'current_timestamp',
+ whensent => \'current_timestamp',
+ state => 'confirmed',
+ cobrand => 'default',
+ latitude => 0,
+ longitude => 0,
+ areas => '',
+ used_map => 't',
+ anonymous => 'f',
+ category => 'Other',
+});
+
+#######################
+
+# This currently creates special 'UK' bodies, with ID == MapIt area_id.
+# We should try and end this, it is just confusing.
+package FixMyStreet::DB::Factory::Body;
+
+use parent -norequire, "FixMyStreet::DB::Factory::Base";
+use mySociety::MaPit;
+
+__PACKAGE__->resultset(FixMyStreet::DB->resultset("Body"));
+
+__PACKAGE__->exclude(['area_id', 'categories']);
+
+__PACKAGE__->fields({
+ id => __PACKAGE__->callback(sub {
+ my $area_id = shift->get('area_id');
+ $area_id;
+ }),
+ name => __PACKAGE__->callback(sub {
+ my $area_id = shift->get('area_id');
+ my $area = mySociety::MaPit::call('area', $area_id);
+ $area->{name};
+ }),
+ body_areas => __PACKAGE__->callback(sub {
+ my $area_id = shift->get('area_id');
+ [ { area_id => $area_id } ]
+ }),
+ contacts => __PACKAGE__->callback(sub {
+ my $categories = shift->get('categories');
+ push @$categories, 'Other' unless @$categories;
+ [ map { FixMyStreet::DB::Factory::Contact->get_fields({ category => $_ }) } @$categories ];
+ }),
+});
+
+sub key_field { 'id' }
+
+#######################
+
+package FixMyStreet::DB::Factory::Contact;
+
+use parent "DBIx::Class::Factory";
+
+__PACKAGE__->resultset(FixMyStreet::DB->resultset("Contact"));
+
+__PACKAGE__->fields({
+ body_id => __PACKAGE__->callback(sub {
+ my $fields = shift;
+ return $fields->get('body')->id if $fields->get('body');
+ }),
+ category => 'Other',
+ email => __PACKAGE__->callback(sub {
+ my $category = shift->get('category');
+ (my $email = lc $_) =~ s/ /-/g;
+ lc $category . '@example.org';
+ }),
+ confirmed => 1,
+ deleted => 0,
+ editor => 'Factory',
+ whenedited => \'current_timestamp',
+ note => 'Created by factory',
+});
+
+#######################
+
+package FixMyStreet::DB::Factory::Comment;
+
+use parent "DBIx::Class::Factory";
+
+__PACKAGE__->resultset(FixMyStreet::DB->resultset("Comment"));
+
+__PACKAGE__->fields({
+ anonymous => 'f',
+ name => __PACKAGE__->callback(sub { shift->get('user')->name }),
+ text => __PACKAGE__->seq(sub { 'Comment #' . (shift()+1) }),
+ confirmed => \'current_timestamp',
+ state => 'confirmed',
+ cobrand => 'default',
+ mark_fixed => 0,
+});
+
+#######################
+
+package FixMyStreet::DB::Factory::User;
+
+use parent -norequire, "FixMyStreet::DB::Factory::Base";
+
+__PACKAGE__->resultset(FixMyStreet::DB->resultset("User"));
+
+__PACKAGE__->exclude(['body', 'permissions']);
+
+__PACKAGE__->fields({
+ name => 'User',
+ email => 'user@example.org',
+ password => 'password',
+ from_body => __PACKAGE__->callback(sub {
+ my $fields = shift;
+ if (my $body = $fields->get('body')) {
+ return $body->id;
+ }
+ }),
+ user_body_permissions => __PACKAGE__->callback(sub {
+ my $fields = shift;
+ my $body = $fields->get('body');
+ my $permissions = $fields->get('permissions');
+ [ map { { body_id => $body->id, permission_type => $_ } } @$permissions ];
+ }),
+});
+
+sub key_field { 'email' }
+
+1;