From 786b69f54dc117501778f77055f53901962f45ac Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Fri, 21 Apr 2017 17:23:41 +0100 Subject: [PATCH] Fix and refactor search test --- lib/Pear/LocalLoop/Controller/Api/Upload.pm | 99 +++++++++------------ t/search.t | 93 ++++++------------- 2 files changed, 67 insertions(+), 125 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Api/Upload.pm b/lib/Pear/LocalLoop/Controller/Api/Upload.pm index 96a0a37..6ce5253 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Upload.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Upload.pm @@ -67,6 +67,9 @@ has error_messages => sub { organisation_name => { required => { message => 'organisation_name is missing', status => 400 }, }, + search_name => { + required => { message => 'search_name is missing', status => 400 }, + }, }; }; @@ -188,75 +191,55 @@ sub post_upload { } -#TODO this should limit the number of responses returned, when location is implemented that would be the main way of filtering. +# TODO Limit search results, possibly paginate them? +# TODO Search by location as well sub post_search { - my $self = shift; - my $userId = $self->get_active_user_id(); + my $c = shift; + my $self = $c; - my $json = $self->req->json; - if ( ! defined $json ) { - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'JSON is missing.', - }, - status => 400,); #Malformed request - } + my $validation = $c->validation; - my $searchName = $json->{searchName}; - if ( ! defined $searchName ) { - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'searchName is missing.', - }, - status => 400,); #Malformed request - } - #Is blank - elsif ( $searchName =~ m/^\s*$/) { - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'searchName is blank.', - }, - status => 400,); #Malformed request - } + $validation->input( $c->stash->{api_json} ); - #Currently ignored - #TODO implement further. - my $searchLocation = $json->{searchLocation}; + $validation->required('search_name'); - my @validatedOrgs = (); - { - my $statementValidated = $self->db->prepare("SELECT OrganisationalId, Name, FullAddress, PostCode FROM Organisations WHERE UPPER( Name ) LIKE ?"); - $statementValidated->execute('%'. uc $searchName.'%'); + return $c->api_validation_error if $validation->has_error; - while (my ($id, $name, $address, $postcode) = $statementValidated->fetchrow_array()) { - push(@validatedOrgs, $self->create_hash($id,$name,$address,$postcode)); - } - } + my $search_name = $validation->param('search_name'); - $self->app->log->debug( "Orgs: " . Dumper @validatedOrgs ); + my $valid_orgs_rs = $c->schema->resultset('Organisation')->search( + { 'LOWER(name)' => { -like => '%' . lc $search_name . '%' } }, + ); - my @unvalidatedOrgs = (); - { - my $statementUnvalidated = $self->db->prepare("SELECT PendingOrganisationId, Name, FullAddress, Postcode FROM PendingOrganisations WHERE UPPER( Name ) LIKE ? AND UserSubmitted_FK = ?"); - $statementUnvalidated->execute('%'. uc $searchName.'%', $userId); + my $pending_orgs_rs = $c->stash->{api_user}->pending_organisations->search( + { 'LOWER(name)' => { -like => '%' . lc $search_name . '%' } }, + ); + + my @valid_orgs = ( + map {{ + id => $_->id, + name => $_->name, + street_name => $_->street_name, + town => $_->town, + postcode => $_->postcode, + }} $valid_orgs_rs->all + ); + + my @pending_orgs = ( + map {{ + id => $_->id, + name => $_->name, + street_name => $_->street_name, + town => $_->town, + postcode => $_->postcode, + }} $pending_orgs_rs->all + ); - while (my ($id, $name, $fullAddress, $postcode) = $statementUnvalidated->fetchrow_array()) { - push(@unvalidatedOrgs, $self->create_hash($id, $name, $fullAddress, $postcode)); - } - } - - $self->app->log->debug( "Non Validated Orgs: " . Dumper @unvalidatedOrgs ); - $self->app->log->debug('Path Success: file:' . __FILE__ . ', line: ' . __LINE__); return $self->render( json => { success => Mojo::JSON->true, - unvalidated => \@unvalidatedOrgs, - validated => \@validatedOrgs, - }, - status => 200,); - + validated => \@valid_orgs, + unvalidated => \@pending_orgs, + }); } 1; diff --git a/t/search.t b/t/search.t index e414785..b4afaed 100644 --- a/t/search.t +++ b/t/search.t @@ -1,63 +1,27 @@ use Mojo::Base -strict; + use Test::More; -use Test::Mojo; use Mojo::JSON; -use Text::ParseWords; +use Test::Pear::LocalLoop; +my $framework = Test::Pear::LocalLoop->new; +my $t = $framework->framework; +my $schema = $t->app->schema; -use FindBin; +my @account_tokens = ('a', 'b'); +$schema->resultset('AccountToken')->populate([ + [ qw/ accounttokenname / ], + map { [ $_ ] } @account_tokens, +]); -BEGIN { - $ENV{MOJO_MODE} = 'testing'; - $ENV{MOJO_LOG_LEVEL} = 'debug'; -} - -my $t = Test::Mojo->new("Pear::LocalLoop"); - -my $dbh = $t->app->db; - -#Dump all pf the test tables and start again. -my $sqlDeployment = Mojo::File->new("$FindBin::Bin/../dropschema.sql")->slurp; -for (split ';', $sqlDeployment){ - $dbh->do($_) or die $dbh->errstr; -} - -$sqlDeployment = Mojo::File->new("$FindBin::Bin/../schema.sql")->slurp; -for (split ';', $sqlDeployment){ - $dbh->do($_) or die $dbh->errstr; -} - -my @accountTokens = ('a', 'b'); -my $tokenStatement = $dbh->prepare('INSERT INTO AccountTokens (AccountTokenName) VALUES (?)'); -foreach (@accountTokens){ - my $rowsAdded = $tokenStatement->execute($_); -} - -#TODO this should be done via the API but as that does not exist at the moment, just add them manually. -my $statement = $dbh->prepare("INSERT INTO Organisations (OrganisationalId, Name, FullAddress, PostCode) VALUES (?, ?, ?, ?)"); - -my $value = 1; -my ($name, $address, $postcode) = ("Avanti Bar & Restaurant","57 Main St, Kirkby Lonsdale, Cumbria","LA6 2AH"); -$statement->execute($value, $name, $address, $postcode); - -$value++; -($name, $address, $postcode) = ("Full House Noodle Bar","21 Common Garden St, Lancaster, Lancashire","LA1 1XD"); -$statement->execute($value, $name, $address, $postcode); - -$value++; -($name, $address, $postcode) = ("The Quay's Fishbar","1 Adcliffe Rd, Lancaster","LA1 1SS"); -$statement->execute($value, $name, $address, $postcode); - -$value++; -($name, $address, $postcode) = ("Dan's Fishop","56 North Rd, Lancaster","LA1 1LT"); -$statement->execute($value, $name, $address, $postcode); - -$value++; -($name, $address, $postcode) = ("Hodgeson's Chippy","96 Prospect St, Lancaster","LA1 3BH"); -$statement->execute($value, $name, $address, $postcode); - - -#This depends on "register.t", "login.t" and "upload.t" working. +$schema->resultset('Organisation')->populate([ + [ qw/ name street_name town postcode / ], + [ "Avanti Bar & Restaurant", "57 Main St", "Kirkby Lonsdale", "LA6 2AH" ], + [ "Full House Noodle Bar", "21 Common Garden St", "Lancaster", "LA1 1XD" ], + [ "The Quay's Fishbar", "1 Adcliffe Rd", "Lancaster", "LA1 1SS" ], + [ "Dan's Fishop", "56 North Rd", "Lancaster", "LA1 1LT" ], + [ "Hodgeson's Chippy", "96 Prospect St", "Lancaster", "LA1 3BH" ], +]); #test with a customer. print "test 1 - Create customer user account (Rufus)\n"; @@ -65,7 +29,7 @@ my $emailRufus = 'rufus@shinra.energy'; my $passwordRufus = 'MakoGold'; my $testJson = { 'usertype' => 'customer', - 'token' => shift(@accountTokens), + 'token' => shift(@account_tokens), 'username' => 'RufusShinra', 'email' => $emailRufus, 'postcode' => 'LA1 1CF', @@ -82,12 +46,13 @@ my $emailBilly = 'choco.billy@chocofarm.org'; my $passwordBilly = 'Choco'; $testJson = { 'usertype' => 'organisation', - 'token' => shift(@accountTokens), + 'token' => shift(@account_tokens), 'username' => 'ChocoBillysGreens', 'email' => $emailBilly, 'postcode' => 'LA1 1HT', 'password' => $passwordBilly, - 'fulladdress' => 'Market St, Lancaster' + 'street_name' => 'Market St', + 'town' => 'Lancaster', }; $t->post_ok('/api/register' => json => $testJson) ->status_is(200) @@ -200,20 +165,11 @@ log_out(); print "test 10 - Login - Rufus (cookies, customer)\n"; login_rufus(); -print "test 11 - search blank\n"; -$t->post_ok('/api/search' => json => { - searchName => " ", - session_key => $session_key, - }) - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/searchName is blank/i); - sub check_vars{ my ($searchTerm, $numValidated, $numUnvalidated) = @_; $t->post_ok('/api/search' => json => { - searchName => $searchTerm, + search_name => $searchTerm, session_key => $session_key, }) ->status_is(200) @@ -233,6 +189,9 @@ sub check_vars{ }; +print "test 11 - search blank\n"; +check_vars(" ", 5, 1); + print "test 12 - Testing expected values with 'booths'\n"; #Expect 0 validated and 0 unvalidated with "booths". check_vars("booths", 0, 0);