9 use Path::Class::File ();
11 use List::Util qw/shuffle/;
12 use Storable qw/nfreeze dclone/;
14 my $schema = DBICTest->init_schema();
16 # The map below generates stuff like:
17 # [ qw/artistid name/ ],
24 my $start_id = 'populateXaaaaaa';
28 $schema->populate('Artist', [ [ qw/artistid name/ ], map { [ ($_ + $offset) => $start_id++ ] } shuffle ( 1 .. $rows ) ] );
30 $schema->resultset ('Artist')->search ({ name => { -like => 'populateX%' } })->count,
32 'populate created correct number of rows with massive AoA bulk insert',
35 my $artist = $schema->resultset ('Artist')
36 ->search ({ 'cds.title' => { '!=', undef } }, { join => 'cds' })
38 my $ex_title = $artist->cds->first->title;
42 $schema->populate('CD', [
45 artist => $artist->id,
49 } ('Huey', 'Dewey', $ex_title, 'Louie')
51 }, qr/\Qexecute_for_fetch() aborted with '\E.+ at populate slice.+$ex_title/ms, 'Readable exception thrown for failed populate');
53 ## make sure populate honors fields/orders in list context
55 my @links = $schema->populate('Link', [
61 my $link2 = shift @links;
62 is($link2->id, 2, 'Link 2 id');
63 is($link2->url, 'burl', 'Link 2 url');
64 is($link2->title, 'btitle', 'Link 2 title');
67 @links = $schema->populate('Link', [
73 my $link3 = shift @links;
74 is($link3->id, 3, 'Link 3 id');
75 is($link3->url, 'curl', 'Link 3 url');
76 is($link3->title, 'ctitle', 'Link 3 title');
78 ## not all physical columns
79 @links = $schema->populate('Link', [
85 my $link4 = shift @links;
86 is($link4->id, 4, 'Link 4 id');
87 is($link4->url, undef, 'Link 4 url');
88 is($link4->title, 'dtitle', 'Link 4 title');
91 ## make sure populate -> insert_bulk honors fields/orders in void context
93 $schema->populate('Link', [
97 my $link5 = $schema->resultset('Link')->find(5);
98 is($link5->id, 5, 'Link 5 id');
99 is($link5->url, 'eurl', 'Link 5 url');
100 is($link5->title, 'etitle', 'Link 5 title');
103 $schema->populate('Link', [
104 [ qw/id title url/ ],
105 [ qw/6 ftitle furl/ ]
107 my $link6 = $schema->resultset('Link')->find(6);
108 is($link6->id, 6, 'Link 6 id');
109 is($link6->url, 'furl', 'Link 6 url');
110 is($link6->title, 'ftitle', 'Link 6 title');
112 ## not all physical columns
113 $schema->populate('Link', [
117 my $link7 = $schema->resultset('Link')->find(7);
118 is($link7->id, 7, 'Link 7 id');
119 is($link7->url, undef, 'Link 7 url');
120 is($link7->title, 'gtitle', 'Link 7 title');
122 # populate with literals
124 my $rs = $schema->resultset('Link');
127 # test insert_bulk with all literal sql (no binds)
131 url => \"'cpan.org'",
132 title => \"'The ''best of'' cpan'",
137 $_->url eq 'cpan.org' &&
138 $_->title eq "The 'best of' cpan",
139 } $rs->all), 5, 'populate with all literal SQL');
143 # test mixed binds with literal sql
147 url => \"'cpan.org'",
148 title => "The 'best of' cpan",
153 $_->url eq 'cpan.org' &&
154 $_->title eq "The 'best of' cpan",
155 } $rs->all), 5, 'populate with all literal SQL');
160 # populate with literal+bind
162 my $rs = $schema->resultset('Link');
165 # test insert_bulk with all literal/bind sql
168 url => \['?', [ {} => 'cpan.org' ] ],
169 title => \['?', [ {} => "The 'best of' cpan" ] ],
174 $_->url eq 'cpan.org' &&
175 $_->title eq "The 'best of' cpan",
176 } $rs->all), 5, 'populate with all literal/bind');
180 # test insert_bulk with mix literal and literal/bind
183 url => \"'cpan.org'",
184 title => \['?', [ {} => "The 'best of' cpan" ] ],
189 $_->url eq 'cpan.org' &&
190 $_->title eq "The 'best of' cpan",
191 } $rs->all), 5, 'populate with all literal/bind SQL');
195 # test mixed binds with literal sql/bind
197 $rs->populate([ map { +{
198 url => \[ '? || ?', [ {} => 'cpan.org_' ], [ undef, $_ ] ],
199 title => "The 'best of' cpan",
203 ok($rs->find({ url => "cpan.org_$_" }), "Row $_ correctly created with dynamic literal/bind populate" );
209 my $rs = $schema->resultset('Artist');
218 artistid => 'foo', # this dies
226 } qr/\Qexecute_for_fetch() aborted with 'datatype mismatch\E\b/, 'bad slice';
228 is($rs->count, 0, 'populate is atomic');
230 # Trying to use a column marked as a bind in the first slice with literal sql in
231 # a later slice should throw.
244 } qr/Literal SQL found where a plain bind value is expected/, 'literal sql where bind expected throws';
246 # ... and vice-versa.
259 } qr/\QIncorrect value (expecting SCALAR-ref/, 'bind where literal sql expected throws';
272 } qr/Inconsistent literal SQL value/, 'literal sql must be the same in all slices';
278 name => \['?', [ {} => 'foo' ] ],
285 } qr/\QIncorrect value (expecting ARRAYREF-ref/, 'literal where literal+bind expected throws';
291 name => \['?', [ { sqlt_datatype => 'foooo' } => 'foo' ] ],
295 name => \['?', [ {} => 'foo' ] ],
298 } qr/\QDiffering bind attributes on literal\/bind values not supported for column 'name'/, 'literal+bind with differing attrs throws';
304 name => \['?', [ undef, 'foo' ] ],
308 name => \['?', [ {} => 'bar' ] ],
311 } 'literal+bind with semantically identical attrs works after normalization';
313 # test all kinds of population with stringified objects
315 local $ENV{DBIC_RT79576_NOWARN};
317 my $rs = $schema->resultset('Artist')->search({}, { columns => [qw(name rank)], order_by => 'artistid' });
319 # the stringification has nothing to do with the artist name
320 # this is solely for testing consistency
321 my $fn = Path::Class::File->new ('somedir/somefilename.tmp');
322 my $fn2 = Path::Class::File->new ('somedir/someotherfilename.tmp');
323 my $rank = Math::BigInt->new(42);
326 'stringifying objects after regular values' => [ map
327 { { name => $_, rank => $rank } }
329 'supplied before stringifying objects',
330 'supplied before stringifying objects 2',
335 'stringifying objects before regular values' => [ map
336 { { name => $_, rank => $rank } }
340 'supplied after stringifying objects',
341 'supplied after stringifying objects 2',
344 'stringifying objects between regular values' => [ map
345 { { name => $_, rank => $rank } }
347 'supplied before stringifying objects',
350 'supplied after stringifying objects',
353 'stringifying objects around regular values' => [ map
354 { { name => $_, rank => $rank } }
357 'supplied between stringifying objects',
363 local $Storable::canonical = 1;
364 my $preimage = nfreeze([$fn, $fn2, $rank, $args]);
366 for my $tst (keys %$args) {
370 $rs->populate($args->{$tst});
374 "Populate() $tst in void context"
379 my $dummy = $rs->populate($args->{$tst});
383 "Populate() $tst in non-void context"
386 # test create() as we have everything set up already
388 $rs->create($_) for @{$args->{$tst}};
398 ($preimage eq nfreeze( [$fn, $fn2, $rank, $args] )),
399 'Arguments fed to populate()/create() unchanged'
404 # warning to be removed around Apr 1st 2015
405 # smokers start failing a month before that
407 ( DBICTest::RunMode->is_author and ( time() > 1427846400 ) )
409 ( DBICTest::RunMode->is_smoker and ( time() > 1425168000 ) )
412 # one unique for populate() and create() each
413 : (qr/\QPOSSIBLE *PAST* DATA CORRUPTION detected \E.+\QTrigger condition encountered at @{[ __FILE__ ]} line\E \d/) x 2
414 ], 'Data integrity warnings as planned';
417 $schema->resultset('TwoKeys')->populate([{
420 fourkeys_to_twokeys => [{
434 } 'multicol-PK has_many populate works';
437 $schema->populate('CD', [
438 {cdid => 10001, artist => $artist->id, title => 'Pretty Much Empty', year => 2011, tracks => []},
440 }, 'empty has_many relationship accepted by populate');