Merge 'prefetch' into 'trunk'
[dbsrgits/DBIx-Class.git] / t / prefetch / pollute_already_joined.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
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
25my $is_broken_sqlite = 0;
26my ($sqlite_major_ver,$sqlite_minor_ver,$sqlite_patch_ver) =
27 split /\./, $schema->storage->dbh->get_info(18);
28if( $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}