::DBI:Replicated - merge connect_info from master to replicants
[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 => 23;
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                         { producer => { name => 'bob' } },
58                         { producer => { name => 'paul' } },
59                         { producer => {
60                           name => 'flemming',
61                           producer_to_cd => [
62                             { cd => {
63                               artist => {
64                                 name => 'kirk',
65                                 cds => [
66                                   {
67                                     title => 'Greatest hits 3',
68                                     year => 2012,
69                                     genre => {
70                                       name => '"Greatest" collections',
71                                     },
72                                     tags => [
73                                       { tag => 'A' },
74                                       { tag => 'B' },
75                                     ],
76                                   },
77                                   {
78                                     title => 'Greatest hits 4',
79                                     year => 2012,
80                                     genre => {
81                                       name => '"Greatest" collections2',
82                                     },
83                                     tags => [
84                                       { tag => 'A' },
85                                       { tag => 'B' },
86                                     ],
87                                   },
88                                 ],
89                               },
90                               title => 'Greatest hits 5',
91                               year => 2013,
92                               genre => {
93                                 name => '"Greatest" collections2',
94                               },
95                             }},
96                           ],
97                         }},
98                       ],
99                     },
100                   ],
101                 },
102                 title => 'Greatest hits 6',
103                 year => 2012,
104                 genre => {
105                   name => '"Greatest" collections',
106                 },
107                 tags => [
108                   { tag => 'A' },
109                   { tag => 'B' },
110                 ],
111               },
112             },
113             {
114               cd => { 
115                 artist => {
116                   name => 'lars',    # should already exist
117                   # even though the artist 'name' is not uniquely constrained
118                   # find_or_create will arguably DWIM 
119                 },
120                 title => 'Greatest hits 7',
121                 year => 2013,
122               },
123             },
124           ],
125         },
126       },
127     ],
128   });
129
130   is ($schema->resultset ('Artist')->count, $counts->{Artist} + 3, '3 new artists created');
131   is ($schema->resultset ('Genre')->count, $counts->{Genre} + 2, '2 additional genres created');
132   is ($schema->resultset ('Producer')->count, $counts->{Producer} + 3, '3 new producer');
133   is ($schema->resultset ('CD')->count, $counts->{CD} + 7, '7 new CDs');
134   is ($schema->resultset ('Tag')->count, $counts->{Tag} + 10, '10 new Tags');
135
136   my $cd_rs = $schema->resultset ('CD')
137     ->search ({ title => { -like => 'Greatest hits %' }}, { order_by => 'title'} );
138   is ($cd_rs->count, 7, '7 greatest hits created');
139
140   my $cds_2012 = $cd_rs->search ({ year => 2012});
141   is ($cds_2012->count, 5, '5 CDs created in 2012');
142
143   is (
144     $cds_2012->search(
145       { 'tags.tag' => { -in => [qw/A B/] } },
146       { join => 'tags', group_by => 'me.cdid' }
147     ),
148     5,
149     'All 10 tags were pairwise distributed between 5 year-2012 CDs'
150   );
151
152   my $paul_prod = $cd_rs->search (
153     { 'producer.name' => 'paul'},
154     { join => { cd_to_producer => 'producer' } }
155   );
156   is ($paul_prod->count, 1, 'Paul had 1 production');
157   my $pauls_cd = $paul_prod->single;
158   is ($pauls_cd->cd_to_producer->count, 3, 'Paul had two co-producers');
159   is (
160     $pauls_cd->search_related ('cd_to_producer',
161       { 'producer.name' => 'flemming'},
162       { join => 'producer' }
163     )->count,
164     1,
165     'The second producer is flemming',
166   );
167
168   my $kirk_cds = $cd_rs->search ({ 'artist.name' => 'kirk' }, { join => 'artist' });
169   is ($kirk_cds, 3, 'Kirk had 3 CDs');
170   is (
171     $kirk_cds->search (
172       { 'cd_to_producer.cd' => { '!=', undef } },
173       { join => 'cd_to_producer' },
174     ),
175     1,
176     'Kirk had a producer only on one cd',
177   );
178
179   my $lars_cds = $cd_rs->search ({ 'artist.name' => 'lars' }, { join => 'artist' });
180   is ($lars_cds->count, 3, 'Lars had 3 CDs');
181   is (
182     $lars_cds->search (
183       { 'cd_to_producer.cd' => undef },
184       { join => 'cd_to_producer' },
185     ),
186     0,
187     'Lars always had a producer',
188   );
189   is (
190     $lars_cds->search_related ('cd_to_producer',
191       { 'producer.name' => 'flemming'},
192       { join => 'producer' }
193     )->count,
194     1,
195     'Lars produced 1 CD with flemming',
196   );
197   is (
198     $lars_cds->search_related ('cd_to_producer',
199       { 'producer.name' => 'bob'},
200       { join => 'producer' }
201     )->count,
202     3,
203     'Lars produced 3 CDs with bob',
204   );
205
206   my $bob_prod = $cd_rs->search (
207     { 'producer.name' => 'bob'},
208     { join => { cd_to_producer => 'producer' } }
209   );
210   is ($bob_prod->count, 4, 'Bob produced a total of 4 CDs');
211   ok ($bob_prod->find ({ title => 'Greatest hits 1'}), '1st Bob production name correct');
212   ok ($bob_prod->find ({ title => 'Greatest hits 6'}), '2nd Bob production name correct');
213   ok ($bob_prod->find ({ title => 'Greatest hits 2'}), '3rd Bob production name correct');
214   ok ($bob_prod->find ({ title => 'Greatest hits 7'}), '4th Bob production name correct');
215
216   is (
217     $bob_prod->search ({ 'artist.name' => 'james' }, { join => 'artist' })->count,
218     1,
219     "Bob produced james' only CD",
220   );
221 };
222 diag $@ if $@;
223
224 1;