1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
use strict;
use warnings;
use Test::More;
use Test::Exception;
use lib qw(t/lib);
use DBICTest;
use Path::Class::File ();
use List::Util qw/shuffle/;
my $schema = DBICTest->init_schema();
# The map below generates stuff like:
# [ qw/artistid name/ ],
# [ 4, "b" ],
# [ 5, "c" ],
# ...
# [ 9999, "ntm" ],
# [ 10000, "ntn" ],
my $start_id = 'populateXaaaaaa';
my $rows = 10_000;
my $offset = 3;
$schema->populate('Artist', [ [ qw/artistid name/ ], map { [ ($_ + $offset) => $start_id++ ] } shuffle ( 1 .. $rows ) ] );
is (
$schema->resultset ('Artist')->search ({ name => { -like => 'populateX%' } })->count,
$rows,
'populate created correct number of rows with massive AoA bulk insert',
);
my $artist = $schema->resultset ('Artist')
->search ({ 'cds.title' => { '!=', undef } }, { join => 'cds' })
->first;
my $ex_title = $artist->cds->first->title;
throws_ok ( sub {
my $i = 600;
$schema->populate('CD', [
map {
{
artist => $artist->id,
title => $_,
year => 2009,
}
} ('Huey', 'Dewey', $ex_title, 'Louie')
])
}, qr/\Qexecute_array() aborted with '\E.+ at populate slice.+$ex_title/ms, 'Readable exception thrown for failed populate');
## make sure populate honors fields/orders in list context
## schema order
my @links = $schema->populate('Link', [
[ qw/id url title/ ],
[ qw/2 burl btitle/ ]
]);
is(scalar @links, 1);
my $link2 = shift @links;
is($link2->id, 2, 'Link 2 id');
is($link2->url, 'burl', 'Link 2 url');
is($link2->title, 'btitle', 'Link 2 title');
## non-schema order
@links = $schema->populate('Link', [
[ qw/id title url/ ],
[ qw/3 ctitle curl/ ]
]);
is(scalar @links, 1);
my $link3 = shift @links;
is($link3->id, 3, 'Link 3 id');
is($link3->url, 'curl', 'Link 3 url');
is($link3->title, 'ctitle', 'Link 3 title');
## not all physical columns
@links = $schema->populate('Link', [
[ qw/id title/ ],
[ qw/4 dtitle/ ]
]);
is(scalar @links, 1);
my $link4 = shift @links;
is($link4->id, 4, 'Link 4 id');
is($link4->url, undef, 'Link 4 url');
is($link4->title, 'dtitle', 'Link 4 title');
## make sure populate -> insert_bulk honors fields/orders in void context
## schema order
$schema->populate('Link', [
[ qw/id url title/ ],
[ qw/5 eurl etitle/ ]
]);
my $link5 = $schema->resultset('Link')->find(5);
is($link5->id, 5, 'Link 5 id');
is($link5->url, 'eurl', 'Link 5 url');
is($link5->title, 'etitle', 'Link 5 title');
## non-schema order
$schema->populate('Link', [
[ qw/id title url/ ],
[ qw/6 ftitle furl/ ]
]);
my $link6 = $schema->resultset('Link')->find(6);
is($link6->id, 6, 'Link 6 id');
is($link6->url, 'furl', 'Link 6 url');
is($link6->title, 'ftitle', 'Link 6 title');
## not all physical columns
$schema->populate('Link', [
[ qw/id title/ ],
[ qw/7 gtitle/ ]
]);
my $link7 = $schema->resultset('Link')->find(7);
is($link7->id, 7, 'Link 7 id');
is($link7->url, undef, 'Link 7 url');
is($link7->title, 'gtitle', 'Link 7 title');
# populate with literals
{
my $rs = $schema->resultset('Link');
$rs->delete;
# test _execute_array_empty (insert_bulk with all literal sql)
$rs->populate([
(+{
url => \"'cpan.org'",
title => \"'The ''best of'' cpan'",
}) x 5
]);
is((grep {
$_->url eq 'cpan.org' &&
$_->title eq "The 'best of' cpan",
} $rs->all), 5, 'populate with all literal SQL');
$rs->delete;
# test mixed binds with literal sql
$rs->populate([
(+{
url => \"'cpan.org'",
title => "The 'best of' cpan",
}) x 5
]);
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([
{
artistid => 1,
name => 'foo1',
},
{
artistid => 'foo', # this dies
name => 'foo2',
},
{
artistid => 3,
name => 'foo3',
},
]);
} qr/\Qexecute_array() aborted with 'datatype mismatch\E\b/, 'bad slice';
is($rs->count, 0, 'populate is atomic');
# Trying to use a column marked as a bind in the first slice with literal sql in
# a later slice should throw.
throws_ok {
$rs->populate([
{
artistid => 1,
name => \"'foo'",
},
{
artistid => \2,
name => \"'foo'",
}
]);
} qr/Literal SQL found where a plain bind value is expected/, 'literal sql where bind expected throws';
# ... and vice-versa.
throws_ok {
$rs->populate([
{
artistid => \1,
name => \"'foo'",
},
{
artistid => 2,
name => \"'foo'",
}
]);
} qr/\QIncorrect value (expecting SCALAR-ref/, 'bind where literal sql expected throws';
throws_ok {
$rs->populate([
{
artistid => 1,
name => \"'foo'",
},
{
artistid => 2,
name => \"'bar'",
}
]);
} 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
my $fn = Path::Class::File->new ('somedir/somefilename.tmp');
my $fn2 = Path::Class::File->new ('somedir/someotherfilename.tmp');
lives_ok {
$rs->populate([
{
name => 'supplied before stringifying object',
},
{
name => $fn,
}
]);
} 'stringifying objects pass through';
# ... and vice-versa.
lives_ok {
$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");
}
$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;
|