X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F23dumpmore.t;h=9490b2fd2fe540e6bc06c860c8ca11aa383a05b0;hb=9ff235d2b5c3ae8d08097744344bce8f119e709b;hp=5c3c495da744c7186cebc8de0b9c355a3a279de6;hpb=1a8fd9497918b9655f0824a520ab4155bbf51a5c;p=dbsrgits%2FDBIx-Class-Schema-Loader.git diff --git a/t/23dumpmore.t b/t/23dumpmore.t index 5c3c495..9490b2f 100644 --- a/t/23dumpmore.t +++ b/t/23dumpmore.t @@ -1,9 +1,9 @@ -use warnings; use strict; - -use File::Temp (); +use warnings; use Test::More; - +use DBIx::Class::Schema::Loader::Utils qw/slurp_file write_file/; +use namespace::clean; +use File::Temp (); use lib qw(t/lib); use dbixcsl_dumper_tests; my $t = 'dbixcsl_dumper_tests'; @@ -13,10 +13,6 @@ $t->cleanup; # test loading external content $t->dump_test( classname => 'DBICTest::Schema::_no_skip_load_external', - warnings => [ - qr/Dumping manual schema for DBICTest::Schema::_no_skip_load_external to directory /, - qr/Schema dump completed/, - ], regexes => { Foo => [ qr/package DBICTest::Schema::_no_skip_load_external::Foo;\nour \$skip_me = "bad mojo";\n1;/ @@ -28,12 +24,8 @@ $t->dump_test( $t->dump_test( classname => 'DBICTest::Schema::_skip_load_external', options => { - skip_load_external => 1 + skip_load_external => 1, }, - warnings => [ - qr/Dumping manual schema for DBICTest::Schema::_skip_load_external to directory /, - qr/Schema dump completed/, - ], neg_regexes => { Foo => [ qr/package DBICTest::Schema::_skip_load_external::Foo;\nour \$skip_me = "bad mojo";\n1;/ @@ -52,10 +44,6 @@ $t->cleanup; $t->dump_test( classname => 'DBICTest::Schema::_config_file', options => { config_file => "$config_file" }, - warnings => [ - qr/Dumping manual schema for DBICTest::Schema::_config_file to directory /, - qr/Schema dump completed/, - ], neg_regexes => { Foo => [ qr/has_many/, @@ -68,65 +56,171 @@ $t->cleanup; $t->dump_test( classname => 'DBICTest::Schema::_clashing_monikers', test_db_class => 'make_dbictest_db_clashing_monikers', - error => qr/tables 'bar', 'bars' reduced to the same source moniker 'Bar'/, + error => qr/tables (?:"bar", "bars"|"bars", "bar") reduced to the same source moniker 'Bar'/, +); + + +$t->cleanup; + +# test naming => { column_accessors => 'preserve' } +# also test POD for unique constraint +$t->dump_test( + classname => 'DBICTest::Schema::_preserve_column_accessors', + test_db_class => 'make_dbictest_db_with_unique', + options => { naming => { column_accessors => 'preserve' } }, + neg_regexes => { + RouteChange => [ + qr/\baccessor\b/, + ], + }, + regexes => { + Baz => [ + qr/\n\n=head1 UNIQUE CONSTRAINTS\n\n=head2 C\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/, + ], + } ); +$t->cleanup; + +# test that rels are sorted +$t->dump_test( + classname => 'DBICTest::Schema::_sorted_rels', + test_db_class => 'make_dbictest_db_with_unique', + regexes => { + Baz => [ + qr/->might_have\(\n "quux".*->belongs_to\(\n "station_visited"/s, + ], + } +); + +$t->cleanup; + +$t->dump_test( + classname => 'DBICTest::Schema::_sorted_uniqs', + test_db_class => 'make_dbictest_db_multi_unique', + regexes => { + Bar => [ + qr/->add_unique_constraint\("uniq1_unique".*->add_unique_constraint\("uniq2_unique"/s, + ], + }, +); + +$t->cleanup; + +# test naming => { monikers => 'plural' } +$t->dump_test( + classname => 'DBICTest::Schema::_plural_monikers', + options => { naming => { monikers => 'plural' } }, + regexes => { + Foos => [ + qr/\n=head1 NAME\n\nDBICTest::Schema::_plural_monikers::Foos\n\n=cut\n\n/, + ], + Bars => [ + qr/\n=head1 NAME\n\nDBICTest::Schema::_plural_monikers::Bars\n\n=cut\n\n/, + ], + }, +); + +$t->cleanup; + +# test naming => { monikers => 'singular' } +$t->dump_test( + classname => 'DBICTest::Schema::_singular_monikers', + test_db_class => 'make_dbictest_db_plural_tables', + options => { naming => { monikers => 'singular' } }, + regexes => { + Foo => [ + qr/\n=head1 NAME\n\nDBICTest::Schema::_singular_monikers::Foo\n\n=cut\n\n/, + ], + Bar => [ + qr/\n=head1 NAME\n\nDBICTest::Schema::_singular_monikers::Bar\n\n=cut\n\n/, + ], + }, +); + +$t->cleanup; + +# test naming => { monikers => 'preserve' } +$t->dump_test( + classname => 'DBICTest::Schema::_preserve_monikers', + test_db_class => 'make_dbictest_db_plural_tables', + options => { naming => { monikers => 'preserve' } }, + regexes => { + Foos => [ + qr/\n=head1 NAME\n\nDBICTest::Schema::_preserve_monikers::Foos\n\n=cut\n\n/, + ], + Bars => [ + qr/\n=head1 NAME\n\nDBICTest::Schema::_preserve_monikers::Bars\n\n=cut\n\n/, + ], + }, +); $t->cleanup; -# test out the POD +# test out the POD and "use utf8;" $t->dump_test( classname => 'DBICTest::DumpMore::1', options => { custom_column_info => sub { my ($table, $col, $info) = @_; return +{ extra => { is_footext => 1 } } if $col eq 'footext'; - } + }, + result_base_class => 'My::ResultBaseClass', + additional_classes => 'TestAdditional', + additional_base_classes => 'TestAdditionalBase', + left_base_classes => 'TestLeftBase', + components => [ 'TestComponent', '+TestComponentFQN' ], }, - warnings => [ - qr/Dumping manual schema for DBICTest::DumpMore::1 to directory /, - qr/Schema dump completed/, - ], regexes => { schema => [ + qr/^use utf8;\n/, qr/package DBICTest::DumpMore::1;/, qr/->load_classes/, ], Foo => [ + qr/^use utf8;\n/, qr/package DBICTest::DumpMore::1::Foo;/, - qr/=head1 NAME\n\nDBICTest::DumpMore::1::Foo\n\n=cut\n\n/, - qr/=head1 ACCESSORS\n\n/, - qr/=head2 fooid\n\n data_type: 'integer'\n is_auto_increment: 1\n is_nullable: 0\n\n/, - qr/=head2 footext\n\n data_type: 'text'\n default_value: 'footext'\n extra: {is_footext => 1}\n is_nullable: 1\n\n/, - qr/->set_primary_key/, - qr/=head1 RELATIONS\n\n/, - qr/=head2 bars\n\nType: has_many\n\nRelated object: L\n\n=cut\n\n/, + qr/\n=head1 NAME\n\nDBICTest::DumpMore::1::Foo\n\n=cut\n\nuse strict;\nuse warnings;\n\n/, + qr/\n=head1 BASE CLASS: L\n\n=cut\n\nuse base 'My::ResultBaseClass';\n\n/, + qr/\n=head1 ADDITIONAL CLASSES USED\n\n=over 4\n\n=item \* L\n\n=back\n\n=cut\n\n/, + qr/\n=head1 ADDITIONAL BASE CLASSES\n\n=over 4\n\n=item \* L\n\n=back\n\n=cut\n\n/, + qr/\n=head1 LEFT BASE CLASSES\n\n=over 4\n\n=item \* L\n\n=back\n\n=cut\n\n/, + qr/\n=head1 COMPONENTS LOADED\n\n=over 4\n\n=item \* L\n\n=item \* L\n\n=back\n\n=cut\n\n/, + qr/\n=head1 TABLE: C\n\n=cut\n\n__PACKAGE__->table\("foo"\);\n\n/, + qr/\n=head1 ACCESSORS\n\n/, + qr/\n=head2 fooid\n\n data_type: 'integer'\n is_auto_increment: 1\n is_nullable: 0\n\n/, + qr/\n=head2 footext\n\n data_type: 'text'\n default_value: 'footext'\n extra: \{is_footext => 1\}\n is_nullable: 1\n\n/, + 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/, + qr/\n=head1 RELATIONS\n\n/, + qr/\n=head2 bars\n\nType: has_many\n\nRelated object: L\n\n=cut\n\n/, qr/1;\n$/, ], Bar => [ + qr/^use utf8;\n/, qr/package DBICTest::DumpMore::1::Bar;/, - qr/=head1 NAME\n\nDBICTest::DumpMore::1::Bar\n\n=cut\n\n/, - qr/=head1 ACCESSORS\n\n/, - qr/=head2 barid\n\n data_type: 'integer'\n is_auto_increment: 1\n is_nullable: 0\n\n/, - qr/=head2 fooref\n\n data_type: 'integer'\n is_foreign_key: 1\n is_nullable: 1\n\n/, - qr/->set_primary_key/, - qr/=head1 RELATIONS\n\n/, - qr/=head2 fooref\n\nType: belongs_to\n\nRelated object: L\n\n=cut\n\n/, - qr/1;\n$/, + qr/\n=head1 NAME\n\nDBICTest::DumpMore::1::Bar\n\n=cut\n\nuse strict;\nuse warnings;\n\n/, + qr/\n=head1 BASE CLASS: L\n\n=cut\n\nuse base 'My::ResultBaseClass';\n\n/, + qr/\n=head1 ADDITIONAL CLASSES USED\n\n=over 4\n\n=item \* L\n\n=back\n\n=cut\n\n/, + qr/\n=head1 ADDITIONAL BASE CLASSES\n\n=over 4\n\n=item \* L\n\n=back\n\n=cut\n\n/, + qr/\n=head1 LEFT BASE CLASSES\n\n=over 4\n\n=item \* L\n\n=back\n\n=cut\n\n/, + qr/\n=head1 COMPONENTS LOADED\n\n=over 4\n\n=item \* L\n\n=item \* L\n\n=back\n\n=cut\n\n/, + qr/\n=head1 TABLE: C\n\n=cut\n\n__PACKAGE__->table\("bar"\);\n\n/, + qr/\n=head1 ACCESSORS\n\n/, + qr/\n=head2 barid\n\n data_type: 'integer'\n is_auto_increment: 1\n is_nullable: 0\n\n/, + qr/\n=head2 fooref\n\n data_type: 'integer'\n is_foreign_key: 1\n is_nullable: 1\n\n/, + 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/, + qr/\n=head1 RELATIONS\n\n/, + qr/\n=head2 fooref\n\nType: belongs_to\n\nRelated object: L\n\n=cut\n\n/, + qr/\n1;\n$/, ], }, ); - $t->append_to_class('DBICTest::DumpMore::1::Foo',q{# XXX This is my custom content XXX}); $t->dump_test( classname => 'DBICTest::DumpMore::1', - warnings => [ - qr/Dumping manual schema for DBICTest::DumpMore::1 to directory /, - qr/Schema dump completed/, - ], regexes => { schema => [ qr/package DBICTest::DumpMore::1;/, @@ -151,13 +245,6 @@ $t->dump_test( options => { really_erase_my_files => 1 }, - warnings => [ - qr/Dumping manual schema for DBICTest::DumpMore::1 to directory /, - qr/Deleting existing file /, - qr/Deleting existing file /, - qr/Deleting existing file /, - qr/Schema dump completed/, - ], regexes => { schema => [ qr/package DBICTest::DumpMore::1;/, @@ -191,10 +278,6 @@ $t->dump_test( use_namespaces => 1, generate_pod => 0 }, - warnings => [ - qr/Dumping manual schema for DBICTest::DumpMore::1 to directory /, - qr/Schema dump completed/, - ], neg_regexes => { 'Result/Foo' => [ qr/^=/m, @@ -211,8 +294,7 @@ $t->dump_test( use_namespaces => 1 }, warnings => [ - qr/Dumping manual schema for DBICTest::DumpMore::1 to directory /, - qr/Schema dump completed/, + qr/^db_schema is not supported on SQLite/, ], regexes => { 'Result/Foo' => [ @@ -223,16 +305,154 @@ $t->dump_test( }, ); +# test qualify_objects $t->dump_test( classname => 'DBICTest::DumpMore::1', options => { - use_namespaces => 1 + db_schema => [ 'foo_schema', 'bar_schema' ], + qualify_objects => 0, + use_namespaces => 1, }, warnings => [ - qr/Dumping manual schema for DBICTest::DumpMore::1 to directory /, - qr/Schema dump completed/, + qr/^db_schema is not supported on SQLite/, ], regexes => { + 'Result/Foo' => [ + # the table name should not include the db schema + qr/^\Q__PACKAGE__->table("foo");\E/m, + ], + 'Result/Bar' => [ + # the table name should not include the db schema + qr/^\Q__PACKAGE__->table("bar");\E/m, + ], + }, +); + +# test moniker_parts +$t->dump_test( + classname => 'DBICTest::DumpMore::1', + options => { + db_schema => 'my_schema', + moniker_parts => ['_schema', 'name'], + qualify_objects => 1, + use_namespaces => 1, + }, + warnings => [ + qr/^db_schema is not supported on SQLite/, + ], + regexes => { + 'Result/MySchemaFoo' => [ + qr/^\Q__PACKAGE__->table("my_schema.foo");\E/m, + # the has_many relname should not have the schema in it, but the class should + qr/^__PACKAGE__->has_many\(\n "bars",\n "DBICTest::DumpMore::1::Result::MySchemaBar"/m, + ], + }, +); + +# test moniker_part_separator +$t->dump_test( + classname => 'DBICTest::DumpMore::1', + options => { + db_schema => 'my_schema', + moniker_parts => ['_schema', 'name'], + moniker_part_separator => '::', + qualify_objects => 1, + use_namespaces => 1, + }, + warnings => [ + qr/^db_schema is not supported on SQLite/, + ], + regexes => { + 'Result/MySchema/Foo' => [ + qr/^package DBICTest::DumpMore::1::Result::MySchema::Foo;/m, + qr/^\Q__PACKAGE__->table("my_schema.foo");\E/m, + # the has_many relname should not have the schema in it, but the class should + qr/^__PACKAGE__->has_many\(\n "bars",\n "DBICTest::DumpMore::1::Result::MySchema::Bar"/m, + ], + }, +); + +# test moniker_part_separator + moniker_map + recursive constraints +$t->dump_test( + classname => 'DBICTest::DumpMore::1', + options => { + db_schema => 'my_schema', + moniker_parts => ['_schema', 'name'], + moniker_part_separator => '::', + qualify_objects => 1, + use_namespaces => 1, + moniker_map => { + my_schema => { foo => "MySchema::Floop" }, + }, + constraint => [ [ qr/my_schema/ => qr/foo|bar/ ] ], + exclude => [ [ qr/my_schema/ => qr/bar/ ] ], + }, + generated_results => [qw(MySchema::Floop)], + warnings => [ + qr/^db_schema is not supported on SQLite/, + ], + regexes => { + 'Result/MySchema/Floop' => [ + qr/^package DBICTest::DumpMore::1::Result::MySchema::Floop;/m, + qr/^\Q__PACKAGE__->table("my_schema.foo");\E/m, + ], + }, + neg_regexes => { + 'Result/MySchema/Floop' => [ + # the bar table should not be loaded, so no relationship should exist + qr/^__PACKAGE__->has_many\(\n "bars"/m, + ], + }, +); + +# test moniker_map + moniker_part_map +$t->dump_test( + classname => 'DBICTest::DumpMore::1', + options => { + db_schema => 'my_schema', + moniker_parts => ['_schema', 'name'], + moniker_part_separator => '::', + moniker_part_map => { + _schema => { + my_schema => 'OtherSchema', + }, + }, + moniker_map => { + my_schema => { + foo => 'MySchema::Floop', + }, + }, + qualify_objects => 1, + use_namespaces => 1, + }, + warnings => [ + qr/^db_schema is not supported on SQLite/, + ], + regexes => { + 'Result/MySchema/Floop' => [ + qr/^package DBICTest::DumpMore::1::Result::MySchema::Floop;/m, + qr/^\Q__PACKAGE__->table("my_schema.foo");\E/m, + # the has_many relname should not have the schema in it, but the class should + qr/^__PACKAGE__->has_many\(\n "bars",\n "DBICTest::DumpMore::1::Result::OtherSchema::Bar"/m, + ], + 'Result/OtherSchema/Bar' => [ + qr/^package DBICTest::DumpMore::1::Result::OtherSchema::Bar;/m, + qr/^\Q__PACKAGE__->table("my_schema.bar");\E/m, + # the has_many relname should not have the schema in it, but the class should + qr/^__PACKAGE__->belongs_to\(\n "fooref",\n "DBICTest::DumpMore::1::Result::MySchema::Floop"/m, + ], + + }, +); + + + +$t->dump_test( + classname => 'DBICTest::DumpMore::1', + options => { + use_namespaces => 1 + }, + regexes => { schema => [ qr/package DBICTest::DumpMore::1;/, qr/->load_namespaces/, @@ -259,17 +479,13 @@ $t->dump_test( resultset_namespace => 'RSet', default_resultset_class => 'RSetBase', }, - warnings => [ - qr/Dumping manual schema for DBICTest::DumpMore::1 to directory /, - qr/Schema dump completed/, - ], regexes => { schema => [ qr/package DBICTest::DumpMore::1;/, qr/->load_namespaces/, - qr/result_namespace => 'Res'/, - qr/resultset_namespace => 'RSet'/, - qr/default_resultset_class => 'RSetBase'/, + qr/result_namespace => "Res"/, + qr/resultset_namespace => "RSet"/, + qr/default_resultset_class => "RSetBase"/, ], 'Res/Foo' => [ qr/package DBICTest::DumpMore::1::Res::Foo;/, @@ -295,17 +511,13 @@ $t->dump_test( result_base_class => 'My::ResultBaseClass', schema_base_class => 'My::SchemaBaseClass', }, - warnings => [ - qr/Dumping manual schema for DBICTest::DumpMore::1 to directory /, - qr/Schema dump completed/, - ], regexes => { schema => [ qr/package DBICTest::DumpMore::1;/, qr/->load_namespaces/, - qr/result_namespace => '\+DBICTest::DumpMore::1::Res'/, - qr/resultset_namespace => 'RSet'/, - qr/default_resultset_class => 'RSetBase'/, + qr/result_namespace => "\+DBICTest::DumpMore::1::Res"/, + qr/resultset_namespace => "RSet"/, + qr/default_resultset_class => "RSetBase"/, qr/use base 'My::SchemaBaseClass'/, ], 'Res/Foo' => [ @@ -323,7 +535,6 @@ $t->dump_test( }, ); - $t->dump_test( classname => 'DBICTest::DumpMore::1', options => { @@ -333,4 +544,101 @@ $t->dump_test( error => qr/My::MissingResultBaseClass.*is not installed/, ); +# test quote_char in connect_info for dbicdump +$t->dump_test( + classname => 'DBICTest::DumpMore::1', + extra_connect_info => [ + '', + '', + { quote_char => '"' }, + ], +); + +# test fix for RT#70507 (end comment and 1; gets lost if left with actual +# custom content) + +$t->dump_test( + classname => 'DBICTest::DumpMore::Upgrade', + options => { + use_namespaces => 0, + }, +); + +my $file = $t->class_file('DBICTest::DumpMore::Upgrade::Foo'); + +my $code = slurp_file $file; + +$code =~ s/(?=# You can replace)/sub custom_method { 'custom_method works' }\n0;\n\n/; + +write_file $file, $code; + +$t->dump_test( + classname => 'DBICTest::DumpMore::Upgrade', + options => { + use_namespaces => 1, + }, + generated_results => [qw(Foo Bar)], + regexes => { + 'Result/Foo' => [ + qr/sub custom_method \{ 'custom_method works' \}\n0;\n\n# You can replace.*\n1;\n\z/, + ], + }, +); + +# test dry-run mode +$t->dump_test( + classname => 'DBICTest::DumpMore::DryRun', + options => { + dry_run => 1, + }, + generated_results => [qw(Foo Bar)], +); + +my $schema_file = $t->class_file('DBICTest::DumpMore::DryRun'); +ok( !-e $schema_file, "dry-run doesn't create file for schema class" ); +(my $schema_dir = $schema_file) =~ s/\.pm\z//; +ok( !-e $schema_dir, "dry-run doesn't create subdirectory for schema namespace" ); + +# test omit_version (RT#92300) +$t->dump_test( + classname => 'DBICTest::DumpMore::omit_version', + options => { + omit_version => 1, + }, + regexes => { + Foo => [ + qr/^\# Created by DBIx::Class::Schema::Loader @ \d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d$/m, + ], + }, +); + +# test omit_timestamp (RT#92300) +$t->dump_test( + classname => 'DBICTest::DumpMore::omit_timestamp', + options => { + omit_timestamp => 1, + }, + regexes => { + Foo => [ + qr/^\# Created by DBIx::Class::Schema::Loader v[\d.]+$/m, + ], + }, +); + +# test omit_version and omit_timestamp simultaneously (RT#92300) +$t->dump_test( + classname => 'DBICTest::DumpMore::omit_both', + options => { + omit_version => 1, + omit_timestamp => 1, + }, + # A positive regex here would match the top comment + neg_regexes => { + Foo => [ + qr/^\# Created by DBIx::Class::Schema::Loader.+$/m, + ], + }, +); + done_testing; +# vim:et sts=4 sw=4 tw=0: