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