Cleanup tests
[dbsrgits/DBIx-Class.git] / t / prefetch / pollute_already_joined.t
1 use strict;
2 use warnings;  
3
4 use Test::More;
5 use Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8 use Data::Dumper;
9
10 my $schema = DBICTest->init_schema();
11
12 my $orig_debug = $schema->storage->debug;
13
14 use IO::File;
15
16 BEGIN {
17     eval "use DBD::SQLite";
18     plan $@
19         ? ( skip_all => 'needs DBD::SQLite for testing' )
20         : ( tests => 10 );
21 }
22
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 }