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