X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fsearch%2Fpreserve_original_rs.t;h=9a087e720eda269040f3850a3eb58bfefab7ebfc;hb=5f0174dc9a33d03e4333fdf60e765dce325bf80a;hp=d628e9b62f5824a530582ed022a5420bd732989e;hpb=7025db7cf067da5d78d0735ef9ce1b8fbc73094d;p=dbsrgits%2FDBIx-Class.git diff --git a/t/search/preserve_original_rs.t b/t/search/preserve_original_rs.t index d628e9b..9a087e7 100644 --- a/t/search/preserve_original_rs.t +++ b/t/search/preserve_original_rs.t @@ -1,58 +1,56 @@ +BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) } + use strict; 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(); +use DBICTest ':DiffSQL'; +use DBIx::Class::_Util 'serialize'; -plan tests => 22; +my $schema = DBICTest->init_schema(); # 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) { - my ($cd_rs, $attrs); + my ($cd_rs, $preimage); # test a real-life case - rs is obtained by an implicit m2m join $cd_rs = $schema->resultset ('Producer')->first->cds; - $attrs = Dumper $cd_rs->{attrs}; + $preimage = serialize $cd_rs->{attrs}; $cd_rs->search ({})->all; - is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after a simple search'); + is ( serialize $cd_rs->{attrs}, $preimage, 'Resultset attributes preserved after a simple search'); lives_ok (sub { $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all; - is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after search with prefetch'); + is ( serialize $cd_rs->{attrs}, $preimage, 'Resultset attributes preserved after search with prefetch'); }, 'first prefetching search ok'); lives_ok (sub { $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all; - is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after another search with prefetch') + is ( serialize $cd_rs->{attrs}, $preimage, 'Resultset attributes preserved after another search with prefetch') }, 'second prefetching search ok'); # test a regular rs with an empty seen_join injected - it should still work! $cd_rs = $schema->resultset ('CD'); $cd_rs->{attrs}{seen_join} = {}; - $attrs = Dumper $cd_rs->{attrs}; + $preimage = serialize $cd_rs->{attrs}; $cd_rs->search ({})->all; - is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after a simple search'); + is ( serialize $cd_rs->{attrs}, $preimage, 'Resultset attributes preserved after a simple search'); lives_ok (sub { $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all; - is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after search with prefetch'); + is ( serialize $cd_rs->{attrs}, $preimage, 'Resultset attributes preserved after search with prefetch'); }, 'first prefetching search ok'); lives_ok (sub { $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all; - is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after another search with prefetch') + is ( serialize $cd_rs->{attrs}, $preimage, 'Resultset attributes preserved after another search with prefetch') }, 'second prefetching search ok'); } @@ -87,3 +85,24 @@ for my $s (qw/a2a artw cd artw_back/) { is_same_sql_bind ($rs->as_query, $q{$s}{query}, "$s resultset unmodified (as_query matches)" ); } +# ensure nothing pollutes the attrs of an existing rs +{ + my $fresh = $schema->resultset('CD'); + + isa_ok ($fresh->find(1), 'DBICTest::CD' ); + isa_ok ($fresh->single({ cdid => 1}), 'DBICTest::CD' ); + isa_ok ($fresh->search({ cdid => 1})->next, 'DBICTest::CD' ); + is ($fresh->count({ cdid => 1}), 1 ); + is ($fresh->count_rs({ cdid => 1})->next, 1 ); + + ok (! exists $fresh->{cursor}, 'Still no cursor on fresh rs'); + ok (! exists $fresh->{_attrs}{_last_sqlmaker_alias_map}, 'aliasmap did not leak through' ); + + my $n = $fresh->next; + + # check that we are not testing for deprecated slotnames + ok ($fresh->{cursor}, 'Cursor at expected slot after fire'); + ok (exists $fresh->{_attrs}{_last_sqlmaker_alias_map}, 'aliasmap at expected slot after fire' ); +} + +done_testing;