Add better error reporting on bulk_insert (ash++)
[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
d35a6fed 9plan tests => 23;
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 {
42 cdid => $i++, # without a PK the bulk insert does not engage - how come?
43 artist => $artist->id,
44 title => $_,
45 year => 2009,
46 }
47 } ('Huey', 'Dewey', $ex_title, 'Louie')
48 ])
49}, qr/columns .+ are not unique for populate slice.+$ex_title/ms, 'Readable exception thrown for failed populate');
b0457415 50
89b2e3e4 51## make sure populate honors fields/orders in list context
b0457415 52## schema order
89b2e3e4 53my @links = $schema->populate('Link', [
b0457415 54[ qw/id url title/ ],
55[ qw/2 burl btitle/ ]
56]);
89b2e3e4 57is(scalar @links, 1);
58
59my $link2 = shift @links;
b0457415 60is($link2->id, 2, 'Link 2 id');
61is($link2->url, 'burl', 'Link 2 url');
62is($link2->title, 'btitle', 'Link 2 title');
63
64## non-schema order
89b2e3e4 65@links = $schema->populate('Link', [
b0457415 66[ qw/id title url/ ],
67[ qw/3 ctitle curl/ ]
68]);
89b2e3e4 69is(scalar @links, 1);
70
71my $link3 = shift @links;
b0457415 72is($link3->id, 3, 'Link 3 id');
73is($link3->url, 'curl', 'Link 3 url');
74is($link3->title, 'ctitle', 'Link 3 title');
75
76## not all physical columns
89b2e3e4 77@links = $schema->populate('Link', [
b0457415 78[ qw/id title/ ],
79[ qw/4 dtitle/ ]
80]);
89b2e3e4 81is(scalar @links, 1);
82
83my $link4 = shift @links;
b0457415 84is($link4->id, 4, 'Link 4 id');
85is($link4->url, undef, 'Link 4 url');
86is($link4->title, 'dtitle', 'Link 4 title');
87
88
89b2e3e4 89## make sure populate -> insert_bulk honors fields/orders in void context
90## schema order
91$schema->populate('Link', [
92[ qw/id url title/ ],
93[ qw/5 eurl etitle/ ]
94]);
95my $link5 = $schema->resultset('Link')->find(5);
96is($link5->id, 5, 'Link 5 id');
97is($link5->url, 'eurl', 'Link 5 url');
98is($link5->title, 'etitle', 'Link 5 title');
99
100## non-schema order
101$schema->populate('Link', [
102[ qw/id title url/ ],
103[ qw/6 ftitle furl/ ]
104]);
105my $link6 = $schema->resultset('Link')->find(6);
106is($link6->id, 6, 'Link 6 id');
107is($link6->url, 'furl', 'Link 6 url');
108is($link6->title, 'ftitle', 'Link 6 title');
109
110## not all physical columns
111$schema->populate('Link', [
112[ qw/id title/ ],
113[ qw/7 gtitle/ ]
114]);
115my $link7 = $schema->resultset('Link')->find(7);
116is($link7->id, 7, 'Link 7 id');
117is($link7->url, undef, 'Link 7 url');
118is($link7->title, 'gtitle', 'Link 7 title');
119