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