Commit | Line | Data |
e9bd1473 |
1 | use strict; |
4086a325 |
2 | use warnings; |
e9bd1473 |
3 | |
4 | use Test::More; |
5 | use Test::Exception; |
4086a325 |
6 | |
e9bd1473 |
7 | use lib qw(t/lib); |
4086a325 |
8 | use DBIC::SqlMakerTest; |
9 | use DBIC::DebugObj; |
e9bd1473 |
10 | use DBICTest; |
bfa46eb5 |
11 | |
12 | # use Data::Dumper comparisons to avoid mesing with coderefs |
e9bd1473 |
13 | use Data::Dumper; |
bfa46eb5 |
14 | $Data::Dumper::Sortkeys = 1; |
e9bd1473 |
15 | |
16 | my $schema = DBICTest->init_schema(); |
17 | |
4086a325 |
18 | plan tests => 22; |
e9bd1473 |
19 | |
e9bd1473 |
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 | } |
4086a325 |
61 | |
62 | # Also test search_related, but now that we have as_query simply compare before and after |
63 | my $artist = $schema->resultset ('Artist')->first; |
64 | my %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 | |
83 | for my $s (qw/a2a artw cd artw_back/) { |
84 | my $rs = $q{$s}{rs}; |
85 | |
7025db7c |
86 | lives_ok ( sub { $rs->first }, "first() on $s does not throw an exception" ); |
4086a325 |
87 | |
7025db7c |
88 | lives_ok ( sub { $rs->count }, "count() on $s does not throw an exception" ); |
4086a325 |
89 | |
90 | is_same_sql_bind ($rs->as_query, $q{$s}{query}, "$s resultset unmodified (as_query matches)" ); |
91 | } |