Improve trigger 'scope' attribute support (RT#119997)
[dbsrgits/SQL-Translator.git] / lib / Test / SQL / Translator.pm
index 2cbf82a..9856350 100644 (file)
@@ -1,23 +1,5 @@
 package Test::SQL::Translator;
 
-# ----------------------------------------------------------------------
-# Copyright (C) 2003 The SQLFairy Authors
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License as
-# published by the Free Software Foundation; version 2.
-#
-# This program is distributed in the hope that it will be useful, but
-# WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-# General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-# 02111-1307  USA
-# -------------------------------------------------------------------
-
 =pod
 
 =head1 NAME
@@ -32,9 +14,9 @@ use Test::More;
 use SQL::Translator::Schema::Constants;
 
 use base qw(Exporter);
-use vars qw($VERSION @EXPORT @EXPORT_OK);
-$VERSION = '1.59';
-@EXPORT = qw(
+our @EXPORT_OK;
+our $VERSION = '1.59';
+our @EXPORT = qw(
     schema_ok
     table_ok
     field_ok
@@ -142,13 +124,13 @@ sub default_attribs {
     my ($hashref, $object_type) = @_;
 
     if ( !exists $ATTRIBUTES{ $object_type } ) {
-        die "Can't add default attribs for unknown Schema " 
+        die "Can't add default attribs for unknown Schema "
         .   "object type '$object_type'.";
     }
 
-    for my $attr ( 
+    for my $attr (
         grep { !exists $hashref->{ $_ } }
-        keys %{ $ATTRIBUTES{ $object_type } } 
+        keys %{ $ATTRIBUTES{ $object_type } }
     ) {
         $hashref->{ $attr } = $ATTRIBUTES{ $object_type }{ $attr }
     }
@@ -254,7 +236,7 @@ sub constraint_ok {
 
     is_deeply( [$obj->options], $test->{options},
     "$t_name    options are '".join(",",@{$test->{options}})."'" );
-    
+
     is_deeply( { $obj->extra }, $test->{extra}, "$t_name    extra" );
 }
 
@@ -275,7 +257,7 @@ sub index_ok {
 
     is_deeply( [$obj->options], $test->{options},
     "$t_name    options are '".join(",",@{$test->{options}})."'" );
-    
+
     is_deeply( { $obj->extra }, $test->{extra}, "$t_name    extra" );
 }
 
@@ -302,8 +284,11 @@ sub trigger_ok {
     is( $obj->on_table, $test->{on_table},
         "$t_name    on_table is '$test->{on_table}'" );
 
+    is( $obj->scope, $test->{scope}, "$t_name    scope is '$test->{scope}'" )
+        if exists $test->{scope};
+
     is( $obj->action, $test->{action}, "$t_name    action is '$test->{action}'" );
-    
+
     is_deeply( { $obj->extra }, $test->{extra}, "$t_name    extra" );
 }
 
@@ -323,7 +308,7 @@ sub view_ok {
 
     is_deeply( [$obj->fields], $test->{fields},
     "$t_name    fields are '".join(",",@{$test->{fields}})."'" );
-    
+
     is_deeply( { $obj->extra }, $test->{extra}, "$t_name    extra" );
 }
 
@@ -341,11 +326,11 @@ sub procedure_ok {
     is_deeply( [$obj->parameters], $test->{parameters},
     "$t_name    parameters are '".join(",",@{$test->{parameters}})."'" );
 
-    is( $obj->comments, $test->{comments}, 
+    is( $obj->comments, $test->{comments},
         "$t_name    comments is '$test->{comments}'" );
 
     is( $obj->owner, $test->{owner}, "$t_name    owner is '$test->{owner}'" );
-   
+
     is_deeply( { $obj->extra }, $test->{extra}, "$t_name    extra" );
 }
 
@@ -366,7 +351,7 @@ sub table_ok {
     # Fields
     if ( $arg{fields} ) {
         my @fldnames = map {$_->{name}} @{$arg{fields}};
-        is_deeply( 
+        is_deeply(
             [ map {$_->name}   $obj->get_fields ],
             [ @fldnames ],
             "${t_name}    field names are ".join(", ",@fldnames)
@@ -402,7 +387,7 @@ sub _test_kids {
             my $meth = "get_$plural";
             my @objects  = $obj->$meth;
             is( scalar(@objects), scalar(@tests),
-                "${t_name}$obj_name has " . scalar(@tests) . " $plural" 
+                "${t_name}$obj_name has " . scalar(@tests) . " $plural"
             );
 
             for my $object (@objects) {
@@ -427,7 +412,7 @@ sub schema_ok {
 
     is( $obj->database, $test->{database},
         "$t_name    database is '$test->{database}'" );
-    
+
     is_deeply( { $obj->extra }, $test->{extra}, "$t_name    extra" );
 
     is( $obj->is_valid, $test->{is_valid},
@@ -466,12 +451,20 @@ sub maybe_plan {
 
     for my $module (@modules) {
         eval "use $module;";
-        if ($@ && $@ =~ /Can't locate (\S+)/) {
+        next if !$@;
+
+        if ($@ =~ /Can't locate (\S+)/) {
             my $mod = $1;
             $mod =~ s/\.pm$//;
             $mod =~ s#/#::#g;
             push @errors, $mod;
         }
+        elsif ($@ =~ /([\w\:]+ version [\d\.]+) required.+?this is only version/) {
+            push @errors, $1;
+        }
+        elsif ($@ =~ /Can't load .+? for module .+?DynaLoader\.pm/i ) {
+          push @errors, $module;
+        }
     }
 
     if (@errors) {
@@ -480,7 +473,9 @@ sub maybe_plan {
             join ", ", @errors;
         plan skip_all => $msg;
     }
-    elsif ($ntests and $ntests ne 'no_plan') {
+    return unless defined $ntests;
+
+    if ($ntests ne 'no_plan') {
         plan tests => $ntests;
     }
     else {
@@ -543,27 +538,27 @@ __END__
      ],
  });
 
-=head1 DESCSIPTION
+=head1 DESCRIPTION
 
 Provides a set of Test::More tests for Schema objects. Testing a parsed
 schema is then as easy as writing a perl data structure describing how you
-expect the schema to look. Also provides maybe_plan for conditionally running
+expect the schema to look. Also provides C<maybe_plan> for conditionally running
 tests based on their dependencies.
 
 The data structures given to the test subs don't have to include all the
 possible values, only the ones you expect to have changed. Any left out will be
-tested to make sure they are still at their default value. This is a usefull
+tested to make sure they are still at their default value. This is a useful
 check that you your parser hasn't accidentally set schema values you didn't
 expect it to.
 
-For an example of the output run the t/16xml-parser.t test.
+For an example of the output run the F<t/16xml-parser.t> test.
 
 =head1 Tests
 
 All the tests take a first arg of the schema object to test, followed by a
 hash ref describing how you expect that object to look (you only need give the
 attributes you expect to have changed from the default).
-The 3rd arg is an optional test name to pre-pend to all the generated test
+The 3rd arg is an optional test name to prepend to all the generated test
 names.
 
 =head2 table_ok
@@ -595,6 +590,9 @@ modules on which test execution depends:
 If one of C<SQL::Translator::Parser::MySQL>'s dependencies does not exist,
 then the test will be skipped.
 
+Instead of a number of tests, you can pass C<undef> if you're using
+C<done_testing()>, or C<'no_plan'> if you don't want a plan at all.
+
 =head1 EXPORTS
 
 table_ok, field_ok, constraint_ok, index_ok, view_ok, trigger_ok, procedure_ok,
@@ -608,30 +606,28 @@ maybe_plan
 
 =item Test Count Constants
 
-Constants to give the number of tests each *_ok sub uses. e.g. How many tests
-does field_ok run? Can then use these to set up the test plan easily.
+Constants to give the number of tests each C<*_ok> sub uses. e.g. How many tests
+does C<field_ok> run? Can then use these to set up the test plan easily.
 
 =item Test skipping
 
-As the test subs wrap up lots of tests in one call you can't skip idividual
+As the test subs wrap up lots of tests in one call you can't skip individual
 tests only whole sets e.g. a whole table or field.
-We could add skip_* items to the test hashes to allow per test skips. e.g.
+We could add C<skip_*> items to the test hashes to allow per test skips. e.g.
 
  skip_is_primary_key => "Need to fix primary key parsing.",
 
 =item yaml test specs
 
-Maybe have the test subs also accept yaml for the test hash ref as its a much
+Maybe have the test subs also accept yaml for the test hash ref as it is much
 nicer for writing big data structures. We can then define tests as in input
 schema file and test yaml file to compare it against.
 
 =back
 
-=head1 BUGS
-
 =head1 AUTHOR
 
-Mark D. Addison E<lt>mark.addison@itn.co.ukE<gt>, 
+Mark D. Addison E<lt>mark.addison@itn.co.ukE<gt>,
 Darren Chamberlain <darren@cpan.org>.
 
 Thanks to Ken Y. Clark for the original table and field test code taken from