From: Matt S Trout Date: Sun, 20 May 2007 00:05:23 +0000 (+0000) Subject: find/next change to return undef rather than () on fail from Bernhard Graf X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=57dc206ebd7065abc265ef81c729f16b97e6ab5a;p=dbsrgits%2FDBIx-Class-Historic.git find/next change to return undef rather than () on fail from Bernhard Graf --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 58a411a..98d660d 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -543,7 +543,7 @@ sub single { $attrs->{where}, $attrs ); - return (@data ? ($self->_construct_object(@data))[0] : ()); + return (@data ? ($self->_construct_object(@data))[0] : undef); } # _is_unique_query @@ -738,7 +738,7 @@ sub next { ? @{delete $self->{stashed_row}} : $self->cursor->next ); - return unless (@row); + return undef unless (@row); my ($row, @more) = $self->_construct_object(@row); $self->{stashed_objects} = \@more if @more; return $row; diff --git a/t/61findnot.t b/t/61findnot.t new file mode 100644 index 0000000..ee7b582 --- /dev/null +++ b/t/61findnot.t @@ -0,0 +1,46 @@ +use strict; +use warnings; + +use Test::More; +use lib qw(t/lib); +use DBICTest; + +my $schema = DBICTest->init_schema(); + +plan tests => 17; + +my $art = $schema->resultset("Artist")->find(4); +ok(!defined($art), 'Find on primary id: artist not found'); +my @cd = $schema->resultset("CD")->find(6); +cmp_ok(@cd, '==', 1, 'Return something even in array context'); +ok(@cd && !defined($cd[0]), 'Array contains an undef as only element'); + +$art = $schema->resultset("Artist")->find({artistid => '4'}); +ok(!defined($art), 'Find on unique constraint: artist not found'); +@cd = $schema->resultset("CD")->find({artist => '2', title => 'Lada-Di Lada-Da'}); +cmp_ok(@cd, '==', 1, 'Return something even in array context'); +ok(@cd && !defined($cd[0]), 'Array contains an undef as only element'); + +$art = $schema->resultset("Artist")->search({name => 'The Jesus And Mary Chain'}); +isa_ok($art, 'DBIx::Class::ResultSet', 'get a DBIx::Class::ResultSet object'); +my $next = $art->next; +ok(!defined($next), 'Nothing next in ResultSet'); +my $cd = $schema->resultset("CD")->search({title => 'Rubbersoul'}); +@cd = $cd->next; +cmp_ok(@cd, '==', 1, 'Return something even in array context'); +ok(@cd && !defined($cd[0]), 'Array contains an undef as only element'); + +$art = $schema->resultset("Artist")->single({name => 'Bikini Bottom Boys'}); +ok(!defined($art), 'Find on primary id: artist not found'); +@cd = $schema->resultset("CD")->single({title => 'The Singles 1962-2006'}); +cmp_ok(@cd, '==', 1, 'Return something even in array context'); +ok(@cd && !defined($cd[0]), 'Array contains an undef as only element'); + +$art = $schema->resultset("Artist")->search({name => 'Random Girl Band'}); +isa_ok($art, 'DBIx::Class::ResultSet', 'get a DBIx::Class::ResultSet object'); +$next = $art->single; +ok(!defined($next), 'Nothing next in ResultSet'); +$cd = $schema->resultset("CD")->search({title => 'Call of the West'}); +@cd = $cd->single; +cmp_ok(@cd, '==', 1, 'Return something even in array context'); +ok(@cd && !defined($cd[0]), 'Array contains an undef as only element');