fixed search with joins from related resultset
[dbsrgits/DBIx-Class.git] / t / 90join_torture.t
1 use strict;
2 use warnings;  
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 my $schema = DBICTest->init_schema();
9
10 plan tests => 9;
11
12 my @rs1a_results = $schema->resultset("Artist")->search_related('cds', {title => 'Forkful of bees'}, {order_by => 'title'});
13 is($rs1a_results[0]->title, 'Forkful of bees', "bare field conditions okay after search related");
14
15 my $rs1 = $schema->resultset("Artist")->search({ 'tags.tag' => 'Blue' }, { join => {'cds' => 'tracks'}, prefetch => {'cds' => 'tags'} });
16 my @artists = $rs1->all;
17 cmp_ok(@artists, '==', 1, "Two artists returned");
18
19 my $rs2 = $rs1->search({ artistid => '1' }, { join => {'cds' => {'cd_to_producer' => 'producer'} } });
20
21 my @artists2 = $rs2->search({ 'producer.name' => 'Matt S Trout' });
22 my @cds = $artists2[0]->cds;
23 cmp_ok(scalar @cds, '==', 1, "condition based on inherited join okay");
24
25 # this is wrong, should accept me.title really
26 my $rs3 = $rs2->search_related('cds');
27 cmp_ok($rs3->count, '==', 9, "Nine artists returned");
28
29 my $rs4 = $schema->resultset("CD")->search({ 'artist.artistid' => '1' }, { join => ['tracks', 'artist'], prefetch => 'artist' });
30 my @rs4_results = $rs4->all;
31
32 is($rs4_results[0]->cdid, 1, "correct artist returned");
33
34 my $rs5 = $rs4->search({'tracks.title' => 'Sticky Honey'});
35 is($rs5->count, 1, "search without using previous joins okay");
36
37 my $record_rs = $schema->resultset("Artist")->search(undef, { join => 'cds' })->search(undef, { prefetch => { 'cds' => 'tracks' }});
38 my $record_jp = $record_rs->next;
39 ok($record_jp, "prefetch on same rel okay");
40
41 my $cd = $schema->resultset("CD")->find(1);
42 my $producers = $cd->producers;
43 is($producers->find(2)->name, 'Bob The Builder', "find on many to many okay");
44
45 my @prods = $producers->search({name => 'Bob The Builder'}, { prefetch => 'producer_to_cd' })->all;
46 is($prods[0]->name, 'Bob The Builder', 'prefetch after has_many rel okay');
47
48 1;