Expand annotations to cover all generated methods
[dbsrgits/DBIx-Class.git] / t / search / preserve_original_rs.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
e9bd1473 3use strict;
4086a325 4use warnings;
e9bd1473 5
6use Test::More;
7use Test::Exception;
4086a325 8
c0329273 9
a5a7bb73 10use DBICTest ':DiffSQL';
1c30a2e4 11use DBIx::Class::_Util 'serialize';
e9bd1473 12
13my $schema = DBICTest->init_schema();
14
e9bd1473 15# A search() with prefetch seems to pollute an already joined resultset
16# in a way that offsets future joins (adapted from a test case by Debolaz)
17{
1c30a2e4 18 my ($cd_rs, $preimage);
e9bd1473 19
20 # test a real-life case - rs is obtained by an implicit m2m join
21 $cd_rs = $schema->resultset ('Producer')->first->cds;
1c30a2e4 22 $preimage = serialize $cd_rs->{attrs};
e9bd1473 23
24 $cd_rs->search ({})->all;
1c30a2e4 25 is ( serialize $cd_rs->{attrs}, $preimage, 'Resultset attributes preserved after a simple search');
e9bd1473 26
27 lives_ok (sub {
28 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
1c30a2e4 29 is ( serialize $cd_rs->{attrs}, $preimage, 'Resultset attributes preserved after search with prefetch');
e9bd1473 30 }, 'first prefetching search ok');
31
32 lives_ok (sub {
33 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
1c30a2e4 34 is ( serialize $cd_rs->{attrs}, $preimage, 'Resultset attributes preserved after another search with prefetch')
e9bd1473 35 }, 'second prefetching search ok');
36
37
38 # test a regular rs with an empty seen_join injected - it should still work!
39 $cd_rs = $schema->resultset ('CD');
40 $cd_rs->{attrs}{seen_join} = {};
1c30a2e4 41 $preimage = serialize $cd_rs->{attrs};
e9bd1473 42
43 $cd_rs->search ({})->all;
1c30a2e4 44 is ( serialize $cd_rs->{attrs}, $preimage, 'Resultset attributes preserved after a simple search');
e9bd1473 45
46 lives_ok (sub {
47 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
1c30a2e4 48 is ( serialize $cd_rs->{attrs}, $preimage, 'Resultset attributes preserved after search with prefetch');
e9bd1473 49 }, 'first prefetching search ok');
50
51 lives_ok (sub {
52 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
1c30a2e4 53 is ( serialize $cd_rs->{attrs}, $preimage, 'Resultset attributes preserved after another search with prefetch')
e9bd1473 54 }, 'second prefetching search ok');
55}
4086a325 56
57# Also test search_related, but now that we have as_query simply compare before and after
58my $artist = $schema->resultset ('Artist')->first;
59my %q;
60
61$q{a2a}{rs} = $artist->search_related ('artwork_to_artist');
62$q{a2a}{query} = $q{a2a}{rs}->as_query;
63
64$q{artw}{rs} = $q{a2a}{rs}->search_related ('artwork',
65 { },
66 { join => ['cd', 'artwork_to_artist'] },
67);
68$q{artw}{query} = $q{artw}{rs}->as_query;
69
70$q{cd}{rs} = $q{artw}{rs}->search_related ('cd', {}, { join => [ 'artist', 'tracks' ] } );
71$q{cd}{query} = $q{cd}{rs}->as_query;
72
73$q{artw_back}{rs} = $q{cd}{rs}->search_related ('artwork',
74 {}, { join => { artwork_to_artist => 'artist' } }
75)->search_related ('artwork_to_artist', {}, { join => 'artist' });
76$q{artw_back}{query} = $q{artw_back}{rs}->as_query;
77
78for my $s (qw/a2a artw cd artw_back/) {
79 my $rs = $q{$s}{rs};
80
7025db7c 81 lives_ok ( sub { $rs->first }, "first() on $s does not throw an exception" );
4086a325 82
7025db7c 83 lives_ok ( sub { $rs->count }, "count() on $s does not throw an exception" );
4086a325 84
85 is_same_sql_bind ($rs->as_query, $q{$s}{query}, "$s resultset unmodified (as_query matches)" );
86}
09d763c8 87
1887cd9f 88# ensure nothing pollutes the attrs of an existing rs
89{
90 my $fresh = $schema->resultset('CD');
91
92 isa_ok ($fresh->find(1), 'DBICTest::CD' );
93 isa_ok ($fresh->single({ cdid => 1}), 'DBICTest::CD' );
94 isa_ok ($fresh->search({ cdid => 1})->next, 'DBICTest::CD' );
95 is ($fresh->count({ cdid => 1}), 1 );
96 is ($fresh->count_rs({ cdid => 1})->next, 1 );
97
735f190c 98 ok (! exists $fresh->{cursor}, 'Still no cursor on fresh rs');
6ae62c5c 99 ok (! exists $fresh->{_attrs}{_last_sqlmaker_alias_map}, 'aliasmap did not leak through' );
100
101 my $n = $fresh->next;
102
103 # check that we are not testing for deprecated slotnames
104 ok ($fresh->{cursor}, 'Cursor at expected slot after fire');
105 ok (exists $fresh->{_attrs}{_last_sqlmaker_alias_map}, 'aliasmap at expected slot after fire' );
1887cd9f 106}
107
09d763c8 108done_testing;