Remove small_datetime from the main schema - it is not a standard datatype
[dbsrgits/DBIx-Class.git] / t / prefetch / with_limit.t
CommitLineData
a04af85f 1# Test to ensure we get a consistent result set wether or not we use the
2# prefetch option in combination rows (LIMIT).
3use strict;
4use warnings;
5
6use Test::More;
61fd5dfc 7use Test::Exception;
a04af85f 8use lib qw(t/lib);
9use DBICTest;
53c29913 10use DBIC::SqlMakerTest;
a04af85f 11
a04af85f 12my $schema = DBICTest->init_schema();
51a296b4 13
14
a04af85f 15my $no_prefetch = $schema->resultset('Artist')->search(
51a296b4 16 [ # search deliberately contrived
17 { 'artwork.cd_id' => undef },
18 { 'tracks.title' => { '!=' => 'blah-blah-1234568' }}
19 ],
96faafb4 20 { rows => 3, join => { cds => [qw/artwork tracks/] },
21 }
22);
23
24my $use_prefetch = $no_prefetch->search(
25 {},
25cac750 26 {
c9d29bb2 27 select => ['me.artistid', 'me.name'],
28 as => ['artistid', 'name'],
25cac750 29 prefetch => 'cds',
51a296b4 30 order_by => { -desc => 'name' },
25cac750 31 }
a04af85f 32);
33
a04af85f 34is($no_prefetch->count, $use_prefetch->count, '$no_prefetch->count == $use_prefetch->count');
5624ba1f 35is(
9117ccfb 36 scalar ($no_prefetch->all),
37 scalar ($use_prefetch->all),
38 "Amount of returned rows is right"
5624ba1f 39);
40
9117ccfb 41my $artist_many_cds = $schema->resultset('Artist')->search ( {}, {
42 join => 'cds',
43 group_by => 'me.artistid',
44 having => \ 'count(cds.cdid) > 1',
45})->first;
46
47
5624ba1f 48$no_prefetch = $schema->resultset('Artist')->search(
9117ccfb 49 { artistid => $artist_many_cds->id },
5624ba1f 50 { rows => 1 }
51);
52
61fd5dfc 53$use_prefetch = $no_prefetch->search ({}, { prefetch => 'cds' });
5624ba1f 54
61fd5dfc 55my $normal_artist = $no_prefetch->single;
56my $prefetch_artist = $use_prefetch->find({ name => $artist_many_cds->name });
57my $prefetch2_artist = $use_prefetch->first;
5624ba1f 58
59is(
60 $prefetch_artist->cds->count,
61 $normal_artist->cds->count,
61fd5dfc 62 "Count of child rel with prefetch + rows => 1 is right (find)"
63);
64is(
65 $prefetch2_artist->cds->count,
66 $normal_artist->cds->count,
67 "Count of child rel with prefetch + rows => 1 is right (first)"
5624ba1f 68);
61fd5dfc 69
9117ccfb 70is (
71 scalar ($prefetch_artist->cds->all),
72 scalar ($normal_artist->cds->all),
61fd5dfc 73 "Amount of child rel rows with prefetch + rows => 1 is right (find)"
74);
75is (
76 scalar ($prefetch2_artist->cds->all),
77 scalar ($normal_artist->cds->all),
78 "Amount of child rel rows with prefetch + rows => 1 is right (first)"
79);
80
81throws_ok (
82 sub { $use_prefetch->single },
83 qr/resultsets prefetching has_many/,
84 'single() with multiprefetch is illegal',
9117ccfb 85);
01c781fe 86
09707a31 87my $artist = $use_prefetch->search({'cds.title' => $artist_many_cds->cds->first->title })->next;
01c781fe 88is($artist->cds->count, 1, "count on search limiting prefetched has_many");
89
a3683eae 90# try with double limit
91my $artist2 = $use_prefetch->search({'cds.title' => { '!=' => $artist_many_cds->cds->first->title } })->slice (0,0)->next;
92is($artist2->cds->count, 2, "count on search limiting prefetched has_many");
93
53c29913 94# make sure 1:1 joins do not force a subquery (no point to exercise the optimizer, if at all available)
95# get cd's that have any tracks and their artists
96my $single_prefetch_rs = $schema->resultset ('CD')->search (
97 { 'me.year' => 2010, 'artist.name' => 'foo' },
98 { prefetch => ['tracks', 'artist'], rows => 15 },
99);
100is_same_sql_bind (
101 $single_prefetch_rs->as_query,
102 '(
103 SELECT
104 me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
3d98c75e 105 tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at,
53c29913 106 artist.artistid, artist.name, artist.rank, artist.charfield
107 FROM (
108 SELECT
109 me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
110 FROM cd me
111 JOIN artist artist ON artist.artistid = me.artist
112 WHERE ( ( artist.name = ? AND me.year = ? ) )
113 LIMIT 15
114 ) me
115 LEFT JOIN track tracks
116 ON tracks.cd = me.cdid
117 JOIN artist artist
118 ON artist.artistid = me.artist
119 WHERE ( ( artist.name = ? AND me.year = ? ) )
120 ORDER BY tracks.cd
121 )',
122 [
123 [ 'artist.name' => 'foo' ],
124 [ 'me.year' => 2010 ],
125 [ 'artist.name' => 'foo' ],
126 [ 'me.year' => 2010 ],
127 ],
128 'No grouping of non-multiplying resultsets',
129);
130
c9d29bb2 131done_testing;