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