reverted [7807], and just changed code to use the custom pg_catalog query, which...
[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
54e0bd06 9my $schema = DBICTest->init_schema();
54e0bd06 10
d35a6fed 11# The map below generates stuff like:
12# [ qw/artistid name/ ],
13# [ 4, "b" ],
14# [ 5, "c" ],
15# ...
16# [ 9999, "ntm" ],
17# [ 10000, "ntn" ],
18
19my $start_id = 'populateXaaaaaa';
20my $rows = 10;
21my $offset = 3;
22
23$schema->populate('Artist', [ [ qw/artistid name/ ], map { [ ($_ + $offset) => $start_id++ ] } ( 1 .. $rows ) ] );
24is (
25 $schema->resultset ('Artist')->search ({ name => { -like => 'populateX%' } })->count,
26 $rows,
27 'populate created correct number of rows with massive AoA bulk insert',
28);
29
30my $artist = $schema->resultset ('Artist')
31 ->search ({ 'cds.title' => { '!=', undef } }, { join => 'cds' })
32 ->first;
33my $ex_title = $artist->cds->first->title;
34
35throws_ok ( sub {
36 my $i = 600;
37 $schema->populate('CD', [
38 map {
39 {
d35a6fed 40 artist => $artist->id,
41 title => $_,
42 year => 2009,
43 }
44 } ('Huey', 'Dewey', $ex_title, 'Louie')
45 ])
46}, qr/columns .+ are not unique for populate slice.+$ex_title/ms, 'Readable exception thrown for failed populate');
b0457415 47
89b2e3e4 48## make sure populate honors fields/orders in list context
b0457415 49## schema order
89b2e3e4 50my @links = $schema->populate('Link', [
b0457415 51[ qw/id url title/ ],
52[ qw/2 burl btitle/ ]
53]);
89b2e3e4 54is(scalar @links, 1);
55
56my $link2 = shift @links;
b0457415 57is($link2->id, 2, 'Link 2 id');
58is($link2->url, 'burl', 'Link 2 url');
59is($link2->title, 'btitle', 'Link 2 title');
60
61## non-schema order
89b2e3e4 62@links = $schema->populate('Link', [
b0457415 63[ qw/id title url/ ],
64[ qw/3 ctitle curl/ ]
65]);
89b2e3e4 66is(scalar @links, 1);
67
68my $link3 = shift @links;
b0457415 69is($link3->id, 3, 'Link 3 id');
70is($link3->url, 'curl', 'Link 3 url');
71is($link3->title, 'ctitle', 'Link 3 title');
72
73## not all physical columns
89b2e3e4 74@links = $schema->populate('Link', [
b0457415 75[ qw/id title/ ],
76[ qw/4 dtitle/ ]
77]);
89b2e3e4 78is(scalar @links, 1);
79
80my $link4 = shift @links;
b0457415 81is($link4->id, 4, 'Link 4 id');
82is($link4->url, undef, 'Link 4 url');
83is($link4->title, 'dtitle', 'Link 4 title');
84
85
89b2e3e4 86## make sure populate -> insert_bulk honors fields/orders in void context
87## schema order
88$schema->populate('Link', [
89[ qw/id url title/ ],
90[ qw/5 eurl etitle/ ]
91]);
92my $link5 = $schema->resultset('Link')->find(5);
93is($link5->id, 5, 'Link 5 id');
94is($link5->url, 'eurl', 'Link 5 url');
95is($link5->title, 'etitle', 'Link 5 title');
96
97## non-schema order
98$schema->populate('Link', [
99[ qw/id title url/ ],
100[ qw/6 ftitle furl/ ]
101]);
102my $link6 = $schema->resultset('Link')->find(6);
103is($link6->id, 6, 'Link 6 id');
104is($link6->url, 'furl', 'Link 6 url');
105is($link6->title, 'ftitle', 'Link 6 title');
106
107## not all physical columns
108$schema->populate('Link', [
109[ qw/id title/ ],
110[ qw/7 gtitle/ ]
111]);
112my $link7 = $schema->resultset('Link')->find(7);
113is($link7->id, 7, 'Link 7 id');
114is($link7->url, undef, 'Link 7 url');
115is($link7->title, 'gtitle', 'Link 7 title');
116
574d7df6 117# test _execute_array_empty (insert_bulk with all literal sql)
118my $rs = $schema->resultset('Artist');
119$rs->delete;
120$rs->populate([
121 (+{
122 name => \"'DT'",
123 rank => \500,
124 charfield => \"'mtfnpy'",
125 }) x 5
126]);
127
128is((grep {
129 $_->name eq 'DT' &&
130 $_->rank == 500 &&
131 $_->charfield eq 'mtfnpy'
132} $rs->all), 5, 'populate with all literal SQL');
bbd6f348 133
134$rs->delete;
135
136throws_ok {
137 $rs->populate([
138 {
139 artistid => 1,
140 name => 'foo1',
141 },
142 {
143 artistid => 'foo', # this dies
144 name => 'foo2',
145 },
146 {
147 artistid => 3,
148 name => 'foo3',
149 },
150 ]);
151} qr/slice/, 'bad slice';
152
153is($rs->count, 0, 'populate is atomic');
154
1295943f 155# Trying to use a column marked as a bind in the first slice with literal sql in
156# a later slice should throw.
157
158throws_ok {
159 $rs->populate([
160 {
161 artistid => 1,
162 name => \"'foo'",
163 },
164 {
165 artistid => \2,
166 name => \"'foo'",
167 }
168 ]);
169} qr/bind expected/, 'literal sql where bind expected throws';
170
171# ... and vice-versa.
172
173throws_ok {
174 $rs->populate([
175 {
176 artistid => \1,
177 name => \"'foo'",
178 },
179 {
180 artistid => 2,
181 name => \"'foo'",
182 }
183 ]);
184} qr/literal SQL expected/i, 'bind where literal sql expected throws';
185
186throws_ok {
187 $rs->populate([
188 {
189 artistid => 1,
190 name => \"'foo'",
191 },
192 {
193 artistid => 2,
194 name => \"'bar'",
195 }
196 ]);
197} qr/inconsistent/, 'literal sql must be the same in all slices';
198
bbd6f348 199done_testing;