Add dry-run mode for static schema creation
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 23dumpmore.t
index 032aa66..f94cc3f 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';
@@ -305,6 +305,29 @@ $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',
@@ -320,12 +343,109 @@ $t->dump_test(
   regexes => {
     'Result/MySchemaFoo' => [
       qr/^\Q__PACKAGE__->table("my_schema.foo");\E/m,
-      # the has_many relname should not have the schema in it!
+      # 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,
     ],
   },
 );
 
+# 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 => {
@@ -433,5 +553,48 @@ $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/,
+        ],
+    },
+);
+
+# test dry-run mode
+$t->dump_test(
+    classname => 'DBICTest::DumpMore::DryRun',
+    options => {
+        dry_run => 1,
+    },
+);
+
+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" );
+
 done_testing;
 # vim:et sts=4 sw=4 tw=0: