Merge 'prefetch' into 'trunk'
[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 # figure out if we've got a version of sqlite that is older than 3.2.6, in
24 # which case COUNT(DISTINCT()) doesn't work
25 my $is_broken_sqlite = 0;
26 my ($sqlite_major_ver,$sqlite_minor_ver,$sqlite_patch_ver) =
27     split /\./, $schema->storage->dbh->get_info(18);
28 if( $schema->storage->dbh->get_info(17) eq 'SQLite' &&
29     ( ($sqlite_major_ver < 3) ||
30       ($sqlite_major_ver == 3 && $sqlite_minor_ver < 2) ||
31       ($sqlite_major_ver == 3 && $sqlite_minor_ver == 2 && $sqlite_patch_ver < 6) ) ) {
32     $is_broken_sqlite = 1;
33 }
34
35 # A search() with prefetch seems to pollute an already joined resultset
36 # in a way that offsets future joins (adapted from a test case by Debolaz)
37 {
38   my ($cd_rs, $attrs);
39
40   # test a real-life case - rs is obtained by an implicit m2m join
41   $cd_rs = $schema->resultset ('Producer')->first->cds;
42   $attrs = Dumper $cd_rs->{attrs};
43
44   $cd_rs->search ({})->all;
45   is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after a simple search');
46
47   lives_ok (sub {
48     $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
49     is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after search with prefetch');
50   }, 'first prefetching search ok');
51
52   lives_ok (sub {
53     $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
54     is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after another search with prefetch')
55   }, 'second prefetching search ok');
56
57
58   # test a regular rs with an empty seen_join injected - it should still work!
59   $cd_rs = $schema->resultset ('CD');
60   $cd_rs->{attrs}{seen_join}  = {};
61   $attrs = Dumper $cd_rs->{attrs};
62
63   $cd_rs->search ({})->all;
64   is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after a simple search');
65
66   lives_ok (sub {
67     $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
68     is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after search with prefetch');
69   }, 'first prefetching search ok');
70
71   lives_ok (sub {
72     $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
73     is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after another search with prefetch')
74   }, 'second prefetching search ok');
75 }