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