1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
8 use DBICTest ':DiffSQL';
10 my $schema = DBICTest->init_schema();
12 my $cd_rs = $schema->resultset('CD')->search (
13 { 'tracks.cd' => { '!=', undef } },
14 { prefetch => ['tracks', 'artist'] },
17 is($cd_rs->count, 5, 'CDs with tracks count');
18 is($cd_rs->search_related('tracks')->count, 15, 'Tracks associated with CDs count (before SELECT()ing)');
20 is($cd_rs->all, 5, 'Amount of CD objects with tracks');
21 is($cd_rs->search_related('tracks')->count, 15, 'Tracks associated with CDs count (after SELECT()ing)');
23 is($cd_rs->search_related ('tracks')->all, 15, 'Track objects associated with CDs (after SELECT()ing)');
25 my $artist = $schema->resultset('Artist')->create({name => 'xxx'});
27 my $artist_rs = $schema->resultset('Artist')->search(
28 {artistid => $artist->id},
29 {prefetch=>'cds', join => 'twokeys' }
32 is($artist_rs->count, 1, "New artist found with prefetch turned on");
33 is(scalar($artist_rs->all), 1, "New artist fetched with prefetch turned on");
34 is($artist_rs->related_resultset('cds')->count, 0, "No CDs counted on a brand new artist");
35 is(scalar($artist_rs->related_resultset('cds')->all), 0, "No CDs fetched on a brand new artist (count == fetch)");
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' });
39 is($artist_rs->related_resultset('cds')->count, 1, "1 CDs counted on a brand new artist");
40 is(scalar($artist_rs->related_resultset('cds')->all), 1, "1 CDs prefetched on a brand new artist (count == fetch)");
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
47 my $cd2 = $artist->create_related ('cds', {
50 tracks => [{ title => 'ping' }, { title => 'pong' }],
53 my $cds = $cd2->search_related ('artist', {}, { join => 'twokeys' })
54 ->search_related ('cds');
55 my $tracks = $cds->search_related ('tracks');
57 is($tracks->count, 2, "2 Tracks counted on cd via artist via one of the cds");
58 is(scalar($tracks->all), 2, "2 Track objects on cd via artist via one of the cds");
60 is($cds->count, 2, "2 CDs counted on artist via one of the cds");
61 is(scalar($cds->all), 2, "2 CD objectson artist via one of the cds");
63 # make sure the join collapses all the way
65 $tracks->count_rs->as_query,
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 = ? )
74 [ [ { sqlt_datatype => 'integer', dbic_colname => 'me.artistid' }
79 local $TODO = "Chaining with prefetch is fundamentally broken";
80 $schema->is_executed_querycount( sub {
82 my $cds = $cd2->search_related ('artist', {}, { prefetch => { cds => 'tracks' }, join => 'twokeys' })
83 ->search_related ('cds');
85 my $tracks = $cds->search_related ('tracks');
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");
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?' );