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