8 use Path::Class::File ();
9 use List::Util qw/shuffle/;
11 my $schema = DBICTest->init_schema();
13 # The map below generates stuff like:
14 # [ qw/artistid name/ ],
21 my $start_id = 'populateXaaaaaa';
25 $schema->populate('Artist', [ [ qw/artistid name/ ], map { [ ($_ + $offset) => $start_id++ ] } shuffle ( 1 .. $rows ) ] );
27 $schema->resultset ('Artist')->search ({ name => { -like => 'populateX%' } })->count,
29 'populate created correct number of rows with massive AoA bulk insert',
32 my $artist = $schema->resultset ('Artist')
33 ->search ({ 'cds.title' => { '!=', undef } }, { join => 'cds' })
35 my $ex_title = $artist->cds->first->title;
39 $schema->populate('CD', [
42 artist => $artist->id,
46 } ('Huey', 'Dewey', $ex_title, 'Louie')
48 }, qr/\Qexecute_for_fetch() aborted with '\E.+ at populate slice.+$ex_title/ms, 'Readable exception thrown for failed populate');
50 ## make sure populate honors fields/orders in list context
52 my @links = $schema->populate('Link', [
58 my $link2 = shift @links;
59 is($link2->id, 2, 'Link 2 id');
60 is($link2->url, 'burl', 'Link 2 url');
61 is($link2->title, 'btitle', 'Link 2 title');
64 @links = $schema->populate('Link', [
70 my $link3 = shift @links;
71 is($link3->id, 3, 'Link 3 id');
72 is($link3->url, 'curl', 'Link 3 url');
73 is($link3->title, 'ctitle', 'Link 3 title');
75 ## not all physical columns
76 @links = $schema->populate('Link', [
82 my $link4 = shift @links;
83 is($link4->id, 4, 'Link 4 id');
84 is($link4->url, undef, 'Link 4 url');
85 is($link4->title, 'dtitle', 'Link 4 title');
88 ## make sure populate -> insert_bulk honors fields/orders in void context
90 $schema->populate('Link', [
94 my $link5 = $schema->resultset('Link')->find(5);
95 is($link5->id, 5, 'Link 5 id');
96 is($link5->url, 'eurl', 'Link 5 url');
97 is($link5->title, 'etitle', 'Link 5 title');
100 $schema->populate('Link', [
101 [ qw/id title url/ ],
102 [ qw/6 ftitle furl/ ]
104 my $link6 = $schema->resultset('Link')->find(6);
105 is($link6->id, 6, 'Link 6 id');
106 is($link6->url, 'furl', 'Link 6 url');
107 is($link6->title, 'ftitle', 'Link 6 title');
109 ## not all physical columns
110 $schema->populate('Link', [
114 my $link7 = $schema->resultset('Link')->find(7);
115 is($link7->id, 7, 'Link 7 id');
116 is($link7->url, undef, 'Link 7 url');
117 is($link7->title, 'gtitle', 'Link 7 title');
119 # populate with literals
121 my $rs = $schema->resultset('Link');
124 # test insert_bulk with all literal sql (no binds)
128 url => \"'cpan.org'",
129 title => \"'The ''best of'' cpan'",
134 $_->url eq 'cpan.org' &&
135 $_->title eq "The 'best of' cpan",
136 } $rs->all), 5, 'populate with all literal SQL');
140 # test mixed binds with literal sql
144 url => \"'cpan.org'",
145 title => "The 'best of' cpan",
150 $_->url eq 'cpan.org' &&
151 $_->title eq "The 'best of' cpan",
152 } $rs->all), 5, 'populate with all literal SQL');
157 # populate with literal+bind
159 my $rs = $schema->resultset('Link');
162 # test insert_bulk with all literal/bind sql
165 url => \['?', [ {} => 'cpan.org' ] ],
166 title => \['?', [ {} => "The 'best of' cpan" ] ],
171 $_->url eq 'cpan.org' &&
172 $_->title eq "The 'best of' cpan",
173 } $rs->all), 5, 'populate with all literal/bind');
177 # test insert_bulk with mix literal and literal/bind
180 url => \"'cpan.org'",
181 title => \['?', [ {} => "The 'best of' cpan" ] ],
186 $_->url eq 'cpan.org' &&
187 $_->title eq "The 'best of' cpan",
188 } $rs->all), 5, 'populate with all literal/bind SQL');
192 # test mixed binds with literal sql/bind
194 $rs->populate([ map { +{
195 url => \[ '? || ?', [ {} => 'cpan.org_' ], [ undef, $_ ] ],
196 title => "The 'best of' cpan",
200 ok($rs->find({ url => "cpan.org_$_" }), "Row $_ correctly created with dynamic literal/bind populate" );
206 my $rs = $schema->resultset('Artist');
215 artistid => 'foo', # this dies
223 } qr/\Qexecute_for_fetch() aborted with 'datatype mismatch\E\b/, 'bad slice';
225 is($rs->count, 0, 'populate is atomic');
227 # Trying to use a column marked as a bind in the first slice with literal sql in
228 # a later slice should throw.
241 } qr/Literal SQL found where a plain bind value is expected/, 'literal sql where bind expected throws';
243 # ... and vice-versa.
256 } qr/\QIncorrect value (expecting SCALAR-ref/, 'bind where literal sql expected throws';
269 } qr/Inconsistent literal SQL value/, 'literal sql must be the same in all slices';
275 name => \['?', [ {} => 'foo' ] ],
282 } qr/\QIncorrect value (expecting ARRAYREF-ref/, 'literal where literal+bind expected throws';
288 name => \['?', [ { sqlt_datatype => 'foooo' } => 'foo' ] ],
292 name => \['?', [ {} => 'foo' ] ],
295 } qr/\QDiffering bind attributes on literal\/bind values not supported for column 'name'/, 'literal+bind with differing attrs throws';
301 name => \['?', [ undef, 'foo' ] ],
305 name => \['?', [ {} => 'bar' ] ],
308 } 'literal+bind with semantically identical attrs works after normalization';
310 # the stringification has nothing to do with the artist name
311 # this is solely for testing consistency
312 my $fn = Path::Class::File->new ('somedir/somefilename.tmp');
313 my $fn2 = Path::Class::File->new ('somedir/someotherfilename.tmp');
318 name => 'supplied before stringifying object',
324 } 'stringifying objects pass through';
326 # ... and vice-versa.
334 name => 'supplied after stringifying object',
337 } 'stringifying objects pass through';
342 'supplied after stringifying object',
343 'supplied before stringifying object'
345 my $row = $rs->find ({name => $_});
346 ok ($row, "Stringification test row '$_' properly inserted");
351 # test stringification with ->create rather than Storage::insert_bulk as well
354 my @dummy = $rs->populate([
356 name => 'supplied before stringifying object',
362 } 'stringifying objects pass through';
364 # ... and vice-versa.
367 my @dummy = $rs->populate([
372 name => 'supplied after stringifying object',
375 } 'stringifying objects pass through';
380 'supplied after stringifying object',
381 'supplied before stringifying object'
383 my $row = $rs->find ({name => $_});
384 ok ($row, "Stringification test row '$_' properly inserted");
388 $schema->resultset('TwoKeys')->populate([{
391 fourkeys_to_twokeys => [{
405 } 'multicol-PK has_many populate works';
408 $schema->populate('CD', [
409 {cdid => 10001, artist => $artist->id, title => 'Pretty Much Empty', year => 2011, tracks => []},
411 }, 'empty has_many relationship accepted by populate');