Expand annotations to cover all generated methods
[dbsrgits/DBIx-Class.git] / t / prefetch / count.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
5eb45f82 3use strict;
4use warnings;
5
6use Test::More;
c0329273 7
a5a7bb73 8use DBICTest ':DiffSQL';
5eb45f82 9
5eb45f82 10my $schema = DBICTest->init_schema();
11
12my $cd_rs = $schema->resultset('CD')->search (
13 { 'tracks.cd' => { '!=', undef } },
14 { prefetch => ['tracks', 'artist'] },
15);
16
5eb45f82 17is($cd_rs->count, 5, 'CDs with tracks count');
18is($cd_rs->search_related('tracks')->count, 15, 'Tracks associated with CDs count (before SELECT()ing)');
19
20is($cd_rs->all, 5, 'Amount of CD objects with tracks');
21is($cd_rs->search_related('tracks')->count, 15, 'Tracks associated with CDs count (after SELECT()ing)');
22
23is($cd_rs->search_related ('tracks')->all, 15, 'Track objects associated with CDs (after SELECT()ing)');
1ec6e7c2 24
610e8c98 25my $artist = $schema->resultset('Artist')->create({name => 'xxx'});
26
27my $artist_rs = $schema->resultset('Artist')->search(
28 {artistid => $artist->id},
29 {prefetch=>'cds', join => 'twokeys' }
30);
31
a7daf36a 32is($artist_rs->count, 1, "New artist found with prefetch turned on");
33is(scalar($artist_rs->all), 1, "New artist fetched with prefetch turned on");
34is($artist_rs->related_resultset('cds')->count, 0, "No CDs counted on a brand new artist");
35is(scalar($artist_rs->related_resultset('cds')->all), 0, "No CDs fetched on a brand new artist (count == fetch)");
1ec6e7c2 36
610e8c98 37# create a cd, and make sure the non-existing join does not skew the count
38$artist->create_related ('cds', { title => 'yyy', year => '1999' });
39is($artist_rs->related_resultset('cds')->count, 1, "1 CDs counted on a brand new artist");
40is(scalar($artist_rs->related_resultset('cds')->all), 1, "1 CDs prefetched on a brand new artist (count == fetch)");
41
f7f14dc8 42# Really fuck shit up with one more cd and some insanity
43# this doesn't quite work as there are the prefetch gets lost
44# on search_related. This however is too esoteric to fix right
45# now
46
47my $cd2 = $artist->create_related ('cds', {
48 title => 'zzz',
49 year => '1999',
50 tracks => [{ title => 'ping' }, { title => 'pong' }],
51});
52
53my $cds = $cd2->search_related ('artist', {}, { join => 'twokeys' })
54 ->search_related ('cds');
55my $tracks = $cds->search_related ('tracks');
56
57is($tracks->count, 2, "2 Tracks counted on cd via artist via one of the cds");
58is(scalar($tracks->all), 2, "2 Track objects on cd via artist via one of the cds");
59
60is($cds->count, 2, "2 CDs counted on artist via one of the cds");
61is(scalar($cds->all), 2, "2 CD objectson artist via one of the cds");
62
63# make sure the join collapses all the way
64is_same_sql_bind (
65 $tracks->count_rs->as_query,
66 '(
67 SELECT COUNT( * )
68 FROM artist me
69 LEFT JOIN twokeys twokeys ON twokeys.artist = me.artistid
70 JOIN cd cds ON cds.artist = me.artistid
71 JOIN track tracks ON tracks.cd = cds.cdid
72 WHERE ( me.artistid = ? )
73 )',
0e773352 74 [ [ { sqlt_datatype => 'integer', dbic_colname => 'me.artistid' }
75 => 4 ] ],
f7f14dc8 76);
77
4ca1fd6f 78{
f7f14dc8 79 local $TODO = "Chaining with prefetch is fundamentally broken";
49eeb48d 80 $schema->is_executed_querycount( sub {
f7f14dc8 81
49eeb48d 82 my $cds = $cd2->search_related ('artist', {}, { prefetch => { cds => 'tracks' }, join => 'twokeys' })
f7f14dc8 83 ->search_related ('cds');
84
49eeb48d 85 my $tracks = $cds->search_related ('tracks');
f7f14dc8 86
49eeb48d 87 is($tracks->count, 2, "2 Tracks counted on cd via artist via one of the cds");
88 is(scalar($tracks->all), 2, "2 Tracks prefetched on cd via artist via one of the cds");
89 is($tracks->count, 2, "Cached 2 Tracks counted on cd via artist via one of the cds");
f7f14dc8 90
49eeb48d 91 is($cds->count, 2, "2 CDs counted on artist via one of the cds");
92 is(scalar($cds->all), 2, "2 CDs prefetched on artist via one of the cds");
93 is($cds->count, 2, "Cached 2 CDs counted on artist via one of the cds");
94 }, 3, '2 counts + 1 prefetch?' );
f7f14dc8 95}
49eeb48d 96
97done_testing;