Untangle strictly-DBICTest constant from the main constant set
[dbsrgits/DBIx-Class.git] / t / multi_create / torture.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 use DBICTest;
9
10 plan tests => 23;
11
12 # an insane multicreate
13 # (should work, despite the fact that no one will probably use it this way)
14
15 my $schema = DBICTest->init_schema();
16
17 # first count how many rows do we initially have
18 my $counts;
19 $counts->{$_} = $schema->resultset($_)->count for qw/Artist CD Genre Producer Tag/;
20
21 # do the crazy create
22 eval {
23   $schema->resultset('CD')->create ({
24     artist => {
25       name => 'james',
26     },
27     title => 'Greatest hits 1',
28     year => '2012',
29     genre => {
30       name => '"Greatest" collections',
31     },
32     tags => [
33       { tag => 'A' },
34       { tag => 'B' },
35     ],
36     cd_to_producer => [
37       {
38         producer => {
39           name => 'bob',
40           producer_to_cd => [
41             {
42               cd => {
43                 artist => {
44                   name => 'lars',
45                   cds => [
46                     {
47                       title => 'Greatest hits 2',
48                       year => 2012,
49                       genre => {
50                         name => '"Greatest" collections',
51                       },
52                       tags => [
53                         { tag => 'A' },
54                         { tag => 'B' },
55                       ],
56                       # This cd is created via artist so it doesn't know about producers
57                       cd_to_producer => [
58                         { producer => { name => 'bob' } },
59                         { producer => { name => 'paul' } },
60                         { producer => {
61                           name => 'flemming',
62                           producer_to_cd => [
63                             { cd => {
64                               artist => {
65                                 name => 'kirk',
66                                 cds => [
67                                   {
68                                     title => 'Greatest hits 3',
69                                     year => 2012,
70                                     genre => {
71                                       name => '"Greatest" collections',
72                                     },
73                                     tags => [
74                                       { tag => 'A' },
75                                       { tag => 'B' },
76                                     ],
77                                   },
78                                   {
79                                     title => 'Greatest hits 4',
80                                     year => 2012,
81                                     genre => {
82                                       name => '"Greatest" collections2',
83                                     },
84                                     tags => [
85                                       { tag => 'A' },
86                                       { tag => 'B' },
87                                     ],
88                                   },
89                                 ],
90                               },
91                               title => 'Greatest hits 5',
92                               year => 2013,
93                               genre => {
94                                 name => '"Greatest" collections2',
95                               },
96                             }},
97                           ],
98                         }},
99                       ],
100                     },
101                   ],
102                 },
103                 title => 'Greatest hits 6',
104                 year => 2012,
105                 genre => {
106                   name => '"Greatest" collections',
107                 },
108                 tags => [
109                   { tag => 'A' },
110                   { tag => 'B' },
111                 ],
112               },
113             },
114             {
115               cd => {
116                 artist => {
117                   name => 'lars',    # should already exist
118                   # even though the artist 'name' is not uniquely constrained
119                   # find_or_create will arguably DWIM
120                 },
121                 title => 'Greatest hits 7',
122                 year => 2013,
123               },
124             },
125           ],
126         },
127       },
128     ],
129   });
130
131   is ($schema->resultset ('Artist')->count, $counts->{Artist} + 3, '3 new artists created');
132   is ($schema->resultset ('Genre')->count, $counts->{Genre} + 2, '2 additional genres created');
133   is ($schema->resultset ('Producer')->count, $counts->{Producer} + 3, '3 new producer');
134   is ($schema->resultset ('CD')->count, $counts->{CD} + 7, '7 new CDs');
135   is ($schema->resultset ('Tag')->count, $counts->{Tag} + 10, '10 new Tags');
136
137   my $cd_rs = $schema->resultset ('CD')
138     ->search ({ title => { -like => 'Greatest hits %' }}, { order_by => 'title'} );
139   is ($cd_rs->count, 7, '7 greatest hits created');
140
141   my $cds_2012 = $cd_rs->search ({ year => 2012});
142   is ($cds_2012->count, 5, '5 CDs created in 2012');
143
144   is (
145     $cds_2012->search(
146       { 'tags.tag' => { -in => [qw/A B/] } },
147       {
148         join => 'tags',
149         group_by => 'me.cdid',
150         having => 'count(me.cdid) = 2',
151       }
152     ),
153     5,
154     'All 10 tags were pairwise distributed between 5 year-2012 CDs'
155   );
156
157   my $paul_prod = $cd_rs->search (
158     { 'producer.name' => 'paul'},
159     { join => { cd_to_producer => 'producer' } }
160   );
161   is ($paul_prod->count, 1, 'Paul had 1 production');
162   my $pauls_cd = $paul_prod->single;
163   is ($pauls_cd->cd_to_producer->count, 3, 'Paul had two co-producers');
164   is (
165     $pauls_cd->search_related ('cd_to_producer',
166       { 'producer.name' => 'flemming'},
167       { join => 'producer' }
168     )->count,
169     1,
170     'The second producer is flemming',
171   );
172
173   my $kirk_cds = $cd_rs->search ({ 'artist.name' => 'kirk' }, { join => 'artist' });
174   is ($kirk_cds, 3, 'Kirk had 3 CDs');
175   is (
176     $kirk_cds->search (
177       { 'cd_to_producer.cd' => { '!=', undef } },
178       { join => 'cd_to_producer' },
179     ),
180     1,
181     'Kirk had a producer only on one cd',
182   );
183
184   my $lars_cds = $cd_rs->search ({ 'artist.name' => 'lars' }, { join => 'artist' });
185   is ($lars_cds->count, 3, 'Lars had 3 CDs');
186   is (
187     $lars_cds->search (
188       { 'cd_to_producer.cd' => undef },
189       { join => 'cd_to_producer' },
190     ),
191     0,
192     'Lars always had a producer',
193   );
194   is (
195     $lars_cds->search_related ('cd_to_producer',
196       { 'producer.name' => 'flemming'},
197       { join => 'producer' }
198     )->count,
199     1,
200     'Lars produced 1 CD with flemming',
201   );
202   is (
203     $lars_cds->search_related ('cd_to_producer',
204       { 'producer.name' => 'bob'},
205       { join => 'producer' }
206     )->count,
207     3,
208     'Lars produced 3 CDs with bob',
209   );
210
211   my $bob_prod = $cd_rs->search (
212     { 'producer.name' => 'bob'},
213     { join => { cd_to_producer => 'producer' } }
214   );
215   is ($bob_prod->count, 4, 'Bob produced a total of 4 CDs');
216   ok ($bob_prod->find ({ title => 'Greatest hits 1'}), '1st Bob production name correct');
217   ok ($bob_prod->find ({ title => 'Greatest hits 6'}), '2nd Bob production name correct');
218   ok ($bob_prod->find ({ title => 'Greatest hits 2'}), '3rd Bob production name correct');
219   ok ($bob_prod->find ({ title => 'Greatest hits 7'}), '4th Bob production name correct');
220
221   is (
222     $bob_prod->search ({ 'artist.name' => 'james' }, { join => 'artist' })->count,
223     1,
224     "Bob produced james' only CD",
225   );
226 };
227 diag $@ if $@;
228
229 1;