Add use_moo option
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 10_02mysql_common.t
index bc457cd..504e60c 100644 (file)
@@ -1,3 +1,6 @@
+use DBIx::Class::Schema::Loader::Optional::Dependencies
+    -skip_all_without => 'test_rdbms_mysql';
+
 use strict;
 use warnings;
 use Test::More;
@@ -25,19 +28,22 @@ my $innodb = $test_innodb ? q{Engine=InnoDB} : '';
 
 my ($schema, $databases_created); # for cleanup in END for extra tests
 
-my $tester = dbixcsl_common_tests->new(
-    vendor           => 'Mysql',
-    auto_inc_pk      => 'INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT',
-    innodb           => $innodb,
-    dsn              => $dsn,
-    user             => $user,
-    password         => $password,
-    connect_info_opts=> { on_connect_call => 'set_strict_mode' },
-    loader_options   => { preserve_case => 1 },
-    skip_rels        => $test_innodb ? 0 : $skip_rels_msg,
-    quote_char       => '`',
-    no_inline_rels   => 1,
-    no_implicit_rels => 1,
+diag $skip_rels_msg if not $test_innodb;
+
+dbixcsl_common_tests->new(
+    vendor            => 'Mysql',
+    auto_inc_pk       => 'INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT',
+    innodb            => $innodb,
+    dsn               => $dsn,
+    user              => $user,
+    password          => $password,
+    connect_info_opts => { on_connect_call => 'set_strict_mode' },
+    loader_options    => { preserve_case => 1 },
+    skip_rels         => $test_innodb ? 0 : $skip_rels_msg,
+    quote_char        => '`',
+    no_inline_rels    => 1,
+    no_implicit_rels  => 1,
+    default_on_clause => 'RESTRICT',
     data_types  => {
         # http://dev.mysql.com/doc/refman/5.5/en/data-type-overview.html
         # Numeric Types
@@ -113,7 +119,6 @@ my $tester = dbixcsl_common_tests->new(
         'time'        => { data_type => 'time' },
         'year'        => { data_type => 'year' },
         'year(4)'     => { data_type => 'year' },
-        'year(2)'     => { data_type => 'year', size => 2 },
 
         # String Types
         'char'         => { data_type => 'char',      size => 1  },
@@ -135,14 +140,17 @@ my $tester = dbixcsl_common_tests->new(
         'longblob'    => { data_type => 'longblob' },
         'longtext'    => { data_type => 'longtext' },
 
-        "enum('foo','bar','baz')"
-                      => { data_type => 'enum', extra => { list => [qw/foo bar baz/] } },
-        "enum('foo \\'bar\\' baz', 'foo ''bar'' quux')"
-                      => { data_type => 'enum', extra => { list => [q{foo 'bar' baz}, q{foo 'bar' quux}] } },
-        "set('foo \\'bar\\' baz', 'foo ''bar'' quux')"
-                      => { data_type => 'set', extra => { list => [q{foo 'bar' baz}, q{foo 'bar' quux}] } },
-        "set('foo','bar','baz')"
-                      => { data_type => 'set',  extra => { list => [qw/foo bar baz/] } },
+        ( map {
+            "$_('','foo','bar','baz')"
+                      => { data_type => $_, extra => { list => ['', qw/foo bar baz/] } },
+            "$_('foo \\'bar\\' baz', 'foo ''bar'' quux')"
+                      => { data_type => $_, extra => { list => [q{foo 'bar' baz}, q{foo 'bar' quux}] } },
+            "$_('''', '''foo', 'bar''')"
+                      => { data_type => $_, extra => { list => [qw(' 'foo bar')] } },
+            "$_('\\'', '\\'foo', 'bar\\'')",
+                      => { data_type => $_, extra => { list => [qw(' 'foo bar')] } },
+            } qw(set enum)
+        ),
 
         # RT#68717
         "enum('11,10 (<500)/0 DUN','4,90 (<120)/0 EUR') NOT NULL default '11,10 (<500)/0 DUN'"
@@ -180,10 +188,24 @@ my $tester = dbixcsl_common_tests->new(
                   PRIMARY KEY (`ISO3_code`)
                 ) $innodb
             },
+            # 4 through 10 are used for the multi-schema tests
+            qq{
+                create table mysql_loader_test11 (
+                    id int auto_increment primary key
+                ) $innodb
+            },
+            qq{
+                create table mysql_loader_test12 (
+                    id int auto_increment primary key,
+                    eleven_id int,
+                    foreign key (eleven_id) references mysql_loader_test11(id)
+                        on delete restrict on update set null
+                ) $innodb
+            },
         ],
         pre_drop_ddl => [ 'DROP VIEW mysql_loader_test2', ],
-        drop => [ 'mysql_loader-test1', 'mysql_loader_test3' ],
-        count => 5 + 30 * 2,
+        drop => [ 'mysql_loader-test1', 'mysql_loader_test3', 'mysql_loader_test11', 'mysql_loader_test12' ],
+        count => 10 + 30 * 2,   # regular + multi-schema * 2
         run => sub {
             my ($monikers, $classes);
             ($schema, $monikers, $classes) = @_;
@@ -196,6 +218,15 @@ my $tester = dbixcsl_common_tests->new(
             is $rsrc->column_info('value')->{data_type}, 'varchar',
                 'view introspected successfully';
 
+            # test that views are marked as such
+            my $view_source = $schema->resultset($monikers->{mysql_loader_test2})->result_source;
+            isa_ok $view_source, 'DBIx::Class::ResultSource::View',
+                'view result source';
+
+            like $view_source->view_definition,
+                qr/\A \s* select \b .* \b from \s+ `.*?` \. `mysql_loader-test1` \s* \z/imsx,
+                'view defintion';
+
             $rsrc = $schema->source('MysqlLoaderTest3');
 
             is_deeply $rsrc->column_info('del_group')->{extra}{list}, ['19,90 (<500)/0 EUR','4,90 (<120)/0 EUR','7,90 (<200)/0 CHF','300 (<6000)/0 CZK','4,90 (<100)/0 EUR','39 (<900)/0 DKK','299 (<5000)/0 EEK','9,90 (<250)/0 EUR','3,90 (<100)/0 GBP','3000 (<70000)/0 HUF','4000 (<70000)/0 JPY','13,90 (<200)/0 LVL','99 (<2500)/0 NOK','39 (<1000)/0 PLN','1000 (<20000)/0 RUB','49 (<2500)/0 SEK','29 (<600)/0 USD','19,90 (<600)/0 EUR','0 EUR','0 CHF'],
@@ -212,6 +243,17 @@ my $tester = dbixcsl_common_tests->new(
             like $code, qr/^=head2 id\n\n(.+:.+\n)+\nThe\nColumn\n\n/m,
                 'column comment and attrs';
 
+            # test on delete/update fk clause introspection
+            ok ((my $rel_info = $schema->source('MysqlLoaderTest12')->relationship_info('eleven')),
+                'got rel info');
+
+            is $rel_info->{attrs}{on_delete}, 'RESTRICT',
+                'ON DELETE clause introspected correctly';
+
+            is $rel_info->{attrs}{on_update}, 'SET NULL',
+                'ON UPDATE clause introspected correctly';
+
+            # multischema tests follow
             SKIP: {
                 my $dbh = $schema->storage->dbh;
 
@@ -219,6 +261,7 @@ my $tester = dbixcsl_common_tests->new(
                     $dbh->do('CREATE DATABASE `dbicsl-test`');
                 }
                 catch {
+                    note "CREATE DATABASE returned error: '$_'";
                     skip "no CREATE DATABASE privileges", 30 * 2;
                 };
 
@@ -302,7 +345,7 @@ EOF
                             $dbh->selectall_arrayref('SHOW DATABASES');
                         }
                         catch {
-                            skip 'no SHOW DATABASES privileges', 28;
+                            skip 'no SHOW DATABASES privileges', 30;
                         }
                     }
 
@@ -319,7 +362,6 @@ EOF
                             {
                                 naming => 'current',
                                 db_schema => $db_schema,
-                                moniker_parts => ['schema', 'name'],
                                 dump_directory => EXTRA_DUMP_DIR,
                                 quiet => 1,
                             },
@@ -338,7 +380,7 @@ EOF
                     } 'connected test schema';
 
                     lives_and {
-                        ok $rsrc = $test_schema->source('DbicslDashTestMysqlLoaderTest4');
+                        ok $rsrc = $test_schema->source('MysqlLoaderTest4');
                     } 'got source for table in database name with dash';
 
                     is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
@@ -351,7 +393,7 @@ EOF
                         'column in database name with dash';
 
                     lives_and {
-                        ok $rs = $test_schema->resultset('DbicslDashTestMysqlLoaderTest4');
+                        ok $rs = $test_schema->resultset('MysqlLoaderTest4');
                     } 'got resultset for table in database name with dash';
 
                     lives_and {
@@ -389,7 +431,7 @@ EOF
                         'unique constraint is correct in database name with dash');
 
                     lives_and {
-                        ok $rsrc = $test_schema->source('DbicslDotTestMysqlLoaderTest6');
+                        ok $rsrc = $test_schema->source('MysqlLoaderTest6');
                     } 'got source for table in database name with dot';
 
                     is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
@@ -402,7 +444,7 @@ EOF
                         'column in database name with dot introspected correctly';
 
                     lives_and {
-                        ok $rs = $test_schema->resultset('DbicslDotTestMysqlLoaderTest6');
+                        ok $rs = $test_schema->resultset('MysqlLoaderTest6');
                     } 'got resultset for table in database name with dot';
 
                     lives_and {
@@ -426,7 +468,7 @@ EOF
                     }
 
                     lives_and {
-                        ok $rsrc = $test_schema->source('DbicslDotTestMysqlLoaderTest7');
+                        ok $rsrc = $test_schema->source('MysqlLoaderTest7');
                     } 'got source for table in database name with dot';
 
                     %uniqs = try { $rsrc->unique_constraints };
@@ -443,22 +485,22 @@ EOF
                         skip 'set the environment variable DBICTEST_MYSQL_INNODB=1 to test relationships', 4 unless $test_innodb;
 
                         lives_and {
-                            ok $test_schema->source('DbicslDotTestMysqlLoaderTest6')
+                            ok $test_schema->source('MysqlLoaderTest6')
                                 ->has_relationship('mysql_loader_test4');
                         } 'cross-database relationship in multi-db_schema';
 
                         lives_and {
-                            ok $test_schema->source('DbicslDashTestMysqlLoaderTest4')
+                            ok $test_schema->source('MysqlLoaderTest4')
                                 ->has_relationship('mysql_loader_test6s');
                         } 'cross-database relationship in multi-db_schema';
 
                         lives_and {
-                            ok $test_schema->source('DbicslDashTestMysqlLoaderTest8')
+                            ok $test_schema->source('MysqlLoaderTest8')
                                 ->has_relationship('mysql_loader_test7');
                         } 'cross-database relationship in multi-db_schema';
 
                         lives_and {
-                            ok $test_schema->source('DbicslDotTestMysqlLoaderTest7')
+                            ok $test_schema->source('MysqlLoaderTest7')
                                 ->has_relationship('mysql_loader_test8s');
                         } 'cross-database relationship in multi-db_schema';
                     }
@@ -466,15 +508,7 @@ EOF
             }
         },
     },
-);
-
-if( !$dsn || !$user ) {
-    $tester->skip_tests('You need to set the DBICTEST_MYSQL_DSN, DBICTEST_MYSQL_USER, and DBICTEST_MYSQL_PASS environment variables');
-}
-else {
-    diag $skip_rels_msg if not $test_innodb;
-    $tester->run_tests();
-}
+)->run_tests;
 
 END {
     if (not $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}) {