Introduce GOVERNANCE document and empty RESOLUTIONS file.
[dbsrgits/DBIx-Class.git] / t / 61findnot.t
index ee7b582..e9fe1ac 100644 (file)
@@ -1,14 +1,16 @@
+BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
+
 use strict;
-use warnings;  
+use warnings;
 
 use Test::More;
-use lib qw(t/lib);
+use Test::Warn;
+use Test::Exception;
+
 use DBICTest;
 
 my $schema = DBICTest->init_schema();
 
-plan tests => 17;
-
 my $art = $schema->resultset("Artist")->find(4);
 ok(!defined($art), 'Find on primary id: artist not found');
 my @cd = $schema->resultset("CD")->find(6);
@@ -44,3 +46,58 @@ $cd = $schema->resultset("CD")->search({title => 'Call of the West'});
 @cd = $cd->single;
 cmp_ok(@cd, '==', 1, 'Return something even in array context');
 ok(@cd && !defined($cd[0]), 'Array contains an undef as only element');
+
+$cd = $schema->resultset("CD")->first;
+my $artist_rs = $schema->resultset("Artist")->search({ artistid => $cd->artist->artistid });
+for my $key ('', 'primary') {
+  my $art = $artist_rs->find({ name => 'some other name' }, { $key ? (key => $key) : () });
+  is($art->artistid, $cd->get_column('artist'), "Artist found through @{[ $key ? 'explicit' : 'implicit' ]} key locked in the resultset");
+}
+
+# collapsing and non-collapsing are separate codepaths, thus the separate tests
+my $ea_count = 0;
+$schema->exception_action(sub {
+  $ea_count++;
+  die @_;
+});
+
+$artist_rs = $schema->resultset("Artist");
+
+warnings_exist {
+  $artist_rs->find({})
+} qr/\QQuery returned more than one row.  SQL that returns multiple rows is DEPRECATED for ->find and ->single/
+    =>  "Non-unique find generated a cursor inexhaustion warning";
+
+throws_ok {
+  $artist_rs->find({}, { key => 'primary' })
+} qr/Unable to satisfy requested constraint 'primary'/;
+
+for (1, 0) {
+  local $ENV{DBIC_NULLABLE_KEY_NOWARN};
+  warnings_like
+    sub {
+      $artist_rs->find({ artistid => undef }, { key => 'primary' })
+    },
+    $_ ? [
+      qr/undef values supplied for requested unique constraint.+almost certainly not what you wanted/,
+    ] : [],
+    'One warning on NULL conditions for constraint'
+  ;
+}
+
+is( $ea_count, 1, "exception action invoked the expected amount of times (just the exception)" );
+
+$schema->exception_action(undef);
+
+
+$artist_rs = $schema->resultset("Artist")->search({}, { prefetch => 'cds' });
+
+warnings_exist {
+  $artist_rs->find({})
+} qr/\QDBIx::Class::ResultSet::find(): Query returned more than one row/, "Non-unique find generated a cursor inexhaustion warning";
+
+throws_ok {
+  $artist_rs->find({}, { key => 'primary' })
+} qr/Unable to satisfy requested constraint 'primary'/;
+
+done_testing;