Commit | Line | Data |
fbd83464 |
1 | package dbixcsl_common_tests; |
a78e3fed |
2 | |
3 | use strict; |
3980d69c |
4 | use warnings; |
a78e3fed |
5 | |
6 | use Test::More; |
0c1d5b47 |
7 | use Test::Exception; |
18fca96a |
8 | use DBIx::Class::Schema::Loader; |
390bbe3f |
9 | use Class::Unload; |
10 | use File::Path; |
a78e3fed |
11 | use DBI; |
4daef04f |
12 | use Digest::MD5; |
13 | use File::Find 'find'; |
3a89a69f |
14 | use Class::Unload (); |
15efd63a |
15 | use DBIx::Class::Schema::Loader::Utils 'dumper_squashed'; |
26334ec1 |
16 | use List::MoreUtils 'apply'; |
ef8e9c69 |
17 | use DBIx::Class::Schema::Loader::Optional::Dependencies (); |
71687093 |
18 | use Try::Tiny; |
dc379dc6 |
19 | use File::Slurp 'slurp'; |
ef8e9c69 |
20 | use namespace::clean; |
a78e3fed |
21 | |
c213fd3d |
22 | use dbixcsl_test_dir qw/$tdir/; |
23 | |
24 | my $DUMP_DIR = "$tdir/common_dump"; |
390bbe3f |
25 | rmtree $DUMP_DIR; |
26 | |
3b17d988 |
27 | use constant RESCAN_WARNINGS => qr/(?i:loader_test|LoaderTest)\d+s? has no primary key|^Dumping manual schema|^Schema dump completed|collides with an inherited method|invalidates \d+ active statement|^Bad table or view/; |
1ad8e8c3 |
28 | |
12b86f07 |
29 | use constant SOURCE_DDL => qr/CREATE (?:TABLE|VIEW) (?!"dbicsl[.-]test")/i; |
30 | |
a78e3fed |
31 | sub new { |
32 | my $class = shift; |
33 | |
34 | my $self; |
35 | |
36 | if( ref($_[0]) eq 'HASH') { |
37 | my $args = shift; |
38 | $self = { (%$args) }; |
39 | } |
40 | else { |
41 | $self = { @_ }; |
42 | } |
43 | |
44 | # Only MySQL uses this |
45 | $self->{innodb} ||= ''; |
0906d55b |
46 | |
ffb03c96 |
47 | # DB2 and Firebird don't support 'field type NULL' |
0906d55b |
48 | $self->{null} = 'NULL' unless defined $self->{null}; |
af6c2665 |
49 | |
50 | $self->{verbose} = $ENV{TEST_VERBOSE} || 0; |
a78e3fed |
51 | |
df637f3d |
52 | # Optional extra tables and tests |
53 | $self->{extra} ||= {}; |
54 | |
8dcf4292 |
55 | $self->{basic_date_datatype} ||= 'DATE'; |
4cbddf8d |
56 | |
e35e5a64 |
57 | # Not all DBS do SQL-standard CURRENT_TIMESTAMP |
6e566cc4 |
58 | $self->{default_function} ||= "current_timestamp"; |
59 | $self->{default_function_def} ||= "timestamp default $self->{default_function}"; |
e35e5a64 |
60 | |
9065de34 |
61 | $self = bless $self, $class; |
62 | |
fa3bad42 |
63 | $self->{preserve_case_tests_table_names} = [qw/LoaderTest40 LoaderTest41/]; |
64 | |
65 | if (lc($self->{vendor}) eq 'mysql' && $^O =~ /^(?:MSWin32|cygwin)\z/) { |
66 | $self->{preserve_case_tests_table_names} = [qw/Loader_Test40 Loader_Test41/]; |
67 | } |
68 | |
9065de34 |
69 | $self->setup_data_type_tests; |
70 | |
71 | return $self; |
a78e3fed |
72 | } |
73 | |
74 | sub skip_tests { |
75 | my ($self, $why) = @_; |
76 | |
77 | plan skip_all => $why; |
78 | } |
79 | |
4350370d |
80 | sub _monikerize { |
81 | my $name = shift; |
82 | return 'LoaderTest2X' if $name =~ /^loader_test2$/i; |
83 | return undef; |
84 | } |
85 | |
a78e3fed |
86 | sub run_tests { |
87 | my $self = shift; |
88 | |
3a89a69f |
89 | my @connect_info; |
a78e3fed |
90 | |
3a89a69f |
91 | if ($self->{dsn}) { |
92 | push @connect_info, [ @{$self}{qw/dsn user password connect_info_opts/ } ]; |
93 | } |
94 | else { |
95 | foreach my $info (@{ $self->{connect_info} || [] }) { |
96 | push @connect_info, [ @{$info}{qw/dsn user password connect_info_opts/ } ]; |
97 | } |
98 | } |
00805149 |
99 | |
100 | if ($ENV{SCHEMA_LOADER_TESTS_EXTRA_ONLY}) { |
101 | $self->run_only_extra_tests(\@connect_info); |
102 | return; |
103 | } |
a78e3fed |
104 | |
00805149 |
105 | my $extra_count = $self->{extra}{count} || 0; |
106 | |
3fea497f |
107 | my $col_accessor_map_tests = 5; |
cfc5dce3 |
108 | my $num_rescans = 5; |
dc379dc6 |
109 | $num_rescans-- if $self->{vendor} eq 'Mysql'; |
b48f3f80 |
110 | $num_rescans++ if $self->{vendor} eq 'mssql'; |
ed18888f |
111 | $num_rescans++ if $self->{vendor} eq 'Firebird'; |
cfc5dce3 |
112 | |
113 | plan tests => @connect_info * |
dc379dc6 |
114 | (203 + $num_rescans * $col_accessor_map_tests + $extra_count + ($self->{data_type_tests}{test_count} || 0)); |
3a89a69f |
115 | |
116 | foreach my $info_idx (0..$#connect_info) { |
117 | my $info = $connect_info[$info_idx]; |
390bbe3f |
118 | |
3a89a69f |
119 | @{$self}{qw/dsn user password connect_info_opts/} = @$info; |
120 | |
121 | $self->create(); |
122 | |
00805149 |
123 | my $schema_class = $self->setup_schema($info); |
3a89a69f |
124 | $self->test_schema($schema_class); |
125 | |
126 | rmtree $DUMP_DIR |
127 | unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP} && $info_idx == $#connect_info; |
128 | } |
390bbe3f |
129 | } |
130 | |
00805149 |
131 | sub run_only_extra_tests { |
132 | my ($self, $connect_info) = @_; |
133 | |
1805763b |
134 | plan tests => @$connect_info * (4 + ($self->{extra}{count} || 0) + ($self->{data_type_tests}{test_count} || 0)); |
00805149 |
135 | |
33aa3462 |
136 | rmtree $DUMP_DIR; |
137 | |
04dcecc8 |
138 | foreach my $info_idx (0..$#$connect_info) { |
139 | my $info = $connect_info->[$info_idx]; |
140 | |
00805149 |
141 | @{$self}{qw/dsn user password connect_info_opts/} = @$info; |
142 | |
cf0ba25b |
143 | $self->drop_extra_tables_only; |
144 | |
145 | my $dbh = $self->dbconnect(1); |
a94a90f2 |
146 | $dbh->do($_) for @{ $self->{pre_create} || [] }; |
c38ec663 |
147 | $dbh->do($_) for @{ $self->{extra}{create} || [] }; |
b48f3f80 |
148 | |
149 | if (not ($self->{vendor} eq 'mssql' && $dbh->{Driver}{Name} eq 'Sybase')) { |
150 | $dbh->do($_) for @{ $self->{data_type_tests}{ddl} || []}; |
151 | } |
152 | |
00805149 |
153 | $self->{_created} = 1; |
154 | |
12b86f07 |
155 | my $file_count = grep $_ =~ SOURCE_DDL, @{ $self->{extra}{create} || [] }; |
cf0ba25b |
156 | $file_count++; # schema |
b48f3f80 |
157 | |
158 | if (not ($self->{vendor} eq 'mssql' && $dbh->{Driver}{Name} eq 'Sybase')) { |
159 | $file_count++ for @{ $self->{data_type_tests}{table_names} || [] }; |
160 | } |
00805149 |
161 | |
cf0ba25b |
162 | my $schema_class = $self->setup_schema($info, $file_count); |
00805149 |
163 | my ($monikers, $classes) = $self->monikers_and_classes($schema_class); |
164 | my $conn = $schema_class->clone; |
165 | |
1805763b |
166 | $self->test_data_types($conn); |
1ad8e8c3 |
167 | $self->{extra}{run}->($conn, $monikers, $classes, $self) if $self->{extra}{run}; |
04dcecc8 |
168 | |
169 | if (not ($ENV{SCHEMA_LOADER_TESTS_NOCLEANUP} && $info_idx == $#$connect_info)) { |
cf0ba25b |
170 | $self->drop_extra_tables_only; |
04dcecc8 |
171 | rmtree $DUMP_DIR; |
172 | } |
00805149 |
173 | } |
174 | } |
175 | |
cf0ba25b |
176 | sub drop_extra_tables_only { |
177 | my $self = shift; |
178 | |
179 | my $dbh = $self->dbconnect(0); |
7b868481 |
180 | |
3b17d988 |
181 | local $^W = 0; # for ADO |
182 | |
c38ec663 |
183 | $dbh->do($_) for @{ $self->{extra}{pre_drop_ddl} || [] }; |
dc379dc6 |
184 | $self->drop_table($dbh, $_) for @{ $self->{extra}{drop} || [] }; |
cf0ba25b |
185 | |
b48f3f80 |
186 | if (not ($self->{vendor} eq 'mssql' && $dbh->{Driver}{Name} eq 'Sybase')) { |
187 | foreach my $data_type_table (@{ $self->{data_type_tests}{table_names} || [] }) { |
dc379dc6 |
188 | $self->drop_table($dbh, $data_type_table); |
b48f3f80 |
189 | } |
cf0ba25b |
190 | } |
191 | } |
192 | |
fdd8ff16 |
193 | # defined in sub create |
194 | my (@statements, @statements_reltests, @statements_advanced, |
195 | @statements_advanced_sqlite, @statements_inline_rels, |
196 | @statements_implicit_rels); |
197 | |
390bbe3f |
198 | sub setup_schema { |
00805149 |
199 | my ($self, $connect_info, $expected_count) = @_; |
390bbe3f |
200 | |
c2849787 |
201 | my $schema_class = 'DBIXCSL_Test::Schema'; |
a78e3fed |
202 | |
af6c2665 |
203 | my $debug = ($self->{verbose} > 1) ? 1 : 0; |
204 | |
d36c8734 |
205 | if ($ENV{SCHEMA_LOADER_TESTS_USE_MOOSE}) { |
206 | if (not DBIx::Class::Schema::Loader::Optional::Dependencies->req_ok_for('use_moose')) { |
207 | die sprintf ("Missing dependencies for SCHEMA_LOADER_TESTS_USE_MOOSE: %s\n", |
208 | DBIx::Class::Schema::Loader::Optional::Dependencies->req_missing_for('use_moose')); |
209 | } |
210 | |
211 | $self->{use_moose} = 1; |
ab0262bd |
212 | } |
6c2b21a5 |
213 | |
af96f52e |
214 | my %loader_opts = ( |
b1e43108 |
215 | constraint => |
6ebd0f33 |
216 | qr/^(?:\S+\.)?(?:(?:$self->{vendor}|extra)[_-]?)?loader[_-]?test[0-9]+(?!.*_)/i, |
507034ad |
217 | relationships => 1, |
218 | additional_classes => 'TestAdditional', |
219 | additional_base_classes => 'TestAdditionalBase', |
220 | left_base_classes => [ qw/TestLeftBase/ ], |
71687093 |
221 | components => [ qw/TestComponent +TestComponentFQN/ ], |
a7116285 |
222 | inflect_plural => { loader_test4_fkid => 'loader_test4zes' }, |
996be9ee |
223 | inflect_singular => { fkid => 'fkid_singular' }, |
4350370d |
224 | moniker_map => \&_monikerize, |
a51d3566 |
225 | custom_column_info => \&_custom_column_info, |
507034ad |
226 | debug => $debug, |
f22644d7 |
227 | use_namespaces => 0, |
390bbe3f |
228 | dump_directory => $DUMP_DIR, |
42e785fa |
229 | datetime_timezone => 'Europe/Berlin', |
18e84656 |
230 | datetime_locale => 'de_DE', |
d36c8734 |
231 | $self->{use_moose} ? ( |
232 | use_moose => 1, |
233 | result_roles => 'TestRole', |
234 | result_roles_map => { LoaderTest2X => 'TestRoleForMap' }, |
235 | ) : (), |
15c4393b |
236 | col_collision_map => { '^(can)\z' => 'caught_collision_%s' }, |
a7116285 |
237 | rel_collision_map => { '^(set_primary_key)\z' => 'caught_rel_collision_%s' }, |
3fea497f |
238 | col_accessor_map => \&test_col_accessor_map, |
d36c8734 |
239 | result_components_map => { LoaderTest2X => 'TestComponentForMap', LoaderTest1 => '+TestComponentForMapFQN' }, |
18e84656 |
240 | %{ $self->{loader_options} || {} }, |
af96f52e |
241 | ); |
242 | |
243 | $loader_opts{db_schema} = $self->{db_schema} if $self->{db_schema}; |
af96f52e |
244 | |
3a89a69f |
245 | Class::Unload->unload($schema_class); |
246 | |
ffb03c96 |
247 | my $file_count; |
996be9ee |
248 | { |
26da4cc3 |
249 | my @loader_warnings; |
c38ec663 |
250 | local $SIG{__WARN__} = sub { push(@loader_warnings, @_); }; |
26da4cc3 |
251 | eval qq{ |
252 | package $schema_class; |
253 | use base qw/DBIx::Class::Schema::Loader/; |
254 | |
255 | __PACKAGE__->loader_options(\%loader_opts); |
00805149 |
256 | __PACKAGE__->connection(\@\$connect_info); |
26da4cc3 |
257 | }; |
258 | |
259 | ok(!$@, "Loader initialization") or diag $@; |
acfcc1fb |
260 | |
26da4cc3 |
261 | find sub { return if -d; $file_count++ }, $DUMP_DIR; |
00805149 |
262 | |
263 | my $standard_sources = not defined $expected_count; |
264 | |
265 | if ($standard_sources) { |
dd87d4c4 |
266 | $expected_count = 36; |
b48f3f80 |
267 | |
268 | if (not ($self->{vendor} eq 'mssql' && $connect_info->[0] =~ /Sybase/)) { |
269 | $expected_count++ for @{ $self->{data_type_tests}{table_names} || [] }; |
270 | } |
00805149 |
271 | |
12b86f07 |
272 | # skip schema-qualified tables |
273 | $expected_count += grep $_ =~ SOURCE_DDL, |
00805149 |
274 | @{ $self->{extra}{create} || [] }; |
275 | |
276 | $expected_count -= grep /CREATE TABLE/, @statements_inline_rels |
277 | if $self->{skip_rels} || $self->{no_inline_rels}; |
278 | |
279 | $expected_count -= grep /CREATE TABLE/, @statements_implicit_rels |
280 | if $self->{skip_rels} || $self->{no_implicit_rels}; |
281 | |
282 | $expected_count -= grep /CREATE TABLE/, ($self->{vendor} =~ /sqlite/ ? @statements_advanced_sqlite : @statements_advanced), @statements_reltests |
283 | if $self->{skip_rels}; |
284 | } |
26da4cc3 |
285 | |
286 | is $file_count, $expected_count, 'correct number of files generated'; |
287 | |
288 | my $warn_count = 2; |
26da4cc3 |
289 | |
290 | $warn_count++ for grep /^Bad table or view/, @loader_warnings; |
291 | |
292 | $warn_count++ for grep /renaming \S+ relation/, @loader_warnings; |
293 | |
26da4cc3 |
294 | $warn_count++ for grep /\b(?!loader_test9)\w+ has no primary key/i, @loader_warnings; |
d073740e |
295 | |
a7116285 |
296 | $warn_count++ for grep /^Column '\w+' in table '\w+' collides with an inherited method\./, @loader_warnings; |
297 | |
298 | $warn_count++ for grep /^Relationship '\w+' in source '\w+' for columns '[^']+' collides with an inherited method\./, @loader_warnings; |
15c4393b |
299 | |
b511f36e |
300 | $warn_count++ for grep { my $w = $_; grep $w =~ $_, @{ $self->{warnings} || [] } } @loader_warnings; |
301 | |
8f65b7e5 |
302 | $warn_count-- for grep { my $w = $_; grep $w =~ $_, @{ $self->{failtrigger_warnings} || [] } } @loader_warnings; |
303 | |
00805149 |
304 | if ($standard_sources) { |
305 | if($self->{skip_rels}) { |
306 | SKIP: { |
307 | is(scalar(@loader_warnings), $warn_count, "No loader warnings") |
308 | or diag @loader_warnings; |
309 | skip "No missing PK warnings without rels", 1; |
310 | } |
311 | } |
312 | else { |
313 | $warn_count++; |
314 | is(scalar(@loader_warnings), $warn_count, "Expected loader warning") |
58dfed76 |
315 | or diag @loader_warnings; |
00805149 |
316 | is(grep(/loader_test9 has no primary key/i, @loader_warnings), 1, |
317 | "Missing PK warning"); |
58dfed76 |
318 | } |
996be9ee |
319 | } |
320 | else { |
00805149 |
321 | SKIP: { |
cf0ba25b |
322 | is scalar(@loader_warnings), $warn_count, 'Correct number of warnings' |
323 | or diag @loader_warnings; |
00805149 |
324 | skip "not testing standard sources", 1; |
325 | } |
996be9ee |
326 | } |
327 | } |
ffb03c96 |
328 | |
e32d24a5 |
329 | exit if ($file_count||0) != $expected_count; |
ffb03c96 |
330 | |
390bbe3f |
331 | return $schema_class; |
332 | } |
333 | |
334 | sub test_schema { |
335 | my $self = shift; |
336 | my $schema_class = shift; |
a4a19f3c |
337 | |
996be9ee |
338 | my $conn = $schema_class->clone; |
243c6ebc |
339 | |
340 | ($self->{before_tests_run} || sub {})->($conn); |
341 | |
00805149 |
342 | my ($monikers, $classes) = $self->monikers_and_classes($schema_class); |
a4a19f3c |
343 | |
22270947 |
344 | my $moniker1 = $monikers->{loader_test1s}; |
345 | my $class1 = $classes->{loader_test1s}; |
3980d69c |
346 | my $rsobj1 = $conn->resultset($moniker1); |
d70c335f |
347 | check_no_duplicate_unique_constraints($class1); |
3980d69c |
348 | |
349 | my $moniker2 = $monikers->{loader_test2}; |
350 | my $class2 = $classes->{loader_test2}; |
351 | my $rsobj2 = $conn->resultset($moniker2); |
d70c335f |
352 | check_no_duplicate_unique_constraints($class2); |
a78e3fed |
353 | |
a1cec524 |
354 | my $moniker23 = $monikers->{LOADER_test23} || $monikers->{loader_test23}; |
355 | my $class23 = $classes->{LOADER_test23} || $classes->{loader_test23}; |
996be9ee |
356 | my $rsobj23 = $conn->resultset($moniker1); |
357 | |
4cbddf8d |
358 | my $moniker24 = $monikers->{LoAdEr_test24} || $monikers->{loader_test24}; |
359 | my $class24 = $classes->{LoAdEr_test24} || $classes->{loader_test24}; |
996be9ee |
360 | my $rsobj24 = $conn->resultset($moniker2); |
361 | |
e35e5a64 |
362 | my $moniker35 = $monikers->{loader_test35}; |
363 | my $class35 = $classes->{loader_test35}; |
364 | my $rsobj35 = $conn->resultset($moniker35); |
365 | |
af6c2665 |
366 | isa_ok( $rsobj1, "DBIx::Class::ResultSet" ); |
367 | isa_ok( $rsobj2, "DBIx::Class::ResultSet" ); |
996be9ee |
368 | isa_ok( $rsobj23, "DBIx::Class::ResultSet" ); |
369 | isa_ok( $rsobj24, "DBIx::Class::ResultSet" ); |
e35e5a64 |
370 | isa_ok( $rsobj35, "DBIx::Class::ResultSet" ); |
996be9ee |
371 | |
8ac8926d |
372 | my @columns_lt2 = $class2->columns; |
dc379dc6 |
373 | is_deeply( \@columns_lt2, [ qw/id dat dat2 set_primary_key can dbix_class_testcomponent dbix_class_testcomponentmap testcomponent_fqn meta test_role_method test_role_for_map_method/ ], "Column Ordering" ); |
6947b5d6 |
374 | |
15c4393b |
375 | is $class2->column_info('can')->{accessor}, 'caught_collision_can', |
376 | 'accessor for column name that conflicts with a UNIVERSAL method renamed based on col_collision_map'; |
9fdf3d5b |
377 | |
d36c8734 |
378 | ok (exists $class2->column_info('set_primary_key')->{accessor} |
379 | && (not defined $class2->column_info('set_primary_key')->{accessor}), |
380 | 'accessor for column name that conflicts with a result base class method removed'); |
381 | |
382 | ok (exists $class2->column_info('dbix_class_testcomponent')->{accessor} |
383 | && (not defined $class2->column_info('dbix_class_testcomponent')->{accessor}), |
384 | 'accessor for column name that conflicts with a component class method removed'); |
9fdf3d5b |
385 | |
dc379dc6 |
386 | ok (exists $class2->column_info('dbix_class_testcomponentmap')->{accessor} |
387 | && (not defined $class2->column_info('dbix_class_testcomponentmap')->{accessor}), |
d36c8734 |
388 | 'accessor for column name that conflicts with a component class method removed'); |
8ac8926d |
389 | |
d36c8734 |
390 | ok (exists $class2->column_info('testcomponent_fqn')->{accessor} |
391 | && (not defined $class2->column_info('testcomponent_fqn')->{accessor}), |
392 | 'accessor for column name that conflicts with a fully qualified component class method removed'); |
4a67befc |
393 | |
d36c8734 |
394 | if ($self->{use_moose}) { |
395 | ok (exists $class2->column_info('meta')->{accessor} |
396 | && (not defined $class2->column_info('meta')->{accessor}), |
397 | 'accessor for column name that conflicts with Moose removed'); |
398 | |
399 | ok (exists $class2->column_info('test_role_for_map_method')->{accessor} |
400 | && (not defined $class2->column_info('test_role_for_map_method')->{accessor}), |
401 | 'accessor for column name that conflicts with a Result role removed'); |
402 | |
403 | ok (exists $class2->column_info('test_role_method')->{accessor} |
404 | && (not defined $class2->column_info('test_role_method')->{accessor}), |
405 | 'accessor for column name that conflicts with a Result role removed'); |
4be0d9b1 |
406 | } |
407 | else { |
d36c8734 |
408 | ok ((not exists $class2->column_info('meta')->{accessor}), |
409 | "not removing 'meta' accessor with use_moose disabled"); |
410 | |
411 | ok ((not exists $class2->column_info('test_role_for_map_method')->{accessor}), |
412 | 'no role method conflicts with use_moose disabled'); |
413 | |
414 | ok ((not exists $class2->column_info('test_role_method')->{accessor}), |
415 | 'no role method conflicts with use_moose disabled'); |
4be0d9b1 |
416 | } |
6c2b21a5 |
417 | |
996be9ee |
418 | my %uniq1 = $class1->unique_constraints; |
419 | my $uniq1_test = 0; |
420 | foreach my $ucname (keys %uniq1) { |
421 | my $cols_arrayref = $uniq1{$ucname}; |
18e84656 |
422 | if(@$cols_arrayref == 1 && $cols_arrayref->[0] eq 'dat') { |
996be9ee |
423 | $uniq1_test = 1; |
424 | last; |
425 | } |
426 | } |
58dfed76 |
427 | ok($uniq1_test, "Unique constraint"); |
996be9ee |
428 | |
22270947 |
429 | is($moniker1, 'LoaderTest1', 'moniker singularisation'); |
430 | |
996be9ee |
431 | my %uniq2 = $class2->unique_constraints; |
432 | my $uniq2_test = 0; |
433 | foreach my $ucname (keys %uniq2) { |
434 | my $cols_arrayref = $uniq2{$ucname}; |
435 | if(@$cols_arrayref == 2 |
18e84656 |
436 | && $cols_arrayref->[0] eq 'dat2' |
437 | && $cols_arrayref->[1] eq 'dat') { |
996be9ee |
438 | $uniq2_test = 2; |
439 | last; |
440 | } |
441 | } |
58dfed76 |
442 | ok($uniq2_test, "Multi-col unique constraint"); |
a78e3fed |
443 | |
4350370d |
444 | is($moniker2, 'LoaderTest2X', "moniker_map testing"); |
445 | |
e61e3fcb |
446 | SKIP: { |
447 | can_ok( $class1, 'test_additional_base' ) |
448 | or skip "Pre-requisite test failed", 1; |
449 | is( $class1->test_additional_base, "test_additional_base", |
450 | "Additional Base method" ); |
451 | } |
c2849787 |
452 | |
e61e3fcb |
453 | SKIP: { |
454 | can_ok( $class1, 'test_additional_base_override' ) |
455 | or skip "Pre-requisite test failed", 1; |
456 | is( $class1->test_additional_base_override, |
457 | "test_left_base_override", |
458 | "Left Base overrides Additional Base method" ); |
459 | } |
c2849787 |
460 | |
e61e3fcb |
461 | SKIP: { |
462 | can_ok( $class1, 'test_additional_base_additional' ) |
463 | or skip "Pre-requisite test failed", 1; |
464 | is( $class1->test_additional_base_additional, "test_additional", |
465 | "Additional Base can use Additional package method" ); |
466 | } |
c2849787 |
467 | |
e61e3fcb |
468 | SKIP: { |
469 | can_ok( $class1, 'dbix_class_testcomponent' ) |
470 | or skip "Pre-requisite test failed", 1; |
471 | is( $class1->dbix_class_testcomponent, |
472 | 'dbix_class_testcomponent works', |
473 | 'Additional Component' ); |
474 | } |
c2849787 |
475 | |
dc379dc6 |
476 | is try { $class2->dbix_class_testcomponentmap }, 'dbix_class_testcomponentmap works', |
5ad21d3c |
477 | 'component from result_component_map'; |
478 | |
dc379dc6 |
479 | isnt try { $class1->dbix_class_testcomponentmap }, 'dbix_class_testcomponentmap works', |
5ad21d3c |
480 | 'component from result_component_map not added to not mapped Result'; |
481 | |
482 | is try { $class1->testcomponent_fqn }, 'TestComponentFQN works', |
483 | 'fully qualified component class'; |
484 | |
485 | is try { $class1->testcomponentformap_fqn }, 'TestComponentForMapFQN works', |
486 | 'fully qualified component class from result_component_map'; |
487 | |
488 | isnt try { $class2->testcomponentformap_fqn }, 'TestComponentForMapFQN works', |
489 | 'fully qualified component class from result_component_map not added to not mapped Result'; |
71687093 |
490 | |
e61e3fcb |
491 | SKIP: { |
d36c8734 |
492 | skip 'not testing role methods with use_moose disabled', 2 |
493 | unless $self->{use_moose}; |
494 | |
495 | is try { $class1->test_role_method }, 'test_role_method works', |
496 | 'role from result_roles applied'; |
497 | |
498 | is try { $class2->test_role_for_map_method }, |
499 | 'test_role_for_map_method works', |
500 | 'role from result_roles_map applied'; |
501 | } |
502 | |
503 | SKIP: { |
e61e3fcb |
504 | can_ok( $class1, 'loader_test1_classmeth' ) |
505 | or skip "Pre-requisite test failed", 1; |
506 | is( $class1->loader_test1_classmeth, 'all is well', 'Class method' ); |
507 | } |
78b7ccaa |
508 | |
18e84656 |
509 | ok( $class1->column_info('id')->{is_auto_increment}, 'is_auto_increment detection' ); |
507034ad |
510 | |
3b17d988 |
511 | my $obj = try { $rsobj1->find(1) }; |
512 | |
513 | is( try { $obj->id }, 1, "Find got the right row" ); |
514 | is( try { $obj->dat }, "foo", "Column value" ); |
58dfed76 |
515 | is( $rsobj2->count, 4, "Count" ); |
b71790b1 |
516 | my $saved_id; |
517 | eval { |
18e84656 |
518 | my $new_obj1 = $rsobj1->create({ dat => 'newthing' }); |
996be9ee |
519 | $saved_id = $new_obj1->id; |
b71790b1 |
520 | }; |
58dfed76 |
521 | ok(!$@, "Inserting new record using a PK::Auto key didn't die") or diag $@; |
522 | ok($saved_id, "Got PK::Auto-generated id"); |
b71790b1 |
523 | |
18e84656 |
524 | my $new_obj1 = $rsobj1->search({ dat => 'newthing' })->first; |
58dfed76 |
525 | ok($new_obj1, "Found newly inserted PK::Auto record"); |
526 | is($new_obj1->id, $saved_id, "Correct PK::Auto-generated id"); |
a78e3fed |
527 | |
18e84656 |
528 | my ($obj2) = $rsobj2->search({ dat => 'bbb' })->first; |
a78e3fed |
529 | is( $obj2->id, 2 ); |
530 | |
3b17d988 |
531 | SKIP: { |
532 | skip 'no DEFAULT on Access', 7 if $self->{vendor} eq 'Access'; |
e35e5a64 |
533 | |
3b17d988 |
534 | is( |
535 | $class35->column_info('a_varchar')->{default_value}, 'foo', |
536 | 'constant character default', |
537 | ); |
e35e5a64 |
538 | |
3b17d988 |
539 | is( |
540 | $class35->column_info('an_int')->{default_value}, 42, |
541 | 'constant integer default', |
542 | ); |
1b3e8f7a |
543 | |
3b17d988 |
544 | is( |
545 | $class35->column_info('a_negative_int')->{default_value}, -42, |
546 | 'constant negative integer default', |
547 | ); |
e35e5a64 |
548 | |
740241f7 |
549 | is( |
550 | sprintf("%.3f", $class35->column_info('a_double')->{default_value}), '10.555', |
3b17d988 |
551 | 'constant numeric default', |
552 | ); |
1b3e8f7a |
553 | |
740241f7 |
554 | is( |
555 | sprintf("%.3f", $class35->column_info('a_negative_double')->{default_value}), -10.555, |
3b17d988 |
556 | 'constant negative numeric default', |
557 | ); |
e35e5a64 |
558 | |
3b17d988 |
559 | my $function_default = $class35->column_info('a_function')->{default_value}; |
560 | |
561 | isa_ok( $function_default, 'SCALAR', 'default_value for function default' ); |
562 | is_deeply( |
563 | $function_default, \$self->{default_function}, |
564 | 'default_value for function default is correct' |
565 | ); |
566 | } |
e35e5a64 |
567 | |
a78e3fed |
568 | SKIP: { |
dc379dc6 |
569 | skip $self->{skip_rels}, 124 if $self->{skip_rels}; |
a78e3fed |
570 | |
3980d69c |
571 | my $moniker3 = $monikers->{loader_test3}; |
572 | my $class3 = $classes->{loader_test3}; |
573 | my $rsobj3 = $conn->resultset($moniker3); |
574 | |
575 | my $moniker4 = $monikers->{loader_test4}; |
576 | my $class4 = $classes->{loader_test4}; |
577 | my $rsobj4 = $conn->resultset($moniker4); |
578 | |
579 | my $moniker5 = $monikers->{loader_test5}; |
580 | my $class5 = $classes->{loader_test5}; |
581 | my $rsobj5 = $conn->resultset($moniker5); |
582 | |
583 | my $moniker6 = $monikers->{loader_test6}; |
584 | my $class6 = $classes->{loader_test6}; |
585 | my $rsobj6 = $conn->resultset($moniker6); |
586 | |
587 | my $moniker7 = $monikers->{loader_test7}; |
588 | my $class7 = $classes->{loader_test7}; |
589 | my $rsobj7 = $conn->resultset($moniker7); |
590 | |
591 | my $moniker8 = $monikers->{loader_test8}; |
592 | my $class8 = $classes->{loader_test8}; |
593 | my $rsobj8 = $conn->resultset($moniker8); |
594 | |
595 | my $moniker9 = $monikers->{loader_test9}; |
596 | my $class9 = $classes->{loader_test9}; |
597 | my $rsobj9 = $conn->resultset($moniker9); |
af6c2665 |
598 | |
996be9ee |
599 | my $moniker16 = $monikers->{loader_test16}; |
600 | my $class16 = $classes->{loader_test16}; |
601 | my $rsobj16 = $conn->resultset($moniker16); |
602 | |
603 | my $moniker17 = $monikers->{loader_test17}; |
604 | my $class17 = $classes->{loader_test17}; |
605 | my $rsobj17 = $conn->resultset($moniker17); |
606 | |
607 | my $moniker18 = $monikers->{loader_test18}; |
608 | my $class18 = $classes->{loader_test18}; |
609 | my $rsobj18 = $conn->resultset($moniker18); |
610 | |
611 | my $moniker19 = $monikers->{loader_test19}; |
612 | my $class19 = $classes->{loader_test19}; |
613 | my $rsobj19 = $conn->resultset($moniker19); |
614 | |
615 | my $moniker20 = $monikers->{loader_test20}; |
616 | my $class20 = $classes->{loader_test20}; |
617 | my $rsobj20 = $conn->resultset($moniker20); |
618 | |
619 | my $moniker21 = $monikers->{loader_test21}; |
620 | my $class21 = $classes->{loader_test21}; |
621 | my $rsobj21 = $conn->resultset($moniker21); |
622 | |
623 | my $moniker22 = $monikers->{loader_test22}; |
624 | my $class22 = $classes->{loader_test22}; |
625 | my $rsobj22 = $conn->resultset($moniker22); |
626 | |
25e3e91d |
627 | my $moniker25 = $monikers->{loader_test25}; |
628 | my $class25 = $classes->{loader_test25}; |
629 | my $rsobj25 = $conn->resultset($moniker25); |
630 | |
631 | my $moniker26 = $monikers->{loader_test26}; |
632 | my $class26 = $classes->{loader_test26}; |
633 | my $rsobj26 = $conn->resultset($moniker26); |
634 | |
26f1c8c9 |
635 | my $moniker27 = $monikers->{loader_test27}; |
636 | my $class27 = $classes->{loader_test27}; |
637 | my $rsobj27 = $conn->resultset($moniker27); |
638 | |
639 | my $moniker28 = $monikers->{loader_test28}; |
640 | my $class28 = $classes->{loader_test28}; |
641 | my $rsobj28 = $conn->resultset($moniker28); |
642 | |
643 | my $moniker29 = $monikers->{loader_test29}; |
644 | my $class29 = $classes->{loader_test29}; |
645 | my $rsobj29 = $conn->resultset($moniker29); |
646 | |
7dba7c70 |
647 | my $moniker31 = $monikers->{loader_test31}; |
648 | my $class31 = $classes->{loader_test31}; |
649 | my $rsobj31 = $conn->resultset($moniker31); |
650 | |
651 | my $moniker32 = $monikers->{loader_test32}; |
652 | my $class32 = $classes->{loader_test32}; |
653 | my $rsobj32 = $conn->resultset($moniker32); |
654 | |
655 | my $moniker33 = $monikers->{loader_test33}; |
656 | my $class33 = $classes->{loader_test33}; |
657 | my $rsobj33 = $conn->resultset($moniker33); |
658 | |
659 | my $moniker34 = $monikers->{loader_test34}; |
660 | my $class34 = $classes->{loader_test34}; |
661 | my $rsobj34 = $conn->resultset($moniker34); |
662 | |
42e785fa |
663 | my $moniker36 = $monikers->{loader_test36}; |
664 | my $class36 = $classes->{loader_test36}; |
665 | my $rsobj36 = $conn->resultset($moniker36); |
666 | |
af6c2665 |
667 | isa_ok( $rsobj3, "DBIx::Class::ResultSet" ); |
668 | isa_ok( $rsobj4, "DBIx::Class::ResultSet" ); |
669 | isa_ok( $rsobj5, "DBIx::Class::ResultSet" ); |
670 | isa_ok( $rsobj6, "DBIx::Class::ResultSet" ); |
671 | isa_ok( $rsobj7, "DBIx::Class::ResultSet" ); |
672 | isa_ok( $rsobj8, "DBIx::Class::ResultSet" ); |
673 | isa_ok( $rsobj9, "DBIx::Class::ResultSet" ); |
996be9ee |
674 | isa_ok( $rsobj16, "DBIx::Class::ResultSet" ); |
675 | isa_ok( $rsobj17, "DBIx::Class::ResultSet" ); |
676 | isa_ok( $rsobj18, "DBIx::Class::ResultSet" ); |
677 | isa_ok( $rsobj19, "DBIx::Class::ResultSet" ); |
678 | isa_ok( $rsobj20, "DBIx::Class::ResultSet" ); |
679 | isa_ok( $rsobj21, "DBIx::Class::ResultSet" ); |
680 | isa_ok( $rsobj22, "DBIx::Class::ResultSet" ); |
25e3e91d |
681 | isa_ok( $rsobj25, "DBIx::Class::ResultSet" ); |
682 | isa_ok( $rsobj26, "DBIx::Class::ResultSet" ); |
26f1c8c9 |
683 | isa_ok( $rsobj27, "DBIx::Class::ResultSet" ); |
684 | isa_ok( $rsobj28, "DBIx::Class::ResultSet" ); |
685 | isa_ok( $rsobj29, "DBIx::Class::ResultSet" ); |
7dba7c70 |
686 | isa_ok( $rsobj31, "DBIx::Class::ResultSet" ); |
687 | isa_ok( $rsobj32, "DBIx::Class::ResultSet" ); |
688 | isa_ok( $rsobj33, "DBIx::Class::ResultSet" ); |
689 | isa_ok( $rsobj34, "DBIx::Class::ResultSet" ); |
42e785fa |
690 | isa_ok( $rsobj36, "DBIx::Class::ResultSet" ); |
a78e3fed |
691 | |
692 | # basic rel test |
3b17d988 |
693 | my $obj4 = try { $rsobj4->find(123) } || $rsobj4->search({ id => 123 })->first; |
694 | isa_ok( try { $obj4->fkid_singular }, $class3); |
a78e3fed |
695 | |
a7116285 |
696 | # test renaming rel that conflicts with a class method |
697 | ok ($obj4->has_relationship('belongs_to_rel'), 'relationship name that conflicts with a method renamed'); |
3b17d988 |
698 | |
699 | isa_ok( try { $obj4->belongs_to_rel }, $class3); |
a7116285 |
700 | |
701 | ok ($obj4->has_relationship('caught_rel_collision_set_primary_key'), |
702 | 'relationship name that conflicts with a method renamed based on rel_collision_map'); |
3b17d988 |
703 | isa_ok( try { $obj4->caught_rel_collision_set_primary_key }, $class3); |
a7116285 |
704 | |
18e84656 |
705 | ok($class4->column_info('fkid')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
706 | |
3b17d988 |
707 | my $obj3 = try { $rsobj3->find(1) } || $rsobj3->search({ id => 1 })->first; |
708 | my $rs_rel4 = try { $obj3->search_related('loader_test4zes') }; |
709 | isa_ok( try { $rs_rel4->first }, $class4); |
c5de0b0c |
710 | |
cfc5dce3 |
711 | is( $class4->column_info('crumb_crisp_coating')->{accessor}, 'trivet', |
3fea497f |
712 | 'col_accessor_map is being run' ); |
cfc5dce3 |
713 | |
c496748b |
714 | # check rel naming with prepositions |
2d945eb3 |
715 | ok ($rsobj4->result_source->has_relationship('loader_test5s_to'), |
c496748b |
716 | "rel with preposition 'to' pluralized correctly"); |
717 | |
2d945eb3 |
718 | ok ($rsobj4->result_source->has_relationship('loader_test5s_from'), |
c496748b |
719 | "rel with preposition 'from' pluralized correctly"); |
720 | |
53ef681d |
721 | # check default relationship attributes |
3b17d988 |
722 | is try { $rsobj3->result_source->relationship_info('loader_test4zes')->{attrs}{cascade_delete} }, 0, |
53ef681d |
723 | 'cascade_delete => 0 on has_many by default'; |
724 | |
3b17d988 |
725 | is try { $rsobj3->result_source->relationship_info('loader_test4zes')->{attrs}{cascade_copy} }, 0, |
53ef681d |
726 | 'cascade_copy => 0 on has_many by default'; |
727 | |
3b17d988 |
728 | ok ((not try { exists $rsobj3->result_source->relationship_info('loader_test4zes')->{attrs}{on_delete} }), |
53ef681d |
729 | 'has_many does not have on_delete'); |
730 | |
3b17d988 |
731 | ok ((not try { exists $rsobj3->result_source->relationship_info('loader_test4zes')->{attrs}{on_update} }), |
53ef681d |
732 | 'has_many does not have on_update'); |
733 | |
3b17d988 |
734 | ok ((not try { exists $rsobj3->result_source->relationship_info('loader_test4zes')->{attrs}{is_deferrable} }), |
ee07e280 |
735 | 'has_many does not have is_deferrable'); |
736 | |
3b17d988 |
737 | is try { $rsobj4->result_source->relationship_info('fkid_singular')->{attrs}{on_delete} }, 'CASCADE', |
53ef681d |
738 | "on_delete => 'CASCADE' on belongs_to by default"; |
739 | |
3b17d988 |
740 | is try { $rsobj4->result_source->relationship_info('fkid_singular')->{attrs}{on_update} }, 'CASCADE', |
53ef681d |
741 | "on_update => 'CASCADE' on belongs_to by default"; |
742 | |
3b17d988 |
743 | is try { $rsobj4->result_source->relationship_info('fkid_singular')->{attrs}{is_deferrable} }, 1, |
aa0867ee |
744 | "is_deferrable => 1 on belongs_to by default"; |
ee07e280 |
745 | |
3b17d988 |
746 | ok ((not try { exists $rsobj4->result_source->relationship_info('fkid_singular')->{attrs}{cascade_delete} }), |
53ef681d |
747 | 'belongs_to does not have cascade_delete'); |
748 | |
3b17d988 |
749 | ok ((not try { exists $rsobj4->result_source->relationship_info('fkid_singular')->{attrs}{cascade_copy} }), |
53ef681d |
750 | 'belongs_to does not have cascade_copy'); |
751 | |
3b17d988 |
752 | is try { $rsobj27->result_source->relationship_info('loader_test28')->{attrs}{cascade_delete} }, 0, |
53ef681d |
753 | 'cascade_delete => 0 on might_have by default'; |
754 | |
3b17d988 |
755 | is try { $rsobj27->result_source->relationship_info('loader_test28')->{attrs}{cascade_copy} }, 0, |
53ef681d |
756 | 'cascade_copy => 0 on might_have by default'; |
757 | |
3b17d988 |
758 | ok ((not try { exists $rsobj27->result_source->relationship_info('loader_test28')->{attrs}{on_delete} }), |
53ef681d |
759 | 'might_have does not have on_delete'); |
760 | |
3b17d988 |
761 | ok ((not try { exists $rsobj27->result_source->relationship_info('loader_test28')->{attrs}{on_update} }), |
53ef681d |
762 | 'might_have does not have on_update'); |
763 | |
3b17d988 |
764 | ok ((not try { exists $rsobj27->result_source->relationship_info('loader_test28')->{attrs}{is_deferrable} }), |
ee07e280 |
765 | 'might_have does not have is_deferrable'); |
766 | |
996be9ee |
767 | # find on multi-col pk |
cc4f11a2 |
768 | if ($conn->_loader->preserve_case) { |
769 | my $obj5 = $rsobj5->find({id1 => 1, iD2 => 1}); |
770 | is $obj5->i_d2, 1, 'Find on multi-col PK'; |
771 | } |
772 | else { |
773 | my $obj5 = $rsobj5->find({id1 => 1, id2 => 1}); |
774 | is $obj5->id2, 1, 'Find on multi-col PK'; |
775 | } |
a78e3fed |
776 | |
4ce22656 |
777 | # mulit-col fk def |
3b17d988 |
778 | my $obj6 = try { $rsobj6->find(1) } || $rsobj6->search({ id => 1 })->first; |
779 | isa_ok( try { $obj6->loader_test2 }, $class2); |
780 | isa_ok( try { $obj6->loader_test5 }, $class5); |
a78e3fed |
781 | |
18e84656 |
782 | ok($class6->column_info('loader_test2_id')->{is_foreign_key}, 'Foreign key detected'); |
783 | ok($class6->column_info('id')->{is_foreign_key}, 'Foreign key detected'); |
948d632b |
784 | |
dc379dc6 |
785 | my $id2_info = try { $class6->column_info('id2') } || |
18e84656 |
786 | $class6->column_info('Id2'); |
948d632b |
787 | ok($id2_info->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
788 | |
dc379dc6 |
789 | unlike slurp($conn->_loader->get_dump_filename($class6)), |
790 | qr/\n__PACKAGE__->(?:belongs_to|has_many|might_have|has_one|many_to_many)\( |
791 | \s+ "(\w+?)" |
792 | .*? |
793 | \n__PACKAGE__->(?:belongs_to|has_many|might_have|has_one|many_to_many)\( |
794 | \s+ "\1"/xs, |
795 | 'did not create two relationships with the same name'; |
796 | |
797 | unlike slurp($conn->_loader->get_dump_filename($class8)), |
798 | qr/\n__PACKAGE__->(?:belongs_to|has_many|might_have|has_one|many_to_many)\( |
799 | \s+ "(\w+?)" |
800 | .*? |
801 | \n__PACKAGE__->(?:belongs_to|has_many|might_have|has_one|many_to_many)\( |
802 | \s+ "\1"/xs, |
803 | 'did not create two relationships with the same name'; |
804 | |
805 | # check naming of ambiguous relationships |
806 | my $rel_info = $class6->relationship_info('lovely_loader_test7') || {}; |
807 | |
808 | ok (($class6->has_relationship('lovely_loader_test7') |
809 | && $rel_info->{cond}{'foreign.lovely_loader_test6'} eq 'self.id' |
810 | && $rel_info->{class} eq $class7 |
811 | && $rel_info->{attrs}{accessor} eq 'single'), |
812 | 'ambiguous relationship named correctly'); |
813 | |
814 | $rel_info = $class8->relationship_info('active_loader_test16') || {}; |
815 | |
816 | ok (($class8->has_relationship('active_loader_test16') |
817 | && $rel_info->{cond}{'foreign.loader_test8_id'} eq 'self.id' |
818 | && $rel_info->{class} eq $class16 |
819 | && $rel_info->{attrs}{accessor} eq 'single'), |
820 | 'ambiguous relationship named correctly'); |
821 | |
a78e3fed |
822 | # fk that references a non-pk key (UNIQUE) |
3b17d988 |
823 | my $obj8 = try { $rsobj8->find(1) } || $rsobj8->search({ id => 1 })->first; |
824 | isa_ok( try { $obj8->loader_test7 }, $class7); |
a78e3fed |
825 | |
18e84656 |
826 | ok($class8->column_info('loader_test7')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
827 | |
996be9ee |
828 | # test double-fk 17 ->-> 16 |
3b17d988 |
829 | my $obj17 = try { $rsobj17->find(33) } || $rsobj17->search({ id => 33 })->first; |
996be9ee |
830 | |
3b17d988 |
831 | my $rs_rel16_one = try { $obj17->loader16_one }; |
996be9ee |
832 | isa_ok($rs_rel16_one, $class16); |
3b17d988 |
833 | is(try { $rs_rel16_one->dat }, 'y16', "Multiple FKs to same table"); |
996be9ee |
834 | |
18e84656 |
835 | ok($class17->column_info('loader16_one')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
836 | |
3b17d988 |
837 | my $rs_rel16_two = try { $obj17->loader16_two }; |
996be9ee |
838 | isa_ok($rs_rel16_two, $class16); |
3b17d988 |
839 | is(try { $rs_rel16_two->dat }, 'z16', "Multiple FKs to same table"); |
996be9ee |
840 | |
18e84656 |
841 | ok($class17->column_info('loader16_two')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
842 | |
3b17d988 |
843 | my $obj16 = try { $rsobj16->find(2) } || $rsobj16->search({ id => 2 })->first; |
844 | my $rs_rel17 = try { $obj16->search_related('loader_test17_loader16_ones') }; |
845 | isa_ok(try { $rs_rel17->first }, $class17); |
846 | is(try { $rs_rel17->first->id }, 3, "search_related with multiple FKs from same table"); |
996be9ee |
847 | |
848 | # XXX test m:m 18 <- 20 -> 19 |
18e84656 |
849 | ok($class20->column_info('parent')->{is_foreign_key}, 'Foreign key detected'); |
850 | ok($class20->column_info('child')->{is_foreign_key}, 'Foreign key detected'); |
996be9ee |
851 | |
852 | # XXX test double-fk m:m 21 <- 22 -> 21 |
18e84656 |
853 | ok($class22->column_info('parent')->{is_foreign_key}, 'Foreign key detected'); |
854 | ok($class22->column_info('child')->{is_foreign_key}, 'Foreign key detected'); |
996be9ee |
855 | |
25e3e91d |
856 | # test double multi-col fk 26 -> 25 |
3b17d988 |
857 | my $obj26 = try { $rsobj26->find(33) } || $rsobj26->search({ id => 33 })->first; |
25e3e91d |
858 | |
3b17d988 |
859 | my $rs_rel25_one = try { $obj26->loader_test25_id_rel1 }; |
25e3e91d |
860 | isa_ok($rs_rel25_one, $class25); |
3b17d988 |
861 | is(try { $rs_rel25_one->dat }, 'x25', "Multiple multi-col FKs to same table"); |
25e3e91d |
862 | |
18e84656 |
863 | ok($class26->column_info('id')->{is_foreign_key}, 'Foreign key detected'); |
864 | ok($class26->column_info('rel1')->{is_foreign_key}, 'Foreign key detected'); |
865 | ok($class26->column_info('rel2')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
866 | |
3b17d988 |
867 | my $rs_rel25_two = try { $obj26->loader_test25_id_rel2 }; |
25e3e91d |
868 | isa_ok($rs_rel25_two, $class25); |
3b17d988 |
869 | is(try { $rs_rel25_two->dat }, 'y25', "Multiple multi-col FKs to same table"); |
25e3e91d |
870 | |
3b17d988 |
871 | my $obj25 = try { $rsobj25->find(3,42) } || $rsobj25->search({ id1 => 3, id2 => 42 })->first; |
872 | my $rs_rel26 = try { $obj25->search_related('loader_test26_id_rel1s') }; |
873 | isa_ok(try { $rs_rel26->first }, $class26); |
874 | is(try { $rs_rel26->first->id }, 3, "search_related with multiple multi-col FKs from same table"); |
25e3e91d |
875 | |
26f1c8c9 |
876 | # test one-to-one rels |
3b17d988 |
877 | my $obj27 = try { $rsobj27->find(1) } || $rsobj27->search({ id => 1 })->first; |
878 | my $obj28 = try { $obj27->loader_test28 }; |
26f1c8c9 |
879 | isa_ok($obj28, $class28); |
3b17d988 |
880 | is(try { $obj28->get_column('id') }, 1, "One-to-one relationship with PRIMARY FK"); |
26f1c8c9 |
881 | |
18e84656 |
882 | ok($class28->column_info('id')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
883 | |
3b17d988 |
884 | my $obj29 = try { $obj27->loader_test29 }; |
26f1c8c9 |
885 | isa_ok($obj29, $class29); |
3b17d988 |
886 | is(try { $obj29->id }, 1, "One-to-one relationship with UNIQUE FK"); |
26f1c8c9 |
887 | |
18e84656 |
888 | ok($class29->column_info('fk')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
889 | |
3b17d988 |
890 | $obj27 = try { $rsobj27->find(2) } || $rsobj27->search({ id => 2 })->first; |
891 | is(try { $obj27->loader_test28 }, undef, "Undef for missing one-to-one row"); |
892 | is(try { $obj27->loader_test29 }, undef, "Undef for missing one-to-one row"); |
26f1c8c9 |
893 | |
7dba7c70 |
894 | # test outer join for nullable referring columns: |
18e84656 |
895 | is $class32->column_info('rel2')->{is_nullable}, 1, |
0fa48bf5 |
896 | 'is_nullable detection'; |
897 | |
18e84656 |
898 | ok($class32->column_info('rel1')->{is_foreign_key}, 'Foreign key detected'); |
899 | ok($class32->column_info('rel2')->{is_foreign_key}, 'Foreign key detected'); |
0fa48bf5 |
900 | |
3b17d988 |
901 | my $obj32 = try { $rsobj32->find(1, { prefetch => [qw/rel1 rel2/] }) } |
902 | || try { $rsobj32->search({ id => 1 }, { prefetch => [qw/rel1 rel2/] })->first } |
903 | || $rsobj32->search({ id => 1 })->first; |
904 | |
905 | my $obj34 = eval { $rsobj34->find(1, { prefetch => [qw/loader_test33_id_rel1 loader_test33_id_rel2/] }) } |
906 | || eval { $rsobj34->search({ id => 1 }, { prefetch => [qw/loader_test33_id_rel1 loader_test33_id_rel2/] })->first } |
907 | || $rsobj34->search({ id => 1 })->first; |
908 | diag $@ if $@; |
909 | |
0fa48bf5 |
910 | isa_ok($obj32,$class32); |
911 | isa_ok($obj34,$class34); |
912 | |
18e84656 |
913 | ok($class34->column_info('id')->{is_foreign_key}, 'Foreign key detected'); |
914 | ok($class34->column_info('rel1')->{is_foreign_key}, 'Foreign key detected'); |
915 | ok($class34->column_info('rel2')->{is_foreign_key}, 'Foreign key detected'); |
0fa48bf5 |
916 | |
3b17d988 |
917 | my $rs_rel31_one = try { $obj32->rel1 }; |
918 | my $rs_rel31_two = try { $obj32->rel2 }; |
0fa48bf5 |
919 | isa_ok($rs_rel31_one, $class31); |
920 | is($rs_rel31_two, undef); |
921 | |
3b17d988 |
922 | my $rs_rel33_one = try { $obj34->loader_test33_id_rel1 }; |
923 | my $rs_rel33_two = try { $obj34->loader_test33_id_rel2 }; |
0fa48bf5 |
924 | |
3b17d988 |
925 | isa_ok($rs_rel33_one, $class33); |
926 | isa_ok($rs_rel33_two, $class33); |
7dba7c70 |
927 | |
a78e3fed |
928 | # from Chisel's tests... |
7fc16540 |
929 | my $moniker10 = $monikers->{loader_test10}; |
930 | my $class10 = $classes->{loader_test10}; |
931 | my $rsobj10 = $conn->resultset($moniker10); |
3980d69c |
932 | |
7fc16540 |
933 | my $moniker11 = $monikers->{loader_test11}; |
934 | my $class11 = $classes->{loader_test11}; |
935 | my $rsobj11 = $conn->resultset($moniker11); |
a78e3fed |
936 | |
a51d3566 |
937 | isa_ok( $rsobj10, "DBIx::Class::ResultSet" ); |
7fc16540 |
938 | isa_ok( $rsobj11, "DBIx::Class::ResultSet" ); |
a78e3fed |
939 | |
18e84656 |
940 | ok($class10->column_info('loader_test11')->{is_foreign_key}, 'Foreign key detected'); |
941 | ok($class11->column_info('loader_test10')->{is_foreign_key}, 'Foreign key detected'); |
d67d058e |
942 | |
18e84656 |
943 | my $obj10 = $rsobj10->create({ subject => 'xyzzy' }); |
a78e3fed |
944 | |
7fc16540 |
945 | $obj10->update(); |
946 | ok( defined $obj10, 'Create row' ); |
a78e3fed |
947 | |
3b17d988 |
948 | my $obj11 = $rsobj11->create({ loader_test10 => (try { $obj10->id() } || $obj10->id10) }); |
7fc16540 |
949 | $obj11->update(); |
950 | ok( defined $obj11, 'Create related row' ); |
a78e3fed |
951 | |
7fc16540 |
952 | eval { |
953 | my $obj10_2 = $obj11->loader_test10; |
18e84656 |
954 | $obj10_2->update({ loader_test11 => $obj11->id11 }); |
7fc16540 |
955 | }; |
243c6ebc |
956 | diag $@ if $@; |
7fc16540 |
957 | ok(!$@, "Setting up circular relationship"); |
a78e3fed |
958 | |
7fc16540 |
959 | SKIP: { |
960 | skip 'Previous eval block failed', 3 if $@; |
961 | |
18e84656 |
962 | my $results = $rsobj10->search({ subject => 'xyzzy' }); |
7fc16540 |
963 | is( $results->count(), 1, 'No duplicate row created' ); |
a78e3fed |
964 | |
7fc16540 |
965 | my $obj10_3 = $results->first(); |
966 | isa_ok( $obj10_3, $class10 ); |
967 | is( $obj10_3->loader_test11()->id(), $obj11->id(), |
968 | 'Circular rel leads back to same row' ); |
a78e3fed |
969 | } |
4ce22656 |
970 | |
971 | SKIP: { |
82617880 |
972 | skip 'This vendor cannot do inline relationship definitions', 11 |
4ce22656 |
973 | if $self->{no_inline_rels}; |
974 | |
3980d69c |
975 | my $moniker12 = $monikers->{loader_test12}; |
976 | my $class12 = $classes->{loader_test12}; |
977 | my $rsobj12 = $conn->resultset($moniker12); |
978 | |
979 | my $moniker13 = $monikers->{loader_test13}; |
980 | my $class13 = $classes->{loader_test13}; |
981 | my $rsobj13 = $conn->resultset($moniker13); |
4ce22656 |
982 | |
983 | isa_ok( $rsobj12, "DBIx::Class::ResultSet" ); |
984 | isa_ok( $rsobj13, "DBIx::Class::ResultSet" ); |
985 | |
18e84656 |
986 | ok($class13->column_info('id')->{is_foreign_key}, 'Foreign key detected'); |
987 | ok($class13->column_info('loader_test12')->{is_foreign_key}, 'Foreign key detected'); |
988 | ok($class13->column_info('dat')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
989 | |
3b17d988 |
990 | my $obj13 = try { $rsobj13->find(1) } || $rsobj13->search({ id => 1 })->first; |
3980d69c |
991 | isa_ok( $obj13->id, $class12 ); |
992 | isa_ok( $obj13->loader_test12, $class12); |
993 | isa_ok( $obj13->dat, $class12); |
d13aecb1 |
994 | |
3b17d988 |
995 | my $obj12 = try { $rsobj12->find(1) } || $rsobj12->search({ id => 1 })->first; |
996 | isa_ok( try { $obj12->loader_test13 }, $class13 ); |
82617880 |
997 | |
998 | # relname is preserved when another fk is added |
999 | |
82617880 |
1000 | { |
1ad8e8c3 |
1001 | local $SIG{__WARN__} = sub { warn @_ unless $_[0] =~ /invalidates \d+ active statement/ }; |
3b17d988 |
1002 | $conn->storage->disconnect; # for mssql and access |
82617880 |
1003 | } |
1004 | |
3b17d988 |
1005 | isa_ok try { $rsobj3->find(1)->loader_test4zes }, 'DBIx::Class::ResultSet'; |
1006 | |
1007 | $conn->storage->disconnect; # for access |
1ad8e8c3 |
1008 | |
dc379dc6 |
1009 | if (lc($self->{vendor}) ne 'sybase') { |
1010 | $conn->storage->dbh->do('ALTER TABLE loader_test4 ADD fkid2 INTEGER REFERENCES loader_test3 (id)'); |
1011 | } |
1012 | else { |
1013 | $conn->storage->dbh->do(<<"EOF"); |
1014 | ALTER TABLE loader_test4 ADD fkid2 INTEGER $self->{null} |
1015 | ALTER TABLE loader_test4 ADD CONSTRAINT loader_test4_to_3_fk FOREIGN KEY (fkid2) REFERENCES loader_test3 (id) |
1016 | EOF |
1017 | } |
1ad8e8c3 |
1018 | |
1019 | $conn->storage->disconnect; # for firebird |
1020 | |
1021 | $self->rescan_without_warnings($conn); |
1022 | |
3b17d988 |
1023 | isa_ok try { $rsobj3->find(1)->loader_test4zes }, 'DBIx::Class::ResultSet', |
82617880 |
1024 | 'relationship name preserved when another foreign key is added in remote table'; |
4ce22656 |
1025 | } |
52bf3f26 |
1026 | |
1027 | SKIP: { |
e7213f4f |
1028 | skip 'This vendor cannot do out-of-line implicit rel defs', 4 |
52bf3f26 |
1029 | if $self->{no_implicit_rels}; |
1030 | my $moniker14 = $monikers->{loader_test14}; |
1031 | my $class14 = $classes->{loader_test14}; |
1032 | my $rsobj14 = $conn->resultset($moniker14); |
1033 | |
1034 | my $moniker15 = $monikers->{loader_test15}; |
1035 | my $class15 = $classes->{loader_test15}; |
1036 | my $rsobj15 = $conn->resultset($moniker15); |
1037 | |
1038 | isa_ok( $rsobj14, "DBIx::Class::ResultSet" ); |
1039 | isa_ok( $rsobj15, "DBIx::Class::ResultSet" ); |
1040 | |
18e84656 |
1041 | ok($class15->column_info('loader_test14')->{is_foreign_key}, 'Foreign key detected'); |
e7213f4f |
1042 | |
3b17d988 |
1043 | my $obj15 = try { $rsobj15->find(1) } || $rsobj15->search({ id => 1 })->first; |
52bf3f26 |
1044 | isa_ok( $obj15->loader_test14, $class14 ); |
1045 | } |
3b7f80f9 |
1046 | } |
a60b5b8d |
1047 | |
c5df7397 |
1048 | # test custom_column_info and datetime_timezone/datetime_locale |
1049 | { |
1050 | my $class35 = $classes->{loader_test35}; |
1051 | my $class36 = $classes->{loader_test36}; |
1052 | |
18e84656 |
1053 | ok($class35->column_info('an_int')->{is_numeric}, 'custom_column_info'); |
c5df7397 |
1054 | |
18e84656 |
1055 | is($class36->column_info('a_date')->{locale},'de_DE','datetime_locale'); |
1056 | is($class36->column_info('a_date')->{timezone},'Europe/Berlin','datetime_timezone'); |
c5df7397 |
1057 | |
18e84656 |
1058 | ok($class36->column_info('b_char_as_data')->{inflate_datetime},'custom_column_info'); |
1059 | is($class36->column_info('b_char_as_data')->{locale},'de_DE','datetime_locale'); |
1060 | is($class36->column_info('b_char_as_data')->{timezone},'Europe/Berlin','datetime_timezone'); |
c5df7397 |
1061 | |
18e84656 |
1062 | ok($class36->column_info('c_char_as_data')->{inflate_date},'custom_column_info'); |
1063 | is($class36->column_info('c_char_as_data')->{locale},'de_DE','datetime_locale'); |
1064 | is($class36->column_info('c_char_as_data')->{timezone},'Europe/Berlin','datetime_timezone'); |
c5df7397 |
1065 | } |
1066 | |
4daef04f |
1067 | # rescan and norewrite test |
0c1d5b47 |
1068 | { |
3b7f80f9 |
1069 | my @statements_rescan = ( |
1070 | qq{ |
657d4b58 |
1071 | CREATE TABLE loader_test30 ( |
3b7f80f9 |
1072 | id INTEGER NOT NULL PRIMARY KEY, |
1073 | loader_test2 INTEGER NOT NULL, |
1074 | FOREIGN KEY (loader_test2) REFERENCES loader_test2 (id) |
1075 | ) $self->{innodb} |
1076 | }, |
657d4b58 |
1077 | q{ INSERT INTO loader_test30 (id,loader_test2) VALUES(123, 1) }, |
1078 | q{ INSERT INTO loader_test30 (id,loader_test2) VALUES(321, 2) }, |
3b7f80f9 |
1079 | ); |
1080 | |
4daef04f |
1081 | # get md5 |
1082 | my $digest = Digest::MD5->new; |
1083 | |
1084 | my $find_cb = sub { |
1085 | return if -d; |
b511f36e |
1086 | return if /^(?:LoaderTest30|LoaderTest1|LoaderTest2X)\.pm\z/; |
4daef04f |
1087 | |
1088 | open my $fh, '<', $_ or die "Could not open $_ for reading: $!"; |
1089 | binmode $fh; |
1090 | $digest->addfile($fh); |
1091 | }; |
1092 | |
1093 | find $find_cb, $DUMP_DIR; |
1094 | |
b511f36e |
1095 | # system "rm -f /tmp/before_rescan/* /tmp/after_rescan/*"; |
c213fd3d |
1096 | # system "cp $tdir/common_dump/DBIXCSL_Test/Schema/*.pm /tmp/before_rescan"; |
b511f36e |
1097 | |
fe8ba914 |
1098 | my $before_digest = $digest->b64digest; |
4daef04f |
1099 | |
bc5afe55 |
1100 | $conn->storage->disconnect; # needed for Firebird and Informix |
3b7f80f9 |
1101 | my $dbh = $self->dbconnect(1); |
c38ec663 |
1102 | $dbh->do($_) for @statements_rescan; |
3b7f80f9 |
1103 | $dbh->disconnect; |
1104 | |
4daef04f |
1105 | sleep 1; |
1106 | |
1ad8e8c3 |
1107 | my @new = $self->rescan_without_warnings($conn); |
1108 | |
892b43b6 |
1109 | is_deeply(\@new, [ qw/LoaderTest30/ ], "Rescan"); |
3b7f80f9 |
1110 | |
b511f36e |
1111 | # system "cp t/_common_dump/DBIXCSL_Test/Schema/*.pm /tmp/after_rescan"; |
1112 | |
4daef04f |
1113 | $digest = Digest::MD5->new; |
1114 | find $find_cb, $DUMP_DIR; |
fe8ba914 |
1115 | my $after_digest = $digest->b64digest; |
4daef04f |
1116 | |
1117 | is $before_digest, $after_digest, |
1118 | 'dumped files are not rewritten when there is no modification'; |
1119 | |
657d4b58 |
1120 | my $rsobj30 = $conn->resultset('LoaderTest30'); |
1121 | isa_ok($rsobj30, 'DBIx::Class::ResultSet'); |
4daef04f |
1122 | |
0c1d5b47 |
1123 | SKIP: { |
1124 | skip 'no rels', 2 if $self->{skip_rels}; |
1125 | |
3b17d988 |
1126 | my $obj30 = try { $rsobj30->find(123) } || $rsobj30->search({ id => 123 })->first; |
0c1d5b47 |
1127 | isa_ok( $obj30->loader_test2, $class2); |
1128 | |
1129 | ok($rsobj30->result_source->column_info('loader_test2')->{is_foreign_key}, |
1130 | 'Foreign key detected'); |
1131 | } |
1132 | |
1133 | $conn->storage->disconnect; # for Firebird |
dc379dc6 |
1134 | $self->drop_table($conn->storage->dbh, 'loader_test30'); |
4daef04f |
1135 | |
1ad8e8c3 |
1136 | @new = $self->rescan_without_warnings($conn); |
1137 | |
0c1d5b47 |
1138 | is_deeply(\@new, [], 'no new tables on rescan'); |
e7213f4f |
1139 | |
0c1d5b47 |
1140 | throws_ok { $conn->resultset('LoaderTest30') } |
1141 | qr/Can't find source/, |
1142 | 'source unregistered for dropped table after rescan'; |
a78e3fed |
1143 | } |
df637f3d |
1144 | |
1805763b |
1145 | $self->test_data_types($conn); |
1146 | |
1147 | # run extra tests |
1ad8e8c3 |
1148 | $self->{extra}{run}->($conn, $monikers, $classes, $self) if $self->{extra}{run}; |
1805763b |
1149 | |
b511f36e |
1150 | $self->test_preserve_case($conn); |
1151 | |
1805763b |
1152 | $self->drop_tables unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}; |
1153 | |
1154 | $conn->storage->disconnect; |
1155 | } |
1156 | |
1157 | sub test_data_types { |
1158 | my ($self, $conn) = @_; |
1159 | |
b48f3f80 |
1160 | SKIP: { |
1161 | if (my $test_count = $self->{data_type_tests}{test_count}) { |
1162 | if ($self->{vendor} eq 'mssql' && $conn->storage->dbh->{Driver}{Name} eq 'Sybase') { |
1163 | skip 'DBD::Sybase does not work with the data_type tests on latest SQL Server', $test_count; |
1164 | } |
1165 | |
1166 | my $data_type_tests = $self->{data_type_tests}; |
9065de34 |
1167 | |
b48f3f80 |
1168 | foreach my $moniker (@{ $data_type_tests->{table_monikers} }) { |
1169 | my $columns = $data_type_tests->{columns}{$moniker}; |
9065de34 |
1170 | |
b48f3f80 |
1171 | my $rsrc = $conn->resultset($moniker)->result_source; |
df956aad |
1172 | |
b48f3f80 |
1173 | while (my ($col_name, $expected_info) = each %$columns) { |
1174 | my %info = %{ $rsrc->column_info($col_name) }; |
1175 | delete @info{qw/is_nullable timezone locale sequence/}; |
df956aad |
1176 | |
b48f3f80 |
1177 | my $text_col_def = dumper_squashed \%info; |
15efd63a |
1178 | |
b48f3f80 |
1179 | my $text_expected_info = dumper_squashed $expected_info; |
df956aad |
1180 | |
b48f3f80 |
1181 | is_deeply \%info, $expected_info, |
1182 | "test column $col_name has definition: $text_col_def expecting: $text_expected_info"; |
1183 | } |
3b29d0ac |
1184 | } |
9065de34 |
1185 | } |
1186 | } |
00805149 |
1187 | } |
1188 | |
b511f36e |
1189 | sub test_preserve_case { |
1190 | my ($self, $conn) = @_; |
1191 | |
1192 | my ($oqt, $cqt) = $self->get_oqt_cqt(always => 1); # open quote, close quote |
1193 | |
c38ec663 |
1194 | my $dbh = $self->dbconnect; |
b511f36e |
1195 | |
fa3bad42 |
1196 | my ($table40_name, $table41_name) = @{ $self->{preserve_case_tests_table_names} }; |
1197 | |
c38ec663 |
1198 | $dbh->do($_) for ( |
b511f36e |
1199 | qq| |
fa3bad42 |
1200 | CREATE TABLE ${oqt}${table40_name}${cqt} ( |
b511f36e |
1201 | ${oqt}Id${cqt} INTEGER NOT NULL PRIMARY KEY, |
1202 | ${oqt}Foo3Bar${cqt} VARCHAR(100) NOT NULL |
1203 | ) $self->{innodb} |
1204 | |, |
1205 | qq| |
fa3bad42 |
1206 | CREATE TABLE ${oqt}${table41_name}${cqt} ( |
b511f36e |
1207 | ${oqt}Id${cqt} INTEGER NOT NULL PRIMARY KEY, |
1208 | ${oqt}LoaderTest40Id${cqt} INTEGER, |
fa3bad42 |
1209 | FOREIGN KEY (${oqt}LoaderTest40Id${cqt}) REFERENCES ${oqt}${table40_name}${cqt} (${oqt}Id${cqt}) |
b511f36e |
1210 | ) $self->{innodb} |
1211 | |, |
fa3bad42 |
1212 | qq| INSERT INTO ${oqt}${table40_name}${cqt} VALUES (1, 'foo') |, |
1213 | qq| INSERT INTO ${oqt}${table41_name}${cqt} VALUES (1, 1) |, |
c38ec663 |
1214 | ); |
b511f36e |
1215 | $conn->storage->disconnect; |
1216 | |
1217 | local $conn->_loader->{preserve_case} = 1; |
1218 | $conn->_loader->_setup; |
1219 | |
1ad8e8c3 |
1220 | $self->rescan_without_warnings($conn); |
b511f36e |
1221 | |
1222 | if (not $self->{skip_rels}) { |
c0767caf |
1223 | ok my $row = try { $conn->resultset('LoaderTest41')->find(1) }, |
1224 | 'row in mixed-case table'; |
1225 | ok my $related_row = try { $row->loader_test40 }, |
1226 | 'rel in mixed-case table'; |
1227 | is try { $related_row->foo3_bar }, 'foo', |
1228 | 'accessor for mixed-case column name in mixed case table'; |
b511f36e |
1229 | } |
1230 | else { |
c0767caf |
1231 | SKIP: { skip 'not testing mixed-case rels with skip_rels', 2 } |
1232 | |
3b17d988 |
1233 | is try { $conn->resultset('LoaderTest40')->find(1)->foo3_bar }, 'foo', |
b511f36e |
1234 | 'accessor for mixed-case column name in mixed case table'; |
1235 | } |
1236 | } |
1237 | |
00805149 |
1238 | sub monikers_and_classes { |
1239 | my ($self, $schema_class) = @_; |
1240 | my ($monikers, $classes); |
1241 | |
1242 | foreach my $source_name ($schema_class->sources) { |
1243 | my $table_name = $schema_class->source($source_name)->from; |
1244 | |
1245 | $table_name = $$table_name if ref $table_name; |
1246 | |
1247 | $monikers->{$table_name} = $source_name; |
1248 | $classes->{$table_name} = $schema_class . q{::} . $source_name; |
1249 | |
1250 | # some DBs (Firebird) uppercase everything |
1251 | $monikers->{lc $table_name} = $source_name; |
1252 | $classes->{lc $table_name} = $schema_class . q{::} . $source_name; |
1253 | } |
1254 | |
1255 | return ($monikers, $classes); |
a78e3fed |
1256 | } |
1257 | |
d70c335f |
1258 | sub check_no_duplicate_unique_constraints { |
1259 | my ($class) = @_; |
1260 | |
1261 | # unique_constraints() automatically includes the PK, if any |
1262 | my %uc_cols; |
1263 | ++$uc_cols{ join ", ", @$_ } |
1264 | for values %{ { $class->unique_constraints } }; |
1265 | my $dup_uc = grep { $_ > 1 } values %uc_cols; |
1266 | |
1267 | is($dup_uc, 0, "duplicate unique constraints ($class)") |
1268 | or diag "uc_cols: @{[ %uc_cols ]}"; |
1269 | } |
1270 | |
a78e3fed |
1271 | sub dbconnect { |
1272 | my ($self, $complain) = @_; |
1273 | |
c0084472 |
1274 | require DBIx::Class::Storage::DBI; |
1275 | my $storage = DBIx::Class::Storage::DBI->new; |
1276 | |
1277 | $complain = defined $complain ? $complain : 1; |
1278 | |
1279 | $storage->connect_info([ |
1280 | @{ $self }{qw/dsn user password/}, |
1281 | { |
1282 | unsafe => 1, |
1283 | RaiseError => $complain, |
1284 | ShowErrorStatement => $complain, |
1285 | PrintError => 0, |
3a89a69f |
1286 | %{ $self->{connect_info_opts} || {} }, |
c0084472 |
1287 | }, |
1288 | ]); |
1289 | |
3b17d988 |
1290 | my $dbh = $storage->dbh; |
c0084472 |
1291 | die "Failed to connect to database: $@" if !$dbh; |
af96f52e |
1292 | |
c0084472 |
1293 | $self->{storage} = $storage; # storage DESTROY disconnects |
af96f52e |
1294 | |
1295 | return $dbh; |
a78e3fed |
1296 | } |
1297 | |
b511f36e |
1298 | sub get_oqt_cqt { |
1299 | my $self = shift; |
1300 | my %opts = @_; |
1301 | |
1302 | if ((not $opts{always}) && $self->{preserve_case_mode_is_exclusive}) { |
1303 | return ('', ''); |
1304 | } |
1305 | |
1306 | # XXX should get quote_char from the storage of an initialized loader. |
1307 | my ($oqt, $cqt); # open quote, close quote |
1308 | if (ref $self->{quote_char}) { |
1309 | ($oqt, $cqt) = @{ $self->{quote_char} }; |
1310 | } |
1311 | else { |
1312 | $oqt = $cqt = $self->{quote_char} || ''; |
1313 | } |
1314 | |
1315 | return ($oqt, $cqt); |
1316 | } |
1317 | |
a78e3fed |
1318 | sub create { |
1319 | my $self = shift; |
1320 | |
996be9ee |
1321 | $self->{_created} = 1; |
1322 | |
c4a49993 |
1323 | $self->drop_tables; |
1324 | |
c1ac681d |
1325 | my $make_auto_inc = $self->{auto_inc_cb} || sub {}; |
fdd8ff16 |
1326 | @statements = ( |
a78e3fed |
1327 | qq{ |
22270947 |
1328 | CREATE TABLE loader_test1s ( |
a78e3fed |
1329 | id $self->{auto_inc_pk}, |
996be9ee |
1330 | dat VARCHAR(32) NOT NULL UNIQUE |
66742793 |
1331 | ) $self->{innodb} |
a78e3fed |
1332 | }, |
22270947 |
1333 | $make_auto_inc->(qw/loader_test1s id/), |
a78e3fed |
1334 | |
22270947 |
1335 | q{ INSERT INTO loader_test1s (dat) VALUES('foo') }, |
1336 | q{ INSERT INTO loader_test1s (dat) VALUES('bar') }, |
1337 | q{ INSERT INTO loader_test1s (dat) VALUES('baz') }, |
a78e3fed |
1338 | |
9fdf3d5b |
1339 | # also test method collision |
a78e3fed |
1340 | qq{ |
1341 | CREATE TABLE loader_test2 ( |
1342 | id $self->{auto_inc_pk}, |
996be9ee |
1343 | dat VARCHAR(32) NOT NULL, |
e35e5a64 |
1344 | dat2 VARCHAR(32) NOT NULL, |
c9486c78 |
1345 | set_primary_key INTEGER $self->{null}, |
6947b5d6 |
1346 | can INTEGER $self->{null}, |
c9486c78 |
1347 | dbix_class_testcomponent INTEGER $self->{null}, |
dc379dc6 |
1348 | dbix_class_testcomponentmap INTEGER $self->{null}, |
4a67befc |
1349 | testcomponent_fqn INTEGER $self->{null}, |
2b74a06b |
1350 | meta INTEGER $self->{null}, |
d36c8734 |
1351 | test_role_method INTEGER $self->{null}, |
1352 | test_role_for_map_method INTEGER $self->{null}, |
5223f24a |
1353 | UNIQUE (dat2, dat) |
996be9ee |
1354 | ) $self->{innodb} |
1355 | }, |
c1ac681d |
1356 | $make_auto_inc->(qw/loader_test2 id/), |
996be9ee |
1357 | |
1358 | q{ INSERT INTO loader_test2 (dat, dat2) VALUES('aaa', 'zzz') }, |
1359 | q{ INSERT INTO loader_test2 (dat, dat2) VALUES('bbb', 'yyy') }, |
1360 | q{ INSERT INTO loader_test2 (dat, dat2) VALUES('ccc', 'xxx') }, |
1361 | q{ INSERT INTO loader_test2 (dat, dat2) VALUES('ddd', 'www') }, |
1362 | |
1363 | qq{ |
a1cec524 |
1364 | CREATE TABLE LOADER_test23 ( |
996be9ee |
1365 | ID INTEGER NOT NULL PRIMARY KEY, |
1366 | DAT VARCHAR(32) NOT NULL UNIQUE |
66742793 |
1367 | ) $self->{innodb} |
a78e3fed |
1368 | }, |
1369 | |
996be9ee |
1370 | qq{ |
1371 | CREATE TABLE LoAdEr_test24 ( |
1372 | iD INTEGER NOT NULL PRIMARY KEY, |
1373 | DaT VARCHAR(32) NOT NULL UNIQUE |
1374 | ) $self->{innodb} |
1375 | }, |
e35e5a64 |
1376 | |
3b17d988 |
1377 | # Access does not support DEFAULT |
1378 | $self->{vendor} ne 'Access' ? qq{ |
e35e5a64 |
1379 | CREATE TABLE loader_test35 ( |
41968729 |
1380 | id INTEGER NOT NULL PRIMARY KEY, |
e35e5a64 |
1381 | a_varchar VARCHAR(100) DEFAULT 'foo', |
1382 | an_int INTEGER DEFAULT 42, |
1b3e8f7a |
1383 | a_negative_int INTEGER DEFAULT -42, |
e35e5a64 |
1384 | a_double DOUBLE PRECISION DEFAULT 10.555, |
1b3e8f7a |
1385 | a_negative_double DOUBLE PRECISION DEFAULT -10.555, |
e35e5a64 |
1386 | a_function $self->{default_function_def} |
1387 | ) $self->{innodb} |
3b17d988 |
1388 | } : qq{ |
1389 | CREATE TABLE loader_test35 ( |
1390 | id INTEGER NOT NULL PRIMARY KEY, |
1391 | a_varchar VARCHAR(100), |
1392 | an_int INTEGER, |
1393 | a_negative_int INTEGER, |
1394 | a_double DOUBLE, |
1395 | a_negative_double DOUBLE, |
1396 | a_function DATETIME |
1397 | ) |
e35e5a64 |
1398 | }, |
42e785fa |
1399 | |
1400 | qq{ |
1401 | CREATE TABLE loader_test36 ( |
1402 | id INTEGER NOT NULL PRIMARY KEY, |
8dcf4292 |
1403 | a_date $self->{basic_date_datatype}, |
4f18b8b2 |
1404 | b_char_as_data VARCHAR(100), |
1405 | c_char_as_data VARCHAR(100) |
42e785fa |
1406 | ) $self->{innodb} |
1407 | }, |
a78e3fed |
1408 | ); |
1409 | |
c930f78b |
1410 | # some DBs require mixed case identifiers to be quoted |
b511f36e |
1411 | my ($oqt, $cqt) = $self->get_oqt_cqt; |
c930f78b |
1412 | |
fdd8ff16 |
1413 | @statements_reltests = ( |
a78e3fed |
1414 | qq{ |
1415 | CREATE TABLE loader_test3 ( |
1416 | id INTEGER NOT NULL PRIMARY KEY, |
1417 | dat VARCHAR(32) |
66742793 |
1418 | ) $self->{innodb} |
a78e3fed |
1419 | }, |
1420 | |
66742793 |
1421 | q{ INSERT INTO loader_test3 (id,dat) VALUES(1,'aaa') }, |
1422 | q{ INSERT INTO loader_test3 (id,dat) VALUES(2,'bbb') }, |
1423 | q{ INSERT INTO loader_test3 (id,dat) VALUES(3,'ccc') }, |
1424 | q{ INSERT INTO loader_test3 (id,dat) VALUES(4,'ddd') }, |
a78e3fed |
1425 | |
1426 | qq{ |
1427 | CREATE TABLE loader_test4 ( |
1428 | id INTEGER NOT NULL PRIMARY KEY, |
1429 | fkid INTEGER NOT NULL, |
1430 | dat VARCHAR(32), |
cfc5dce3 |
1431 | crumb_crisp_coating VARCHAR(32) $self->{null}, |
a7116285 |
1432 | belongs_to INTEGER $self->{null}, |
1433 | set_primary_key INTEGER $self->{null}, |
1434 | FOREIGN KEY( fkid ) REFERENCES loader_test3 (id), |
1435 | FOREIGN KEY( belongs_to ) REFERENCES loader_test3 (id), |
1436 | FOREIGN KEY( set_primary_key ) REFERENCES loader_test3 (id) |
66742793 |
1437 | ) $self->{innodb} |
a78e3fed |
1438 | }, |
1439 | |
a7116285 |
1440 | q{ INSERT INTO loader_test4 (id,fkid,dat,belongs_to,set_primary_key) VALUES(123,1,'aaa',1,1) }, |
1441 | q{ INSERT INTO loader_test4 (id,fkid,dat,belongs_to,set_primary_key) VALUES(124,2,'bbb',2,2) }, |
1442 | q{ INSERT INTO loader_test4 (id,fkid,dat,belongs_to,set_primary_key) VALUES(125,3,'ccc',3,3) }, |
1443 | q{ INSERT INTO loader_test4 (id,fkid,dat,belongs_to,set_primary_key) VALUES(126,4,'ddd',4,4) }, |
a78e3fed |
1444 | |
c930f78b |
1445 | qq| |
a78e3fed |
1446 | CREATE TABLE loader_test5 ( |
9b046d97 |
1447 | id1 INTEGER NOT NULL, |
c930f78b |
1448 | ${oqt}iD2${cqt} INTEGER NOT NULL, |
9b046d97 |
1449 | dat VARCHAR(8), |
3a89a69f |
1450 | from_id INTEGER $self->{null}, |
1451 | to_id INTEGER $self->{null}, |
c930f78b |
1452 | PRIMARY KEY (id1,${oqt}iD2${cqt}), |
c496748b |
1453 | FOREIGN KEY (from_id) REFERENCES loader_test4 (id), |
1454 | FOREIGN KEY (to_id) REFERENCES loader_test4 (id) |
66742793 |
1455 | ) $self->{innodb} |
c930f78b |
1456 | |, |
a78e3fed |
1457 | |
c930f78b |
1458 | qq| INSERT INTO loader_test5 (id1,${oqt}iD2${cqt},dat) VALUES (1,1,'aaa') |, |
a78e3fed |
1459 | |
c930f78b |
1460 | qq| |
a78e3fed |
1461 | CREATE TABLE loader_test6 ( |
708c0939 |
1462 | id INTEGER NOT NULL PRIMARY KEY, |
c930f78b |
1463 | ${oqt}Id2${cqt} INTEGER, |
e7886624 |
1464 | loader_test2_id INTEGER, |
9b046d97 |
1465 | dat VARCHAR(8), |
e7886624 |
1466 | FOREIGN KEY (loader_test2_id) REFERENCES loader_test2 (id), |
c930f78b |
1467 | FOREIGN KEY(id,${oqt}Id2${cqt}) REFERENCES loader_test5 (id1,${oqt}iD2${cqt}) |
66742793 |
1468 | ) $self->{innodb} |
c930f78b |
1469 | |, |
a78e3fed |
1470 | |
c930f78b |
1471 | (qq| INSERT INTO loader_test6 (id, ${oqt}Id2${cqt},loader_test2_id,dat) | . |
66742793 |
1472 | q{ VALUES (1, 1,1,'aaa') }), |
a78e3fed |
1473 | |
dc379dc6 |
1474 | # here we are testing adjective detection |
1475 | |
a78e3fed |
1476 | qq{ |
1477 | CREATE TABLE loader_test7 ( |
1478 | id INTEGER NOT NULL PRIMARY KEY, |
1479 | id2 VARCHAR(8) NOT NULL UNIQUE, |
dc379dc6 |
1480 | dat VARCHAR(8), |
1481 | lovely_loader_test6 INTEGER NOT NULL UNIQUE, |
1482 | FOREIGN KEY (lovely_loader_test6) REFERENCES loader_test6 (id) |
66742793 |
1483 | ) $self->{innodb} |
a78e3fed |
1484 | }, |
1485 | |
dc379dc6 |
1486 | q{ INSERT INTO loader_test7 (id,id2,dat,lovely_loader_test6) VALUES (1,'aaa','bbb',1) }, |
1487 | |
1488 | # for some DBs we need a named FK to drop later |
1489 | ($self->{vendor} =~ /^(mssql|sybase|access|mysql)\z/i ? ( |
1490 | (q{ ALTER TABLE loader_test6 ADD } . |
1491 | qq{ loader_test7_id INTEGER $self->{null} }), |
1492 | (q{ ALTER TABLE loader_test6 ADD CONSTRAINT loader_test6_to_7_fk } . |
1493 | q{ FOREIGN KEY (loader_test7_id) } . |
1494 | q{ REFERENCES loader_test7 (id) }) |
1495 | ) : ( |
1496 | (q{ ALTER TABLE loader_test6 ADD } . |
1497 | qq{ loader_test7_id INTEGER $self->{null} REFERENCES loader_test7 (id) }), |
1498 | )), |
a78e3fed |
1499 | |
1500 | qq{ |
1501 | CREATE TABLE loader_test8 ( |
1502 | id INTEGER NOT NULL PRIMARY KEY, |
1503 | loader_test7 VARCHAR(8) NOT NULL, |
9b046d97 |
1504 | dat VARCHAR(8), |
a78e3fed |
1505 | FOREIGN KEY (loader_test7) REFERENCES loader_test7 (id2) |
66742793 |
1506 | ) $self->{innodb} |
a78e3fed |
1507 | }, |
1508 | |
dc379dc6 |
1509 | (q{ INSERT INTO loader_test8 (id,loader_test7,dat) VALUES (1,'aaa','bbb') }), |
1510 | (q{ INSERT INTO loader_test8 (id,loader_test7,dat) VALUES (2,'aaa','bbb') }), |
1511 | (q{ INSERT INTO loader_test8 (id,loader_test7,dat) VALUES (3,'aaa','bbb') }), |
a78e3fed |
1512 | |
1513 | qq{ |
1514 | CREATE TABLE loader_test9 ( |
9b046d97 |
1515 | loader_test9 VARCHAR(8) NOT NULL |
66742793 |
1516 | ) $self->{innodb} |
a78e3fed |
1517 | }, |
996be9ee |
1518 | |
1519 | qq{ |
1520 | CREATE TABLE loader_test16 ( |
1521 | id INTEGER NOT NULL PRIMARY KEY, |
dc379dc6 |
1522 | dat VARCHAR(8), |
1523 | loader_test8_id INTEGER NOT NULL UNIQUE, |
1524 | FOREIGN KEY (loader_test8_id) REFERENCES loader_test8 (id) |
996be9ee |
1525 | ) $self->{innodb} |
1526 | }, |
1527 | |
dc379dc6 |
1528 | qq{ INSERT INTO loader_test16 (id,dat,loader_test8_id) VALUES (2,'x16',1) }, |
1529 | qq{ INSERT INTO loader_test16 (id,dat,loader_test8_id) VALUES (4,'y16',2) }, |
1530 | qq{ INSERT INTO loader_test16 (id,dat,loader_test8_id) VALUES (6,'z16',3) }, |
1531 | |
1532 | # for some DBs we need a named FK to drop later |
1533 | ($self->{vendor} =~ /^(mssql|sybase|access|mysql)\z/i ? ( |
1534 | (q{ ALTER TABLE loader_test8 ADD } . |
1535 | qq{ loader_test16_id INTEGER $self->{null} }), |
1536 | (q{ ALTER TABLE loader_test8 ADD CONSTRAINT loader_test8_to_16_fk } . |
1537 | q{ FOREIGN KEY (loader_test16_id) } . |
1538 | q{ REFERENCES loader_test16 (id) }) |
1539 | ) : ( |
1540 | (q{ ALTER TABLE loader_test8 ADD } . |
1541 | qq{ loader_test16_id INTEGER $self->{null} REFERENCES loader_test16 (id) }), |
1542 | )), |
996be9ee |
1543 | |
1544 | qq{ |
1545 | CREATE TABLE loader_test17 ( |
1546 | id INTEGER NOT NULL PRIMARY KEY, |
1547 | loader16_one INTEGER, |
1548 | loader16_two INTEGER, |
1549 | FOREIGN KEY (loader16_one) REFERENCES loader_test16 (id), |
1550 | FOREIGN KEY (loader16_two) REFERENCES loader_test16 (id) |
1551 | ) $self->{innodb} |
1552 | }, |
1553 | |
1554 | qq{ INSERT INTO loader_test17 (id, loader16_one, loader16_two) VALUES (3, 2, 4) }, |
1555 | qq{ INSERT INTO loader_test17 (id, loader16_one, loader16_two) VALUES (33, 4, 6) }, |
1556 | |
1557 | qq{ |
1558 | CREATE TABLE loader_test18 ( |
1559 | id INTEGER NOT NULL PRIMARY KEY, |
1560 | dat VARCHAR(8) |
1561 | ) $self->{innodb} |
1562 | }, |
1563 | |
1564 | qq{ INSERT INTO loader_test18 (id,dat) VALUES (1,'x18') }, |
1565 | qq{ INSERT INTO loader_test18 (id,dat) VALUES (2,'y18') }, |
1566 | qq{ INSERT INTO loader_test18 (id,dat) VALUES (3,'z18') }, |
1567 | |
1568 | qq{ |
1569 | CREATE TABLE loader_test19 ( |
1570 | id INTEGER NOT NULL PRIMARY KEY, |
1571 | dat VARCHAR(8) |
1572 | ) $self->{innodb} |
1573 | }, |
1574 | |
1575 | qq{ INSERT INTO loader_test19 (id,dat) VALUES (4,'x19') }, |
1576 | qq{ INSERT INTO loader_test19 (id,dat) VALUES (5,'y19') }, |
1577 | qq{ INSERT INTO loader_test19 (id,dat) VALUES (6,'z19') }, |
1578 | |
1579 | qq{ |
1580 | CREATE TABLE loader_test20 ( |
1581 | parent INTEGER NOT NULL, |
1582 | child INTEGER NOT NULL, |
1583 | PRIMARY KEY (parent, child), |
1584 | FOREIGN KEY (parent) REFERENCES loader_test18 (id), |
1585 | FOREIGN KEY (child) REFERENCES loader_test19 (id) |
1586 | ) $self->{innodb} |
1587 | }, |
1588 | |
1589 | q{ INSERT INTO loader_test20 (parent, child) VALUES (1,4) }, |
1590 | q{ INSERT INTO loader_test20 (parent, child) VALUES (2,5) }, |
1591 | q{ INSERT INTO loader_test20 (parent, child) VALUES (3,6) }, |
1592 | |
1593 | qq{ |
1594 | CREATE TABLE loader_test21 ( |
1595 | id INTEGER NOT NULL PRIMARY KEY, |
1596 | dat VARCHAR(8) |
1597 | ) $self->{innodb} |
1598 | }, |
1599 | |
1600 | q{ INSERT INTO loader_test21 (id,dat) VALUES (7,'a21')}, |
1601 | q{ INSERT INTO loader_test21 (id,dat) VALUES (11,'b21')}, |
1602 | q{ INSERT INTO loader_test21 (id,dat) VALUES (13,'c21')}, |
1603 | q{ INSERT INTO loader_test21 (id,dat) VALUES (17,'d21')}, |
1604 | |
1605 | qq{ |
1606 | CREATE TABLE loader_test22 ( |
1607 | parent INTEGER NOT NULL, |
1608 | child INTEGER NOT NULL, |
1609 | PRIMARY KEY (parent, child), |
1610 | FOREIGN KEY (parent) REFERENCES loader_test21 (id), |
1611 | FOREIGN KEY (child) REFERENCES loader_test21 (id) |
1612 | ) $self->{innodb} |
1613 | }, |
1614 | |
1615 | q{ INSERT INTO loader_test22 (parent, child) VALUES (7,11)}, |
1616 | q{ INSERT INTO loader_test22 (parent, child) VALUES (11,13)}, |
1617 | q{ INSERT INTO loader_test22 (parent, child) VALUES (13,17)}, |
25e3e91d |
1618 | |
1619 | qq{ |
1620 | CREATE TABLE loader_test25 ( |
1621 | id1 INTEGER NOT NULL, |
1622 | id2 INTEGER NOT NULL, |
1623 | dat VARCHAR(8), |
1624 | PRIMARY KEY (id1,id2) |
1625 | ) $self->{innodb} |
1626 | }, |
1627 | |
1628 | q{ INSERT INTO loader_test25 (id1,id2,dat) VALUES (33,5,'x25') }, |
1629 | q{ INSERT INTO loader_test25 (id1,id2,dat) VALUES (33,7,'y25') }, |
1630 | q{ INSERT INTO loader_test25 (id1,id2,dat) VALUES (3,42,'z25') }, |
1631 | |
1632 | qq{ |
1633 | CREATE TABLE loader_test26 ( |
1634 | id INTEGER NOT NULL PRIMARY KEY, |
1635 | rel1 INTEGER NOT NULL, |
1636 | rel2 INTEGER NOT NULL, |
1637 | FOREIGN KEY (id, rel1) REFERENCES loader_test25 (id1, id2), |
1638 | FOREIGN KEY (id, rel2) REFERENCES loader_test25 (id1, id2) |
1639 | ) $self->{innodb} |
1640 | }, |
1641 | |
1642 | q{ INSERT INTO loader_test26 (id,rel1,rel2) VALUES (33,5,7) }, |
1643 | q{ INSERT INTO loader_test26 (id,rel1,rel2) VALUES (3,42,42) }, |
26f1c8c9 |
1644 | |
1645 | qq{ |
1646 | CREATE TABLE loader_test27 ( |
1647 | id INTEGER NOT NULL PRIMARY KEY |
1648 | ) $self->{innodb} |
1649 | }, |
1650 | |
1651 | q{ INSERT INTO loader_test27 (id) VALUES (1) }, |
1652 | q{ INSERT INTO loader_test27 (id) VALUES (2) }, |
1653 | |
1654 | qq{ |
1655 | CREATE TABLE loader_test28 ( |
1656 | id INTEGER NOT NULL PRIMARY KEY, |
1657 | FOREIGN KEY (id) REFERENCES loader_test27 (id) |
1658 | ) $self->{innodb} |
1659 | }, |
1660 | |
1661 | q{ INSERT INTO loader_test28 (id) VALUES (1) }, |
1662 | |
1663 | qq{ |
1664 | CREATE TABLE loader_test29 ( |
1665 | id INTEGER NOT NULL PRIMARY KEY, |
92267f92 |
1666 | fk INTEGER NOT NULL UNIQUE, |
26f1c8c9 |
1667 | FOREIGN KEY (fk) REFERENCES loader_test27 (id) |
1668 | ) $self->{innodb} |
1669 | }, |
1670 | |
1671 | q{ INSERT INTO loader_test29 (id,fk) VALUES (1,1) }, |
7dba7c70 |
1672 | |
1673 | qq{ |
1674 | CREATE TABLE loader_test31 ( |
1675 | id INTEGER NOT NULL PRIMARY KEY |
1676 | ) $self->{innodb} |
1677 | }, |
1678 | q{ INSERT INTO loader_test31 (id) VALUES (1) }, |
1679 | |
1680 | qq{ |
1681 | CREATE TABLE loader_test32 ( |
1682 | id INTEGER NOT NULL PRIMARY KEY, |
1683 | rel1 INTEGER NOT NULL, |
0906d55b |
1684 | rel2 INTEGER $self->{null}, |
7dba7c70 |
1685 | FOREIGN KEY (rel1) REFERENCES loader_test31(id), |
1686 | FOREIGN KEY (rel2) REFERENCES loader_test31(id) |
1687 | ) $self->{innodb} |
1688 | }, |
1689 | q{ INSERT INTO loader_test32 (id,rel1) VALUES (1,1) }, |
1690 | |
1691 | qq{ |
1692 | CREATE TABLE loader_test33 ( |
1693 | id1 INTEGER NOT NULL, |
1694 | id2 INTEGER NOT NULL, |
1695 | PRIMARY KEY (id1,id2) |
1696 | ) $self->{innodb} |
1697 | }, |
1698 | q{ INSERT INTO loader_test33 (id1,id2) VALUES (1,2) }, |
1699 | |
1700 | qq{ |
1701 | CREATE TABLE loader_test34 ( |
1702 | id INTEGER NOT NULL PRIMARY KEY, |
1703 | rel1 INTEGER NOT NULL, |
0906d55b |
1704 | rel2 INTEGER $self->{null}, |
7dba7c70 |
1705 | FOREIGN KEY (id,rel1) REFERENCES loader_test33(id1,id2), |
1706 | FOREIGN KEY (id,rel2) REFERENCES loader_test33(id1,id2) |
1707 | ) $self->{innodb} |
1708 | }, |
3b17d988 |
1709 | q{ INSERT INTO loader_test34 (id,rel1,rel2) VALUES (1,2,2) }, |
a78e3fed |
1710 | ); |
1711 | |
fdd8ff16 |
1712 | @statements_advanced = ( |
a78e3fed |
1713 | qq{ |
1714 | CREATE TABLE loader_test10 ( |
1715 | id10 $self->{auto_inc_pk}, |
1716 | subject VARCHAR(8), |
0906d55b |
1717 | loader_test11 INTEGER $self->{null} |
66742793 |
1718 | ) $self->{innodb} |
a78e3fed |
1719 | }, |
c1ac681d |
1720 | $make_auto_inc->(qw/loader_test10 id10/), |
a78e3fed |
1721 | |
3b17d988 |
1722 | # Access does not support DEFAULT. |
a78e3fed |
1723 | qq{ |
1724 | CREATE TABLE loader_test11 ( |
1725 | id11 $self->{auto_inc_pk}, |
3b17d988 |
1726 | a_message VARCHAR(8) @{[ $self->{vendor} ne 'Access' ? "DEFAULT 'foo'" : '' ]}, |
0906d55b |
1727 | loader_test10 INTEGER $self->{null}, |
a78e3fed |
1728 | FOREIGN KEY (loader_test10) REFERENCES loader_test10 (id10) |
66742793 |
1729 | ) $self->{innodb} |
a78e3fed |
1730 | }, |
c1ac681d |
1731 | $make_auto_inc->(qw/loader_test11 id11/), |
a78e3fed |
1732 | |
bc5afe55 |
1733 | (lc($self->{vendor}) ne 'informix' ? |
1734 | (q{ ALTER TABLE loader_test10 ADD CONSTRAINT loader_test11_fk } . |
1735 | q{ FOREIGN KEY (loader_test11) } . |
1736 | q{ REFERENCES loader_test11 (id11) }) |
1737 | : |
1738 | (q{ ALTER TABLE loader_test10 ADD CONSTRAINT } . |
1739 | q{ FOREIGN KEY (loader_test11) } . |
1740 | q{ REFERENCES loader_test11 (id11) } . |
1741 | q{ CONSTRAINT loader_test11_fk }) |
1742 | ), |
a78e3fed |
1743 | ); |
1744 | |
fdd8ff16 |
1745 | @statements_advanced_sqlite = ( |
7fc16540 |
1746 | qq{ |
1747 | CREATE TABLE loader_test10 ( |
1748 | id10 $self->{auto_inc_pk}, |
1749 | subject VARCHAR(8) |
1750 | ) $self->{innodb} |
1751 | }, |
1752 | $make_auto_inc->(qw/loader_test10 id10/), |
1753 | |
1754 | qq{ |
1755 | CREATE TABLE loader_test11 ( |
1756 | id11 $self->{auto_inc_pk}, |
8793567f |
1757 | a_message VARCHAR(8) DEFAULT 'foo', |
7fc16540 |
1758 | loader_test10 INTEGER $self->{null}, |
1759 | FOREIGN KEY (loader_test10) REFERENCES loader_test10 (id10) |
1760 | ) $self->{innodb} |
1761 | }, |
1762 | $make_auto_inc->(qw/loader_test11 id11/), |
1763 | |
1764 | (q{ ALTER TABLE loader_test10 ADD COLUMN } . |
1765 | q{ loader_test11 INTEGER REFERENCES loader_test11 (id11) }), |
1766 | ); |
1767 | |
fdd8ff16 |
1768 | @statements_inline_rels = ( |
4ce22656 |
1769 | qq{ |
1770 | CREATE TABLE loader_test12 ( |
1771 | id INTEGER NOT NULL PRIMARY KEY, |
1772 | id2 VARCHAR(8) NOT NULL UNIQUE, |
cfb6add1 |
1773 | dat VARCHAR(8) NOT NULL UNIQUE |
4ce22656 |
1774 | ) $self->{innodb} |
1775 | }, |
1776 | |
1777 | q{ INSERT INTO loader_test12 (id,id2,dat) VALUES (1,'aaa','bbb') }, |
1778 | |
1779 | qq{ |
1780 | CREATE TABLE loader_test13 ( |
1781 | id INTEGER NOT NULL PRIMARY KEY REFERENCES loader_test12, |
1782 | loader_test12 VARCHAR(8) NOT NULL REFERENCES loader_test12 (id2), |
1783 | dat VARCHAR(8) REFERENCES loader_test12 (dat) |
1784 | ) $self->{innodb} |
1785 | }, |
1786 | |
1787 | (q{ INSERT INTO loader_test13 (id,loader_test12,dat) } . |
1788 | q{ VALUES (1,'aaa','bbb') }), |
1789 | ); |
1790 | |
1791 | |
fdd8ff16 |
1792 | @statements_implicit_rels = ( |
52bf3f26 |
1793 | qq{ |
1794 | CREATE TABLE loader_test14 ( |
1795 | id INTEGER NOT NULL PRIMARY KEY, |
1796 | dat VARCHAR(8) |
1797 | ) $self->{innodb} |
1798 | }, |
1799 | |
1800 | q{ INSERT INTO loader_test14 (id,dat) VALUES (123,'aaa') }, |
1801 | |
1802 | qq{ |
1803 | CREATE TABLE loader_test15 ( |
1804 | id INTEGER NOT NULL PRIMARY KEY, |
1805 | loader_test14 INTEGER NOT NULL, |
1806 | FOREIGN KEY (loader_test14) REFERENCES loader_test14 |
1807 | ) $self->{innodb} |
1808 | }, |
1809 | |
1810 | q{ INSERT INTO loader_test15 (id,loader_test14) VALUES (1,123) }, |
a60b5b8d |
1811 | ); |
52bf3f26 |
1812 | |
af96f52e |
1813 | $self->drop_tables; |
1814 | |
a78e3fed |
1815 | my $dbh = $self->dbconnect(1); |
ddf91322 |
1816 | |
12333562 |
1817 | $dbh->do($_) for @{ $self->{pre_create} || [] }; |
1818 | |
3b29d0ac |
1819 | $dbh->do($_) foreach (@statements); |
9065de34 |
1820 | |
b48f3f80 |
1821 | if (not ($self->{vendor} eq 'mssql' && $dbh->{Driver}{Name} eq 'Sybase')) { |
1822 | $dbh->do($_) foreach (@{ $self->{data_type_tests}{ddl} || [] }); |
1823 | } |
9065de34 |
1824 | |
a78e3fed |
1825 | unless($self->{skip_rels}) { |
af96f52e |
1826 | # hack for now, since DB2 doesn't like inline comments, and we need |
1827 | # to test one for mysql, which works on everyone else... |
1828 | # this all needs to be refactored anyways. |
3b17d988 |
1829 | |
1830 | for my $stmt (@statements_reltests) { |
1831 | try { |
1832 | $dbh->do($stmt); |
1833 | } |
1834 | catch { |
1835 | die "Error executing '$stmt': $_\n"; |
1836 | }; |
1837 | } |
7fc16540 |
1838 | if($self->{vendor} =~ /sqlite/i) { |
1839 | $dbh->do($_) for (@statements_advanced_sqlite); |
1840 | } |
1841 | else { |
a78e3fed |
1842 | $dbh->do($_) for (@statements_advanced); |
1843 | } |
4ce22656 |
1844 | unless($self->{no_inline_rels}) { |
1845 | $dbh->do($_) for (@statements_inline_rels); |
1846 | } |
52bf3f26 |
1847 | unless($self->{no_implicit_rels}) { |
1848 | $dbh->do($_) for (@statements_implicit_rels); |
1849 | } |
a78e3fed |
1850 | } |
df637f3d |
1851 | |
1852 | $dbh->do($_) for @{ $self->{extra}->{create} || [] }; |
a78e3fed |
1853 | $dbh->disconnect(); |
1854 | } |
1855 | |
af96f52e |
1856 | sub drop_tables { |
a78e3fed |
1857 | my $self = shift; |
1858 | |
a78e3fed |
1859 | my @tables = qw/ |
c4a49993 |
1860 | loader_test1 |
22270947 |
1861 | loader_test1s |
a78e3fed |
1862 | loader_test2 |
a1cec524 |
1863 | LOADER_test23 |
996be9ee |
1864 | LoAdEr_test24 |
e35e5a64 |
1865 | loader_test35 |
42e785fa |
1866 | loader_test36 |
a78e3fed |
1867 | /; |
c1ac681d |
1868 | |
1869 | my @tables_auto_inc = ( |
22270947 |
1870 | [ qw/loader_test1s id/ ], |
c1ac681d |
1871 | [ qw/loader_test2 id/ ], |
1872 | ); |
a78e3fed |
1873 | |
1874 | my @tables_reltests = qw/ |
1875 | loader_test4 |
1876 | loader_test3 |
1877 | loader_test6 |
1878 | loader_test5 |
1879 | loader_test8 |
1880 | loader_test7 |
1881 | loader_test9 |
996be9ee |
1882 | loader_test17 |
1883 | loader_test16 |
1884 | loader_test20 |
1885 | loader_test19 |
1886 | loader_test18 |
1887 | loader_test22 |
1888 | loader_test21 |
25e3e91d |
1889 | loader_test26 |
1890 | loader_test25 |
26f1c8c9 |
1891 | loader_test28 |
1892 | loader_test29 |
1893 | loader_test27 |
7dba7c70 |
1894 | loader_test32 |
1895 | loader_test31 |
1896 | loader_test34 |
1897 | loader_test33 |
a78e3fed |
1898 | /; |
1899 | |
1900 | my @tables_advanced = qw/ |
1901 | loader_test11 |
1902 | loader_test10 |
1903 | /; |
c1ac681d |
1904 | |
1905 | my @tables_advanced_auto_inc = ( |
1906 | [ qw/loader_test10 id10/ ], |
1907 | [ qw/loader_test11 id11/ ], |
1908 | ); |
a78e3fed |
1909 | |
4ce22656 |
1910 | my @tables_inline_rels = qw/ |
1911 | loader_test13 |
1912 | loader_test12 |
1913 | /; |
1914 | |
52bf3f26 |
1915 | my @tables_implicit_rels = qw/ |
1916 | loader_test15 |
1917 | loader_test14 |
1918 | /; |
1919 | |
657d4b58 |
1920 | my @tables_rescan = qw/ loader_test30 /; |
a60b5b8d |
1921 | |
fa3bad42 |
1922 | my @tables_preserve_case_tests = @{ $self->{preserve_case_tests_table_names} }; |
b511f36e |
1923 | |
dc379dc6 |
1924 | my %drop_columns = ( |
1925 | loader_test6 => 'loader_test7_id', |
1926 | loader_test7 => 'lovely_loader_test6', |
1927 | loader_test8 => 'loader_test16_id', |
1928 | loader_test16 => 'loader_test8_id', |
1929 | ); |
a78e3fed |
1930 | |
dc379dc6 |
1931 | my %drop_constraints = ( |
1932 | loader_test10 => 'loader_test11_fk', |
1933 | loader_test6 => 'loader_test6_to_7_fk', |
1934 | loader_test8 => 'loader_test8_to_16_fk', |
1935 | ); |
a78e3fed |
1936 | |
c4a49993 |
1937 | # For some reason some tests do this twice (I guess dependency issues?) |
1938 | # do it twice for all drops |
1939 | for (1,2) { |
3b17d988 |
1940 | local $^W = 0; # for ADO |
1941 | |
b48f3f80 |
1942 | my $dbh = $self->dbconnect(0); |
a78e3fed |
1943 | |
b48f3f80 |
1944 | $dbh->do($_) for @{ $self->{extra}{pre_drop_ddl} || [] }; |
7b868481 |
1945 | |
dc379dc6 |
1946 | $self->drop_table($dbh, $_) for @{ $self->{extra}{drop} || [] }; |
df637f3d |
1947 | |
b48f3f80 |
1948 | my $drop_auto_inc = $self->{auto_inc_drop_cb} || sub {}; |
c1ac681d |
1949 | |
b48f3f80 |
1950 | unless($self->{skip_rels}) { |
dc379dc6 |
1951 | # drop the circular rel columns if possible, this |
1952 | # doesn't work on all DBs |
1953 | foreach my $table (keys %drop_columns) { |
1954 | $dbh->do("ALTER TABLE $table DROP $drop_columns{$table}"); |
1955 | $dbh->do("ALTER TABLE $table DROP COLUMN $drop_columns{$table}"); |
b48f3f80 |
1956 | } |
dc379dc6 |
1957 | |
1958 | foreach my $table (keys %drop_constraints) { |
1959 | # for MSSQL |
1960 | $dbh->do("ALTER TABLE $table DROP $drop_constraints{$table}"); |
1961 | # for Sybase and Access |
1962 | $dbh->do("ALTER TABLE $table DROP CONSTRAINT $drop_constraints{$table}"); |
1963 | # for MySQL |
1964 | $dbh->do("ALTER TABLE $table DROP FOREIGN KEY $drop_constraints{$table}"); |
b48f3f80 |
1965 | } |
dc379dc6 |
1966 | |
1967 | $self->drop_table($dbh, $_) for (@tables_reltests); |
1968 | $self->drop_table($dbh, $_) for (@tables_reltests); |
1969 | |
b48f3f80 |
1970 | $dbh->do($_) for map { $drop_auto_inc->(@$_) } @tables_advanced_auto_inc; |
dc379dc6 |
1971 | |
1972 | $self->drop_table($dbh, $_) for (@tables_advanced); |
7fc16540 |
1973 | |
b48f3f80 |
1974 | unless($self->{no_inline_rels}) { |
dc379dc6 |
1975 | $self->drop_table($dbh, $_) for (@tables_inline_rels); |
b48f3f80 |
1976 | } |
1977 | unless($self->{no_implicit_rels}) { |
dc379dc6 |
1978 | $self->drop_table($dbh, $_) for (@tables_implicit_rels); |
b48f3f80 |
1979 | } |
52bf3f26 |
1980 | } |
b48f3f80 |
1981 | $dbh->do($_) for map { $drop_auto_inc->(@$_) } @tables_auto_inc; |
dc379dc6 |
1982 | $self->drop_table($dbh, $_) for (@tables, @tables_rescan); |
9065de34 |
1983 | |
b48f3f80 |
1984 | if (not ($self->{vendor} eq 'mssql' && $dbh->{Driver}{Name} eq 'Sybase')) { |
1985 | foreach my $data_type_table (@{ $self->{data_type_tests}{table_names} || [] }) { |
dc379dc6 |
1986 | $self->drop_table($dbh, $data_type_table); |
b48f3f80 |
1987 | } |
1988 | } |
b511f36e |
1989 | |
dc379dc6 |
1990 | $self->drop_table($dbh, $_) for @tables_preserve_case_tests; |
ffb03c96 |
1991 | |
b48f3f80 |
1992 | $dbh->disconnect; |
c4a49993 |
1993 | } |
a78e3fed |
1994 | } |
1995 | |
dc379dc6 |
1996 | sub drop_table { |
1997 | my ($self, $dbh, $table) = @_; |
1998 | |
1999 | local $^W = 0; # for ADO |
2000 | |
2001 | try { $dbh->do("DROP TABLE $table CASCADE CONSTRAINTS") }; # oracle |
2002 | try { $dbh->do("DROP TABLE $table CASCADE") }; # postgres and ? |
2003 | try { $dbh->do("DROP TABLE $table") }; |
2004 | |
2005 | # if table name is case sensitive |
2006 | my ($oqt, $cqt) = $self->get_oqt_cqt(always => 1); |
2007 | |
2008 | try { $dbh->do("DROP TABLE ${oqt}${table}${cqt}") }; |
2009 | } |
2010 | |
53ef681d |
2011 | sub _custom_column_info { |
2012 | my ( $table_name, $column_name, $column_info ) = @_; |
2013 | |
2014 | $table_name = lc ( $table_name ); |
2015 | $column_name = lc ( $column_name ); |
2016 | |
2017 | if ( $table_name eq 'loader_test35' |
2018 | and $column_name eq 'an_int' |
2019 | ){ |
2020 | return { is_numeric => 1 } |
2021 | } |
2022 | # Set inflate_datetime or inflate_date to check |
2023 | # datetime_timezone and datetime_locale |
2024 | if ( $table_name eq 'loader_test36' ){ |
2025 | return { inflate_datetime => 1 } if |
2026 | ( $column_name eq 'b_char_as_data' ); |
2027 | return { inflate_date => 1 } if |
2028 | ( $column_name eq 'c_char_as_data' ); |
2029 | } |
2030 | |
2031 | return; |
2032 | } |
2033 | |
3b29d0ac |
2034 | my %DATA_TYPE_MULTI_TABLE_OVERRIDES = ( |
7640ef4b |
2035 | oracle => qr/\blong\b/i, |
2036 | mssql => qr/\b(?:timestamp|rowversion)\b/i, |
c7e6dc1f |
2037 | informix => qr/\b(?:bigserial|serial8)\b/i, |
3b29d0ac |
2038 | ); |
2039 | |
9065de34 |
2040 | sub setup_data_type_tests { |
2041 | my $self = shift; |
2042 | |
2043 | return unless my $types = $self->{data_types}; |
2044 | |
2045 | my $tests = $self->{data_type_tests} = {}; |
9065de34 |
2046 | |
3b29d0ac |
2047 | # split types into tables based on overrides |
dd87d4c4 |
2048 | my (@types, @split_off_types, @first_table_types); |
2049 | { |
7640ef4b |
2050 | my $split_off_re = $DATA_TYPE_MULTI_TABLE_OVERRIDES{lc($self->{vendor})} || qr/(?!)/; |
dd87d4c4 |
2051 | |
2052 | @types = keys %$types; |
7640ef4b |
2053 | @split_off_types = grep /$split_off_re/, @types; |
2054 | @first_table_types = grep !/$split_off_re/, @types; |
dd87d4c4 |
2055 | } |
9065de34 |
2056 | |
3b29d0ac |
2057 | @types = +{ map +($_, $types->{$_}), @first_table_types }, |
2058 | map +{ $_, $types->{$_} }, @split_off_types; |
9065de34 |
2059 | |
2060 | my $test_count = 0; |
3b29d0ac |
2061 | my $table_num = 10000; |
9065de34 |
2062 | |
3b29d0ac |
2063 | foreach my $types (@types) { |
2064 | my $table_name = "loader_test$table_num"; |
2065 | push @{ $tests->{table_names} }, $table_name; |
f30ddf15 |
2066 | |
3b29d0ac |
2067 | my $table_moniker = "LoaderTest$table_num"; |
2068 | push @{ $tests->{table_monikers} }, $table_moniker; |
26334ec1 |
2069 | |
3b29d0ac |
2070 | $table_num++; |
9065de34 |
2071 | |
3b29d0ac |
2072 | my $cols = $tests->{columns}{$table_moniker} = {}; |
9dc968df |
2073 | |
3b29d0ac |
2074 | my $ddl = "CREATE TABLE $table_name (\n id INTEGER NOT NULL PRIMARY KEY,\n"; |
9dc968df |
2075 | |
3b29d0ac |
2076 | my %seen_col_names; |
cf0ba25b |
2077 | |
3b29d0ac |
2078 | while (my ($col_def, $expected_info) = each %$types) { |
2079 | (my $type_alias = $col_def) =~ s/\( ([^)]+) \)//xg; |
9065de34 |
2080 | |
3b29d0ac |
2081 | my $size = $1; |
2082 | $size = '' unless defined $size; |
2083 | $size =~ s/\s+//g; |
2084 | my @size = split /,/, $size; |
9065de34 |
2085 | |
3b29d0ac |
2086 | # some DBs don't like very long column names |
ebed3e6e |
2087 | if ($self->{vendor} =~ /^(?:firebird|sqlanywhere|oracle|db2)\z/i) { |
3b29d0ac |
2088 | my ($col_def, $default) = $type_alias =~ /^(.*)(default.*)?\z/i; |
5163dc4a |
2089 | |
3b29d0ac |
2090 | $type_alias = substr $col_def, 0, 15; |
bc1cb85e |
2091 | |
3b29d0ac |
2092 | $type_alias .= '_with_dflt' if $default; |
2093 | } |
f30ddf15 |
2094 | |
3b29d0ac |
2095 | $type_alias =~ s/\s/_/g; |
2096 | $type_alias =~ s/\W//g; |
9065de34 |
2097 | |
3b29d0ac |
2098 | my $col_name = 'col_' . $type_alias; |
2099 | |
2100 | if (@size) { |
2101 | my $size_name = join '_', apply { s/\W//g } @size; |
9065de34 |
2102 | |
3b29d0ac |
2103 | $col_name .= "_sz_$size_name"; |
2104 | } |
2105 | |
2106 | # XXX would be better to check _loader->preserve_case |
2107 | $col_name = lc $col_name; |
2108 | |
2109 | $col_name .= '_' . $seen_col_names{$col_name} if $seen_col_names{$col_name}++; |
2110 | |
2111 | $ddl .= " $col_name $col_def,\n"; |
2112 | |
2113 | $cols->{$col_name} = $expected_info; |
9065de34 |
2114 | |
3b29d0ac |
2115 | $test_count++; |
2116 | } |
2117 | |
2118 | $ddl =~ s/,\n\z/\n)/; |
2119 | |
2120 | push @{ $tests->{ddl} }, $ddl; |
2121 | } |
9065de34 |
2122 | |
9065de34 |
2123 | $tests->{test_count} = $test_count; |
2124 | |
2125 | return $test_count; |
2126 | } |
2127 | |
1ad8e8c3 |
2128 | sub rescan_without_warnings { |
2129 | my ($self, $conn) = @_; |
2130 | |
2131 | local $SIG{__WARN__} = sub { warn @_ unless $_[0] =~ RESCAN_WARNINGS }; |
2132 | return $conn->rescan; |
2133 | } |
2134 | |
3fea497f |
2135 | sub test_col_accessor_map { |
cfc5dce3 |
2136 | my ( $column_name, $default_name, $context ) = @_; |
61d1cca1 |
2137 | if( lc($column_name) eq 'crumb_crisp_coating' ) { |
cfc5dce3 |
2138 | |
3fea497f |
2139 | is( $default_name, 'crumb_crisp_coating', 'col_accessor_map was passed the default name' ); |
2140 | ok( $context->{$_}, "col_accessor_map func was passed the $_" ) |
cfc5dce3 |
2141 | for qw( table_name table_class table_moniker schema_class ); |
2142 | |
2143 | return 'trivet'; |
2144 | } else { |
2145 | return $default_name; |
2146 | } |
2147 | } |
2148 | |
996be9ee |
2149 | sub DESTROY { |
2150 | my $self = shift; |
565335e6 |
2151 | unless ($ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}) { |
c4a49993 |
2152 | $self->drop_tables if $self->{_created}; |
2153 | rmtree $DUMP_DIR |
565335e6 |
2154 | } |
996be9ee |
2155 | } |
af96f52e |
2156 | |
a78e3fed |
2157 | 1; |
df956aad |
2158 | # vim:et sts=4 sw=4 tw=0: |