9 use DBIx::Class::_Util 'sigwarn_silencer';
10 use Path::Class::File ();
12 use List::Util qw/shuffle/;
13 use Storable qw/nfreeze dclone/;
15 my $schema = DBICTest->init_schema();
17 # The map below generates stuff like:
18 # [ qw/artistid name/ ],
25 my $start_id = 'populateXaaaaaa';
29 $schema->populate('Artist', [ [ qw/artistid name/ ], map { [ ($_ + $offset) => $start_id++ ] } shuffle ( 1 .. $rows ) ] );
31 $schema->resultset ('Artist')->search ({ name => { -like => 'populateX%' } })->count,
33 'populate created correct number of rows with massive AoA bulk insert',
36 my $artist = $schema->resultset ('Artist')
37 ->search ({ 'cds.title' => { '!=', undef } }, { join => 'cds' })
39 my $ex_title = $artist->cds->first->title;
43 $schema->populate('CD', [
46 artist => $artist->id,
50 } ('Huey', 'Dewey', $ex_title, 'Louie')
52 }, qr/\Qexecute_for_fetch() aborted with '\E.+ at populate slice.+$ex_title/ms, 'Readable exception thrown for failed populate');
54 ## make sure populate honors fields/orders in list context
56 my @links = $schema->populate('Link', [
62 my $link2 = shift @links;
63 is($link2->id, 2, 'Link 2 id');
64 is($link2->url, 'burl', 'Link 2 url');
65 is($link2->title, 'btitle', 'Link 2 title');
68 @links = $schema->populate('Link', [
74 my $link3 = shift @links;
75 is($link3->id, 3, 'Link 3 id');
76 is($link3->url, 'curl', 'Link 3 url');
77 is($link3->title, 'ctitle', 'Link 3 title');
79 ## not all physical columns
80 @links = $schema->populate('Link', [
86 my $link4 = shift @links;
87 is($link4->id, 4, 'Link 4 id');
88 is($link4->url, undef, 'Link 4 url');
89 is($link4->title, 'dtitle', 'Link 4 title');
92 ## make sure populate -> insert_bulk honors fields/orders in void context
94 $schema->populate('Link', [
98 my $link5 = $schema->resultset('Link')->find(5);
99 is($link5->id, 5, 'Link 5 id');
100 is($link5->url, 'eurl', 'Link 5 url');
101 is($link5->title, 'etitle', 'Link 5 title');
104 $schema->populate('Link', [
105 [ qw/id title url/ ],
106 [ qw/6 ftitle furl/ ]
108 my $link6 = $schema->resultset('Link')->find(6);
109 is($link6->id, 6, 'Link 6 id');
110 is($link6->url, 'furl', 'Link 6 url');
111 is($link6->title, 'ftitle', 'Link 6 title');
113 ## not all physical columns
114 $schema->populate('Link', [
118 my $link7 = $schema->resultset('Link')->find(7);
119 is($link7->id, 7, 'Link 7 id');
120 is($link7->url, undef, 'Link 7 url');
121 is($link7->title, 'gtitle', 'Link 7 title');
123 # populate with literals
125 my $rs = $schema->resultset('Link');
128 # test insert_bulk with all literal sql (no binds)
132 url => \"'cpan.org'",
133 title => \"'The ''best of'' cpan'",
138 $_->url eq 'cpan.org' &&
139 $_->title eq "The 'best of' cpan",
140 } $rs->all), 5, 'populate with all literal SQL');
144 # test mixed binds with literal sql
148 url => \"'cpan.org'",
149 title => "The 'best of' cpan",
154 $_->url eq 'cpan.org' &&
155 $_->title eq "The 'best of' cpan",
156 } $rs->all), 5, 'populate with all literal SQL');
161 # populate with literal+bind
163 my $rs = $schema->resultset('Link');
166 # test insert_bulk with all literal/bind sql
169 url => \['?', [ {} => 'cpan.org' ] ],
170 title => \['?', [ {} => "The 'best of' cpan" ] ],
175 $_->url eq 'cpan.org' &&
176 $_->title eq "The 'best of' cpan",
177 } $rs->all), 5, 'populate with all literal/bind');
181 # test insert_bulk with mix literal and literal/bind
184 url => \"'cpan.org'",
185 title => \['?', [ {} => "The 'best of' cpan" ] ],
190 $_->url eq 'cpan.org' &&
191 $_->title eq "The 'best of' cpan",
192 } $rs->all), 5, 'populate with all literal/bind SQL');
196 # test mixed binds with literal sql/bind
198 $rs->populate([ map { +{
199 url => \[ '? || ?', [ {} => 'cpan.org_' ], [ undef, $_ ] ],
200 title => "The 'best of' cpan",
204 ok($rs->find({ url => "cpan.org_$_" }), "Row $_ correctly created with dynamic literal/bind populate" );
210 my $rs = $schema->resultset('Artist');
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/ );
224 artistid => 'foo', # this dies
232 } qr/\Qexecute_for_fetch() aborted with 'datatype mismatch\E\b/, 'bad slice fails PK insert';
234 is($rs->count, 0, 'populate is atomic');
236 # Trying to use a column marked as a bind in the first slice with literal sql in
237 # a later slice should throw.
250 } qr/Literal SQL found where a plain bind value is expected/, 'literal sql where bind expected throws';
252 # ... and vice-versa.
265 } qr/\QIncorrect value (expecting SCALAR-ref/, 'bind where literal sql expected throws';
279 } qr/Inconsistent literal SQL value/, 'literal sql must be the same in all slices';
285 name => \['?', [ {} => 'foo' ] ],
292 } qr/\QIncorrect value (expecting ARRAYREF-ref/, 'literal where literal+bind expected throws';
298 name => \['?', [ { sqlt_datatype => 'foooo' } => 'foo' ] ],
302 name => \['?', [ {} => 'foo' ] ],
305 } qr/\QDiffering bind attributes on literal\/bind values not supported for column 'name'/, 'literal+bind with differing attrs throws';
311 name => \['?', [ undef, 'foo' ] ],
315 name => \['?', [ {} => 'bar' ] ],
318 } 'literal+bind with semantically identical attrs works after normalization';
320 # test all kinds of population with stringified objects
322 local $ENV{DBIC_RT79576_NOWARN};
324 my $rs = $schema->resultset('Artist')->search({}, { columns => [qw(name rank)], order_by => 'artistid' });
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);
333 'stringifying objects after regular values' => [ map
334 { { name => $_, rank => $rank } }
336 'supplied before stringifying objects',
337 'supplied before stringifying objects 2',
342 'stringifying objects before regular values' => [ map
343 { { name => $_, rank => $rank } }
347 'supplied after stringifying objects',
348 'supplied after stringifying objects 2',
351 'stringifying objects between regular values' => [ map
352 { { name => $_, rank => $rank } }
354 'supplied before stringifying objects',
357 'supplied after stringifying objects',
360 'stringifying objects around regular values' => [ map
361 { { name => $_, rank => $rank } }
364 'supplied between stringifying objects',
370 local $Storable::canonical = 1;
371 my $preimage = nfreeze([$fn, $fn2, $rank, $args]);
373 for my $tst (keys %$args) {
377 $rs->populate($args->{$tst});
381 "Populate() $tst in void context"
386 my $dummy = $rs->populate($args->{$tst});
390 "Populate() $tst in non-void context"
393 # test create() as we have everything set up already
395 $rs->create($_) for @{$args->{$tst}};
405 ($preimage eq nfreeze( [$fn, $fn2, $rank, $args] )),
406 'Arguments fed to populate()/create() unchanged'
411 # warning to be removed around Apr 1st 2015
412 # smokers start failing a month before that
414 ( DBICTest::RunMode->is_author and ( time() > 1427846400 ) )
416 ( DBICTest::RunMode->is_smoker and ( time() > 1425168000 ) )
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';
424 $schema->resultset('TwoKeys')->populate([{
427 fourkeys_to_twokeys => [{
441 } 'multicol-PK has_many populate works';
444 $schema->populate('CD', [
445 {cdid => 10001, artist => $artist->id, title => 'Pretty Much Empty', year => 2011, tracks => []},
447 }, 'empty has_many relationship accepted by populate');
451 use DDP; use Data::Dumper;
452 my $q = $schema->resultset('Artist')
456 columns => [qw/name rank/]
461 #p $schema->resultset('Artist')->result_source;
463 $schema->storage->insert_bulk($schema->resultset('Artist')->result_source, [qw/name rank/], $q);