Commit | Line | Data |
b6678114 |
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 | |
186c7bef |
9 | plan tests => 23; |
b6678114 |
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 => [ |
186c7bef |
57 | { producer => { name => 'bob' } }, |
b6678114 |
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/] } }, |
2746448f |
146 | { |
147 | join => 'tags', |
148 | group_by => 'me.cdid', |
149 | having => 'count(me.cdid) = 2', |
150 | } |
b6678114 |
151 | ), |
152 | 5, |
153 | 'All 10 tags were pairwise distributed between 5 year-2012 CDs' |
154 | ); |
155 | |
156 | my $paul_prod = $cd_rs->search ( |
157 | { 'producer.name' => 'paul'}, |
158 | { join => { cd_to_producer => 'producer' } } |
159 | ); |
160 | is ($paul_prod->count, 1, 'Paul had 1 production'); |
161 | my $pauls_cd = $paul_prod->single; |
186c7bef |
162 | is ($pauls_cd->cd_to_producer->count, 3, 'Paul had two co-producers'); |
b6678114 |
163 | is ( |
164 | $pauls_cd->search_related ('cd_to_producer', |
165 | { 'producer.name' => 'flemming'}, |
166 | { join => 'producer' } |
167 | )->count, |
168 | 1, |
169 | 'The second producer is flemming', |
170 | ); |
171 | |
172 | my $kirk_cds = $cd_rs->search ({ 'artist.name' => 'kirk' }, { join => 'artist' }); |
173 | is ($kirk_cds, 3, 'Kirk had 3 CDs'); |
174 | is ( |
175 | $kirk_cds->search ( |
176 | { 'cd_to_producer.cd' => { '!=', undef } }, |
177 | { join => 'cd_to_producer' }, |
178 | ), |
179 | 1, |
180 | 'Kirk had a producer only on one cd', |
181 | ); |
182 | |
183 | my $lars_cds = $cd_rs->search ({ 'artist.name' => 'lars' }, { join => 'artist' }); |
184 | is ($lars_cds->count, 3, 'Lars had 3 CDs'); |
185 | is ( |
186 | $lars_cds->search ( |
187 | { 'cd_to_producer.cd' => undef }, |
188 | { join => 'cd_to_producer' }, |
189 | ), |
190 | 0, |
191 | 'Lars always had a producer', |
192 | ); |
193 | is ( |
194 | $lars_cds->search_related ('cd_to_producer', |
195 | { 'producer.name' => 'flemming'}, |
196 | { join => 'producer' } |
197 | )->count, |
198 | 1, |
199 | 'Lars produced 1 CD with flemming', |
200 | ); |
201 | is ( |
202 | $lars_cds->search_related ('cd_to_producer', |
203 | { 'producer.name' => 'bob'}, |
204 | { join => 'producer' } |
205 | )->count, |
186c7bef |
206 | 3, |
207 | 'Lars produced 3 CDs with bob', |
b6678114 |
208 | ); |
209 | |
210 | my $bob_prod = $cd_rs->search ( |
211 | { 'producer.name' => 'bob'}, |
212 | { join => { cd_to_producer => 'producer' } } |
213 | ); |
186c7bef |
214 | is ($bob_prod->count, 4, 'Bob produced a total of 4 CDs'); |
215 | ok ($bob_prod->find ({ title => 'Greatest hits 1'}), '1st Bob production name correct'); |
216 | ok ($bob_prod->find ({ title => 'Greatest hits 6'}), '2nd Bob production name correct'); |
217 | ok ($bob_prod->find ({ title => 'Greatest hits 2'}), '3rd Bob production name correct'); |
218 | ok ($bob_prod->find ({ title => 'Greatest hits 7'}), '4th Bob production name correct'); |
b6678114 |
219 | |
220 | is ( |
221 | $bob_prod->search ({ 'artist.name' => 'james' }, { join => 'artist' })->count, |
222 | 1, |
223 | "Bob produced james' only CD", |
224 | ); |
225 | }; |
226 | diag $@ if $@; |
227 | |
228 | 1; |