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';
278 } qr/Inconsistent literal SQL value/, 'literal sql must be the same in all slices';
284 name => \['?', [ {} => 'foo' ] ],
291 } qr/\QIncorrect value (expecting ARRAYREF-ref/, 'literal where literal+bind expected throws';
297 name => \['?', [ { sqlt_datatype => 'foooo' } => 'foo' ] ],
301 name => \['?', [ {} => 'foo' ] ],
304 } qr/\QDiffering bind attributes on literal\/bind values not supported for column 'name'/, 'literal+bind with differing attrs throws';
310 name => \['?', [ undef, 'foo' ] ],
314 name => \['?', [ {} => 'bar' ] ],
317 } 'literal+bind with semantically identical attrs works after normalization';
319 # test all kinds of population with stringified objects
321 local $ENV{DBIC_RT79576_NOWARN};
323 my $rs = $schema->resultset('Artist')->search({}, { columns => [qw(name rank)], order_by => 'artistid' });
325 # the stringification has nothing to do with the artist name
326 # this is solely for testing consistency
327 my $fn = Path::Class::File->new ('somedir/somefilename.tmp');
328 my $fn2 = Path::Class::File->new ('somedir/someotherfilename.tmp');
329 my $rank = Math::BigInt->new(42);
332 'stringifying objects after regular values' => [ map
333 { { name => $_, rank => $rank } }
335 'supplied before stringifying objects',
336 'supplied before stringifying objects 2',
341 'stringifying objects before regular values' => [ map
342 { { name => $_, rank => $rank } }
346 'supplied after stringifying objects',
347 'supplied after stringifying objects 2',
350 'stringifying objects between regular values' => [ map
351 { { name => $_, rank => $rank } }
353 'supplied before stringifying objects',
356 'supplied after stringifying objects',
359 'stringifying objects around regular values' => [ map
360 { { name => $_, rank => $rank } }
363 'supplied between stringifying objects',
369 local $Storable::canonical = 1;
370 my $preimage = nfreeze([$fn, $fn2, $rank, $args]);
372 for my $tst (keys %$args) {
376 $rs->populate($args->{$tst});
380 "Populate() $tst in void context"
385 my $dummy = $rs->populate($args->{$tst});
389 "Populate() $tst in non-void context"
392 # test create() as we have everything set up already
394 $rs->create($_) for @{$args->{$tst}};
404 ($preimage eq nfreeze( [$fn, $fn2, $rank, $args] )),
405 'Arguments fed to populate()/create() unchanged'
410 # warning to be removed around Apr 1st 2015
411 # smokers start failing a month before that
413 ( DBICTest::RunMode->is_author and ( time() > 1427846400 ) )
415 ( DBICTest::RunMode->is_smoker and ( time() > 1425168000 ) )
418 # one unique for populate() and create() each
419 : (qr/\QPOSSIBLE *PAST* DATA CORRUPTION detected \E.+\QTrigger condition encountered at @{[ __FILE__ ]} line\E \d/) x 2
420 ], 'Data integrity warnings as planned';
423 $schema->resultset('TwoKeys')->populate([{
426 fourkeys_to_twokeys => [{
440 } 'multicol-PK has_many populate works';
443 $schema->populate('CD', [
444 {cdid => 10001, artist => $artist->id, title => 'Pretty Much Empty', year => 2011, tracks => []},
446 }, 'empty has_many relationship accepted by populate');