Test for an insane yet valid rel condition with subquery
[dbsrgits/DBIx-Class.git] / t / relationship / custom.t
index 68936e3..c136224 100644 (file)
@@ -43,10 +43,19 @@ is_same_sql_bind(
     WHERE ( ( me.artist = ? AND ( me.year < ? AND me.year > ? ) ) )
   )',
   [
-    [ 'me.artist' => 21   ],
-    [ 'me.year' => 1990 ],
-    [ 'me.year' => 1979 ],
-  ]
+    [
+      { sqlt_datatype => 'integer', dbic_colname => 'me.artist' }
+        => 21
+    ],
+    [
+      { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' }
+        => 1990
+    ],
+    [
+      { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' }
+        => 1979
+    ],
+  ],
 );
 my @cds_80s = $cds_80s_rs->all;
 is(@cds_80s, 6, '6 80s cds found (1980 - 1985)');
@@ -64,9 +73,17 @@ is_same_sql_bind(
       WHERE ( artist__row.artistid = ? )
   )',
   [
-    [ 'me.year' => 2000 ],
-    [ 'me.year' => 1989 ],
-    [ 'artist__row.artistid' => 22 ],
+    [
+      { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' }
+        => 2000
+    ],
+    [
+      { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' }
+        => 1989
+    ],
+    [ { sqlt_datatype => 'integer', dbic_colname => 'artist__row.artistid' }
+        => 22
+    ],
   ]
 );
 my @cds_90s = $cds_90s_rs->all;
@@ -110,7 +127,8 @@ is_deeply(
 # try to create_related a 80s cd
 throws_ok {
   $artist->create_related('cds_80s', { title => 'related creation 1' });
-} qr/\Qunable to set_from_related via complex 'cds_80s' condition on column(s): 'year'/, 'Create failed - complex cond';
+} qr/\QCustom relationship 'cds_80s' not definitive - returns conditions instead of values for column(s): 'year'/,
+'Create failed - complex cond';
 
 # now supply an explicit arg overwriting the ambiguous cond
 my $id_2020 = $artist->create_related('cds_80s', { title => 'related creation 2', year => '2020' })->id;
@@ -131,28 +149,40 @@ is(
 # try a specific everything via a non-simplified rel
 throws_ok {
   $artist->create_related('cds_90s', { title => 'related_creation 4', year => '2038' });
-} qr/\Qunable to set_from_related - no simplified condition available for 'cds_90s'/, 'Create failed - non-simplified rel';
+} qr/\QCustom relationship 'cds_90s' does not resolve to a join-free condition fragment/,
+'Create failed - non-simplified rel';
 
 # Do a self-join last-entry search
-my @last_track_ids;
+my @last_tracks;
 for my $cd ($schema->resultset('CD')->search ({}, { order_by => 'cdid'})->all) {
-  push @last_track_ids, $cd->tracks
-                            ->search ({}, { order_by => { -desc => 'position'} })
-                              ->get_column ('trackid')
-                                ->next;
+  push @last_tracks, $cd->tracks
+                         ->search ({}, { order_by => { -desc => 'position'} })
+                          ->next || ();
 }
 
-my $last_tracks = $schema->resultset('Track')->search (
+my $last_tracks_rs = $schema->resultset('Track')->search (
   {'next_track.trackid' => undef},
   { join => 'next_track', order_by => 'me.cd' },
 );
 
 is_deeply (
-  [$last_tracks->get_column ('trackid')->all],
-  [ grep { $_ } @last_track_ids ],
+  [$last_tracks_rs->get_column ('trackid')->all],
+  [ map { $_->trackid } @last_tracks ],
   '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');
@@ -187,4 +217,27 @@ lives_ok {
 is(scalar @artists, 1, 'only one artist is associated');
 
 
+# Make a single for each last_track
+my @singles = map {
+  $_->create_related('cd_single', {
+    title => $_->title . ' (the single)',
+    artist => $artist,
+    year => 1999,
+  }) } @last_tracks
+;
+
+# See if chaining works
+is_deeply (
+  [ map { $_->title } $last_tracks_rs->search_related('cd_single')->all ],
+  [ map { $_->title } @singles ],
+  'Retrieved singles in proper order'
+);
+
+# See if prefetch works
+is_deeply (
+  [ map { $_->cd_single->title } $last_tracks_rs->search({}, { prefetch => 'cd_single' })->all ],
+  [ map { $_->title } @singles ],
+  'Prefetched singles in proper order'
+);
+
 done_testing;