Step up the error reporting on unexpected NULLs during collapse
[dbsrgits/DBIx-Class.git] / t / resultset / misled_rowparser.t
diff --git a/t/resultset/misled_rowparser.t b/t/resultset/misled_rowparser.t
new file mode 100644 (file)
index 0000000..2c76aed
--- /dev/null
@@ -0,0 +1,63 @@
+BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
+
+use strict;
+use warnings;
+
+use Test::More;
+use Test::Exception;
+
+use DBICTest;
+my $schema = DBICTest->init_schema();
+
+# The nullchecks metadata for this collapse resolution is:
+#
+# mandatory => { 0 => 1 }
+# from_first_encounter => [ [ 1, 2, 3 ] ]
+# all_or_nothing => [ { 1 => 1, 2 => 1 } ]
+#
+my $rs = $schema->resultset('Artist')->search({}, {
+  collapse => 1,
+  join => { cds => 'tracks' },
+  columns => [qw(
+    me.artistid
+    cds.artist
+    cds.title
+  ),
+  { 'cds.tracks.title' => 'tracks.title' },
+  ],
+});
+
+my @cases = (
+  "'artistid'"
+    => [ undef, 0, 0, undef ],
+
+  "'artistid', 'cds.title'"
+    => [ undef, 0, undef, undef ],
+
+  "'artistid', 'cds.artist'"
+    => [ undef, undef, 0, undef ],
+
+  "'cds.artist'"
+    => [ 0, undef, 0, 0 ],
+
+  "'cds.title'"
+    => [ 0, 0, undef, 0 ],
+
+  # petrhaps need to report cds.title here as well, but that'll complicate checks even more...
+  "'cds.artist'"
+    => [ 0, undef, undef, 0 ],
+);
+
+while (@cases) {
+  my ($err, $cursor) = splice @cases, 0, 2;
+
+  $rs->{_stashed_rows} = [ $cursor ];
+
+  throws_ok
+    { $rs->next }
+    qr/\Qthe following columns are declared (or defaulted to) non-nullable within DBIC but NULLs were retrieved from storage: $err within data row/,
+    "Correct exception on non-nullable-yet-NULL $err"
+  ;
+}
+
+done_testing;