Extra test from Will Hawes
[dbsrgits/DBIx-Class-Historic.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
99DBICTest::Schema::CD->add_relationship(
100 artist => 'DBICTest::Schema::Artist',
101 { 'foreign.artistid' => 'self.artist' },
102 { accessor => 'filter' },
103);
104
105DBICTest::Schema::CD->add_relationship(
106 liner_notes => 'DBICTest::Schema::LinerNotes',
107 { 'foreign.liner_id' => 'self.cdid' },
108 { join_type => 'LEFT', accessor => 'single' });
109
3712e4f4 110$rs = DBICTest->class("CD")->search(
0567538f 111 { 'artist.name' => 'Caterwauler McCrae' },
112 { prefetch => [ qw/artist liner_notes/ ],
113 order_by => 'me.cdid' });
114
115cmp_ok($rs->count, '==', 3, 'Correct number of records returned');
116
117# start test for prefetch SELECT count
118unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
119DBI->trace(1, 't/var/dbic.trace');
120
121my @cd = $rs->all;
122
123is($cd[0]->title, 'Spoonful of bees', 'First record returned ok');
124
7cd300ea 125ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join');
0567538f 126
127is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN');
128
dd417d06 129is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class');
130
0567538f 131is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok');
132
133# count the SELECTs
134DBI->trace(0);
135my $selects = 0;
136my $trace = IO::File->new('t/var/dbic.trace', '<')
137 or die "Unable to read trace file";
138while (<$trace>) {
139 $selects++ if /SELECT/;
140}
141$trace->close;
142unlink 't/var/dbic.trace';
143is($selects, 1, 'prefetch ran only 1 select statement');
144
3712e4f4 145my ($artist) = DBICTest->class("Artist")->search({ 'cds.year' => 2001 },
0567538f 146 { order_by => 'artistid DESC', join => 'cds' });
147
148is($artist->name, 'Random Boy Band', "Join search by object ok");
149
3712e4f4 150my @cds = DBICTest->class("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
0567538f 151 { join => 'liner_notes' });
152
153cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");
154
155is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");
156
3712e4f4 157my @artists = DBICTest->class("Artist")->search({ 'tags.tag' => 'Shiny' },
0567538f 158 { join => { 'cds' => 'tags' } });
159
160cmp_ok( @artists, '==', 2, "two-join search ok" );
161
162}
163
1641;