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