Back out "support for prefetch from resultsource using extended_rels"
[dbsrgits/DBIx-Class.git] / t / relationship / custom.t
index 24556f9..4fb96ff 100644 (file)
@@ -5,13 +5,17 @@ use Test::More;
 use Test::Exception;
 use lib qw(t/lib);
 use DBICTest;
+use DBIC::SqlMakerTest;
 
 my $schema = DBICTest->init_schema();
 
-my $artist  = $schema->resultset("Artist")->create({ name => 'Michael Jackson', rank => 20 });
-my $artist2 = $schema->resultset("Artist")->create({ name => 'Chico Buarque', rank => 1 }) ;
-my $artist3 = $schema->resultset("Artist")->create({ name => 'Ziraldo', rank => 1 });
-my $artist4 = $schema->resultset("Artist")->create({ name => 'Paulo Caruso', rank => 20 });
+$schema->resultset('Artist')->delete;
+$schema->resultset('CD')->delete;
+
+my $artist  = $schema->resultset("Artist")->create({ artistid => 21, name => 'Michael Jackson', rank => 20 });
+my $artist2 = $schema->resultset("Artist")->create({ artistid => 22, name => 'Chico Buarque', rank => 1 }) ;
+my $artist3 = $schema->resultset("Artist")->create({ artistid => 23, name => 'Ziraldo', rank => 1 });
+my $artist4 = $schema->resultset("Artist")->create({ artistid => 24, name => 'Paulo Caruso', rank => 20 });
 
 my @artworks;
 
@@ -29,30 +33,54 @@ foreach my $artwork (@artworks) {
   $artwork->create_related('artwork_to_artist', { artist => $_ }) for ($artist3, $artist4);
 }
 
-my @cds_80s = $artist->cds_80s;
+
+my $cds_80s_rs = $artist->cds_80s;
+is_same_sql_bind($cds_80s_rs->as_query,
+                 '(SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me'.
+                 ' WHERE ( ( me.artist = ? AND ( me.year < ? AND me.year > ? ) ) ))',
+                 [
+                  [ 'me.artist' => 4    ],
+                  [ 'me.year'   => 1990 ],
+                  [ 'me.year'   => 1979 ],
+                 ]);
+my @cds_80s = $cds_80s_rs->all;
 is(@cds_80s, 6, '6 80s cds found (1980 - 1985)');
 map { ok($_->year < 1990 && $_->year > 1979) } @cds_80s;
 
-my @cds_90s = $artist2->cds_90s;
+# this is the current version, enhanced version below.
+my $cds_90s_rs = $artist2->cds_90s;
+is_same_sql_bind($cds_90s_rs->as_query,
+                 '(SELECT cds_90s.cdid, cds_90s.artist, cds_90s.title, cds_90s.year, cds_90s.genreid,'.
+                 ' cds_90s.single_track FROM artist artist__row JOIN cd cds_90s ON ( cds_90s.artist = artist__row.artistid'.
+                 ' AND ( cds_90s.year < ? AND cds_90s.year > ? ) ) WHERE ( artist__row.artistid = ? ))',
+                 [
+                  [ 'cds_90s.year' => 2000 ],
+                  [ 'cds_90s.year' => 1989 ],
+                  [ 'artist__row.artistid'  => 5    ],
+                 ]);
+
+TODO: {
+  local $TODO = 'enhanced aliasing in search_related';
+  my $cds_90s_rs = $artist2->cds_90s;
+  is_same_sql_bind($cds_90s_rs->as_query,
+                   '(SELECT me.cdid, me.artist, me.title, me.year, me.genreid,'.
+                   ' me.single_track FROM artist artist__row JOIN cd me ON ( me.artist = artist__row.artistid'.
+                   ' AND ( me.year < ? AND me.year > ? ) ) WHERE ( artist__row.artistid = ? ))',
+                   [
+                    [ 'me.year' => 2000 ],
+                    [ 'me.year' => 1989 ],
+                    [ 'artist__row.artistid'  => 5    ],
+                   ]);
+}
+my @cds_90s = $cds_90s_rs->all;
 is(@cds_90s, 6, '6 90s cds found (1990 - 1995) even with non-optimized search');
 map { ok($_->year < 2000 && $_->year > 1989) } @cds_90s;
 
-# search for all artists prefetching published cds in the 80s...
-#####
-# the join must be a prefetch, but it can't work until the collapse rewrite is finished
-# (right-side vs left-side order)
-#####
-lives_ok {
-  my @all_artists_with_80_cds = $schema->resultset("Artist")->search
-    ({ 'cds_80s.cdid' => { '!=' => undef } }, { prefetch => 'cds_80s' })->all;
-
-  is_deeply
-    ([ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds ) ],
-     [ sort (1980..1989, 1980..1985) ],
-     '16 correct cds found'
-    );
-} 'prefetchy-fetchy-fetch';
+my @cds_90s_95 = $artist2->cds_90s->search({ 'year' => 1995 });
+is(@cds_90s_95, 1, '1 90s (95) cds found even with non-optimized search');
+map { ok($_->year == 1995) } @cds_90s_95;
 
+# search for all artists prefetching published cds in the 80s...
 my @all_artists_with_80_cds = $schema->resultset("Artist")->search
   ({ 'cds_80s.cdid' => { '!=' => undef } }, { join => 'cds_80s', distinct => 1 });
 
@@ -62,6 +90,24 @@ is_deeply(
   '16 correct cds found'
 );
 
+TODO: {
+local $TODO = 'Prefetch on custom rels can not work until the collapse rewrite is finished '
+  . '(currently collapser requires a right-side (which is indeterministic) order-by)';
+lives_ok {
+
+my @all_artists_with_80_cds_pref = $schema->resultset("Artist")->search
+  ({ 'cds_80s.cdid' => { '!=' => undef } }, { prefetch => 'cds_80s' });
+
+is_deeply(
+  [ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds_pref ) ],
+  [ sort (1980..1989, 1980..1985) ],
+  '16 correct cds found'
+);
+
+} 'prefetchy-fetchy-fetch';
+} # end of TODO
+
+
 # try to create_related a 80s cd
 throws_ok {
   $artist->create_related('cds_80s', { title => 'related creation 1' });