Commit | Line | Data |
a04af85f |
1 | # Test to ensure we get a consistent result set wether or not we use the |
2 | # prefetch option in combination rows (LIMIT). |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More; |
61fd5dfc |
7 | use Test::Exception; |
a04af85f |
8 | use lib qw(t/lib); |
9 | use DBICTest; |
10 | |
a3683eae |
11 | plan tests => 9; |
a04af85f |
12 | |
13 | my $schema = DBICTest->init_schema(); |
51a296b4 |
14 | |
15 | |
a04af85f |
16 | my $no_prefetch = $schema->resultset('Artist')->search( |
51a296b4 |
17 | [ # search deliberately contrived |
18 | { 'artwork.cd_id' => undef }, |
19 | { 'tracks.title' => { '!=' => 'blah-blah-1234568' }} |
20 | ], |
96faafb4 |
21 | { rows => 3, join => { cds => [qw/artwork tracks/] }, |
22 | } |
23 | ); |
24 | |
25 | my $use_prefetch = $no_prefetch->search( |
26 | {}, |
25cac750 |
27 | { |
28 | prefetch => 'cds', |
51a296b4 |
29 | order_by => { -desc => 'name' }, |
25cac750 |
30 | } |
a04af85f |
31 | ); |
32 | |
a04af85f |
33 | is($no_prefetch->count, $use_prefetch->count, '$no_prefetch->count == $use_prefetch->count'); |
5624ba1f |
34 | is( |
9117ccfb |
35 | scalar ($no_prefetch->all), |
36 | scalar ($use_prefetch->all), |
37 | "Amount of returned rows is right" |
5624ba1f |
38 | ); |
39 | |
9117ccfb |
40 | my $artist_many_cds = $schema->resultset('Artist')->search ( {}, { |
41 | join => 'cds', |
42 | group_by => 'me.artistid', |
43 | having => \ 'count(cds.cdid) > 1', |
44 | })->first; |
45 | |
46 | |
5624ba1f |
47 | $no_prefetch = $schema->resultset('Artist')->search( |
9117ccfb |
48 | { artistid => $artist_many_cds->id }, |
5624ba1f |
49 | { rows => 1 } |
50 | ); |
51 | |
61fd5dfc |
52 | $use_prefetch = $no_prefetch->search ({}, { prefetch => 'cds' }); |
5624ba1f |
53 | |
61fd5dfc |
54 | my $normal_artist = $no_prefetch->single; |
55 | my $prefetch_artist = $use_prefetch->find({ name => $artist_many_cds->name }); |
56 | my $prefetch2_artist = $use_prefetch->first; |
5624ba1f |
57 | |
58 | is( |
59 | $prefetch_artist->cds->count, |
60 | $normal_artist->cds->count, |
61fd5dfc |
61 | "Count of child rel with prefetch + rows => 1 is right (find)" |
62 | ); |
63 | is( |
64 | $prefetch2_artist->cds->count, |
65 | $normal_artist->cds->count, |
66 | "Count of child rel with prefetch + rows => 1 is right (first)" |
5624ba1f |
67 | ); |
61fd5dfc |
68 | |
9117ccfb |
69 | is ( |
70 | scalar ($prefetch_artist->cds->all), |
71 | scalar ($normal_artist->cds->all), |
61fd5dfc |
72 | "Amount of child rel rows with prefetch + rows => 1 is right (find)" |
73 | ); |
74 | is ( |
75 | scalar ($prefetch2_artist->cds->all), |
76 | scalar ($normal_artist->cds->all), |
77 | "Amount of child rel rows with prefetch + rows => 1 is right (first)" |
78 | ); |
79 | |
80 | throws_ok ( |
81 | sub { $use_prefetch->single }, |
82 | qr/resultsets prefetching has_many/, |
83 | 'single() with multiprefetch is illegal', |
9117ccfb |
84 | ); |
01c781fe |
85 | |
09707a31 |
86 | my $artist = $use_prefetch->search({'cds.title' => $artist_many_cds->cds->first->title })->next; |
01c781fe |
87 | is($artist->cds->count, 1, "count on search limiting prefetched has_many"); |
88 | |
a3683eae |
89 | # try with double limit |
90 | my $artist2 = $use_prefetch->search({'cds.title' => { '!=' => $artist_many_cds->cds->first->title } })->slice (0,0)->next; |
91 | is($artist2->cds->count, 2, "count on search limiting prefetched has_many"); |
92 | |