From f69b06bda2015a9575a9325ffbbb26950e02c335 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Fri, 21 Apr 2017 21:20:47 +0100 Subject: [PATCH] Refactored customer table and fixed all tests form that --- lib/Pear/LocalLoop/Controller/Api/Register.pm | 10 +- lib/Pear/LocalLoop/Controller/Register.pm | 4 +- lib/Pear/LocalLoop/Schema/Result/AgeRange.pm | 90 +----------- lib/Pear/LocalLoop/Schema/Result/Customer.pm | 137 ++++-------------- lib/Pear/LocalLoop/Schema/Result/User.pm | 2 +- lib/Test/Pear/LocalLoop.pm | 2 +- t/admin-approve.t | 6 +- t/admin-merge.t | 4 +- t/api/login.t | 2 +- t/register.t | 32 ++-- t/search.t | 2 +- t/upload.t | 4 +- templates/register/index.html.ep | 4 +- 13 files changed, 67 insertions(+), 232 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Api/Register.pm b/lib/Pear/LocalLoop/Controller/Api/Register.pm index 9f38b3a..54260fc 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Register.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Register.pm @@ -79,7 +79,7 @@ sub post_register{ if ( $usertype eq 'customer' ) { my $age_rs = $c->schema->resultset('AgeRange'); - $validation->required('age')->in_resultset('agerangestring', $age_rs); + $validation->required('age')->in_resultset('id', $age_rs); } elsif ( $usertype eq 'organisation' ) { @@ -103,8 +103,6 @@ sub post_register{ } if ($usertype eq 'customer'){ - # TODO replace with actually using the value on the post request - my $ageForeignKey = $c->get_age_foreign_key( $validation->param('age') ); $c->schema->txn_do( sub { $c->schema->resultset('AccountToken')->find({ @@ -113,9 +111,9 @@ sub post_register{ })->update({ used => 1 }); $c->schema->resultset('User')->create({ customer => { - username => $validation->param('username'), - agerange_fk => $ageForeignKey, - postcode => $validation->param('postcode'), + username => $validation->param('username'), + age_range_id => $validation->param('age'), + postcode => $validation->param('postcode'), }, email => $validation->param('email'), password => $validation->param('password'), diff --git a/lib/Pear/LocalLoop/Controller/Register.pm b/lib/Pear/LocalLoop/Controller/Register.pm index 0c90f54..e34cbbb 100644 --- a/lib/Pear/LocalLoop/Controller/Register.pm +++ b/lib/Pear/LocalLoop/Controller/Register.pm @@ -38,7 +38,7 @@ sub register { $validation->required('token')->in_resultset('accounttokenname', $token_rs); my $age_rs = $c->schema->resultset('AgeRange'); - $validation->required('agerange')->in_resultset('agerangeid', $age_rs); + $validation->required('agerange')->in_resultset('id', $age_rs); my @error_messages; if ( $validation->has_error ) { @@ -51,7 +51,7 @@ sub register { customer => { username => $validation->param('name'), postcode => $validation->param('postcode'), - agerange_fk => $validation->param('agerange'), + age_range_id => $validation->param('agerange'), }, }); if ( $new_user->in_storage ) { diff --git a/lib/Pear/LocalLoop/Schema/Result/AgeRange.pm b/lib/Pear/LocalLoop/Schema/Result/AgeRange.pm index 1adf168..3120873 100644 --- a/lib/Pear/LocalLoop/Schema/Result/AgeRange.pm +++ b/lib/Pear/LocalLoop/Schema/Result/AgeRange.pm @@ -1,107 +1,29 @@ use utf8; package Pear::LocalLoop::Schema::Result::AgeRange; -# Created by DBIx::Class::Schema::Loader -# DO NOT MODIFY THE FIRST PART OF THIS FILE - -=head1 NAME - -Pear::LocalLoop::Schema::Result::AgeRange - -=cut - use strict; use warnings; use base 'DBIx::Class::Core'; -=head1 COMPONENTS LOADED - -=over 4 - -=item * L - -=back - -=cut - -__PACKAGE__->load_components("InflateColumn::DateTime"); - -=head1 TABLE: C - -=cut - -__PACKAGE__->table("AgeRanges"); - -=head1 ACCESSORS - -=head2 agerangeid - - data_type: 'integer' - is_auto_increment: 1 - is_nullable: 0 - -=head2 agerangestring - - data_type: 'text' - is_nullable: 0 - -=cut +__PACKAGE__->table("age_ranges"); __PACKAGE__->add_columns( - "agerangeid", + "id", { data_type => "integer", is_auto_increment => 1, is_nullable => 0 }, - "agerangestring", + "string", { data_type => "text", is_nullable => 0 }, ); -=head1 PRIMARY KEY +__PACKAGE__->set_primary_key("id"); -=over 4 - -=item * L - -=back - -=cut - -__PACKAGE__->set_primary_key("agerangeid"); - -=head1 UNIQUE CONSTRAINTS - -=head2 C - -=over 4 - -=item * L - -=back - -=cut - -__PACKAGE__->add_unique_constraint("agerangestring_unique", ["agerangestring"]); - -=head1 RELATIONS - -=head2 customers - -Type: has_many - -Related object: L - -=cut +__PACKAGE__->add_unique_constraint(["string"]); __PACKAGE__->has_many( "customers", "Pear::LocalLoop::Schema::Result::Customer", - { "foreign.agerange_fk" => "self.agerangeid" }, + { "foreign.age_range_id" => "self.id" }, { cascade_copy => 0, cascade_delete => 0 }, ); - -# Created by DBIx::Class::Schema::Loader v0.07046 @ 2017-02-24 17:32:21 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:4zGNm0RlwptF9hlj9oErVA - - -# You can replace this text with custom code or comments, and it will be preserved on regeneration 1; diff --git a/lib/Pear/LocalLoop/Schema/Result/Customer.pm b/lib/Pear/LocalLoop/Schema/Result/Customer.pm index 466a029..04c4c14 100644 --- a/lib/Pear/LocalLoop/Schema/Result/Customer.pm +++ b/lib/Pear/LocalLoop/Schema/Result/Customer.pm @@ -1,137 +1,52 @@ use utf8; package Pear::LocalLoop::Schema::Result::Customer; -# Created by DBIx::Class::Schema::Loader -# DO NOT MODIFY THE FIRST PART OF THIS FILE - -=head1 NAME - -Pear::LocalLoop::Schema::Result::Customer - -=cut - use strict; use warnings; use base 'DBIx::Class::Core'; -=head1 COMPONENTS LOADED - -=over 4 - -=item * L - -=back - -=cut - -__PACKAGE__->load_components("InflateColumn::DateTime"); - -=head1 TABLE: C - -=cut - -__PACKAGE__->table("Customers"); - -=head1 ACCESSORS - -=head2 customerid - - data_type: 'integer' - is_auto_increment: 1 - is_nullable: 0 - -=head2 username - - data_type: 'text' - is_nullable: 0 - -=head2 agerange_fk - - data_type: 'integer' - is_foreign_key: 1 - is_nullable: 0 - -=head2 postcode - - data_type: 'text' - is_nullable: 0 - -=cut +__PACKAGE__->table("customers"); __PACKAGE__->add_columns( - "customerid", - { data_type => "integer", is_auto_increment => 1, is_nullable => 0 }, - "username", - { data_type => "text", is_nullable => 0 }, - "agerange_fk", - { data_type => "integer", is_foreign_key => 1, is_nullable => 0 }, - "postcode", - { data_type => "text", is_nullable => 0 }, + "id" => { + data_type => "integer", + is_auto_increment => 1, + is_nullable => 0, + }, + "username" => { + data_type => "varchar", + size => 255, + is_nullable => 0, + }, + "age_range_id" => { + data_type => "integer", + is_foreign_key => 1, + is_nullable => 0, + }, + "postcode" => { + data_type => "varchar", + size => 16, + is_nullable => 0, + }, ); -=head1 PRIMARY KEY +__PACKAGE__->set_primary_key("id"); -=over 4 - -=item * L - -=back - -=cut - -__PACKAGE__->set_primary_key("customerid"); - -=head1 UNIQUE CONSTRAINTS - -=head2 C - -=over 4 - -=item * L - -=back - -=cut - -__PACKAGE__->add_unique_constraint("username_unique", ["username"]); - -=head1 RELATIONS - -=head2 agerange_fk - -Type: belongs_to - -Related object: L - -=cut +__PACKAGE__->add_unique_constraint(["username"]); __PACKAGE__->belongs_to( - "agerange_fk", + "age_range", "Pear::LocalLoop::Schema::Result::AgeRange", - { agerangeid => "agerange_fk" }, + { id => "age_range_id" }, { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" }, ); -=head2 user - -Type: might_have - -Related object: L - -=cut - __PACKAGE__->might_have( "user", "Pear::LocalLoop::Schema::Result::User", - { "foreign.customer_id" => "self.customerid" }, + { "foreign.customer_id" => "self.id" }, { cascade_copy => 0, cascade_delete => 0 }, ); - -# Created by DBIx::Class::Schema::Loader v0.07046 @ 2017-02-24 17:32:21 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:ocoCGZYvw9O9wxzr14okiQ - - -# You can replace this text with custom code or comments, and it will be preserved on regeneration 1; diff --git a/lib/Pear/LocalLoop/Schema/Result/User.pm b/lib/Pear/LocalLoop/Schema/Result/User.pm index 72ec624..4294d9b 100644 --- a/lib/Pear/LocalLoop/Schema/Result/User.pm +++ b/lib/Pear/LocalLoop/Schema/Result/User.pm @@ -70,7 +70,7 @@ __PACKAGE__->might_have( __PACKAGE__->belongs_to( "customer", "Pear::LocalLoop::Schema::Result::Customer", - { customerid => "customer_id" }, + { id => "customer_id" }, { is_deferrable => 0, join_type => "LEFT", diff --git a/lib/Test/Pear/LocalLoop.pm b/lib/Test/Pear/LocalLoop.pm index f18e24e..0640649 100644 --- a/lib/Test/Pear/LocalLoop.pm +++ b/lib/Test/Pear/LocalLoop.pm @@ -29,7 +29,7 @@ has framework => sub { $schema->deploy; $schema->resultset('AgeRange')->populate([ - [ qw/ agerangestring / ], + [ qw/ string / ], [ '20-35' ], [ '35-50' ], [ '50+' ], diff --git a/t/admin-approve.t b/t/admin-approve.t index 2c9324b..29d22ed 100644 --- a/t/admin-approve.t +++ b/t/admin-approve.t @@ -29,10 +29,10 @@ my $testJson = { 'email' => $emailReno, 'postcode' => 'E1 MP01', 'password' => $passwordReno, - 'age' => '20-35' + 'age' => 1 }; $t->post_ok('/api/register' => json => $testJson) - ->status_is(200) + ->status_is(200)->or($dump_error) ->json_is('/success', Mojo::JSON->true); print "test 2 - Create organisation user account (Choco Billy)\n"; @@ -62,7 +62,7 @@ $testJson = { 'email' => $emailAdmin, 'postcode' => 'NW1 W01', 'password' => $passwordAdmin, - 'age' => '35-50' + 'age' => 2 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(200) diff --git a/t/admin-merge.t b/t/admin-merge.t index 00b9d2c..9b240c4 100644 --- a/t/admin-merge.t +++ b/t/admin-merge.t @@ -29,7 +29,7 @@ my $testJson = { 'email' => $emailReno, 'postcode' => 'E1 MP01', 'password' => $passwordReno, - 'age' => '20-35' + 'age' => 1 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(200) @@ -63,7 +63,7 @@ $testJson = { 'email' => $emailAdmin, 'postcode' => 'NW1 W01', 'password' => $passwordAdmin, - 'age' => '35-50' + 'age' => 2 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(200) diff --git a/t/api/login.t b/t/api/login.t index f3382c0..830699f 100644 --- a/t/api/login.t +++ b/t/api/login.t @@ -23,7 +23,7 @@ my $test_json = { 'email' => $email, 'postcode' => 'LA1 1AA', 'password' => $password, - 'age' => '20-35' + 'age' => 1 }; $t->post_ok('/api/register' => json => $test_json) ->status_is(200) diff --git a/t/register.t b/t/register.t index f4e620d..e7dd305 100644 --- a/t/register.t +++ b/t/register.t @@ -36,7 +36,7 @@ $testJson = { 'email' => shift(@emails), 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => '50+' + 'age' => 3 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -52,7 +52,7 @@ $testJson = { 'email' => shift(@emails), 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => '50+' + 'age' => 3 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(401) @@ -67,7 +67,7 @@ $testJson = { 'email' => shift(@emails), 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => '50+' + 'age' => 3 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -83,7 +83,7 @@ $testJson = { 'email' => shift(@emails), 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => '50+' + 'age' => 3 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -99,7 +99,7 @@ $testJson = { 'email' => shift(@emails), 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => '50+' + 'age' => 3 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -117,7 +117,7 @@ $testJson = { 'email' => $emailToReuse, 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => '50+' + 'age' => 3 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(200) @@ -131,7 +131,7 @@ $testJson = { 'email' => shift(@emails), 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => '35-50' + 'age' => 2 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(200) @@ -145,7 +145,7 @@ $testJson = { 'email' => shift(@emails), 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => '20-35' + 'age' => 1 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(200) @@ -159,7 +159,7 @@ $testJson = { 'email' => shift(@emails), 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => '50+' + 'age' => 3 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(403) @@ -173,7 +173,7 @@ $testJson = { 'username' => shift(@names), 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => '50+' + 'age' => 3 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -188,7 +188,7 @@ $testJson = { 'email' => 'dfsd@.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => '35-50' + 'age' => 2 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -204,7 +204,7 @@ $testJson = { 'email' => 'dfsd@com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => '35-50' + 'age' => 2 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -220,7 +220,7 @@ $testJson = { 'email' => $emailToReuse, 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => '35-50' + 'age' => 2 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(403) @@ -235,7 +235,7 @@ $testJson = { 'username' => shift(@names), 'email' => shift(@emails), 'password' => 'Meh', - 'age' => '50+' + 'age' => 3 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -251,7 +251,7 @@ $testJson = { 'username' => shift(@names), 'email' => shift(@emails), 'postcode' => 'LA1 1AA', - 'age' => '50+' + 'age' => 3 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -267,7 +267,7 @@ $testJson = { 'email' => shift(@emails), 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => '50+' + 'age' => 3 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) diff --git a/t/search.t b/t/search.t index b4afaed..be6e302 100644 --- a/t/search.t +++ b/t/search.t @@ -34,7 +34,7 @@ my $testJson = { 'email' => $emailRufus, 'postcode' => 'LA1 1CF', 'password' => $passwordRufus, - 'age' => '20-35' + 'age' => 1 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(200) diff --git a/t/upload.t b/t/upload.t index 3724714..56a541d 100644 --- a/t/upload.t +++ b/t/upload.t @@ -42,7 +42,7 @@ my $testJson = { 'email' => $emailRufus, 'postcode' => 'E1 MP01', 'password' => $passwordRufus, - 'age' => '20-35' + 'age' => 1 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(200) @@ -58,7 +58,7 @@ $testJson = { 'email' => $emailHojo, 'postcode' => 'E1 MP01', 'password' => $passwordHojo, - 'age' => '35-50' + 'age' => 1 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(200) diff --git a/templates/register/index.html.ep b/templates/register/index.html.ep index e5eb1eb..e1aa6a9 100644 --- a/templates/register/index.html.ep +++ b/templates/register/index.html.ep @@ -73,8 +73,8 @@ body {