Commit | Line | Data |
c0329273 |
1 | BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) } |
2 | |
57dc206e |
3 | use strict; |
b7743dab |
4 | use warnings; |
57dc206e |
5 | |
6 | use Test::More; |
550adccc |
7 | use Test::Warn; |
b7743dab |
8 | use Test::Exception; |
c0329273 |
9 | |
57dc206e |
10 | use DBICTest; |
11 | |
12 | my $schema = DBICTest->init_schema(); |
13 | |
57dc206e |
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'); |
550adccc |
49 | |
50 | $cd = $schema->resultset("CD")->first; |
51 | my $artist_rs = $schema->resultset("Artist")->search({ artistid => $cd->artist->artistid }); |
d681f1bb |
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 | } |
550adccc |
56 | |
b7743dab |
57 | # collapsing and non-collapsing are separate codepaths, thus the separate tests |
ddcc02d1 |
58 | my $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 |
66 | warnings_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 |
71 | throws_ok { |
72 | $artist_rs->find({}, { key => 'primary' }) |
cf48da0c |
73 | } qr/Unable to satisfy requested constraint 'primary'/; |
550adccc |
74 | |
65c1217d |
75 | for (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 |
88 | is( $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 |
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"; |
65c1217d |
98 | |
b7743dab |
99 | throws_ok { |
550adccc |
100 | $artist_rs->find({}, { key => 'primary' }) |
cf48da0c |
101 | } qr/Unable to satisfy requested constraint 'primary'/; |
b7743dab |
102 | |
103 | done_testing; |