Run t/storage/global_destruction.t using SQLite as well
[dbsrgits/DBIx-Class.git] / t / 90join_torture.t
CommitLineData
ae515736 1use strict;
22e40557 2use warnings;
ae515736 3
4use Test::More;
22e40557 5use Test::Exception;
ae515736 6use lib qw(t/lib);
7use DBICTest;
22e40557 8use DBIC::SqlMakerTest;
ae515736 9my $schema = DBICTest->init_schema();
10
c9733800 11 {
12 my $rs = $schema->resultset( 'CD' )->search(
13 {
14 'producer.name' => 'blah',
15 'producer_2.name' => 'foo',
16 },
17 {
18 'join' => [
19 { cd_to_producer => 'producer' },
20 { cd_to_producer => 'producer' },
21 ],
22 'prefetch' => [
23 'artist',
24 { cd_to_producer => 'producer' },
25 ],
26 }
27 );
28
29 lives_ok {
30 my @rows = $rs->all();
31 };
32 }
22e40557 33
00a80124 34
35my @rs1a_results = $schema->resultset("Artist")->search_related('cds', {title => 'Forkful of bees'}, {order_by => 'title'});
36is($rs1a_results[0]->title, 'Forkful of bees', "bare field conditions okay after search related");
ae515736 37my $rs1 = $schema->resultset("Artist")->search({ 'tags.tag' => 'Blue' }, { join => {'cds' => 'tracks'}, prefetch => {'cds' => 'tags'} });
38my @artists = $rs1->all;
b25e9fa0 39cmp_ok(@artists, '==', 2, "Two artists returned");
ae515736 40
41my $rs2 = $rs1->search({ artistid => '1' }, { join => {'cds' => {'cd_to_producer' => 'producer'} } });
b253a82b 42my @artists2 = $rs2->search({ 'producer.name' => 'Matt S Trout' });
43my @cds = $artists2[0]->cds;
44cmp_ok(scalar @cds, '==', 1, "condition based on inherited join okay");
45
cd40c868 46my $rs3 = $rs2->search_related('cds');
1d78a406 47
8124a75c 48cmp_ok(scalar($rs3->all), '==', 15, "All cds for artist returned");
1d78a406 49
8124a75c 50cmp_ok($rs3->count, '==', 15, "All cds for artist returned via count");
ae515736 51
52my $rs4 = $schema->resultset("CD")->search({ 'artist.artistid' => '1' }, { join => ['tracks', 'artist'], prefetch => 'artist' });
53my @rs4_results = $rs4->all;
54
ae515736 55is($rs4_results[0]->cdid, 1, "correct artist returned");
56
57my $rs5 = $rs4->search({'tracks.title' => 'Sticky Honey'});
58is($rs5->count, 1, "search without using previous joins okay");
59
08a39415 60my $record_rs = $schema->resultset("Artist")->search(undef, { join => 'cds' })->search(undef, { prefetch => { 'cds' => 'tracks' }});
61my $record_jp = $record_rs->next;
62ok($record_jp, "prefetch on same rel okay");
63
b7439887 64my $artist = $schema->resultset("Artist")->find(1);
65my $cds = $artist->cds;
66is($cds->find(2)->title, 'Forkful of bees', "find on has many rs okay");
67
68my $cd = $cds->search({'me.title' => 'Forkful of bees'}, { prefetch => 'tracks' })->first;
69my @tracks = $cd->tracks->all;
70is(scalar(@tracks), 3, 'right number of prefetched tracks after has many');
71
4e460493 72#causes ambig col error due to order_by
b7439887 73#my $tracks_rs = $cds->search_related('tracks', { 'tracks.position' => '2', 'disc.title' => 'Forkful of bees' });
74#my $first_tracks_rs = $tracks_rs->first;
08a39415 75
48c9af02 76my $related_rs = $schema->resultset("Artist")->search({ name => 'Caterwauler McCrae' })->search_related('cds', { year => '2001'})->search_related('tracks', { 'position' => '2' });
77is($related_rs->first->trackid, '5', 'search related on search related okay');
78
4e460493 79#causes ambig col error due to order_by
48c9af02 80#$related_rs->search({'cd.year' => '2001'}, {join => ['cd', 'cd']})->all;
81
82my $title = $schema->resultset("Artist")->search_related('twokeys')->search_related('cd')->search({'tracks.position' => '2'}, {join => 'tracks', order_by => 'tracks.trackid'})->next->title;
83is($title, 'Forkful of bees', 'search relateds with order by okay');
cd40c868 84
4e460493 85my $prod_rs = $schema->resultset("CD")->find(1)->producers_sorted;
86my $prod_rs2 = $prod_rs->search({ name => 'Matt S Trout' });
87my $prod_first = $prod_rs2->first;
88is($prod_first->id, '1', 'somewhat pointless search on rel with order_by on it okay');
d0609d74 89
90my $prod_map_rs = $schema->resultset("Artist")->find(1)->cds->search_related('cd_to_producer', {}, { join => 'producer', prefetch => 'producer' });
10481a5d 91ok($prod_map_rs->next->producer, 'search related with prefetch okay');
d0609d74 92
4e460493 93my $stupid = $schema->resultset("Artist")->search_related('artist_undirected_maps', {}, { prefetch => 'artist1' })->search_related('mapped_artists')->search_related('cds', {'cds.cdid' => '2'}, { prefetch => 'tracks' });
d0609d74 94
4e460493 95my $cd_final = $schema->resultset("Artist")->search_related('artist_undirected_maps', {}, { prefetch => 'artist1' })->search_related('mapped_artists')->search_related('cds', {'cds.cdid' => '2'}, { prefetch => 'tracks' })->first;
96is($cd_final->cdid, '2', 'bonkers search_related-with-join-midway okay');
d0609d74 97
e083fb1e 98# should end up with cds and cds_2 joined
99my $merge_rs_1 = $schema->resultset("Artist")->search({ 'cds_2.cdid' => '2' }, { join => ['cds', 'cds'] });
100is(scalar(@{$merge_rs_1->{attrs}->{join}}), 2, 'both joins kept');
101ok($merge_rs_1->next, 'query on double joined rel runs okay');
102
103# should only end up with cds joined
104my $merge_rs_2 = $schema->resultset("Artist")->search({ }, { join => 'cds' })->search({ 'cds.cdid' => '2' }, { join => 'cds' });
105is(scalar(@{$merge_rs_2->{attrs}->{join}}), 1, 'only one join kept when inherited');
106my $merge_rs_2_cd = $merge_rs_2->next;
107
faeb2407 108lives_ok (sub {
fe7748d8 109
110 my @rs_with_prefetch = $schema->resultset('TreeLike')
111 ->search(
112 {'me.id' => 1},
113 {
114 prefetch => [ 'parent', { 'children' => 'parent' } ],
115 });
116
faeb2407 117}, 'pathological prefetch ok');
fe7748d8 118
aab9e887 119my $rs = $schema->resultset("Artist")->search({}, { join => 'twokeys' });
120my $second_search_rs = $rs->search({ 'cds_2.cdid' => '2' }, { join =>
121['cds', 'cds'] });
122is(scalar(@{$second_search_rs->{attrs}->{join}}), 3, 'both joins kept');
123ok($second_search_rs->next, 'query on double joined rel runs okay');
124
faeb2407 125# test joinmap pruner
126lives_ok ( sub {
127 my $rs = $schema->resultset('Artwork')->search (
128 {
129 },
130 {
131 distinct => 1,
132 join => [
133 { artwork_to_artist => 'artist' },
134 { cd => 'artist' },
135 ],
136 },
137 );
138
139 is_same_sql_bind (
140 $rs->count_rs->as_query,
141 '(
142 SELECT COUNT( * )
143 FROM (
144 SELECT me.cd_id
145 FROM cd_artwork me
146 JOIN cd cd ON cd.cdid = me.cd_id
147 JOIN artist artist_2 ON artist_2.artistid = cd.artist
148 GROUP BY me.cd_id
336feb8e 149 ) me
faeb2407 150 )',
151 [],
152 );
153
154 ok (defined $rs->count);
155});
156
a4812caa 157# make sure multiplying endpoints do not lose heir join-path
158lives_ok (sub {
159 my $rs = $schema->resultset('CD')->search (
160 { },
161 { join => { artwork => 'images' } },
162 )->get_column('cdid');
163
164 is_same_sql_bind (
165 $rs->as_query,
166 '(
167 SELECT me.cdid
168 FROM cd me
169 LEFT JOIN cd_artwork artwork
170 ON artwork.cd_id = me.cdid
171 LEFT JOIN images images
172 ON images.artwork_id = artwork.cd_id
173 )',
174 [],
175 );
176
177 # execution
178 $rs->next;
179});
180
22e40557 181done_testing;