10 my $schema = DBICTest->init_schema(no_populate => 1);
12 $schema->resultset('Artist')->create({ name => 'JMJ', cds => [{
13 title => 'Magnetic Fields',
15 genre => { name => 'electro' },
24 $schema->resultset('CD')->create({
27 artist => { name => 'JMJ' },
28 genre => { name => 'electro' },
39 artist => { name => 'JMJ' },
41 { title => 'o2', position => 2}, # the position should not be here, bug in MC
47 my $rs = $schema->resultset ('CD')->search ({}, {
48 join => [ 'tracks', { single_track => { cd => { artist => { cds => 'tracks' } } } } ],
51 { 'year' => 'me.year' }, # non-unique
52 { 'genreid' => 'me.genreid' }, # nullable
53 { 'tracks.title' => 'tracks.title' }, # non-unique (no me.id)
54 { 'single_track.cd.artist.cds.cdid' => 'cds.cdid' }, # to give uniquiness to ...tracks.title below
55 { 'single_track.cd.artist.artistid' => 'artist.artistid' }, # uniqufies entire parental chain
56 { 'single_track.cd.artist.cds.year' => 'cds.year' }, # non-unique
57 { 'single_track.cd.artist.cds.genreid' => 'cds.genreid' }, # nullable
58 { 'single_track.cd.artist.cds.tracks.title' => 'tracks_2.title' }, # unique when combined with ...cds.cdid above
59 { 'latest_cd' => \ "(SELECT MAX(year) FROM cd)" }, # random function
60 { 'title' => 'me.title' }, # uniquiness for me
61 { 'artist' => 'me.artist' }, # uniquiness for me
63 order_by => [{ -desc => 'cds.year' }, { -desc => 'me.title'} ],
66 my $hri_rs = $rs->search({}, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' });
150 single_track => undef,
166 single_track => undef,
167 title => "Magnetic Fields",
185 'W00T, manual prefetch with collapse works'
190 local $TODO = 'Something is wrong with filter type rels, they throw on incomplete objects >.<';
194 { $row->single_track->get_columns },
196 'empty intermediate object ok',
201 is ($rs->cursor->next, undef, 'cursor exhausted');
205 local $TODO = 'this does not work at all, need to promote rsattrs to an object on its own';
206 # make sure has_many column redirection does not do weird stuff when collapse is requested
208 { prefetch => 'cds'},
211 for my $col_and_join_args (
212 { '+columns' => { 'cd_title' => 'cds_2.title' }, join => [ 'cds', 'cds' ] },
213 { '+columns' => { 'cd_title' => 'cds.title' }, join => 'cds', }
216 my $weird_rs = $schema->resultset('Artist')->search({}, {
217 %$col_and_join_args, %$pref_args,
220 for (qw/next all first/) {
221 throws_ok { $weird_rs->$_ } qr/not yet determined exception text/;
227 # multi-has_many with underdefined root, with rather random order
228 $rs = $schema->resultset ('CD')->search ({}, {
229 join => [ 'tracks', { single_track => { cd => { artist => { cds => 'tracks' } } } } ],
232 { 'single_track.trackid' => 'single_track.trackid' }, # definitive link to root from 1:1:1:1:M:M chain
233 { 'year' => 'me.year' }, # non-unique
234 { 'tracks.cd' => 'tracks.cd' }, # \ together both uniqueness for second multirel
235 { 'tracks.title' => 'tracks.title' }, # / and definitive link back to root
236 { 'single_track.cd.artist.cds.cdid' => 'cds.cdid' }, # to give uniquiness to ...tracks.title below
237 { 'single_track.cd.artist.cds.year' => 'cds.year' }, # non-unique
238 { 'single_track.cd.artist.artistid' => 'artist.artistid' }, # uniqufies entire parental chain
239 { 'single_track.cd.artist.cds.genreid' => 'cds.genreid' }, # nullable
240 { 'single_track.cd.artist.cds.tracks.title' => 'tracks_2.title' }, # unique when combined with ...cds.cdid above
245 $rs->create({ artist => 1, year => 1977, title => "fuzzy_$_" });
248 my $rs_random = $rs->search({}, { order_by => \ 'RANDOM()' });
249 is ($rs_random->count, 6, 'row count matches');
251 if ($ENV{TEST_VERBOSE}) {
253 [ "What are we actually trying to collapse (Select/As, tests below will see a *DIFFERENT* random order):" ],
254 [ map { my $s = $_; $s =~ s/single_track\./sngl_tr./; $s } @{$rs_random->{_attrs}{select} } ],
255 $rs_random->{_attrs}{as},
257 $rs_random->cursor->all,
260 diag join ' # ', map { sprintf '% 15s', (defined $_ ? $_ : 'NULL') } @$_
265 $schema->storage->debugcb(sub { $queries++ });
266 my $orig_debug = $schema->storage->debug;
267 $schema->storage->debug (1);
269 for my $use_next (0, 1) {
272 while (my $o = $rs_random->next) {
273 push @random_cds, $o;
277 @random_cds = $rs_random->all;
280 is (@random_cds, 6, 'object count matches');
282 for my $cd (@random_cds) {
283 if ($cd->year == 1977) {
284 is( scalar $cd->tracks, 0, 'no tracks on 1977 cd' );
285 is( $cd->single_track, undef, 'no single_track on 1977 cd' );
287 elsif ($cd->year == 1976) {
288 is( scalar $cd->tracks, 2, 'Two tracks on 1976 cd' );
289 like( $_->title, qr/^o\d/, "correct title" )
291 is( $cd->single_track, undef, 'no single_track on 1976 cd' );
293 elsif ($cd->year == 1981) {
294 is( scalar $cd->tracks, 4, 'Four tracks on 1981 cd' );
295 like( $_->title, qr/^m\d/, "correct title" )
297 is( $cd->single_track, undef, 'no single_track on 1981 cd' );
299 elsif ($cd->year == 1978) {
300 is( scalar $cd->tracks, 3, 'Three tracks on 1978 cd' );
301 like( $_->title, qr/^e\d/, "correct title" )
303 ok( defined $cd->single_track, 'single track prefetched on 1987 cd' );
304 is( $cd->single_track->cd->artist->id, 1, 'Single_track->cd->artist prefetched on 1978 cd' );
305 is( scalar $cd->single_track->cd->artist->cds, 6, '6 cds prefetched on artist' );
310 $schema->storage->debugcb(undef);
311 $schema->storage->debug($orig_debug);
312 is ($queries, 2, "Only two queries for rwo prefetch calls total");
314 # can't cmp_deeply a random set - need *some* order
315 my @hris = sort { $a->{year} cmp $b->{year} } @{$rs->search({}, {
316 order_by => [ 'tracks_2.title', 'tracks.title', 'cds.cdid', \ 'RANDOM()' ],
318 is (@hris, 6, 'hri count matches' );
320 cmp_deeply (\@hris, [
322 single_track => undef,
336 single_track => undef,
341 single_track => undef,
346 single_track => undef,
444 single_track => undef,
465 ], 'W00T, multi-has_many manual underdefined root prefetch with collapse works');