Allow matching all the moniker parts in constraint/exclude
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 23dumpmore.t
index a2d0247..07b1bc1 100644 (file)
@@ -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';
@@ -56,7 +56,7 @@ $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'/,
 );
 
 
@@ -95,6 +95,18 @@ $t->dump_test(
 
 $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',
@@ -145,7 +157,7 @@ $t->dump_test(
 
 $t->cleanup;
 
-# test out the POD
+# test out the POD and "use utf8;"
 $t->dump_test(
   classname => 'DBICTest::DumpMore::1',
   options => {
@@ -161,10 +173,12 @@ $t->dump_test(
   },
   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/\n=head1 NAME\n\nDBICTest::DumpMore::1::Foo\n\n=cut\n\nuse strict;\nuse warnings;\n\n/,
       qr/\n=head1 BASE CLASS: L<My::ResultBaseClass>\n\n=cut\n\nuse base 'My::ResultBaseClass';\n\n/,
@@ -182,6 +196,7 @@ $t->dump_test(
       qr/1;\n$/,
     ],
     Bar => [
+      qr/^use utf8;\n/,
       qr/package DBICTest::DumpMore::1::Bar;/,
       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<My::ResultBaseClass>\n\n=cut\n\nuse base 'My::ResultBaseClass';\n\n/,
@@ -278,6 +293,9 @@ $t->dump_test(
     qualify_objects => 1,
     use_namespaces => 1
   },
+  warnings => [
+    qr/^db_schema is not supported on SQLite/,
+  ],
   regexes => {
     'Result/Foo' => [
       qr/^\Q__PACKAGE__->table("foo_schema.foo");\E/m,
@@ -287,6 +305,105 @@ $t->dump_test(
   },
 );
 
+# test qualify_objects
+$t->dump_test(
+  classname => 'DBICTest::DumpMore::1',
+  options => {
+    db_schema => [ 'foo_schema', 'bar_schema' ],
+    qualify_objects => 0,
+    use_namespaces => 1,
+  },
+  warnings => [
+    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/ ] ],
+  },
+  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,
+    ],
+  },
+);
+
 $t->dump_test(
   classname => 'DBICTest::DumpMore::1',
   options => {
@@ -394,5 +511,35 @@ $t->dump_test(
   ],
 );
 
+# 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,
+    },
+    regexes => {
+        'Result/Foo' => [
+            qr/sub custom_method { 'custom_method works' }\n0;\n\n# You can replace.*\n1;\n\z/,
+        ],
+    },
+);
+
 done_testing;
 # vim:et sts=4 sw=4 tw=0: