8 use Path::Class::File ();
10 my $schema = DBICTest->init_schema();
12 # The map below generates stuff like:
13 # [ qw/artistid name/ ],
20 my $start_id = 'populateXaaaaaa';
24 $schema->populate('Artist', [ [ qw/artistid name/ ], map { [ ($_ + $offset) => $start_id++ ] } ( 1 .. $rows ) ] );
26 $schema->resultset ('Artist')->search ({ name => { -like => 'populateX%' } })->count,
28 'populate created correct number of rows with massive AoA bulk insert',
31 my $artist = $schema->resultset ('Artist')
32 ->search ({ 'cds.title' => { '!=', undef } }, { join => 'cds' })
34 my $ex_title = $artist->cds->first->title;
38 $schema->populate('CD', [
41 artist => $artist->id,
45 } ('Huey', 'Dewey', $ex_title, 'Louie')
47 }, qr/columns .+ are not unique for populate slice.+$ex_title/ms, 'Readable exception thrown for failed populate');
49 ## make sure populate honors fields/orders in list context
51 my @links = $schema->populate('Link', [
57 my $link2 = shift @links;
58 is($link2->id, 2, 'Link 2 id');
59 is($link2->url, 'burl', 'Link 2 url');
60 is($link2->title, 'btitle', 'Link 2 title');
63 @links = $schema->populate('Link', [
69 my $link3 = shift @links;
70 is($link3->id, 3, 'Link 3 id');
71 is($link3->url, 'curl', 'Link 3 url');
72 is($link3->title, 'ctitle', 'Link 3 title');
74 ## not all physical columns
75 @links = $schema->populate('Link', [
81 my $link4 = shift @links;
82 is($link4->id, 4, 'Link 4 id');
83 is($link4->url, undef, 'Link 4 url');
84 is($link4->title, 'dtitle', 'Link 4 title');
87 ## make sure populate -> insert_bulk honors fields/orders in void context
89 $schema->populate('Link', [
93 my $link5 = $schema->resultset('Link')->find(5);
94 is($link5->id, 5, 'Link 5 id');
95 is($link5->url, 'eurl', 'Link 5 url');
96 is($link5->title, 'etitle', 'Link 5 title');
99 $schema->populate('Link', [
100 [ qw/id title url/ ],
101 [ qw/6 ftitle furl/ ]
103 my $link6 = $schema->resultset('Link')->find(6);
104 is($link6->id, 6, 'Link 6 id');
105 is($link6->url, 'furl', 'Link 6 url');
106 is($link6->title, 'ftitle', 'Link 6 title');
108 ## not all physical columns
109 $schema->populate('Link', [
113 my $link7 = $schema->resultset('Link')->find(7);
114 is($link7->id, 7, 'Link 7 id');
115 is($link7->url, undef, 'Link 7 url');
116 is($link7->title, 'gtitle', 'Link 7 title');
118 # populate with literals
120 my $rs = $schema->resultset('Link');
123 # test _execute_array_empty (insert_bulk with all literal sql)
127 url => \"'cpan.org'",
128 title => \"'The ''best of'' cpan'",
133 $_->url eq 'cpan.org' &&
134 $_->title eq "The 'best of' cpan",
135 } $rs->all), 5, 'populate with all literal SQL');
139 # test mixed binds with literal sql
143 url => \"'cpan.org'",
144 title => "The 'best of' cpan",
149 $_->url eq 'cpan.org' &&
150 $_->title eq "The 'best of' cpan",
151 } $rs->all), 5, 'populate with all literal SQL');
156 my $rs = $schema->resultset('Artist');
165 artistid => 'foo', # this dies
173 } qr/slice/, 'bad slice';
175 is($rs->count, 0, 'populate is atomic');
177 # Trying to use a column marked as a bind in the first slice with literal sql in
178 # a later slice should throw.
191 } qr/bind expected/, 'literal sql where bind expected throws';
193 # ... and vice-versa.
206 } qr/literal SQL expected/i, 'bind where literal sql expected throws';
219 } qr/inconsistent/, 'literal sql must be the same in all slices';
221 # the stringification has nothing to do with the artist name
222 # this is solely for testing consistency
223 my $fn = Path::Class::File->new ('somedir/somefilename.tmp');
224 my $fn2 = Path::Class::File->new ('somedir/someotherfilename.tmp');
229 name => 'supplied before stringifying object',
235 } 'stringifying objects pass through';
237 # ... and vice-versa.
245 name => 'supplied after stringifying object',
248 } 'stringifying objects pass through';
253 'supplied after stringifying object',
254 'supplied before stringifying object'
256 my $row = $rs->find ({name => $_});
257 ok ($row, "Stringification test row '$_' properly inserted");
262 # test stringification with ->create rather than Storage::insert_bulk as well
265 my @dummy = $rs->populate([
267 name => 'supplied before stringifying object',
273 } 'stringifying objects pass through';
275 # ... and vice-versa.
278 my @dummy = $rs->populate([
283 name => 'supplied after stringifying object',
286 } 'stringifying objects pass through';
291 'supplied after stringifying object',
292 'supplied before stringifying object'
294 my $row = $rs->find ({name => $_});
295 ok ($row, "Stringification test row '$_' properly inserted");
299 $schema->resultset('TwoKeys')->populate([{
302 fourkeys_to_twokeys => [{
316 } 'multicol-PK has_many populate works';