Failing test case for the bug.
[dbsrgits/DBIx-Class.git] / t / prefetch / via_search_related.t
CommitLineData
5fc1d686 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
6
7use lib qw(t/lib);
8use DBICTest;
9
10my $schema = DBICTest->init_schema();
11
e711f316 12lives_ok ( sub {
13 my $no_prefetch = $schema->resultset('Track')->search_related(cd =>
14 {
15 'cd.year' => "2000",
16 },
17 {
18 join => 'tags',
19 order_by => 'me.trackid',
20 rows => 1,
21 }
22 );
23
24 my $use_prefetch = $no_prefetch->search(
25 {},
26 {
27 prefetch => 'tags',
28 }
29 );
30
31 is($use_prefetch->count, $no_prefetch->count, 'counts with and without prefetch match');
32 is(
33 scalar ($use_prefetch->all),
34 scalar ($no_prefetch->all),
35 "Amount of returned rows is right"
36 );
37
38}, 'search_related prefetch with order_by works');
39
40
340926fd 41lives_ok ( sub {
42 my $no_prefetch = $schema->resultset('Track')->search_related(cd =>
43 {
44 'cd.year' => "2000",
45 'tagid' => 1,
46 },
47 {
48 join => 'tags',
49 rows => 1,
50 }
51 );
52
53 my $use_prefetch = $no_prefetch->search(
54 undef,
55 {
56 prefetch => 'tags',
57 }
58 );
59
60 is($use_prefetch->count, $no_prefetch->count, 'counts with and without prefetch match');
61 is(
62 scalar ($use_prefetch->all),
63 scalar ($no_prefetch->all),
64 "Amount of returned rows is right"
65 );
66
67}, 'search_related prefetch with condition referencing unqualified column of a joined table works');
68
69
5fc1d686 70lives_ok (sub {
71 my $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
72 ->search_related('artwork_to_artist')->search_related('artist',
73 undef,
74 { prefetch => 'cds' },
75 );
76 is($rs->all, 0, 'prefetch without WHERE (objects)');
77 is($rs->count, 0, 'prefetch without WHERE (count)');
78
79 $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
80 ->search_related('artwork_to_artist')->search_related('artist',
81 { 'cds.title' => 'foo' },
82 { prefetch => 'cds' },
83 );
84 is($rs->all, 0, 'prefetch with WHERE (objects)');
85 is($rs->count, 0, 'prefetch with WHERE (count)');
86
87
88# test where conditions at the root of the related chain
5e8cb53c 89 my $artist_rs = $schema->resultset("Artist")->search({artistid => 2});
90 my $artist = $artist_rs->next;
91 $artist->create_related ('cds', $_) for (
92 {
93 year => 1999, title => 'vague cd', genre => { name => 'vague genre' }
94 },
95 {
96 year => 1999, title => 'vague cd2', genre => { name => 'vague genre' }
97 },
98 );
5fc1d686 99
100 $rs = $artist_rs->search_related('cds')->search_related('genre',
5e8cb53c 101 { 'genre.name' => 'vague genre' },
5fc1d686 102 { prefetch => 'cds' },
103 );
5e8cb53c 104 is($rs->all, 1, 'base without distinct (objects)');
105 is($rs->count, 1, 'base without distinct (count)');
106 # artist -> 2 cds -> 2 genres -> 2 cds for each genre = 4
107 is($rs->search_related('cds')->all, 4, 'prefetch without distinct (objects)');
108 is($rs->search_related('cds')->count, 4, 'prefetch without distinct (count)');
5fc1d686 109
110
111 $rs = $artist_rs->search(undef, {distinct => 1})
112 ->search_related('cds')->search_related('genre',
5e8cb53c 113 { 'genre.name' => 'vague genre' },
5fc1d686 114 );
5e8cb53c 115 is($rs->all, 1, 'distinct without prefetch (objects)');
116 is($rs->count, 1, 'distinct without prefetch (count)');
5fc1d686 117
118
119 $rs = $artist_rs->search({}, {distinct => 1})
120 ->search_related('cds')->search_related('genre',
5e8cb53c 121 { 'genre.name' => 'vague genre' },
5fc1d686 122 { prefetch => 'cds' },
123 );
5e8cb53c 124 is($rs->all, 1, 'distinct with prefetch (objects)');
125 is($rs->count, 1, 'distinct with prefetch (count)');
126 # artist -> 2 cds -> 2 genres -> 2 cds for each genre + distinct = 2
127 is($rs->search_related('cds')->all, 2, 'prefetched distinct with prefetch (objects)');
128 is($rs->search_related('cds')->count, 2, 'prefetched distinct with prefetch (count)');
5fc1d686 129
130
131
e711f316 132}, 'distinct generally works with prefetch on deep search_related chains');
5fc1d686 133
134done_testing;