'undef =>' isn't what one would want
[dbsrgits/DBIx-Class-Historic.git] / t / 61findnot.t
CommitLineData
57dc206e 1use strict;
b7743dab 2use warnings;
57dc206e 3
4use Test::More;
550adccc 5use Test::Warn;
b7743dab 6use Test::Exception;
57dc206e 7use lib qw(t/lib);
8use DBICTest;
9
10my $schema = DBICTest->init_schema();
11
57dc206e 12my $art = $schema->resultset("Artist")->find(4);
13ok(!defined($art), 'Find on primary id: artist not found');
14my @cd = $schema->resultset("CD")->find(6);
15cmp_ok(@cd, '==', 1, 'Return something even in array context');
16ok(@cd && !defined($cd[0]), 'Array contains an undef as only element');
17
18$art = $schema->resultset("Artist")->find({artistid => '4'});
19ok(!defined($art), 'Find on unique constraint: artist not found');
20@cd = $schema->resultset("CD")->find({artist => '2', title => 'Lada-Di Lada-Da'});
21cmp_ok(@cd, '==', 1, 'Return something even in array context');
22ok(@cd && !defined($cd[0]), 'Array contains an undef as only element');
23
24$art = $schema->resultset("Artist")->search({name => 'The Jesus And Mary Chain'});
25isa_ok($art, 'DBIx::Class::ResultSet', 'get a DBIx::Class::ResultSet object');
26my $next = $art->next;
27ok(!defined($next), 'Nothing next in ResultSet');
28my $cd = $schema->resultset("CD")->search({title => 'Rubbersoul'});
29@cd = $cd->next;
30cmp_ok(@cd, '==', 1, 'Return something even in array context');
31ok(@cd && !defined($cd[0]), 'Array contains an undef as only element');
32
33$art = $schema->resultset("Artist")->single({name => 'Bikini Bottom Boys'});
34ok(!defined($art), 'Find on primary id: artist not found');
35@cd = $schema->resultset("CD")->single({title => 'The Singles 1962-2006'});
36cmp_ok(@cd, '==', 1, 'Return something even in array context');
37ok(@cd && !defined($cd[0]), 'Array contains an undef as only element');
38
39$art = $schema->resultset("Artist")->search({name => 'Random Girl Band'});
40isa_ok($art, 'DBIx::Class::ResultSet', 'get a DBIx::Class::ResultSet object');
41$next = $art->single;
42ok(!defined($next), 'Nothing next in ResultSet');
43$cd = $schema->resultset("CD")->search({title => 'Call of the West'});
44@cd = $cd->single;
45cmp_ok(@cd, '==', 1, 'Return something even in array context');
46ok(@cd && !defined($cd[0]), 'Array contains an undef as only element');
550adccc 47
48$cd = $schema->resultset("CD")->first;
49my $artist_rs = $schema->resultset("Artist")->search({ artistid => $cd->artist->artistid });
d681f1bb 50for my $key ('', 'primary') {
51 my $art = $artist_rs->find({ name => 'some other name' }, { $key ? (key => $key) : () });
52 is($art->artistid, $cd->get_column('artist'), "Artist found through @{[ $key ? 'explicit' : 'implicit' ]} key locked in the resultset");
53}
550adccc 54
b7743dab 55# collapsing and non-collapsing are separate codepaths, thus the separate tests
56
65c1217d 57
550adccc 58$artist_rs = $schema->resultset("Artist");
65c1217d 59
b7743dab 60warnings_exist {
61 $artist_rs->find({})
5e0e5426 62} qr/\QQuery returned more than one row. SQL that returns multiple rows is DEPRECATED for ->find and ->single/
1a4e8d7c 63 => "Non-unique find generated a cursor inexhaustion warning";
65c1217d 64
b7743dab 65throws_ok {
66 $artist_rs->find({}, { key => 'primary' })
cf48da0c 67} qr/Unable to satisfy requested constraint 'primary'/;
550adccc 68
65c1217d 69for (1, 0) {
eed5492f 70 local $ENV{DBIC_NULLABLE_KEY_NOWARN};
65c1217d 71 warnings_like
72 sub {
73 $artist_rs->find({ artistid => undef }, { key => 'primary' })
74 },
75 $_ ? [
76 qr/undef values supplied for requested unique constraint.+almost certainly not what you wanted/,
77 ] : [],
78 'One warning on NULL conditions for constraint'
79 ;
80}
81
82
550adccc 83$artist_rs = $schema->resultset("Artist")->search({}, { prefetch => 'cds' });
65c1217d 84
b7743dab 85warnings_exist {
86 $artist_rs->find({})
87} qr/\QDBIx::Class::ResultSet::find(): Query returned more than one row/, "Non-unique find generated a cursor inexhaustion warning";
65c1217d 88
b7743dab 89throws_ok {
550adccc 90 $artist_rs->find({}, { key => 'primary' })
cf48da0c 91} qr/Unable to satisfy requested constraint 'primary'/;
b7743dab 92
93done_testing;