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