getRequestType with no arguments returns full list

I debated whether to create a getRequestTypes() instead, but this seemed
reasonable.  I am too far out of Perl5 programming culture to know if
this sort of interface is recommended practice.
This commit is contained in:
Bradley M. Kuhn 2016-03-11 10:58:05 -08:00
parent 7398b2f3f7
commit 1577a5613c
2 changed files with 29 additions and 9 deletions

View file

@ -682,22 +682,27 @@ Arguments:
=item type
A string describing the request.
A string describing the request. Argument is optional.
=back
Returns the id value of the request_type entry. undef is returned if there
is no request of that type.
If type is given, returns a scalar the id value of the request_type entry.
undef is returned if there is no request of that type.
If type is not given, a list of all known request types is returned.
=cut
sub getRequestType($$) {
sub getRequestType($;$) {
my($self, $type) = @_;
return undef if not defined $type;
if (not defined $type) {
return @{$self->dbh()->selectcol_arrayref("SELECT type, id FROM request_type ORDER BY id", { Columns=>[1] })};
} else {
my $val = $self->dbh()->selectall_hashref("SELECT id, type FROM request_type WHERE type = '$type'", 'type');
return $val->{$type}{id} if (defined $val and defined $val->{$type} and defined $val->{$type}{id});
return undef;
}
}
######################################################################

View file

@ -8,7 +8,7 @@
use strict;
use warnings;
use Test::More tests => 331;
use Test::More tests => 334;
use Test::Exception;
use Sub::Override;
use File::Temp qw/tempfile/;
@ -413,6 +413,10 @@ lives_ok { $tShirt0RequestTypeId = $sp->addRequestType('t-shirt-0'); }
ok( (defined $tShirt0RequestTypeId and looks_like_number($tShirt0RequestTypeId) and $tShirt0RequestTypeId > 0),
"addRequestType: id is a number");
my @allRequestsList = $sp->getRequestType();
is_deeply(\@allRequestsList, ['t-shirt-0' ], "getRequestType: no argument returns full list of request types (1)");
my $testSameRequestType;
lives_ok { $testSameRequestType = $sp->addRequestType('t-shirt-0'); }
@ -490,6 +494,11 @@ my $joinEmailListRequestId = $sp->getRequestType("join-announce-email-list");
ok((defined $joinEmailListRequestId and looks_like_number($joinEmailListRequestId) and $joinEmailListRequestId > 0),
"addRequest: underlying call to addRequestType works properly, per getRequestType");
@allRequestsList = $sp->getRequestType();
is_deeply(\@allRequestsList, ['t-shirt-0', 'join-announce-email-list'],
"getRequestType: no argument returns full list of request types (2)");
my $tshirtSmallRequestId;
lives_ok { $tshirtSmallRequestId =
@ -501,7 +510,13 @@ lives_ok { $tshirtSmallRequestId =
ok( (defined $tshirtSmallRequestId and looks_like_number($tshirtSmallRequestId) and $tshirtSmallRequestId > 0),
"addRequest: successful call returns an integer id.");
@allRequestsList = $sp->getRequestType();
is_deeply(\@allRequestsList, ['t-shirt-0', 'join-announce-email-list', 't-shirt-small-only' ],
"getRequestType: no argument returns full list of request types (3)");
my $tShirt0RequestId;
lives_ok { $tShirt0RequestId =
$sp->addRequest({ donorId => $drapperId, requestTypeId => $tShirt0RequestTypeId,
requestConfigurationId => $tShirt0Data->{$tShirt0RequestTypeId}{'MenL'} }); }