Merge 'mystery_join' into 'trunk'
Peter Rabbitson [Wed, 10 Jun 2009 11:33:37 +0000 (11:33 +0000)]
r6544@Thesaurus (orig r6543):  ribasushi | 2009-06-08 11:44:59 +0200
Attempt to figure out why do we repeat joins on complex search_related
r6586@Thesaurus (orig r6585):  ribasushi | 2009-06-10 11:22:05 +0200
Move the rs preservation test to a more suitable place
r6589@Thesaurus (orig r6588):  ribasushi | 2009-06-10 13:15:48 +0200
Finally commit trully failing test
r6590@Thesaurus (orig r6589):  ribasushi | 2009-06-10 13:33:14 +0200
Duh, this was a pretty simple bug

lib/DBIx/Class/ResultSet.pm
t/lib/DBICTest/Schema/Artist.pm
t/search/preserve_original_rs.t [moved from t/prefetch/pollute_already_joined.t with 61% similarity]

index 7b34a86..515e45f 100644 (file)
@@ -2430,12 +2430,15 @@ sub _resolve_from {
   my $source = $self->result_source;
   my $attrs = $self->{attrs};
 
-  my $from = $attrs->{from}
-    || [ {
-      -result_source => $source,
-      -alias => $attrs->{alias},
-      $attrs->{alias} => $source->from,
-    } ];
+  my $from = [ @{
+      $attrs->{from}
+        ||
+      [{
+        -result_source => $source,
+        -alias => $attrs->{alias},
+        $attrs->{alias} => $source->from,
+      }]
+  }];
 
   my $seen = { %{$attrs->{seen_join} || {} } };
 
index b9794fe..be46d16 100644 (file)
@@ -54,9 +54,9 @@ __PACKAGE__->has_many(
 );
 
 __PACKAGE__->has_many(
-    artist_to_artwork => 'DBICTest::Schema::Artwork_to_Artist' => 'artist_id'
+    artwork_to_artist => 'DBICTest::Schema::Artwork_to_Artist' => 'artist_id'
 );
-__PACKAGE__->many_to_many('artworks', 'artist_to_artwork', 'artwork');
+__PACKAGE__->many_to_many('artworks', 'artwork_to_artist', 'artwork');
 
 
 sub sqlt_deploy_hook {
similarity index 61%
rename from t/prefetch/pollute_already_joined.t
rename to t/search/preserve_original_rs.t
index 152ca56..d628e9b 100644 (file)
@@ -1,24 +1,18 @@
 use strict;
-use warnings;  
+use warnings;
 
 use Test::More;
 use Test::Exception;
+
 use lib qw(t/lib);
+use DBIC::SqlMakerTest;
+use DBIC::DebugObj;
 use DBICTest;
 use Data::Dumper;
 
 my $schema = DBICTest->init_schema();
 
-my $orig_debug = $schema->storage->debug;
-
-use IO::File;
-
-BEGIN {
-    eval "use DBD::SQLite";
-    plan $@
-        ? ( skip_all => 'needs DBD::SQLite for testing' )
-        : ( tests => 10 );
-}
+plan tests => 22;
 
 # A search() with prefetch seems to pollute an already joined resultset
 # in a way that offsets future joins (adapted from a test case by Debolaz)
@@ -61,3 +55,35 @@ BEGIN {
     is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after another search with prefetch')
   }, 'second prefetching search ok');
 }
+
+# Also test search_related, but now that we have as_query simply compare before and after
+my $artist = $schema->resultset ('Artist')->first;
+my %q;
+
+$q{a2a}{rs} = $artist->search_related ('artwork_to_artist');
+$q{a2a}{query} = $q{a2a}{rs}->as_query;
+
+$q{artw}{rs} = $q{a2a}{rs}->search_related ('artwork',
+  { },
+  { join => ['cd', 'artwork_to_artist'] },
+);
+$q{artw}{query} = $q{artw}{rs}->as_query;
+
+$q{cd}{rs} = $q{artw}{rs}->search_related ('cd', {}, { join => [ 'artist', 'tracks' ] } );
+$q{cd}{query} = $q{cd}{rs}->as_query;
+
+$q{artw_back}{rs} = $q{cd}{rs}->search_related ('artwork',
+  {}, { join => { artwork_to_artist => 'artist' } }
+)->search_related ('artwork_to_artist', {}, { join => 'artist' });
+$q{artw_back}{query} = $q{artw_back}{rs}->as_query;
+
+for my $s (qw/a2a artw cd artw_back/) {
+  my $rs = $q{$s}{rs};
+
+  lives_ok ( sub { $rs->first }, "first() on $s does not throw an exception" );
+
+  lives_ok ( sub { $rs->count }, "count() on $s does not throw an exception" );
+
+  is_same_sql_bind ($rs->as_query, $q{$s}{query}, "$s resultset unmodified (as_query matches)" );
+}
+