Fix and refactor search test
This commit is contained in:
parent
cf28556d2f
commit
786b69f54d
2 changed files with 67 additions and 125 deletions
|
@ -67,6 +67,9 @@ has error_messages => sub {
|
||||||
organisation_name => {
|
organisation_name => {
|
||||||
required => { message => 'organisation_name is missing', status => 400 },
|
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 {
|
sub post_search {
|
||||||
my $self = shift;
|
my $c = shift;
|
||||||
my $userId = $self->get_active_user_id();
|
my $self = $c;
|
||||||
|
|
||||||
my $json = $self->req->json;
|
my $validation = $c->validation;
|
||||||
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 $searchName = $json->{searchName};
|
$validation->input( $c->stash->{api_json} );
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
#Currently ignored
|
$validation->required('search_name');
|
||||||
#TODO implement further.
|
|
||||||
my $searchLocation = $json->{searchLocation};
|
|
||||||
|
|
||||||
my @validatedOrgs = ();
|
return $c->api_validation_error if $validation->has_error;
|
||||||
{
|
|
||||||
my $statementValidated = $self->db->prepare("SELECT OrganisationalId, Name, FullAddress, PostCode FROM Organisations WHERE UPPER( Name ) LIKE ?");
|
|
||||||
$statementValidated->execute('%'. uc $searchName.'%');
|
|
||||||
|
|
||||||
while (my ($id, $name, $address, $postcode) = $statementValidated->fetchrow_array()) {
|
my $search_name = $validation->param('search_name');
|
||||||
push(@validatedOrgs, $self->create_hash($id,$name,$address,$postcode));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$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 $pending_orgs_rs = $c->stash->{api_user}->pending_organisations->search(
|
||||||
{
|
{ 'LOWER(name)' => { -like => '%' . lc $search_name . '%' } },
|
||||||
my $statementUnvalidated = $self->db->prepare("SELECT PendingOrganisationId, Name, FullAddress, Postcode FROM PendingOrganisations WHERE UPPER( Name ) LIKE ? AND UserSubmitted_FK = ?");
|
);
|
||||||
$statementUnvalidated->execute('%'. uc $searchName.'%', $userId);
|
|
||||||
|
|
||||||
while (my ($id, $name, $fullAddress, $postcode) = $statementUnvalidated->fetchrow_array()) {
|
my @valid_orgs = (
|
||||||
push(@unvalidatedOrgs, $self->create_hash($id, $name, $fullAddress, $postcode));
|
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
|
||||||
|
);
|
||||||
|
|
||||||
$self->app->log->debug( "Non Validated Orgs: " . Dumper @unvalidatedOrgs );
|
|
||||||
$self->app->log->debug('Path Success: file:' . __FILE__ . ', line: ' . __LINE__);
|
|
||||||
return $self->render( json => {
|
return $self->render( json => {
|
||||||
success => Mojo::JSON->true,
|
success => Mojo::JSON->true,
|
||||||
unvalidated => \@unvalidatedOrgs,
|
validated => \@valid_orgs,
|
||||||
validated => \@validatedOrgs,
|
unvalidated => \@pending_orgs,
|
||||||
},
|
});
|
||||||
status => 200,);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
|
93
t/search.t
93
t/search.t
|
@ -1,63 +1,27 @@
|
||||||
use Mojo::Base -strict;
|
use Mojo::Base -strict;
|
||||||
|
|
||||||
use Test::More;
|
use Test::More;
|
||||||
use Test::Mojo;
|
|
||||||
use Mojo::JSON;
|
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 {
|
$schema->resultset('Organisation')->populate([
|
||||||
$ENV{MOJO_MODE} = 'testing';
|
[ qw/ name street_name town postcode / ],
|
||||||
$ENV{MOJO_LOG_LEVEL} = 'debug';
|
[ "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" ],
|
||||||
my $t = Test::Mojo->new("Pear::LocalLoop");
|
[ "Dan's Fishop", "56 North Rd", "Lancaster", "LA1 1LT" ],
|
||||||
|
[ "Hodgeson's Chippy", "96 Prospect St", "Lancaster", "LA1 3BH" ],
|
||||||
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.
|
|
||||||
|
|
||||||
#test with a customer.
|
#test with a customer.
|
||||||
print "test 1 - Create customer user account (Rufus)\n";
|
print "test 1 - Create customer user account (Rufus)\n";
|
||||||
|
@ -65,7 +29,7 @@ my $emailRufus = 'rufus@shinra.energy';
|
||||||
my $passwordRufus = 'MakoGold';
|
my $passwordRufus = 'MakoGold';
|
||||||
my $testJson = {
|
my $testJson = {
|
||||||
'usertype' => 'customer',
|
'usertype' => 'customer',
|
||||||
'token' => shift(@accountTokens),
|
'token' => shift(@account_tokens),
|
||||||
'username' => 'RufusShinra',
|
'username' => 'RufusShinra',
|
||||||
'email' => $emailRufus,
|
'email' => $emailRufus,
|
||||||
'postcode' => 'LA1 1CF',
|
'postcode' => 'LA1 1CF',
|
||||||
|
@ -82,12 +46,13 @@ my $emailBilly = 'choco.billy@chocofarm.org';
|
||||||
my $passwordBilly = 'Choco';
|
my $passwordBilly = 'Choco';
|
||||||
$testJson = {
|
$testJson = {
|
||||||
'usertype' => 'organisation',
|
'usertype' => 'organisation',
|
||||||
'token' => shift(@accountTokens),
|
'token' => shift(@account_tokens),
|
||||||
'username' => 'ChocoBillysGreens',
|
'username' => 'ChocoBillysGreens',
|
||||||
'email' => $emailBilly,
|
'email' => $emailBilly,
|
||||||
'postcode' => 'LA1 1HT',
|
'postcode' => 'LA1 1HT',
|
||||||
'password' => $passwordBilly,
|
'password' => $passwordBilly,
|
||||||
'fulladdress' => 'Market St, Lancaster'
|
'street_name' => 'Market St',
|
||||||
|
'town' => 'Lancaster',
|
||||||
};
|
};
|
||||||
$t->post_ok('/api/register' => json => $testJson)
|
$t->post_ok('/api/register' => json => $testJson)
|
||||||
->status_is(200)
|
->status_is(200)
|
||||||
|
@ -200,20 +165,11 @@ log_out();
|
||||||
print "test 10 - Login - Rufus (cookies, customer)\n";
|
print "test 10 - Login - Rufus (cookies, customer)\n";
|
||||||
login_rufus();
|
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{
|
sub check_vars{
|
||||||
my ($searchTerm, $numValidated, $numUnvalidated) = @_;
|
my ($searchTerm, $numValidated, $numUnvalidated) = @_;
|
||||||
|
|
||||||
$t->post_ok('/api/search' => json => {
|
$t->post_ok('/api/search' => json => {
|
||||||
searchName => $searchTerm,
|
search_name => $searchTerm,
|
||||||
session_key => $session_key,
|
session_key => $session_key,
|
||||||
})
|
})
|
||||||
->status_is(200)
|
->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";
|
print "test 12 - Testing expected values with 'booths'\n";
|
||||||
#Expect 0 validated and 0 unvalidated with "booths".
|
#Expect 0 validated and 0 unvalidated with "booths".
|
||||||
check_vars("booths", 0, 0);
|
check_vars("booths", 0, 0);
|
||||||
|
|
Reference in a new issue