1use strict; 2use warnings; 3 4use Test::More; 5use Test::Exception; 6 7use lib qw(t/lib); 8use DBIC::SqlMakerTest; 9use DBIC::DebugObj; 10use DBICTest; 11 12# use Data::Dumper comparisons to avoid mesing with coderefs 13use Data::Dumper; 14$Data::Dumper::Sortkeys = 1; 15 16my $schema = DBICTest->init_schema(); 17 18plan tests => 22; 19 20# A search() with prefetch seems to pollute an already joined resultset 21# in a way that offsets future joins (adapted from a test case by Debolaz) 22{ 23 my ($cd_rs, $attrs); 24 25 # test a real-life case - rs is obtained by an implicit m2m join 26 $cd_rs = $schema->resultset ('Producer')->first->cds; 27 $attrs = Dumper $cd_rs->{attrs}; 28 29 $cd_rs->search ({})->all; 30 is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after a simple search'); 31 32 lives_ok (sub { 33 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all; 34 is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after search with prefetch'); 35 }, 'first prefetching search ok'); 36 37 lives_ok (sub { 38 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all; 39 is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after another search with prefetch') 40 }, 'second prefetching search ok'); 41 42 43 # test a regular rs with an empty seen_join injected - it should still work! 44 $cd_rs = $schema->resultset ('CD'); 45 $cd_rs->{attrs}{seen_join} = {}; 46 $attrs = Dumper $cd_rs->{attrs}; 47 48 $cd_rs->search ({})->all; 49 is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after a simple search'); 50 51 lives_ok (sub { 52 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all; 53 is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after search with prefetch'); 54 }, 'first prefetching search ok'); 55 56 lives_ok (sub { 57 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all; 58 is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after another search with prefetch') 59 }, 'second prefetching search ok'); 60} 61 62# Also test search_related, but now that we have as_query simply compare before and after 63my $artist = $schema->resultset ('Artist')->first; 64my %q; 65 66$q{a2a}{rs} = $artist->search_related ('artwork_to_artist'); 67$q{a2a}{query} = $q{a2a}{rs}->as_query; 68 69$q{artw}{rs} = $q{a2a}{rs}->search_related ('artwork', 70 { }, 71 { join => ['cd', 'artwork_to_artist'] }, 72); 73$q{artw}{query} = $q{artw}{rs}->as_query; 74 75$q{cd}{rs} = $q{artw}{rs}->search_related ('cd', {}, { join => [ 'artist', 'tracks' ] } ); 76$q{cd}{query} = $q{cd}{rs}->as_query; 77 78$q{artw_back}{rs} = $q{cd}{rs}->search_related ('artwork', 79 {}, { join => { artwork_to_artist => 'artist' } } 80)->search_related ('artwork_to_artist', {}, { join => 'artist' }); 81$q{artw_back}{query} = $q{artw_back}{rs}->as_query; 82 83for my $s (qw/a2a artw cd artw_back/) { 84 my $rs = $q{$s}{rs}; 85 86 lives_ok ( sub { $rs->first }, "first() on $s does not throw an exception" ); 87 88 lives_ok ( sub { $rs->count }, "count() on $s does not throw an exception" ); 89 90 is_same_sql_bind ($rs->as_query, $q{$s}{query}, "$s resultset unmodified (as_query matches)" ); 91} 92