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