Institute a central "load this first in testing" package
[dbsrgits/DBIx-Class.git] / t / 61findnot.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Warn;
8 use Test::Exception;
9
10 use DBICTest;
11
12 my $schema = DBICTest->init_schema();
13
14 my $art = $schema->resultset("Artist")->find(4);
15 ok(!defined($art), 'Find on primary id: artist not found');
16 my @cd = $schema->resultset("CD")->find(6);
17 cmp_ok(@cd, '==', 1, 'Return something even in array context');
18 ok(@cd && !defined($cd[0]), 'Array contains an undef as only element');
19
20 $art = $schema->resultset("Artist")->find({artistid => '4'});
21 ok(!defined($art), 'Find on unique constraint: artist not found');
22 @cd = $schema->resultset("CD")->find({artist => '2', title => 'Lada-Di Lada-Da'});
23 cmp_ok(@cd, '==', 1, 'Return something even in array context');
24 ok(@cd && !defined($cd[0]), 'Array contains an undef as only element');
25
26 $art = $schema->resultset("Artist")->search({name => 'The Jesus And Mary Chain'});
27 isa_ok($art, 'DBIx::Class::ResultSet', 'get a DBIx::Class::ResultSet object');
28 my $next = $art->next;
29 ok(!defined($next), 'Nothing next in ResultSet');
30 my $cd = $schema->resultset("CD")->search({title => 'Rubbersoul'});
31 @cd = $cd->next;
32 cmp_ok(@cd, '==', 1, 'Return something even in array context');
33 ok(@cd && !defined($cd[0]), 'Array contains an undef as only element');
34
35 $art = $schema->resultset("Artist")->single({name => 'Bikini Bottom Boys'});
36 ok(!defined($art), 'Find on primary id: artist not found');
37 @cd = $schema->resultset("CD")->single({title => 'The Singles 1962-2006'});
38 cmp_ok(@cd, '==', 1, 'Return something even in array context');
39 ok(@cd && !defined($cd[0]), 'Array contains an undef as only element');
40
41 $art = $schema->resultset("Artist")->search({name => 'Random Girl Band'});
42 isa_ok($art, 'DBIx::Class::ResultSet', 'get a DBIx::Class::ResultSet object');
43 $next = $art->single;
44 ok(!defined($next), 'Nothing next in ResultSet');
45 $cd = $schema->resultset("CD")->search({title => 'Call of the West'});
46 @cd = $cd->single;
47 cmp_ok(@cd, '==', 1, 'Return something even in array context');
48 ok(@cd && !defined($cd[0]), 'Array contains an undef as only element');
49
50 $cd = $schema->resultset("CD")->first;
51 my $artist_rs = $schema->resultset("Artist")->search({ artistid => $cd->artist->artistid });
52 for 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 }
56
57 # collapsing and non-collapsing are separate codepaths, thus the separate tests
58 my $ea_count = 0;
59 $schema->exception_action(sub {
60   $ea_count++;
61   die @_;
62 });
63
64 $artist_rs = $schema->resultset("Artist");
65
66 warnings_exist {
67   $artist_rs->find({})
68 } qr/\QQuery returned more than one row.  SQL that returns multiple rows is DEPRECATED for ->find and ->single/
69     =>  "Non-unique find generated a cursor inexhaustion warning";
70
71 throws_ok {
72   $artist_rs->find({}, { key => 'primary' })
73 } qr/Unable to satisfy requested constraint 'primary'/;
74
75 for (1, 0) {
76   local $ENV{DBIC_NULLABLE_KEY_NOWARN};
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
88 is( $ea_count, 1, "exception action invoked the expected amount of times (just the exception)" );
89
90 $schema->exception_action(undef);
91
92
93 $artist_rs = $schema->resultset("Artist")->search({}, { prefetch => 'cds' });
94
95 warnings_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";
98
99 throws_ok {
100   $artist_rs->find({}, { key => 'primary' })
101 } qr/Unable to satisfy requested constraint 'primary'/;
102
103 done_testing;