use lib qw(t/lib);
use DBICTest;
use Path::Class::File ();
+use List::Util qw/shuffle/;
my $schema = DBICTest->init_schema();
# [ 10000, "ntn" ],
my $start_id = 'populateXaaaaaa';
-my $rows = 10;
+my $rows = 10_000;
my $offset = 3;
-$schema->populate('Artist', [ [ qw/artistid name/ ], map { [ ($_ + $offset) => $start_id++ ] } ( 1 .. $rows ) ] );
+$schema->populate('Artist', [ [ qw/artistid name/ ], map { [ ($_ + $offset) => $start_id++ ] } shuffle ( 1 .. $rows ) ] );
is (
$schema->resultset ('Artist')->search ({ name => { -like => 'populateX%' } })->count,
$rows,
}
} ('Huey', 'Dewey', $ex_title, 'Louie')
])
-}, qr/columns .+ are not unique for populate slice.+$ex_title/ms, 'Readable exception thrown for failed populate');
+}, qr/\Qexecute_array() aborted with 'constraint failed\E.+ at populate slice.+$ex_title/ms, 'Readable exception thrown for failed populate');
## make sure populate honors fields/orders in list context
## schema order
is($link7->url, undef, 'Link 7 url');
is($link7->title, 'gtitle', 'Link 7 title');
-my $rs = $schema->resultset('Artist');
-$rs->delete;
+# populate with literals
+{
+ my $rs = $schema->resultset('Link');
+ $rs->delete;
-# test _execute_array_empty (insert_bulk with all literal sql)
+ # test _execute_array_empty (insert_bulk with all literal sql)
-$rs->populate([
+ $rs->populate([
(+{
- name => \"'DT'",
- rank => \500,
- charfield => \"'mtfnpy'",
+ url => \"'cpan.org'",
+ title => \"'The ''best of'' cpan'",
}) x 5
-]);
+ ]);
-is((grep {
- $_->name eq 'DT' &&
- $_->rank == 500 &&
- $_->charfield eq 'mtfnpy'
-} $rs->all), 5, 'populate with all literal SQL');
+ is((grep {
+ $_->url eq 'cpan.org' &&
+ $_->title eq "The 'best of' cpan",
+ } $rs->all), 5, 'populate with all literal SQL');
-$rs->delete;
+ $rs->delete;
-# test mixed binds with literal sql
+ # test mixed binds with literal sql
-$rs->populate([
+ $rs->populate([
(+{
- name => \"'DT'",
- rank => 500,
- charfield => \"'mtfnpy'",
+ url => \"'cpan.org'",
+ title => "The 'best of' cpan",
}) x 5
-]);
-
-is((grep {
- $_->name eq 'DT' &&
- $_->rank == 500 &&
- $_->charfield eq 'mtfnpy'
-} $rs->all), 5, 'populate with all literal SQL');
+ ]);
-$rs->delete;
+ is((grep {
+ $_->url eq 'cpan.org' &&
+ $_->title eq "The 'best of' cpan",
+ } $rs->all), 5, 'populate with all literal SQL');
-###
+ $rs->delete;
+}
+my $rs = $schema->resultset('Artist');
+$rs->delete;
throws_ok {
$rs->populate([
{
name => 'foo3',
},
]);
-} qr/slice/, 'bad slice';
+} qr/\Qexecute_array() aborted with 'datatype mismatch\E\b/, 'bad slice';
is($rs->count, 0, 'populate is atomic');
name => \"'foo'",
}
]);
-} qr/bind expected/, 'literal sql where bind expected throws';
+} qr/Literal SQL found where a plain bind value is expected/, 'literal sql where bind expected throws';
# ... and vice-versa.
name => \"'foo'",
}
]);
-} qr/literal SQL expected/i, 'bind where literal sql expected throws';
+} qr/\QIncorrect value (expecting SCALAR-ref/, 'bind where literal sql expected throws';
throws_ok {
$rs->populate([
name => \"'bar'",
}
]);
-} qr/inconsistent/, 'literal sql must be the same in all slices';
+} qr/Inconsistent literal SQL value/, 'literal sql must be the same in all slices';
# the stringification has nothing to do with the artist name
# this is solely for testing consistency
ok ($row, "Stringification test row '$_' properly inserted");
}
+$rs->delete;
+
+# test stringification with ->create rather than Storage::insert_bulk as well
+
+lives_ok {
+ my @dummy = $rs->populate([
+ {
+ name => 'supplied before stringifying object',
+ },
+ {
+ name => $fn,
+ }
+ ]);
+} 'stringifying objects pass through';
+
+# ... and vice-versa.
+
+lives_ok {
+ my @dummy = $rs->populate([
+ {
+ name => $fn2,
+ },
+ {
+ name => 'supplied after stringifying object',
+ },
+ ]);
+} 'stringifying objects pass through';
+
+for (
+ $fn,
+ $fn2,
+ 'supplied after stringifying object',
+ 'supplied before stringifying object'
+) {
+ my $row = $rs->find ({name => $_});
+ ok ($row, "Stringification test row '$_' properly inserted");
+}
+
+lives_ok {
+ $schema->resultset('TwoKeys')->populate([{
+ artist => 1,
+ cd => 5,
+ fourkeys_to_twokeys => [{
+ f_foo => 1,
+ f_bar => 1,
+ f_hello => 1,
+ f_goodbye => 1,
+ autopilot => 'a',
+ },{
+ f_foo => 2,
+ f_bar => 2,
+ f_hello => 2,
+ f_goodbye => 2,
+ autopilot => 'b',
+ }]
+ }])
+} 'multicol-PK has_many populate works';
+
+lives_ok ( sub {
+ $schema->populate('CD', [
+ {cdid => 10001, artist => $artist->id, title => 'Pretty Much Empty', year => 2011, tracks => []},
+ ])
+}, 'empty has_many relationship accepted by populate');
+
done_testing;