Release shipped, Changes update and version bumped
[dbsrgits/DBIx-Class.git] / t / run / 16joins.tl
CommitLineData
0567538f 1sub run_tests {
2
3use IO::File;
4
5BEGIN {
6 eval "use DBD::SQLite";
7 plan $@
8 ? ( skip_all => 'needs DBD::SQLite for testing' )
8fe164b9 9 : ( tests => 23 );
0567538f 10}
11
12# test the abstract join => SQL generator
13my $sa = new DBIC::SQL::Abstract;
14
15my @j = (
16 { child => 'person' },
17 [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ],
18 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
19);
20my $match = 'person child JOIN person father ON ( father.person_id = '
21 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
22 . '= child.mother_id )'
23 ;
24is( $sa->_recurse_from(@j), $match, 'join 1 ok' );
25
26my @j2 = (
27 { mother => 'person' },
28 [ [ { child => 'person' },
29 [ { father => 'person' },
30 { 'father.person_id' => 'child.father_id' }
31 ]
32 ],
33 { 'mother.person_id' => 'child.mother_id' }
34 ],
35);
36$match = 'person mother JOIN (person child JOIN person father ON ('
37 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
38 . 'child.mother_id )'
39 ;
40is( $sa->_recurse_from(@j2), $match, 'join 2 ok' );
41
42my @j3 = (
43 { child => 'person' },
44 [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ],
45 [ { mother => 'person', -join_type => 'inner' }, { 'mother.person_id' => 'child.mother_id' } ],
46);
47$match = 'person child INNER JOIN person father ON ( father.person_id = '
48 . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id '
49 . '= child.mother_id )'
50 ;
51
52is( $sa->_recurse_from(@j3), $match, 'join 3 (inner join) ok');
53
3712e4f4 54my $rs = DBICTest->class("CD")->search(
0567538f 55 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
56 { from => [ { 'me' => 'cd' },
57 [
58 { artist => 'artist' },
59 { 'me.artist' => 'artist.artistid' }
60 ] ] }
61 );
62
63cmp_ok( $rs->count, '==', 1, "Single record in resultset");
64
65is($rs->first->title, 'Forkful of bees', 'Correct record returned');
66
3712e4f4 67$rs = DBICTest->class("CD")->search(
0567538f 68 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
69 { join => 'artist' });
70
71cmp_ok( $rs->count, '==', 1, "Single record in resultset");
72
73is($rs->first->title, 'Forkful of bees', 'Correct record returned');
74
3712e4f4 75$rs = DBICTest->class("CD")->search(
0567538f 76 { 'artist.name' => 'We Are Goth',
77 'liner_notes.notes' => 'Kill Yourself!' },
78 { join => [ qw/artist liner_notes/ ] });
79
80cmp_ok( $rs->count, '==', 1, "Single record in resultset");
81
82is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
83
8fe164b9 84# when using join attribute, make sure slice()ing all objects has same count as all()
85$rs = DBICTest->class("CD")->search(
86 { 'artist' => 1 },
87 { join => [qw/artist/], order_by => 'artist.name' }
88);
89cmp_ok( scalar $rs->all, '==', scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
90
3712e4f4 91$rs = DBICTest->class("Artist")->search(
0567538f 92 { 'liner_notes.notes' => 'Kill Yourself!' },
93 { join => { 'cds' => 'liner_notes' } });
94
95cmp_ok( $rs->count, '==', 1, "Single record in resultset");
96
97is($rs->first->name, 'We Are Goth', 'Correct record returned');
98
3712e4f4 99$rs = DBICTest->class("CD")->search(
0567538f 100 { 'artist.name' => 'Caterwauler McCrae' },
101 { prefetch => [ qw/artist liner_notes/ ],
102 order_by => 'me.cdid' });
103
104cmp_ok($rs->count, '==', 3, 'Correct number of records returned');
105
106# start test for prefetch SELECT count
107unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
108DBI->trace(1, 't/var/dbic.trace');
109
110my @cd = $rs->all;
111
112is($cd[0]->title, 'Spoonful of bees', 'First record returned ok');
113
7cd300ea 114ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join');
0567538f 115
116is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN');
117
dd417d06 118is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class');
119
0567538f 120is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok');
121
122# count the SELECTs
123DBI->trace(0);
124my $selects = 0;
125my $trace = IO::File->new('t/var/dbic.trace', '<')
126 or die "Unable to read trace file";
127while (<$trace>) {
128 $selects++ if /SELECT/;
129}
130$trace->close;
131unlink 't/var/dbic.trace';
132is($selects, 1, 'prefetch ran only 1 select statement');
133
3712e4f4 134my ($artist) = DBICTest->class("Artist")->search({ 'cds.year' => 2001 },
0567538f 135 { order_by => 'artistid DESC', join => 'cds' });
136
137is($artist->name, 'Random Boy Band', "Join search by object ok");
138
3712e4f4 139my @cds = DBICTest->class("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
0567538f 140 { join => 'liner_notes' });
141
142cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");
143
144is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");
145
3712e4f4 146my @artists = DBICTest->class("Artist")->search({ 'tags.tag' => 'Shiny' },
0567538f 147 { join => { 'cds' => 'tags' } });
148
149cmp_ok( @artists, '==', 2, "two-join search ok" );
150
151}
152
1531;