removed some dead code, added fix and test for _execute_array_empty
[dbsrgits/DBIx-Class.git] / t / 100populate.t
CommitLineData
54e0bd06 1use strict;
d35a6fed 2use warnings;
54e0bd06 3
4use Test::More;
d35a6fed 5use Test::Exception;
54e0bd06 6use lib qw(t/lib);
7use DBICTest;
8
574d7df6 9plan tests => 24;
54e0bd06 10
11my $schema = DBICTest->init_schema();
54e0bd06 12
d35a6fed 13# The map below generates stuff like:
14# [ qw/artistid name/ ],
15# [ 4, "b" ],
16# [ 5, "c" ],
17# ...
18# [ 9999, "ntm" ],
19# [ 10000, "ntn" ],
20
21my $start_id = 'populateXaaaaaa';
22my $rows = 10;
23my $offset = 3;
24
25$schema->populate('Artist', [ [ qw/artistid name/ ], map { [ ($_ + $offset) => $start_id++ ] } ( 1 .. $rows ) ] );
26is (
27 $schema->resultset ('Artist')->search ({ name => { -like => 'populateX%' } })->count,
28 $rows,
29 'populate created correct number of rows with massive AoA bulk insert',
30);
31
32my $artist = $schema->resultset ('Artist')
33 ->search ({ 'cds.title' => { '!=', undef } }, { join => 'cds' })
34 ->first;
35my $ex_title = $artist->cds->first->title;
36
37throws_ok ( sub {
38 my $i = 600;
39 $schema->populate('CD', [
40 map {
41 {
d35a6fed 42 artist => $artist->id,
43 title => $_,
44 year => 2009,
45 }
46 } ('Huey', 'Dewey', $ex_title, 'Louie')
47 ])
48}, qr/columns .+ are not unique for populate slice.+$ex_title/ms, 'Readable exception thrown for failed populate');
b0457415 49
89b2e3e4 50## make sure populate honors fields/orders in list context
b0457415 51## schema order
89b2e3e4 52my @links = $schema->populate('Link', [
b0457415 53[ qw/id url title/ ],
54[ qw/2 burl btitle/ ]
55]);
89b2e3e4 56is(scalar @links, 1);
57
58my $link2 = shift @links;
b0457415 59is($link2->id, 2, 'Link 2 id');
60is($link2->url, 'burl', 'Link 2 url');
61is($link2->title, 'btitle', 'Link 2 title');
62
63## non-schema order
89b2e3e4 64@links = $schema->populate('Link', [
b0457415 65[ qw/id title url/ ],
66[ qw/3 ctitle curl/ ]
67]);
89b2e3e4 68is(scalar @links, 1);
69
70my $link3 = shift @links;
b0457415 71is($link3->id, 3, 'Link 3 id');
72is($link3->url, 'curl', 'Link 3 url');
73is($link3->title, 'ctitle', 'Link 3 title');
74
75## not all physical columns
89b2e3e4 76@links = $schema->populate('Link', [
b0457415 77[ qw/id title/ ],
78[ qw/4 dtitle/ ]
79]);
89b2e3e4 80is(scalar @links, 1);
81
82my $link4 = shift @links;
b0457415 83is($link4->id, 4, 'Link 4 id');
84is($link4->url, undef, 'Link 4 url');
85is($link4->title, 'dtitle', 'Link 4 title');
86
87
89b2e3e4 88## make sure populate -> insert_bulk honors fields/orders in void context
89## schema order
90$schema->populate('Link', [
91[ qw/id url title/ ],
92[ qw/5 eurl etitle/ ]
93]);
94my $link5 = $schema->resultset('Link')->find(5);
95is($link5->id, 5, 'Link 5 id');
96is($link5->url, 'eurl', 'Link 5 url');
97is($link5->title, 'etitle', 'Link 5 title');
98
99## non-schema order
100$schema->populate('Link', [
101[ qw/id title url/ ],
102[ qw/6 ftitle furl/ ]
103]);
104my $link6 = $schema->resultset('Link')->find(6);
105is($link6->id, 6, 'Link 6 id');
106is($link6->url, 'furl', 'Link 6 url');
107is($link6->title, 'ftitle', 'Link 6 title');
108
109## not all physical columns
110$schema->populate('Link', [
111[ qw/id title/ ],
112[ qw/7 gtitle/ ]
113]);
114my $link7 = $schema->resultset('Link')->find(7);
115is($link7->id, 7, 'Link 7 id');
116is($link7->url, undef, 'Link 7 url');
117is($link7->title, 'gtitle', 'Link 7 title');
118
574d7df6 119# test _execute_array_empty (insert_bulk with all literal sql)
120my $rs = $schema->resultset('Artist');
121$rs->delete;
122$rs->populate([
123 (+{
124 name => \"'DT'",
125 rank => \500,
126 charfield => \"'mtfnpy'",
127 }) x 5
128]);
129
130is((grep {
131 $_->name eq 'DT' &&
132 $_->rank == 500 &&
133 $_->charfield eq 'mtfnpy'
134} $rs->all), 5, 'populate with all literal SQL');