8 use DBIC::SqlMakerTest;
12 # use Data::Dumper comparisons to avoid mesing with coderefs
14 $Data::Dumper::Sortkeys = 1;
16 my $schema = DBICTest->init_schema();
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)
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};
29 $cd_rs->search ({})->all;
30 is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after a simple search');
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');
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');
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};
48 $cd_rs->search ({})->all;
49 is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after a simple search');
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');
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');
62 # Also test search_related, but now that we have as_query simply compare before and after
63 my $artist = $schema->resultset ('Artist')->first;
66 $q{a2a}{rs} = $artist->search_related ('artwork_to_artist');
67 $q{a2a}{query} = $q{a2a}{rs}->as_query;
69 $q{artw}{rs} = $q{a2a}{rs}->search_related ('artwork',
71 { join => ['cd', 'artwork_to_artist'] },
73 $q{artw}{query} = $q{artw}{rs}->as_query;
75 $q{cd}{rs} = $q{artw}{rs}->search_related ('cd', {}, { join => [ 'artist', 'tracks' ] } );
76 $q{cd}{query} = $q{cd}{rs}->as_query;
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;
83 for my $s (qw/a2a artw cd artw_back/) {
86 lives_ok ( sub { $rs->first }, "first() on $s does not throw an exception" );
88 lives_ok ( sub { $rs->count }, "count() on $s does not throw an exception" );
90 is_same_sql_bind ($rs->as_query, $q{$s}{query}, "$s resultset unmodified (as_query matches)" );