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