4 use DBIx::Class::Schema::Loader::Utils qw/slurp_file write_file/;
8 use dbixcsl_test_dir '$tdir';
9 use dbixcsl_dumper_tests;
10 my $t = 'dbixcsl_dumper_tests';
14 # test loading external content
16 classname => 'DBICTest::Schema::_no_skip_load_external',
19 qr/package DBICTest::Schema::_no_skip_load_external::Foo;.*\nour \$skip_me = "bad mojo";\n1;/s
24 # test skipping external content
26 classname => 'DBICTest::Schema::_skip_load_external',
28 skip_load_external => 1,
32 qr/package DBICTest::Schema::_skip_load_external::Foo;.*\nour \$skip_me = "bad mojo";\n1;/s
40 my $config_file = File::Temp->new (UNLINK => 1);
42 print $config_file "{ skip_relationships => 1 }\n";
46 classname => 'DBICTest::Schema::_config_file',
47 options => { config_file => "$config_file" },
58 classname => 'DBICTest::Schema::_clashing_monikers',
59 test_db_class => 'make_dbictest_db_clashing_monikers',
60 error => qr/tables (?:"bar", "bars"|"bars", "bar") reduced to the same source moniker 'Bar'/,
66 # test naming => { column_accessors => 'preserve' }
67 # also test POD for unique constraint
69 classname => 'DBICTest::Schema::_preserve_column_accessors',
70 test_db_class => 'make_dbictest_db_with_unique',
71 options => { naming => { column_accessors => 'preserve' } },
79 qr/\n\n=head1 UNIQUE CONSTRAINTS\n\n=head2 C<baz_num_unique>\n\n=over 4\n\n=item \* L<\/baz_num>\n\n=back\n\n=cut\n\n__PACKAGE__->add_unique_constraint\("baz_num_unique"\, \["baz_num"\]\);\n\n/,
86 # test that rels are sorted
88 classname => 'DBICTest::Schema::_sorted_rels',
89 test_db_class => 'make_dbictest_db_with_unique',
92 qr/->might_have\(\n "quux".*->belongs_to\(\n "station_visited"/s,
100 classname => 'DBICTest::Schema::_sorted_uniqs',
101 test_db_class => 'make_dbictest_db_multi_unique',
104 qr/->add_unique_constraint\("uniq1_unique".*->add_unique_constraint\("uniq2_unique"/s,
111 # test naming => { monikers => 'plural' }
113 classname => 'DBICTest::Schema::_plural_monikers',
114 options => { naming => { monikers => 'plural' } },
117 qr/\n=head1 NAME\n\nDBICTest::Schema::_plural_monikers::Foos\n\n=cut\n\n/,
120 qr/\n=head1 NAME\n\nDBICTest::Schema::_plural_monikers::Bars\n\n=cut\n\n/,
127 # test naming => { monikers => 'singular' }
129 classname => 'DBICTest::Schema::_singular_monikers',
130 test_db_class => 'make_dbictest_db_plural_tables',
131 options => { naming => { monikers => 'singular' } },
134 qr/\n=head1 NAME\n\nDBICTest::Schema::_singular_monikers::Foo\n\n=cut\n\n/,
137 qr/\n=head1 NAME\n\nDBICTest::Schema::_singular_monikers::Bar\n\n=cut\n\n/,
144 # test naming => { monikers => 'preserve' }
146 classname => 'DBICTest::Schema::_preserve_monikers',
147 test_db_class => 'make_dbictest_db_plural_tables',
148 options => { naming => { monikers => 'preserve' } },
151 qr/\n=head1 NAME\n\nDBICTest::Schema::_preserve_monikers::Foos\n\n=cut\n\n/,
154 qr/\n=head1 NAME\n\nDBICTest::Schema::_preserve_monikers::Bars\n\n=cut\n\n/,
161 # test out the POD and "use utf8;"
163 classname => 'DBICTest::DumpMore::1',
165 custom_column_info => sub {
166 my ($table, $col, $info) = @_;
167 return +{ extra => { is_footext => 1 } } if $col eq 'footext';
169 result_base_class => 'My::ResultBaseClass',
170 additional_classes => 'TestAdditional',
171 additional_base_classes => 'TestAdditionalBase',
172 left_base_classes => 'TestLeftBase',
173 components => [ 'TestComponent', '+TestComponentFQN' ],
178 qr/package DBICTest::DumpMore::1;/,
183 qr/package DBICTest::DumpMore::1::Foo;/,
184 qr/\n=head1 NAME\n\nDBICTest::DumpMore::1::Foo\n\n=cut\n\nuse strict;\nuse warnings;\n\n/,
185 qr/\n=head1 BASE CLASS: L<My::ResultBaseClass>\n\n=cut\n\nuse base 'My::ResultBaseClass';\n\n/,
186 qr/\n=head1 ADDITIONAL CLASSES USED\n\n=over 4\n\n=item \* L<TestAdditional>\n\n=back\n\n=cut\n\n/,
187 qr/\n=head1 ADDITIONAL BASE CLASSES\n\n=over 4\n\n=item \* L<TestAdditionalBase>\n\n=back\n\n=cut\n\n/,
188 qr/\n=head1 LEFT BASE CLASSES\n\n=over 4\n\n=item \* L<TestLeftBase>\n\n=back\n\n=cut\n\n/,
189 qr/\n=head1 COMPONENTS LOADED\n\n=over 4\n\n=item \* L<DBIx::Class::TestComponent>\n\n=item \* L<TestComponentFQN>\n\n=back\n\n=cut\n\n/,
190 qr/\n=head1 TABLE: C<foo>\n\n=cut\n\n__PACKAGE__->table\("foo"\);\n\n/,
191 qr/\n=head1 ACCESSORS\n\n/,
192 qr/\n=head2 fooid\n\n data_type: 'integer'\n is_auto_increment: 1\n is_nullable: 0\n\n/,
193 qr/\n=head2 footext\n\n data_type: 'text'\n default_value: 'footext'\n extra: \{is_footext => 1\}\n is_nullable: 1\n\n/,
194 qr/\n=head1 PRIMARY KEY\n\n=over 4\n\n=item \* L<\/fooid>\n\n=back\n\n=cut\n\n__PACKAGE__->set_primary_key\("fooid"\);\n/,
195 qr/\n=head1 RELATIONS\n\n/,
196 qr/\n=head2 bars\n\nType: has_many\n\nRelated object: L<DBICTest::DumpMore::1::Bar>\n\n=cut\n\n/,
201 qr/package DBICTest::DumpMore::1::Bar;/,
202 qr/\n=head1 NAME\n\nDBICTest::DumpMore::1::Bar\n\n=cut\n\nuse strict;\nuse warnings;\n\n/,
203 qr/\n=head1 BASE CLASS: L<My::ResultBaseClass>\n\n=cut\n\nuse base 'My::ResultBaseClass';\n\n/,
204 qr/\n=head1 ADDITIONAL CLASSES USED\n\n=over 4\n\n=item \* L<TestAdditional>\n\n=back\n\n=cut\n\n/,
205 qr/\n=head1 ADDITIONAL BASE CLASSES\n\n=over 4\n\n=item \* L<TestAdditionalBase>\n\n=back\n\n=cut\n\n/,
206 qr/\n=head1 LEFT BASE CLASSES\n\n=over 4\n\n=item \* L<TestLeftBase>\n\n=back\n\n=cut\n\n/,
207 qr/\n=head1 COMPONENTS LOADED\n\n=over 4\n\n=item \* L<DBIx::Class::TestComponent>\n\n=item \* L<TestComponentFQN>\n\n=back\n\n=cut\n\n/,
208 qr/\n=head1 TABLE: C<bar>\n\n=cut\n\n__PACKAGE__->table\("bar"\);\n\n/,
209 qr/\n=head1 ACCESSORS\n\n/,
210 qr/\n=head2 barid\n\n data_type: 'integer'\n is_auto_increment: 1\n is_nullable: 0\n\n/,
211 qr/\n=head2 fooref\n\n data_type: 'integer'\n is_foreign_key: 1\n is_nullable: 1\n\n/,
212 qr/\n=head1 PRIMARY KEY\n\n=over 4\n\n=item \* L<\/barid>\n\n=back\n\n=cut\n\n__PACKAGE__->set_primary_key\("barid"\);\n/,
213 qr/\n=head1 RELATIONS\n\n/,
214 qr/\n=head2 fooref\n\nType: belongs_to\n\nRelated object: L<DBICTest::DumpMore::1::Foo>\n\n=cut\n\n/,
220 $t->append_to_class('DBICTest::DumpMore::1::Foo',q{# XXX This is my custom content XXX});
224 classname => 'DBICTest::DumpMore::1',
227 qr/package DBICTest::DumpMore::1;/,
231 qr/package DBICTest::DumpMore::1::Foo;/,
232 qr/->set_primary_key/,
233 qr/1;\n# XXX This is my custom content XXX/,
236 qr/package DBICTest::DumpMore::1::Bar;/,
237 qr/->set_primary_key/,
245 classname => 'DBICTest::DumpMore::1',
247 really_erase_my_files => 1
251 qr/package DBICTest::DumpMore::1;/,
255 qr/package DBICTest::DumpMore::1::Foo;/,
256 qr/->set_primary_key/,
260 qr/package DBICTest::DumpMore::1::Bar;/,
261 qr/->set_primary_key/,
267 qr/# XXX This is my custom content XXX/,
277 classname => 'DBICTest::DumpMore::1',
291 classname => 'DBICTest::DumpMore::1',
293 db_schema => 'foo_schema',
294 qualify_objects => 1,
298 qr/^db_schema is not supported on SQLite/,
302 qr/^\Q__PACKAGE__->table("foo_schema.foo");\E/m,
303 # the has_many relname should not have the schema in it!
304 qr/^__PACKAGE__->has_many\(\n "bars"/m,
309 # test qualify_objects
311 classname => 'DBICTest::DumpMore::1',
313 db_schema => [ 'foo_schema', 'bar_schema' ],
314 qualify_objects => 0,
318 qr/^db_schema is not supported on SQLite/,
322 # the table name should not include the db schema
323 qr/^\Q__PACKAGE__->table("foo");\E/m,
326 # the table name should not include the db schema
327 qr/^\Q__PACKAGE__->table("bar");\E/m,
334 classname => 'DBICTest::DumpMore::1',
336 db_schema => 'my_schema',
337 moniker_parts => ['_schema', 'name'],
338 qualify_objects => 1,
342 qr/^db_schema is not supported on SQLite/,
345 'Result/MySchemaFoo' => [
346 qr/^\Q__PACKAGE__->table("my_schema.foo");\E/m,
347 # the has_many relname should not have the schema in it, but the class should
348 qr/^__PACKAGE__->has_many\(\n "bars",\n "DBICTest::DumpMore::1::Result::MySchemaBar"/m,
353 # test moniker_part_separator
355 classname => 'DBICTest::DumpMore::1',
357 db_schema => 'my_schema',
358 moniker_parts => ['_schema', 'name'],
359 moniker_part_separator => '::',
360 qualify_objects => 1,
364 qr/^db_schema is not supported on SQLite/,
367 'Result/MySchema/Foo' => [
368 qr/^package DBICTest::DumpMore::1::Result::MySchema::Foo;/m,
369 qr/^\Q__PACKAGE__->table("my_schema.foo");\E/m,
370 # the has_many relname should not have the schema in it, but the class should
371 qr/^__PACKAGE__->has_many\(\n "bars",\n "DBICTest::DumpMore::1::Result::MySchema::Bar"/m,
376 # test moniker_part_separator + moniker_map + recursive constraints
378 classname => 'DBICTest::DumpMore::1',
380 db_schema => 'my_schema',
381 moniker_parts => ['_schema', 'name'],
382 moniker_part_separator => '::',
383 qualify_objects => 1,
386 my_schema => { foo => "MySchema::Floop" },
388 constraint => [ [ qr/my_schema/ => qr/foo|bar/ ] ],
389 exclude => [ [ qr/my_schema/ => qr/bar/ ] ],
391 generated_results => [qw(MySchema::Floop)],
393 qr/^db_schema is not supported on SQLite/,
396 'Result/MySchema/Floop' => [
397 qr/^package DBICTest::DumpMore::1::Result::MySchema::Floop;/m,
398 qr/^\Q__PACKAGE__->table("my_schema.foo");\E/m,
402 'Result/MySchema/Floop' => [
403 # the bar table should not be loaded, so no relationship should exist
404 qr/^__PACKAGE__->has_many\(\n "bars"/m,
409 # test moniker_map + moniker_part_map
411 classname => 'DBICTest::DumpMore::1',
413 db_schema => 'my_schema',
414 moniker_parts => ['_schema', 'name'],
415 moniker_part_separator => '::',
416 moniker_part_map => {
418 my_schema => 'OtherSchema',
423 foo => 'MySchema::Floop',
426 qualify_objects => 1,
430 qr/^db_schema is not supported on SQLite/,
433 'Result/MySchema/Floop' => [
434 qr/^package DBICTest::DumpMore::1::Result::MySchema::Floop;/m,
435 qr/^\Q__PACKAGE__->table("my_schema.foo");\E/m,
436 # the has_many relname should not have the schema in it, but the class should
437 qr/^__PACKAGE__->has_many\(\n "bars",\n "DBICTest::DumpMore::1::Result::OtherSchema::Bar"/m,
439 'Result/OtherSchema/Bar' => [
440 qr/^package DBICTest::DumpMore::1::Result::OtherSchema::Bar;/m,
441 qr/^\Q__PACKAGE__->table("my_schema.bar");\E/m,
442 # the has_many relname should not have the schema in it, but the class should
443 qr/^__PACKAGE__->belongs_to\(\n "fooref",\n "DBICTest::DumpMore::1::Result::MySchema::Floop"/m,
452 classname => 'DBICTest::DumpMore::1',
458 qr/package DBICTest::DumpMore::1;/,
459 qr/->load_namespaces/,
462 qr/package DBICTest::DumpMore::1::Result::Foo;/,
463 qr/->set_primary_key/,
467 qr/package DBICTest::DumpMore::1::Result::Bar;/,
468 qr/->set_primary_key/,
476 classname => 'DBICTest::DumpMore::1',
479 result_namespace => 'Res',
480 resultset_namespace => 'RSet',
481 default_resultset_class => 'RSetBase',
485 qr/package DBICTest::DumpMore::1;/,
486 qr/->load_namespaces/,
487 qr/result_namespace => "Res"/,
488 qr/resultset_namespace => "RSet"/,
489 qr/default_resultset_class => "RSetBase"/,
492 qr/package DBICTest::DumpMore::1::Res::Foo;/,
493 qr/->set_primary_key/,
497 qr/package DBICTest::DumpMore::1::Res::Bar;/,
498 qr/->set_primary_key/,
506 classname => 'DBICTest::DumpMore::1',
509 result_namespace => '+DBICTest::DumpMore::1::Res',
510 resultset_namespace => 'RSet',
511 default_resultset_class => 'RSetBase',
512 result_base_class => 'My::ResultBaseClass',
513 schema_base_class => 'My::SchemaBaseClass',
517 qr/package DBICTest::DumpMore::1;/,
518 qr/->load_namespaces/,
519 qr/result_namespace => "\+DBICTest::DumpMore::1::Res"/,
520 qr/resultset_namespace => "RSet"/,
521 qr/default_resultset_class => "RSetBase"/,
522 qr/use base 'My::SchemaBaseClass'/,
525 qr/package DBICTest::DumpMore::1::Res::Foo;/,
526 qr/use base 'My::ResultBaseClass'/,
527 qr/->set_primary_key/,
531 qr/package DBICTest::DumpMore::1::Res::Bar;/,
532 qr/use base 'My::ResultBaseClass'/,
533 qr/->set_primary_key/,
540 classname => 'DBICTest::DumpMore::1',
543 result_base_class => 'My::MissingResultBaseClass',
545 error => qr/My::MissingResultBaseClass.*is not installed/,
548 # test quote_char in connect_info for dbicdump
550 classname => 'DBICTest::DumpMore::1',
551 extra_connect_info => [
554 { quote_char => '"' },
558 # test fix for RT#70507 (end comment and 1; gets lost if left with actual
562 classname => 'DBICTest::DumpMore::Upgrade',
568 my $file = $t->class_file('DBICTest::DumpMore::Upgrade::Foo');
570 my $code = slurp_file $file;
572 $code =~ s/(?=# You can replace)/sub custom_method { 'custom_method works' }\n0;\n\n/;
574 write_file $file, $code;
577 classname => 'DBICTest::DumpMore::Upgrade',
581 generated_results => [qw(Foo Bar)],
584 qr/sub custom_method \{ 'custom_method works' \}\n0;\n\n# You can replace.*\n1;\n\z/,
591 classname => 'DBICTest::DumpMore::DryRun',
595 generated_results => [qw(Foo Bar)],
598 my $schema_file = $t->class_file('DBICTest::DumpMore::DryRun');
599 ok( !-e $schema_file, "dry-run doesn't create file for schema class" );
600 (my $schema_dir = $schema_file) =~ s/\.pm\z//;
601 ok( !-e $schema_dir, "dry-run doesn't create subdirectory for schema namespace" );
603 # test omit_version (RT#92300)
605 classname => 'DBICTest::DumpMore::omit_version',
611 qr/^\# Created by DBIx::Class::Schema::Loader @ \d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d$/m,
616 # test omit_timestamp (RT#92300)
618 classname => 'DBICTest::DumpMore::omit_timestamp',
624 qr/^\# Created by DBIx::Class::Schema::Loader v[\d.]+$/m,
629 # test omit_version and omit_timestamp simultaneously (RT#92300)
631 classname => 'DBICTest::DumpMore::omit_both',
636 # A positive regex here would match the top comment
639 qr/^\# Created by DBIx::Class::Schema::Loader.+$/m,
644 my $copy = $t->copy_class('DBICTest::DumpMore::1::Foo', 'dump_copy');
646 unshift @INC, "$tdir/dump_copy";
649 classname => 'DBICTest::DumpMore::1',
652 qr/^# These lines were loaded from/m,
657 $t->append_to_class('DBICTest::DumpMore::1::Foo', qq{# XXX This is my external custom content\n}, 'dump_copy');
660 classname => 'DBICTest::DumpMore::1',
662 really_erase_my_files => 1,
666 qr/^# XXX This is my external custom content/m,
671 qr/^# These lines were loaded from.*^# Created by DBIx::Class::Schema::Loader/,
678 # vim:et sts=4 sw=4 tw=0: