findDonor: empty criteria finds everyone.

Rather than die() when the criteria list is empty, instead return the
entire list.
This commit is contained in:
Bradley M. Kuhn 2015-12-30 20:53:28 -08:00
parent 97f08a7bd8
commit dbeb98d0f2
2 changed files with 18 additions and 6 deletions

View file

@ -1081,6 +1081,8 @@ Arguments:
A hash reference, the following keys are considered, and are "anded" together A hash reference, the following keys are considered, and are "anded" together
-- in that the donor sought must have all these criteria to be found. -- in that the donor sought must have all these criteria to be found.
If no criteria are given, all donors are returned.
=over =over
=item emailAddress =item emailAddress
@ -1104,8 +1106,11 @@ Returns a list of donorIds that meets the criteria, or none if not found.
sub findDonor($$) { sub findDonor($$) {
my($self, $params) = @_; my($self, $params) = @_;
die "findDonor: no search criteria given"
unless defined $params->{ledgerEntityId} or defined $params->{emailAddress}; unless (defined $params->{ledgerEntityId} or defined $params->{emailAddress}) {
my $rr = $self->dbh()->selectall_hashref("SELECT id FROM donor", 'id');
return keys %$rr;
}
my @donorIds; my @donorIds;
if (not defined $params->{emailAddress}) { if (not defined $params->{emailAddress}) {

View file

@ -8,7 +8,7 @@
use strict; use strict;
use warnings; use warnings;
use Test::More tests => 259; use Test::More tests => 260;
use Test::Exception; use Test::Exception;
use Sub::Override; use Sub::Override;
use File::Temp qw/tempfile/; use File::Temp qw/tempfile/;
@ -692,8 +692,15 @@ is($ret, 'drapper@example.org', "getPreferredEmailAddress: ....and returns the c
my @lookupDonorIds; my @lookupDonorIds;
dies_ok { @lookupDonorIds = $sp->findDonor({}); } lives_ok { @lookupDonorIds = $sp->findDonor({}); }
"findDonor: no search criteria dies"; "findDonor: no search criteria succeeds and...";
my(%vals);
@vals{@lookupDonorIds} = @lookupDonorIds;
is_deeply(\%vals, { $campbellId => $campbellId, $sterlingId => $sterlingId,
$olsonId => $olsonId, $drapperId => $drapperId },
"findDonor: ... and returns all donorIds.");
lives_ok { @lookupDonorIds = $sp->findDonor({ledgerEntityId => "NotFound" }); } lives_ok { @lookupDonorIds = $sp->findDonor({ledgerEntityId => "NotFound" }); }
"findDonor: 1 lookup of known missing succeeds ..."; "findDonor: 1 lookup of known missing succeeds ...";
@ -733,7 +740,7 @@ is_deeply(\@lookupDonorIds, [$olsonId], "findDonor: ... and finds right entry.")
lives_ok { @lookupDonorIds = $sp->findDonor({emailAddress => 'everyone@example.net'}); } lives_ok { @lookupDonorIds = $sp->findDonor({emailAddress => 'everyone@example.net'}); }
"findDonor: single criteria find expecting multiple records succeeds..."; "findDonor: single criteria find expecting multiple records succeeds...";
my(%vals); %vals = ();
@vals{@lookupDonorIds} = @lookupDonorIds; @vals{@lookupDonorIds} = @lookupDonorIds;
is_deeply(\%vals, { $olsonId => $olsonId, $drapperId => $drapperId }, "findDonor: ... and finds the right entires."); is_deeply(\%vals, { $olsonId => $olsonId, $drapperId => $drapperId }, "findDonor: ... and finds the right entires.");