8 use DBICTest ':DiffSQL';
10 use Storable 'dclone';
12 my $schema = DBICTest->init_schema();
14 # A search() with prefetch seems to pollute an already joined resultset
15 # in a way that offsets future joins (adapted from a test case by Debolaz)
19 # test a real-life case - rs is obtained by an implicit m2m join
20 $cd_rs = $schema->resultset ('Producer')->first->cds;
21 $attrs = dclone( $cd_rs->{attrs} );
23 $cd_rs->search ({})->all;
24 is_deeply (dclone($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after a simple search');
27 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
28 is_deeply (dclone($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after search with prefetch');
29 }, 'first prefetching search ok');
32 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
33 is_deeply (dclone($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after another search with prefetch')
34 }, 'second prefetching search ok');
37 # test a regular rs with an empty seen_join injected - it should still work!
38 $cd_rs = $schema->resultset ('CD');
39 $cd_rs->{attrs}{seen_join} = {};
40 $attrs = dclone( $cd_rs->{attrs} );
42 $cd_rs->search ({})->all;
43 is_deeply (dclone($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after a simple search');
46 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
47 is_deeply (dclone($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after search with prefetch');
48 }, 'first prefetching search ok');
51 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
52 is_deeply (dclone($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after another search with prefetch')
53 }, 'second prefetching search ok');
56 # Also test search_related, but now that we have as_query simply compare before and after
57 my $artist = $schema->resultset ('Artist')->first;
60 $q{a2a}{rs} = $artist->search_related ('artwork_to_artist');
61 $q{a2a}{query} = $q{a2a}{rs}->as_query;
63 $q{artw}{rs} = $q{a2a}{rs}->search_related ('artwork',
65 { join => ['cd', 'artwork_to_artist'] },
67 $q{artw}{query} = $q{artw}{rs}->as_query;
69 $q{cd}{rs} = $q{artw}{rs}->search_related ('cd', {}, { join => [ 'artist', 'tracks' ] } );
70 $q{cd}{query} = $q{cd}{rs}->as_query;
72 $q{artw_back}{rs} = $q{cd}{rs}->search_related ('artwork',
73 {}, { join => { artwork_to_artist => 'artist' } }
74 )->search_related ('artwork_to_artist', {}, { join => 'artist' });
75 $q{artw_back}{query} = $q{artw_back}{rs}->as_query;
77 for my $s (qw/a2a artw cd artw_back/) {
80 lives_ok ( sub { $rs->first }, "first() on $s does not throw an exception" );
82 lives_ok ( sub { $rs->count }, "count() on $s does not throw an exception" );
84 is_same_sql_bind ($rs->as_query, $q{$s}{query}, "$s resultset unmodified (as_query matches)" );
87 # ensure nothing pollutes the attrs of an existing rs
89 my $fresh = $schema->resultset('CD');
91 isa_ok ($fresh->find(1), 'DBICTest::CD' );
92 isa_ok ($fresh->single({ cdid => 1}), 'DBICTest::CD' );
93 isa_ok ($fresh->search({ cdid => 1})->next, 'DBICTest::CD' );
94 is ($fresh->count({ cdid => 1}), 1 );
95 is ($fresh->count_rs({ cdid => 1})->next, 1 );
97 ok (! exists $fresh->{cursor}, 'Still no cursor on fresh rs');
98 ok (! exists $fresh->{_attrs}{_last_sqlmaker_alias_map}, 'aliasmap did not leak through' );
100 my $n = $fresh->next;
102 # check that we are not testing for deprecated slotnames
103 ok ($fresh->{cursor}, 'Cursor at expected slot after fire');
104 ok (exists $fresh->{_attrs}{_last_sqlmaker_alias_map}, 'aliasmap at expected slot after fire' );