continued efforts, squash
[dbsrgits/DBIx-Class.git] / t / 100populate.t
CommitLineData
54e0bd06 1use strict;
d35a6fed 2use warnings;
54e0bd06 3
4use Test::More;
d35a6fed 5use Test::Exception;
75a1d824 6use Test::Warn;
54e0bd06 7use lib qw(t/lib);
8use DBICTest;
a4c52abc 9use DBIx::Class::_Util 'sigwarn_silencer';
c0d8cb1f 10use Path::Class::File ();
75a1d824 11use Math::BigInt;
569b9fe6 12use List::Util qw/shuffle/;
75a1d824 13use Storable qw/nfreeze dclone/;
54e0bd06 14
54e0bd06 15my $schema = DBICTest->init_schema();
54e0bd06 16
d35a6fed 17# The map below generates stuff like:
18# [ qw/artistid name/ ],
19# [ 4, "b" ],
20# [ 5, "c" ],
21# ...
22# [ 9999, "ntm" ],
23# [ 10000, "ntn" ],
24
25my $start_id = 'populateXaaaaaa';
d58f0098 26my $rows = 10;
d35a6fed 27my $offset = 3;
28
569b9fe6 29$schema->populate('Artist', [ [ qw/artistid name/ ], map { [ ($_ + $offset) => $start_id++ ] } shuffle ( 1 .. $rows ) ] );
d35a6fed 30is (
31 $schema->resultset ('Artist')->search ({ name => { -like => 'populateX%' } })->count,
32 $rows,
33 'populate created correct number of rows with massive AoA bulk insert',
34);
35
36my $artist = $schema->resultset ('Artist')
37 ->search ({ 'cds.title' => { '!=', undef } }, { join => 'cds' })
38 ->first;
39my $ex_title = $artist->cds->first->title;
40
41throws_ok ( sub {
42 my $i = 600;
43 $schema->populate('CD', [
44 map {
45 {
d35a6fed 46 artist => $artist->id,
47 title => $_,
48 year => 2009,
49 }
50 } ('Huey', 'Dewey', $ex_title, 'Louie')
51 ])
52cef7e3 52}, qr/\Qexecute_for_fetch() aborted with '\E.+ at populate slice.+$ex_title/ms, 'Readable exception thrown for failed populate');
b0457415 53
89b2e3e4 54## make sure populate honors fields/orders in list context
b0457415 55## schema order
89b2e3e4 56my @links = $schema->populate('Link', [
b0457415 57[ qw/id url title/ ],
58[ qw/2 burl btitle/ ]
59]);
89b2e3e4 60is(scalar @links, 1);
61
62my $link2 = shift @links;
b0457415 63is($link2->id, 2, 'Link 2 id');
64is($link2->url, 'burl', 'Link 2 url');
65is($link2->title, 'btitle', 'Link 2 title');
66
67## non-schema order
89b2e3e4 68@links = $schema->populate('Link', [
b0457415 69[ qw/id title url/ ],
70[ qw/3 ctitle curl/ ]
71]);
89b2e3e4 72is(scalar @links, 1);
73
74my $link3 = shift @links;
b0457415 75is($link3->id, 3, 'Link 3 id');
76is($link3->url, 'curl', 'Link 3 url');
77is($link3->title, 'ctitle', 'Link 3 title');
78
79## not all physical columns
89b2e3e4 80@links = $schema->populate('Link', [
b0457415 81[ qw/id title/ ],
82[ qw/4 dtitle/ ]
83]);
89b2e3e4 84is(scalar @links, 1);
85
86my $link4 = shift @links;
b0457415 87is($link4->id, 4, 'Link 4 id');
88is($link4->url, undef, 'Link 4 url');
89is($link4->title, 'dtitle', 'Link 4 title');
90
91
89b2e3e4 92## make sure populate -> insert_bulk honors fields/orders in void context
93## schema order
94$schema->populate('Link', [
95[ qw/id url title/ ],
96[ qw/5 eurl etitle/ ]
97]);
98my $link5 = $schema->resultset('Link')->find(5);
99is($link5->id, 5, 'Link 5 id');
100is($link5->url, 'eurl', 'Link 5 url');
101is($link5->title, 'etitle', 'Link 5 title');
102
103## non-schema order
104$schema->populate('Link', [
105[ qw/id title url/ ],
106[ qw/6 ftitle furl/ ]
107]);
108my $link6 = $schema->resultset('Link')->find(6);
109is($link6->id, 6, 'Link 6 id');
110is($link6->url, 'furl', 'Link 6 url');
111is($link6->title, 'ftitle', 'Link 6 title');
112
113## not all physical columns
114$schema->populate('Link', [
115[ qw/id title/ ],
116[ qw/7 gtitle/ ]
117]);
118my $link7 = $schema->resultset('Link')->find(7);
119is($link7->id, 7, 'Link 7 id');
120is($link7->url, undef, 'Link 7 url');
121is($link7->title, 'gtitle', 'Link 7 title');
122
84f7e8a1 123# populate with literals
124{
125 my $rs = $schema->resultset('Link');
126 $rs->delete;
aac0bfd0 127
52cef7e3 128 # test insert_bulk with all literal sql (no binds)
aac0bfd0 129
84f7e8a1 130 $rs->populate([
574d7df6 131 (+{
84f7e8a1 132 url => \"'cpan.org'",
133 title => \"'The ''best of'' cpan'",
574d7df6 134 }) x 5
84f7e8a1 135 ]);
574d7df6 136
84f7e8a1 137 is((grep {
138 $_->url eq 'cpan.org' &&
139 $_->title eq "The 'best of' cpan",
140 } $rs->all), 5, 'populate with all literal SQL');
bbd6f348 141
84f7e8a1 142 $rs->delete;
bbd6f348 143
84f7e8a1 144 # test mixed binds with literal sql
aac0bfd0 145
84f7e8a1 146 $rs->populate([
aac0bfd0 147 (+{
84f7e8a1 148 url => \"'cpan.org'",
149 title => "The 'best of' cpan",
aac0bfd0 150 }) x 5
84f7e8a1 151 ]);
aac0bfd0 152
84f7e8a1 153 is((grep {
154 $_->url eq 'cpan.org' &&
155 $_->title eq "The 'best of' cpan",
156 } $rs->all), 5, 'populate with all literal SQL');
aac0bfd0 157
84f7e8a1 158 $rs->delete;
159}
aac0bfd0 160
a9bac98f 161# populate with literal+bind
162{
163 my $rs = $schema->resultset('Link');
164 $rs->delete;
165
166 # test insert_bulk with all literal/bind sql
167 $rs->populate([
168 (+{
169 url => \['?', [ {} => 'cpan.org' ] ],
170 title => \['?', [ {} => "The 'best of' cpan" ] ],
171 }) x 5
172 ]);
173
174 is((grep {
175 $_->url eq 'cpan.org' &&
176 $_->title eq "The 'best of' cpan",
177 } $rs->all), 5, 'populate with all literal/bind');
178
179 $rs->delete;
180
181 # test insert_bulk with mix literal and literal/bind
182 $rs->populate([
183 (+{
184 url => \"'cpan.org'",
185 title => \['?', [ {} => "The 'best of' cpan" ] ],
186 }) x 5
187 ]);
188
189 is((grep {
190 $_->url eq 'cpan.org' &&
191 $_->title eq "The 'best of' cpan",
192 } $rs->all), 5, 'populate with all literal/bind SQL');
193
194 $rs->delete;
195
196 # test mixed binds with literal sql/bind
197
198 $rs->populate([ map { +{
199 url => \[ '? || ?', [ {} => 'cpan.org_' ], [ undef, $_ ] ],
200 title => "The 'best of' cpan",
201 } } (1 .. 5) ]);
202
203 for (1 .. 5) {
204 ok($rs->find({ url => "cpan.org_$_" }), "Row $_ correctly created with dynamic literal/bind populate" );
205 }
206
207 $rs->delete;
208}
209
84f7e8a1 210my $rs = $schema->resultset('Artist');
211$rs->delete;
bbd6f348 212throws_ok {
a4c52abc 213 # this warning is correct, but we are not testing it here
214 # what we are after is the correct exception when an int
215 # fails to coerce into a sqlite rownum
216 local $SIG{__WARN__} = sigwarn_silencer( qr/datatype mismatch.+ foo as integer/ );
217
bbd6f348 218 $rs->populate([
219 {
220 artistid => 1,
221 name => 'foo1',
222 },
223 {
224 artistid => 'foo', # this dies
225 name => 'foo2',
226 },
227 {
228 artistid => 3,
229 name => 'foo3',
230 },
231 ]);
a4c52abc 232} qr/\Qexecute_for_fetch() aborted with 'datatype mismatch\E\b/, 'bad slice fails PK insert';
bbd6f348 233
234is($rs->count, 0, 'populate is atomic');
235
1295943f 236# Trying to use a column marked as a bind in the first slice with literal sql in
237# a later slice should throw.
238
239throws_ok {
240 $rs->populate([
241 {
242 artistid => 1,
243 name => \"'foo'",
244 },
245 {
246 artistid => \2,
247 name => \"'foo'",
248 }
249 ]);
f6faeab8 250} qr/Literal SQL found where a plain bind value is expected/, 'literal sql where bind expected throws';
1295943f 251
252# ... and vice-versa.
253
254throws_ok {
255 $rs->populate([
256 {
257 artistid => \1,
258 name => \"'foo'",
259 },
260 {
261 artistid => 2,
262 name => \"'foo'",
263 }
264 ]);
f6faeab8 265} qr/\QIncorrect value (expecting SCALAR-ref/, 'bind where literal sql expected throws';
390722b4 266die;
1295943f 267
268throws_ok {
269 $rs->populate([
270 {
271 artistid => 1,
272 name => \"'foo'",
273 },
274 {
275 artistid => 2,
276 name => \"'bar'",
277 }
278 ]);
f6faeab8 279} qr/Inconsistent literal SQL value/, 'literal sql must be the same in all slices';
1295943f 280
a9bac98f 281throws_ok {
282 $rs->populate([
283 {
284 artistid => 1,
285 name => \['?', [ {} => 'foo' ] ],
286 },
287 {
288 artistid => 2,
289 name => \"'bar'",
290 }
291 ]);
292} qr/\QIncorrect value (expecting ARRAYREF-ref/, 'literal where literal+bind expected throws';
293
294throws_ok {
295 $rs->populate([
296 {
297 artistid => 1,
298 name => \['?', [ { sqlt_datatype => 'foooo' } => 'foo' ] ],
299 },
300 {
301 artistid => 2,
302 name => \['?', [ {} => 'foo' ] ],
303 }
304 ]);
305} qr/\QDiffering bind attributes on literal\/bind values not supported for column 'name'/, 'literal+bind with differing attrs throws';
306
307lives_ok {
308 $rs->populate([
309 {
310 artistid => 1,
311 name => \['?', [ undef, 'foo' ] ],
312 },
313 {
314 artistid => 2,
315 name => \['?', [ {} => 'bar' ] ],
316 }
317 ]);
318} 'literal+bind with semantically identical attrs works after normalization';
319
75a1d824 320# test all kinds of population with stringified objects
321warnings_like {
eed5492f 322 local $ENV{DBIC_RT79576_NOWARN};
323
75a1d824 324 my $rs = $schema->resultset('Artist')->search({}, { columns => [qw(name rank)], order_by => 'artistid' });
325
326 # the stringification has nothing to do with the artist name
327 # this is solely for testing consistency
328 my $fn = Path::Class::File->new ('somedir/somefilename.tmp');
329 my $fn2 = Path::Class::File->new ('somedir/someotherfilename.tmp');
330 my $rank = Math::BigInt->new(42);
331
332 my $args = {
333 'stringifying objects after regular values' => [ map
334 { { name => $_, rank => $rank } }
335 (
336 'supplied before stringifying objects',
337 'supplied before stringifying objects 2',
338 $fn,
339 $fn2,
340 )
341 ],
342 'stringifying objects before regular values' => [ map
343 { { name => $_, rank => $rank } }
344 (
345 $fn,
346 $fn2,
347 'supplied after stringifying objects',
348 'supplied after stringifying objects 2',
349 )
350 ],
351 'stringifying objects between regular values' => [ map
352 { { name => $_, rank => $rank } }
353 (
354 'supplied before stringifying objects',
355 $fn,
356 $fn2,
357 'supplied after stringifying objects',
358 )
359 ],
360 'stringifying objects around regular values' => [ map
361 { { name => $_, rank => $rank } }
362 (
363 $fn,
364 'supplied between stringifying objects',
365 $fn2,
366 )
367 ],
368 };
369
370 local $Storable::canonical = 1;
371 my $preimage = nfreeze([$fn, $fn2, $rank, $args]);
372
373 for my $tst (keys %$args) {
374
375 # test void ctx
376 $rs->delete;
377 $rs->populate($args->{$tst});
378 is_deeply(
379 $rs->all_hri,
380 $args->{$tst},
381 "Populate() $tst in void context"
382 );
383
384 # test non-void ctx
385 $rs->delete;
386 my $dummy = $rs->populate($args->{$tst});
387 is_deeply(
388 $rs->all_hri,
389 $args->{$tst},
390 "Populate() $tst in non-void context"
391 );
392
393 # test create() as we have everything set up already
394 $rs->delete;
395 $rs->create($_) for @{$args->{$tst}};
396
397 is_deeply(
398 $rs->all_hri,
399 $args->{$tst},
400 "Create() $tst"
401 );
402 }
8464d1a4 403
75a1d824 404 ok (
405 ($preimage eq nfreeze( [$fn, $fn2, $rank, $args] )),
406 'Arguments fed to populate()/create() unchanged'
407 );
8464d1a4 408
75a1d824 409 $rs->delete;
410} [
411 # warning to be removed around Apr 1st 2015
412 # smokers start failing a month before that
413 (
414 ( DBICTest::RunMode->is_author and ( time() > 1427846400 ) )
415 or
416 ( DBICTest::RunMode->is_smoker and ( time() > 1425168000 ) )
417 )
418 ? ()
419 # one unique for populate() and create() each
420 : (qr/\QPOSSIBLE *PAST* DATA CORRUPTION detected \E.+\QTrigger condition encountered at @{[ __FILE__ ]} line\E \d/) x 2
421], 'Data integrity warnings as planned';
8464d1a4 422
18d80024 423lives_ok {
424 $schema->resultset('TwoKeys')->populate([{
425 artist => 1,
426 cd => 5,
427 fourkeys_to_twokeys => [{
428 f_foo => 1,
429 f_bar => 1,
430 f_hello => 1,
431 f_goodbye => 1,
432 autopilot => 'a',
433 },{
434 f_foo => 2,
435 f_bar => 2,
436 f_hello => 2,
437 f_goodbye => 2,
438 autopilot => 'b',
439 }]
440 }])
d6eda469 441} 'multicol-PK has_many populate works';
18d80024 442
d6170b26 443lives_ok ( sub {
444 $schema->populate('CD', [
445 {cdid => 10001, artist => $artist->id, title => 'Pretty Much Empty', year => 2011, tracks => []},
446 ])
447}, 'empty has_many relationship accepted by populate');
448
bbd6f348 449done_testing;
3b39ea8c 450
451use DDP; use Data::Dumper;
452my $q = $schema->resultset('Artist')
453 ->search({
750ff9db 454 },
455 {
456 columns => [qw/name rank/]
3b39ea8c 457 })->as_query;
458 use DDP; p $q;
459#p $q;
460#diag Dumper($q);
461#p $schema->resultset('Artist')->result_source;
462#p Dumper $q;
750ff9db 463$schema->storage->insert_bulk($schema->resultset('Artist')->result_source, [qw/name rank/], $q);