Improve the leak tracer - hook bless() as early as possible
[dbsrgits/DBIx-Class.git] / t / 61findnot.t
1 use strict;
2 use warnings;  
3
4 use Test::More;
5 use Test::Warn;
6 use lib qw(t/lib);
7 use DBICTest;
8
9 my $schema = DBICTest->init_schema();
10
11 plan tests => 20;
12
13 my $art = $schema->resultset("Artist")->find(4);
14 ok(!defined($art), 'Find on primary id: artist not found');
15 my @cd = $schema->resultset("CD")->find(6);
16 cmp_ok(@cd, '==', 1, 'Return something even in array context');
17 ok(@cd && !defined($cd[0]), 'Array contains an undef as only element');
18
19 $art = $schema->resultset("Artist")->find({artistid => '4'});
20 ok(!defined($art), 'Find on unique constraint: artist not found');
21 @cd = $schema->resultset("CD")->find({artist => '2', title => 'Lada-Di Lada-Da'});
22 cmp_ok(@cd, '==', 1, 'Return something even in array context');
23 ok(@cd && !defined($cd[0]), 'Array contains an undef as only element');
24
25 $art = $schema->resultset("Artist")->search({name => 'The Jesus And Mary Chain'});
26 isa_ok($art, 'DBIx::Class::ResultSet', 'get a DBIx::Class::ResultSet object');
27 my $next = $art->next;
28 ok(!defined($next), 'Nothing next in ResultSet');
29 my $cd = $schema->resultset("CD")->search({title => 'Rubbersoul'});
30 @cd = $cd->next;
31 cmp_ok(@cd, '==', 1, 'Return something even in array context');
32 ok(@cd && !defined($cd[0]), 'Array contains an undef as only element');
33
34 $art = $schema->resultset("Artist")->single({name => 'Bikini Bottom Boys'});
35 ok(!defined($art), 'Find on primary id: artist not found');
36 @cd = $schema->resultset("CD")->single({title => 'The Singles 1962-2006'});
37 cmp_ok(@cd, '==', 1, 'Return something even in array context');
38 ok(@cd && !defined($cd[0]), 'Array contains an undef as only element');
39
40 $art = $schema->resultset("Artist")->search({name => 'Random Girl Band'});
41 isa_ok($art, 'DBIx::Class::ResultSet', 'get a DBIx::Class::ResultSet object');
42 $next = $art->single;
43 ok(!defined($next), 'Nothing next in ResultSet');
44 $cd = $schema->resultset("CD")->search({title => 'Call of the West'});
45 @cd = $cd->single;
46 cmp_ok(@cd, '==', 1, 'Return something even in array context');
47 ok(@cd && !defined($cd[0]), 'Array contains an undef as only element');
48
49 $cd = $schema->resultset("CD")->first;
50 my $artist_rs = $schema->resultset("Artist")->search({ artistid => $cd->artist->artistid });
51 $art = $artist_rs->find({ name => 'some other name' }, { key => 'primary' });
52 ok($art, 'Artist found by key in the resultset');
53
54 $artist_rs = $schema->resultset("Artist");
55 warning_is {
56   $artist_rs->find({}, { key => 'primary' })
57 } "DBIx::Class::ResultSet::find(): Query returned more than one row.  SQL that returns multiple rows is DEPRECATED for ->find and ->single"
58     =>  "Non-unique find generated a cursor inexhaustion warning";
59
60 $artist_rs = $schema->resultset("Artist")->search({}, { prefetch => 'cds' });
61 warning_is {
62   $artist_rs->find({}, { key => 'primary' })
63 } "DBIx::Class::ResultSet::find(): Query returned more than one row", "Non-unique find generated a cursor inexhaustion warning";