Fix regression breaking search on prefetched rel (broken by 5e2a0518)
[dbsrgits/DBIx-Class.git] / t / prefetch / refined_search_on_relation.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 my $schema = DBICTest->init_schema();
9
10 my $art = $schema->resultset('Artist')->find(
11   { 'me.artistid' => 1 },
12   { prefetch => 'cds', order_by => { -desc => 'cds.year' } }
13 );
14
15 is (
16   $art->cds->search({ year => 1999 })->next->year,
17   1999,
18   'Found expected CD with year 1999 after refined search',
19 );
20
21 is (
22   $art->cds->count({ year => 1999 }),
23   1,
24   'Correct refined count',
25 );
26
27 # this still should emit no queries:
28 {
29   my $queries = 0;
30   my $orig_debug = $schema->storage->debug;
31   $schema->storage->debugcb(sub { $queries++; });
32   $schema->storage->debug(1);
33
34   my $cds = $art->cds;
35   is (
36     $cds->count,
37     3,
38     'Correct prefetched count',
39   );
40
41   my @years = qw(2001 1999 1997);
42   while (my $cd = $cds->next) {
43     is (
44       $cd->year,
45       (shift @years),
46       'Correct prefetched cd year',
47     );
48   }
49
50   $schema->storage->debug($orig_debug);
51   $schema->storage->debugcb(undef);
52
53   is ($queries, 0, 'No queries on prefetched operations');
54 }
55
56 done_testing;