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