Test for an insane yet valid rel condition with subquery
Moritz Onken [Tue, 25 Jan 2011 16:08:16 +0000 (17:08 +0100)]
t/lib/DBICTest/Schema/CD.pm
t/relationship/custom.t

index 552f16e..9a147d8 100644 (file)
@@ -93,4 +93,37 @@ __PACKAGE__->belongs_to('genre_inefficient', 'DBICTest::Schema::Genre',
     },
 );
 
+
+# This is insane. Don't ever do anything like that
+# This is for testing purposes only!
+
+# mst: mo: DBIC is an "object relational mapper"
+# mst: mo: not an "object relational hider-because-mo-doesn't-understand-databases
+# ribasushi: mo: try it with a subselect nevertheless, I'd love to be proven wrong
+# ribasushi: mo: does sqlite actually take this?
+# ribasushi: an order in a correlated subquery is insane - how long does it take you on real data?
+
+__PACKAGE__->might_have(
+    'last_track',
+    'DBICTest::Schema::Track',
+    sub {
+        my $args = shift;
+        return (
+            {
+                "$args->{foreign_alias}.trackid" => { '=' =>
+                    $args->{self_resultsource}->schema->resultset('Track')->search(
+                       { 'correlated_tracks.cd' => { -ident => "$args->{self_alias}.cdid" } },
+                       {
+                          order_by => { -desc => 'position' },
+                          rows     => 1,
+                          alias    => 'correlated_tracks',
+                          columns  => ['trackid']
+                       },
+                    )->as_query
+                }
+            }
+        );
+    },
+);
+
 1;
index 99a0786..c136224 100644 (file)
@@ -171,6 +171,18 @@ is_deeply (
   'last group-entry via self-join works',
 );
 
+is_deeply (
+  [map { $_->last_track->id } grep { $_->last_track } $schema->resultset('CD')->search ({}, { order_by => 'cdid', prefetch => 'last_track'})->all],
+  [ map { $_->trackid } @last_tracks ],
+  'last_track via insane subquery condition works',
+);
+
+is_deeply (
+  [map { $_->last_track->id } grep { $_->last_track } $schema->resultset('CD')->search ({}, { order_by => 'cdid'})->all],
+  [ map { $_->trackid } @last_tracks ],
+  'last_track via insane subquery condition works, even without prefetch',
+);
+
 my $artwork = $schema->resultset('Artwork')->search({},{ order_by => 'cd_id' })->first;
 my @artists = $artwork->artists->all;
 is(scalar @artists, 2, 'the two artists are associated');