Move the rs preservation test to a more suitable place
[dbsrgits/DBIx-Class.git] / t / search / preserve_original_rs.t
CommitLineData
e9bd1473 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
6use lib qw(t/lib);
7use DBICTest;
8use Data::Dumper;
9
10my $schema = DBICTest->init_schema();
11
12my $orig_debug = $schema->storage->debug;
13
14use IO::File;
15
16BEGIN {
17 eval "use DBD::SQLite";
18 plan $@
19 ? ( skip_all => 'needs DBD::SQLite for testing' )
20 : ( tests => 10 );
21}
22
e9bd1473 23# A search() with prefetch seems to pollute an already joined resultset
24# in a way that offsets future joins (adapted from a test case by Debolaz)
25{
26 my ($cd_rs, $attrs);
27
28 # test a real-life case - rs is obtained by an implicit m2m join
29 $cd_rs = $schema->resultset ('Producer')->first->cds;
30 $attrs = Dumper $cd_rs->{attrs};
31
32 $cd_rs->search ({})->all;
33 is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after a simple search');
34
35 lives_ok (sub {
36 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
37 is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after search with prefetch');
38 }, 'first prefetching search ok');
39
40 lives_ok (sub {
41 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
42 is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after another search with prefetch')
43 }, 'second prefetching search ok');
44
45
46 # test a regular rs with an empty seen_join injected - it should still work!
47 $cd_rs = $schema->resultset ('CD');
48 $cd_rs->{attrs}{seen_join} = {};
49 $attrs = Dumper $cd_rs->{attrs};
50
51 $cd_rs->search ({})->all;
52 is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after a simple search');
53
54 lives_ok (sub {
55 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
56 is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after search with prefetch');
57 }, 'first prefetching search ok');
58
59 lives_ok (sub {
60 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
61 is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after another search with prefetch')
62 }, 'second prefetching search ok');
63}