8 use DBICTest ':DiffSQL';
9 use DBIx::Class::_Util 'serialize';
11 my $schema = DBICTest->init_schema();
13 # A search() with prefetch seems to pollute an already joined resultset
14 # in a way that offsets future joins (adapted from a test case by Debolaz)
16 my ($cd_rs, $preimage);
18 # test a real-life case - rs is obtained by an implicit m2m join
19 $cd_rs = $schema->resultset ('Producer')->first->cds;
20 $preimage = serialize $cd_rs->{attrs};
22 $cd_rs->search ({})->all;
23 is ( serialize $cd_rs->{attrs}, $preimage, 'Resultset attributes preserved after a simple search');
26 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
27 is ( serialize $cd_rs->{attrs}, $preimage, 'Resultset attributes preserved after search with prefetch');
28 }, 'first prefetching search ok');
31 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
32 is ( serialize $cd_rs->{attrs}, $preimage, 'Resultset attributes preserved after another search with prefetch')
33 }, 'second prefetching search ok');
36 # test a regular rs with an empty seen_join injected - it should still work!
37 $cd_rs = $schema->resultset ('CD');
38 $cd_rs->{attrs}{seen_join} = {};
39 $preimage = serialize $cd_rs->{attrs};
41 $cd_rs->search ({})->all;
42 is ( serialize $cd_rs->{attrs}, $preimage, 'Resultset attributes preserved after a simple search');
45 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
46 is ( serialize $cd_rs->{attrs}, $preimage, 'Resultset attributes preserved after search with prefetch');
47 }, 'first prefetching search ok');
50 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
51 is ( serialize $cd_rs->{attrs}, $preimage, 'Resultset attributes preserved after another search with prefetch')
52 }, 'second prefetching search ok');
55 # Also test search_related, but now that we have as_query simply compare before and after
56 my $artist = $schema->resultset ('Artist')->first;
59 $q{a2a}{rs} = $artist->search_related ('artwork_to_artist');
60 $q{a2a}{query} = $q{a2a}{rs}->as_query;
62 $q{artw}{rs} = $q{a2a}{rs}->search_related ('artwork',
64 { join => ['cd', 'artwork_to_artist'] },
66 $q{artw}{query} = $q{artw}{rs}->as_query;
68 $q{cd}{rs} = $q{artw}{rs}->search_related ('cd', {}, { join => [ 'artist', 'tracks' ] } );
69 $q{cd}{query} = $q{cd}{rs}->as_query;
71 $q{artw_back}{rs} = $q{cd}{rs}->search_related ('artwork',
72 {}, { join => { artwork_to_artist => 'artist' } }
73 )->search_related ('artwork_to_artist', {}, { join => 'artist' });
74 $q{artw_back}{query} = $q{artw_back}{rs}->as_query;
76 for my $s (qw/a2a artw cd artw_back/) {
79 lives_ok ( sub { $rs->first }, "first() on $s does not throw an exception" );
81 lives_ok ( sub { $rs->count }, "count() on $s does not throw an exception" );
83 is_same_sql_bind ($rs->as_query, $q{$s}{query}, "$s resultset unmodified (as_query matches)" );
86 # ensure nothing pollutes the attrs of an existing rs
88 my $fresh = $schema->resultset('CD');
90 isa_ok ($fresh->find(1), 'DBICTest::CD' );
91 isa_ok ($fresh->single({ cdid => 1}), 'DBICTest::CD' );
92 isa_ok ($fresh->search({ cdid => 1})->next, 'DBICTest::CD' );
93 is ($fresh->count({ cdid => 1}), 1 );
94 is ($fresh->count_rs({ cdid => 1})->next, 1 );
96 ok (! exists $fresh->{cursor}, 'Still no cursor on fresh rs');
97 ok (! exists $fresh->{_attrs}{_last_sqlmaker_alias_map}, 'aliasmap did not leak through' );
101 # check that we are not testing for deprecated slotnames
102 ok ($fresh->{cursor}, 'Cursor at expected slot after fire');
103 ok (exists $fresh->{_attrs}{_last_sqlmaker_alias_map}, 'aliasmap at expected slot after fire' );