fixed sql mistakes from hierarchical query tests
[dbsrgits/DBIx-Class.git] / t / 61findnot.t
CommitLineData
57dc206e 1use strict;
2use warnings;
3
4use Test::More;
550adccc 5use Test::Warn;
57dc206e 6use lib qw(t/lib);
7use DBICTest;
8
9my $schema = DBICTest->init_schema();
10
550adccc 11plan tests => 20;
57dc206e 12
13my $art = $schema->resultset("Artist")->find(4);
14ok(!defined($art), 'Find on primary id: artist not found');
15my @cd = $schema->resultset("CD")->find(6);
16cmp_ok(@cd, '==', 1, 'Return something even in array context');
17ok(@cd && !defined($cd[0]), 'Array contains an undef as only element');
18
19$art = $schema->resultset("Artist")->find({artistid => '4'});
20ok(!defined($art), 'Find on unique constraint: artist not found');
21@cd = $schema->resultset("CD")->find({artist => '2', title => 'Lada-Di Lada-Da'});
22cmp_ok(@cd, '==', 1, 'Return something even in array context');
23ok(@cd && !defined($cd[0]), 'Array contains an undef as only element');
24
25$art = $schema->resultset("Artist")->search({name => 'The Jesus And Mary Chain'});
26isa_ok($art, 'DBIx::Class::ResultSet', 'get a DBIx::Class::ResultSet object');
27my $next = $art->next;
28ok(!defined($next), 'Nothing next in ResultSet');
29my $cd = $schema->resultset("CD")->search({title => 'Rubbersoul'});
30@cd = $cd->next;
31cmp_ok(@cd, '==', 1, 'Return something even in array context');
32ok(@cd && !defined($cd[0]), 'Array contains an undef as only element');
33
34$art = $schema->resultset("Artist")->single({name => 'Bikini Bottom Boys'});
35ok(!defined($art), 'Find on primary id: artist not found');
36@cd = $schema->resultset("CD")->single({title => 'The Singles 1962-2006'});
37cmp_ok(@cd, '==', 1, 'Return something even in array context');
38ok(@cd && !defined($cd[0]), 'Array contains an undef as only element');
39
40$art = $schema->resultset("Artist")->search({name => 'Random Girl Band'});
41isa_ok($art, 'DBIx::Class::ResultSet', 'get a DBIx::Class::ResultSet object');
42$next = $art->single;
43ok(!defined($next), 'Nothing next in ResultSet');
44$cd = $schema->resultset("CD")->search({title => 'Call of the West'});
45@cd = $cd->single;
46cmp_ok(@cd, '==', 1, 'Return something even in array context');
47ok(@cd && !defined($cd[0]), 'Array contains an undef as only element');
550adccc 48
49$cd = $schema->resultset("CD")->first;
50my $artist_rs = $schema->resultset("Artist")->search({ artistid => $cd->artist->artistid });
51$art = $artist_rs->find({ name => 'some other name' }, { key => 'primary' });
52ok($art, 'Artist found by key in the resultset');
53
54$artist_rs = $schema->resultset("Artist");
55warning_is {
56 $artist_rs->find({}, { key => 'primary' })
1a4e8d7c 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";
550adccc 59
60$artist_rs = $schema->resultset("Artist")->search({}, { prefetch => 'cds' });
61warning_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";