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); |
a5a7bb73 |
8 | use DBICTest ':DiffSQL'; |
1c30a2e4 |
9 | use DBIx::Class::_Util 'serialize'; |
e9bd1473 |
10 | |
11 | my $schema = DBICTest->init_schema(); |
12 | |
e9bd1473 |
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) |
15 | { |
1c30a2e4 |
16 | my ($cd_rs, $preimage); |
e9bd1473 |
17 | |
18 | # test a real-life case - rs is obtained by an implicit m2m join |
19 | $cd_rs = $schema->resultset ('Producer')->first->cds; |
1c30a2e4 |
20 | $preimage = serialize $cd_rs->{attrs}; |
e9bd1473 |
21 | |
22 | $cd_rs->search ({})->all; |
1c30a2e4 |
23 | is ( serialize $cd_rs->{attrs}, $preimage, 'Resultset attributes preserved after a simple search'); |
e9bd1473 |
24 | |
25 | lives_ok (sub { |
26 | $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all; |
1c30a2e4 |
27 | is ( serialize $cd_rs->{attrs}, $preimage, 'Resultset attributes preserved after search with prefetch'); |
e9bd1473 |
28 | }, 'first prefetching search ok'); |
29 | |
30 | lives_ok (sub { |
31 | $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all; |
1c30a2e4 |
32 | is ( serialize $cd_rs->{attrs}, $preimage, 'Resultset attributes preserved after another search with prefetch') |
e9bd1473 |
33 | }, 'second prefetching search ok'); |
34 | |
35 | |
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} = {}; |
1c30a2e4 |
39 | $preimage = serialize $cd_rs->{attrs}; |
e9bd1473 |
40 | |
41 | $cd_rs->search ({})->all; |
1c30a2e4 |
42 | is ( serialize $cd_rs->{attrs}, $preimage, 'Resultset attributes preserved after a simple search'); |
e9bd1473 |
43 | |
44 | lives_ok (sub { |
45 | $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all; |
1c30a2e4 |
46 | is ( serialize $cd_rs->{attrs}, $preimage, 'Resultset attributes preserved after search with prefetch'); |
e9bd1473 |
47 | }, 'first prefetching search ok'); |
48 | |
49 | lives_ok (sub { |
50 | $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all; |
1c30a2e4 |
51 | is ( serialize $cd_rs->{attrs}, $preimage, 'Resultset attributes preserved after another search with prefetch') |
e9bd1473 |
52 | }, 'second prefetching search ok'); |
53 | } |
4086a325 |
54 | |
55 | # Also test search_related, but now that we have as_query simply compare before and after |
56 | my $artist = $schema->resultset ('Artist')->first; |
57 | my %q; |
58 | |
59 | $q{a2a}{rs} = $artist->search_related ('artwork_to_artist'); |
60 | $q{a2a}{query} = $q{a2a}{rs}->as_query; |
61 | |
62 | $q{artw}{rs} = $q{a2a}{rs}->search_related ('artwork', |
63 | { }, |
64 | { join => ['cd', 'artwork_to_artist'] }, |
65 | ); |
66 | $q{artw}{query} = $q{artw}{rs}->as_query; |
67 | |
68 | $q{cd}{rs} = $q{artw}{rs}->search_related ('cd', {}, { join => [ 'artist', 'tracks' ] } ); |
69 | $q{cd}{query} = $q{cd}{rs}->as_query; |
70 | |
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; |
75 | |
76 | for my $s (qw/a2a artw cd artw_back/) { |
77 | my $rs = $q{$s}{rs}; |
78 | |
7025db7c |
79 | lives_ok ( sub { $rs->first }, "first() on $s does not throw an exception" ); |
4086a325 |
80 | |
7025db7c |
81 | lives_ok ( sub { $rs->count }, "count() on $s does not throw an exception" ); |
4086a325 |
82 | |
83 | is_same_sql_bind ($rs->as_query, $q{$s}{query}, "$s resultset unmodified (as_query matches)" ); |
84 | } |
09d763c8 |
85 | |
1887cd9f |
86 | # ensure nothing pollutes the attrs of an existing rs |
87 | { |
88 | my $fresh = $schema->resultset('CD'); |
89 | |
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 ); |
95 | |
735f190c |
96 | ok (! exists $fresh->{cursor}, 'Still no cursor on fresh rs'); |
6ae62c5c |
97 | ok (! exists $fresh->{_attrs}{_last_sqlmaker_alias_map}, 'aliasmap did not leak through' ); |
98 | |
99 | my $n = $fresh->next; |
100 | |
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' ); |
1887cd9f |
104 | } |
105 | |
09d763c8 |
106 | done_testing; |