Hit an MC hash-order bug - simplify create, and solve it another time
[dbsrgits/DBIx-Class.git] / t / prefetch / manual.t
CommitLineData
69ab63d4 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
6use lib qw(t/lib);
7use DBICTest;
8
908aa1bb 9my $schema = DBICTest->init_schema(no_populate => 1);
10
742280c7 11$schema->resultset('Artist')->create({ name => 'JMJ', cds => [{
12 title => 'Magnetic Fields',
13 year => 1981,
14 genre => { name => 'electro' },
15 tracks => [
16 { title => 'm1' },
17 { title => 'm2' },
18 { title => 'm3' },
19 { title => 'm4' },
20 ],
21} ] });
22
908aa1bb 23$schema->resultset('CD')->create({
24 title => 'Equinoxe',
25 year => 1978,
26 artist => { name => 'JMJ' },
27 genre => { name => 'electro' },
28 tracks => [
29 { title => 'e1' },
30 { title => 'e2' },
31 { title => 'e3' },
32 ],
33 single_track => {
34 title => 'o1',
35 cd => {
36 title => 'Oxygene',
37 year => 1976,
742280c7 38 artist => { name => 'JMJ' },
908aa1bb 39 tracks => [
40 { title => 'o2', position => 2}, # the position should not be here, bug in MC
41 ],
42 },
43 },
44});
69ab63d4 45
46my $rs = $schema->resultset ('CD')->search ({}, {
47 join => [ 'tracks', { single_track => { cd => { artist => { cds => 'tracks' } } } } ],
48 collapse => 1,
49 columns => [
50 { 'year' => 'me.year' }, # non-unique
51 { 'genreid' => 'me.genreid' }, # nullable
52 { 'tracks.title' => 'tracks.title' }, # non-unique (no me.id)
53 { 'single_track.cd.artist.cds.cdid' => 'cds.cdid' }, # to give uniquiness to ...tracks.title below
3904d3c3 54 { 'single_track.cd.artist.artistid' => 'artist.artistid' }, # uniqufies entire parental chain
69ab63d4 55 { 'single_track.cd.artist.cds.year' => 'cds.year' }, # non-unique
56 { 'single_track.cd.artist.cds.genreid' => 'cds.genreid' }, # nullable
57 { 'single_track.cd.artist.cds.tracks.title' => 'tracks_2.title' }, # unique when combined with ...cds.cdid above
908aa1bb 58 { 'latest_cd' => \ "(SELECT MAX(year) FROM cd)" }, # random function
3904d3c3 59 { 'title' => 'me.title' }, # uniquiness for me
60 { 'artist' => 'me.artist' }, # uniquiness for me
69ab63d4 61 ],
908aa1bb 62 order_by => [{ -desc => 'cds.year' }, { -desc => 'me.title'} ],
69ab63d4 63});
64
908aa1bb 65my $hri_rs = $rs->search({}, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' });
66
67is_deeply (
68 [$hri_rs->all],
69 [
70 {
71 artist => 1,
72 genreid => 1,
73 latest_cd => 1981,
74 single_track => {
75 cd => {
76 artist => {
77 artistid => 1,
78 cds => [
79 {
80 cdid => 1,
81 genreid => 1,
82 tracks => [
83 {
84 title => "m1"
85 },
86 {
87 title => "m2"
88 },
89 {
90 title => "m3"
91 },
92 {
93 title => "m4"
94 }
95 ],
96 year => 1981
97 },
98 {
99 cdid => 3,
100 genreid => 1,
101 tracks => [
102 {
103 title => "e1"
104 },
105 {
106 title => "e2"
107 },
108 {
109 title => "e3"
110 }
111 ],
112 year => 1978
113 },
114 {
115 cdid => 2,
116 genreid => undef,
117 tracks => [
118 {
119 title => "o1"
120 },
121 {
122 title => "o2"
123 }
124 ],
125 year => 1976
126 }
127 ]
128 }
129 }
130 },
131 title => "Equinoxe",
132 tracks => [
133 {
134 title => "e1"
135 },
136 {
137 title => "e2"
138 },
139 {
140 title => "e3"
141 }
142 ],
143 year => 1978
144 },
145 {
146 artist => 1,
147 genreid => undef,
148 latest_cd => 1981,
149 single_track => undef,
150 title => "Oxygene",
151 tracks => [
152 {
153 title => "o1"
154 },
155 {
156 title => "o2"
157 }
158 ],
159 year => 1976
160 },
161 {
162 artist => 1,
163 genreid => 1,
164 latest_cd => 1981,
165 single_track => undef,
166 title => "Magnetic Fields",
167 tracks => [
168 {
169 title => "m1"
170 },
171 {
172 title => "m2"
173 },
174 {
175 title => "m3"
176 },
177 {
178 title => "m4"
179 }
180 ],
181 year => 1981
182 },
183 ],
184 'W00T, manual prefetch with collapse works'
185);
186
187my $row = $rs->next;
188
189TODO: {
190 local $TODO = 'Something is wrong with filter type rels, they throw on incomplete objects >.<';
191
192 lives_ok {
193 is_deeply (
194 { $row->single_track->get_columns },
195 {},
196 'empty intermediate object ok',
197 )
198 } 'no exception';
199}
69ab63d4 200
908aa1bb 201is ($rs->cursor->next, undef, 'cursor exhausted');
69ab63d4 202
4e9fc3f3 203TODO: {
204local $TODO = 'this does not work at all, need to promote rsattrs to an object on its own';
205# make sure has_many column redirection does not do weird stuff when collapse is requested
206for my $pref_args (
207 { prefetch => 'cds'},
208 { collapse => 1 }
209) {
210 for my $col_and_join_args (
211 { '+columns' => { 'cd_title' => 'cds_2.title' }, join => [ 'cds', 'cds' ] },
212 { '+columns' => { 'cd_title' => 'cds.title' }, join => 'cds', }
213 ) {
214
215 my $weird_rs = $schema->resultset('Artist')->search({}, {
216 %$col_and_join_args, %$pref_args,
217 });
218
219 for (qw/next all first/) {
220 throws_ok { $weird_rs->$_ } qr/not yet determined exception text/;
221 }
222 }
223}
224}
225
908aa1bb 226done_testing;