d9e9575a09708bdb5a7d16ee98efd35615b5b86f
[dbsrgits/DBIx-Class.git] / t / prefetch / _internals.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 my $schema = DBICTest->init_schema(no_deploy => 1);
9
10
11 my $irow = $schema->source ('Artwork')->_parse_row (
12   {
13     'cd_id' => '1',
14
15     'artwork_to_artist.artist_id' => '2',
16     'artwork_to_artist.artwork_cd_id' => '1',
17
18     'cd.artist' => '1',
19     'cd.cdid' => '1',
20     'cd.title' => 'Spoonful of bees',
21
22     'cd.artist.artistid' => '1',
23     'cd.artist.name' => 'Caterwauler McCrae',
24   },
25   'will collapse'
26 );
27
28 is_deeply (
29   $irow,
30   [
31     {
32       'cd_id' => '1'
33     },
34     {
35       'artwork_to_artist' => [
36         [
37           {
38             'artist_id' => '2',
39             'artwork_cd_id' => '1'
40           }
41         ]
42       ],
43
44       'cd' => [
45         {
46           'artist' => '1',
47           'cdid' => '1',
48           'title' => 'Spoonful of bees',
49         },
50         {
51           'artist' => [
52             {
53               'artistid' => '1',
54               'name' => 'Caterwauler McCrae',
55             }
56           ]
57         }
58       ]
59     }
60   ],
61   '_parse_row works as expected with expected collapse',
62 );
63
64 $irow = $schema->source ('Artist')->_parse_row (
65   {
66     'name' => 'Caterwauler McCrae',
67     'cds.tracks.cd' => '3',
68     'cds.tracks.title' => 'Fowlin',
69     'cds.tracks.cd_single.title' => 'Awesome single',
70   }
71 );
72 is_deeply (
73   $irow,
74   [
75     {
76       'name' => 'Caterwauler McCrae'
77     },
78     {
79       'cds' => [
80         {},
81         {
82           'tracks' => [
83             {
84               'cd' => '3',
85               'title' => 'Fowlin'
86             },
87             {
88               'cd_single' => [
89                 {
90                   title => 'Awesome single',
91                 },
92               ],
93             },
94           ]
95         }
96       ]
97     }
98   ],
99   '_parse_row works over missing joins without collapse',
100 );
101
102 my ($collapse_map, $order) = $schema->source ('CD')->_resolve_collapse (
103   [
104     'year',                                   # non-unique
105     'genreid',                                # nullable
106     'tracks.title',                           # non-unique (no me.id)
107     'single_track.cd.artist.cds.cdid',        # to give uniquiness to ...tracks.title below
108     'single_track.cd.artist.cds.artist',      # non-unique
109     'single_track.cd.artist.cds.year',        # non-unique
110     'single_track.cd.artist.cds.genreid',     # nullable
111     'single_track.cd.artist.cds.tracks.title',# unique when combined with ...cds.cdid above
112     'latest_cd',                              # random function
113   ],
114 );
115
116 is_deeply (
117   $collapse_map,
118   {
119     -collapse_on => {
120       "single_track.cd.artist.cds.artist" => 1
121     },
122
123     single_track => {
124       -collapse_on => {
125        "single_track.cd.artist.cds.artist" => 1
126       },
127
128       cd => {
129         -collapse_on => {
130           "single_track.cd.artist.cds.artist" => 1
131         },
132
133         artist => {
134           -collapse_on => {
135             "single_track.cd.artist.cds.artist" => 1
136           },
137
138           cds => {
139             -collapse_on => {
140               "single_track.cd.artist.cds.cdid" => 1
141             },
142
143             tracks => {
144               -collapse_on => {
145                 "single_track.cd.artist.cds.cdid" => 1,
146                 "single_track.cd.artist.cds.tracks.title" => 1
147               }
148             }
149           }
150         }
151       }
152     },
153     tracks => {
154       -collapse_on => {
155         "single_track.cd.artist.cds.artist" => 1,
156         "tracks.title" => 1
157       }
158     }
159   },
160   "Proper collapse map constructed",
161 );
162
163 done_testing;