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