8 use DBIC::SqlMakerTest;
13 my $schema = DBICTest->init_schema();
17 # A search() with prefetch seems to pollute an already joined resultset
18 # in a way that offsets future joins (adapted from a test case by Debolaz)
22 # test a real-life case - rs is obtained by an implicit m2m join
23 $cd_rs = $schema->resultset ('Producer')->first->cds;
24 $attrs = Dumper $cd_rs->{attrs};
26 $cd_rs->search ({})->all;
27 is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after a simple search');
30 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
31 is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after search with prefetch');
32 }, 'first prefetching search ok');
35 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
36 is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after another search with prefetch')
37 }, 'second prefetching search ok');
40 # test a regular rs with an empty seen_join injected - it should still work!
41 $cd_rs = $schema->resultset ('CD');
42 $cd_rs->{attrs}{seen_join} = {};
43 $attrs = Dumper $cd_rs->{attrs};
45 $cd_rs->search ({})->all;
46 is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after a simple search');
49 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
50 is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after search with prefetch');
51 }, 'first prefetching search ok');
54 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
55 is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after another search with prefetch')
56 }, 'second prefetching search ok');
59 # Also test search_related, but now that we have as_query simply compare before and after
60 my $artist = $schema->resultset ('Artist')->first;
63 $q{a2a}{rs} = $artist->search_related ('artwork_to_artist');
64 $q{a2a}{query} = $q{a2a}{rs}->as_query;
66 $q{artw}{rs} = $q{a2a}{rs}->search_related ('artwork',
68 { join => ['cd', 'artwork_to_artist'] },
70 $q{artw}{query} = $q{artw}{rs}->as_query;
72 $q{cd}{rs} = $q{artw}{rs}->search_related ('cd', {}, { join => [ 'artist', 'tracks' ] } );
73 $q{cd}{query} = $q{cd}{rs}->as_query;
75 $q{artw_back}{rs} = $q{cd}{rs}->search_related ('artwork',
76 {}, { join => { artwork_to_artist => 'artist' } }
77 )->search_related ('artwork_to_artist', {}, { join => 'artist' });
78 $q{artw_back}{query} = $q{artw_back}{rs}->as_query;
80 for my $s (qw/a2a artw cd artw_back/) {
83 lives_ok ( sub { $rs->first }, "first on $s does not throw an exception" );
85 lives_ok ( sub { $rs->count }, "count on $s does not throw an exception" );
87 is_same_sql_bind ($rs->as_query, $q{$s}{query}, "$s resultset unmodified (as_query matches)" );