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