All Schema objects now have an extra attribute. Added parsing support (and
[dbsrgits/SQL-Translator.git] / lib / Test / SQL / Translator.pm
index b2d67e9..7a9a475 100644 (file)
@@ -1,7 +1,7 @@
 package Test::SQL::Translator;
 
 # ----------------------------------------------------------------------
-# $Id: Translator.pm,v 1.1 2004-02-29 18:26:53 grommit Exp $
+# $Id: Translator.pm,v 1.7 2004-11-05 15:03:10 grommit Exp $
 # ----------------------------------------------------------------------
 # Copyright (C) 2003 The SQLFairy Authors
 #
@@ -34,8 +34,9 @@ use warnings;
 use base qw(Exporter);
 
 use vars qw($VERSION @EXPORT @EXPORT_OK);
-$VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/;
-@EXPORT = qw( 
+$VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/;
+@EXPORT = qw(
+    schema_ok
     table_ok
     field_ok
     constraint_ok
@@ -43,11 +44,10 @@ $VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/;
     view_ok
     trigger_ok
     procedure_ok
+    maybe_plan
 );
-# TODO schema_ok
 
 use Test::More;
-use Test::Exception;
 use SQL::Translator::Schema::Constants;
 
 # $ATTRIBUTES{ <schema_object_name> } = { <attribname> => <default>, ... }
@@ -81,6 +81,7 @@ $ATTRIBUTES{constraint} = {
     on_update => '',
     reference_fields => [],
     reference_table => '',
+    extra => {},
 };
 $ATTRIBUTES{'index'} = {
     fields => [],
@@ -88,11 +89,14 @@ $ATTRIBUTES{'index'} = {
     name => "",
     options => [],
     type => NORMAL,
+    extra => {},
 };
 $ATTRIBUTES{'view'} = {
     name => "",
     sql => "",
     fields => [],
+    is_valid  => 1,
+    extra => {},
 };
 $ATTRIBUTES{'trigger'} = {
     name                => '',
@@ -100,6 +104,8 @@ $ATTRIBUTES{'trigger'} = {
     database_event      => undef,
     on_table            => undef,
     action              => undef,
+    is_valid            => 1,
+    extra => {},
 };
 $ATTRIBUTES{'procedure'} = {
     name       => '',
@@ -107,6 +113,7 @@ $ATTRIBUTES{'procedure'} = {
     parameters => [],
     owner      => '',
     comments   => '',
+    extra => {},
 };
 $ATTRIBUTES{table} = {
     comments   => undef,
@@ -117,6 +124,18 @@ $ATTRIBUTES{table} = {
     fields      => undef,
     constraints => undef,
     indices     => undef,
+    is_valid    => 1,
+    extra       => {},
+};
+$ATTRIBUTES{schema} = {
+    name       => '',
+    database   => '',
+    procedures => undef, # [] when set
+    tables     => undef, # [] when set
+    triggers   => undef, # [] when set
+    views      => undef, # [] when set
+    is_valid   => 1,
+    extra => {},
 };
 
 
@@ -148,13 +167,17 @@ sub field_ok {
 
     unless ($f1) {
         fail " Field '$test->{name}' doesn't exist!";
+        # TODO Do a skip on the following tests. Currently the test counts wont
+        # match at the end. So at least it fails.
         return;
     }
 
-    is( $f1->name, $test->{name}, "${t_name}Field name '$test->{name}'" );
+    my $full_name = $f1->table->name.".".$test->{name};
+
+    is( $f1->name, $test->{name}, "${t_name}Field '$full_name'" );
 
     is( $f1->is_valid, $test->{is_valid},
-    "$t_name    is".($test->{is_valid} ? '' : 'not ').'valid' );
+    "$t_name    is ".($test->{is_valid} ? '' : 'not ').'valid' );
 
     is( $f1->data_type, $test->{data_type},
         "$t_name    type is '$test->{data_type}'" );
@@ -227,6 +250,8 @@ 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" );
 }
 
 sub index_ok {
@@ -246,6 +271,8 @@ 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" );
 }
 
 sub trigger_ok {
@@ -268,6 +295,8 @@ sub trigger_ok {
         "$t_name    on_table is '$test->{on_table}'" );
 
     is( $obj->action, $test->{action}, "$t_name    action is '$test->{action}'" );
+    
+    is_deeply( { $obj->extra }, $test->{extra}, "$t_name    extra" );
 }
 
 sub view_ok {
@@ -286,6 +315,8 @@ 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" );
 }
 
 sub procedure_ok {
@@ -306,6 +337,8 @@ sub procedure_ok {
         "$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" );
 }
 
 sub table_ok {
@@ -315,41 +348,55 @@ sub table_ok {
     my %arg = %$test;
 
     my $tbl_name = $arg{name} || die "Need a table name to test.";
-    is( $obj->{name}, $arg{name}, "${t_name}Table name '$arg{name}'" );
+    is( $obj->{name}, $arg{name}, "${t_name}Table '$arg{name}'" );
 
     is_deeply( [$obj->options], $test->{options},
     "$t_name    options are '".join(",",@{$test->{options}})."'" );
 
+    is_deeply( { $obj->extra }, $test->{extra}, "$t_name    extra" );
+
     # Fields
     if ( $arg{fields} ) {
-        my @fldnames = map { $_->{name} } @{$arg{fields}};
-        is_deeply( [ map {$_->name}   $obj->get_fields ],
-                   [ map {$_->{name}} @{$arg{fields}} ],
-                   "${t_name}Table $tbl_name fields match" );
+        my @fldnames = map {$_->{name}} @{$arg{fields}};
+        is_deeply( 
+            [ map {$_->name}   $obj->get_fields ],
+            [ @fldnames ],
+            "${t_name}    field names are ".join(", ",@fldnames)
+        );
         foreach ( @{$arg{fields}} ) {
             my $f_name = $_->{name} || die "Need a field name to test.";
-            field_ok( $obj->get_field($f_name), $_, $name );
+            next unless my $fld = $obj->get_field($f_name);
+            field_ok( $fld, $_, $name );
         }
     }
     else {
         is(scalar($obj->get_fields), undef,
-            "${t_name}Table $tbl_name has no fields.");
+            "${t_name}    has no fields.");
     }
 
-    # Constraints and indices
-    my %bits = (
+    # Constraints and Indices
+    _test_kids($obj, $test, $name, {
         constraint => "constraints",
         'index'    => "indices",
-    );
-    while ( my($foo,$plural) = each %bits ) {
-        next unless defined $arg{$plural};
-        if ( my @tfoo = @{$arg{$plural}} ) {
+    });
+}
+
+sub _test_kids {
+    my ($obj, $test, $name, $kids) = @_; 
+    my $t_name = t_name($name);
+    my $obj_name = ref $obj;
+    ($obj_name) = $obj_name =~ m/^.*::(.*)$/;
+
+    while ( my($foo,$plural) = each %$kids ) {
+        next unless defined $test->{$plural};
+        if ( my @tfoo = @{$test->{$plural}} ) {
             my $meth = "get_$plural";
             my @foo = $obj->$meth;
             is(scalar(@foo), scalar(@tfoo),
-            "${t_name}Table $tbl_name has ".scalar(@tfoo)." $plural");
+            "${t_name}$obj_name has ".scalar(@tfoo)." $plural");
             foreach ( @foo ) {
-                my $ans = { table => $obj->name, %{shift @tfoo}};
+                my $ans = { lc($obj_name) => $obj->name, %{shift @tfoo}};
+                #my $ans = shift @tfoo;
                 my $meth = "${foo}_ok";
                 { no strict 'refs';
                     $meth->( $_, $ans, $name  );
@@ -363,6 +410,67 @@ sub schema_ok {
     my ($obj,$test,$name) = @_;
     my $t_name = t_name($name);
     default_attribs($test,"schema");
+
+    is( $obj->name, $test->{name}, "${t_name}Schema name is '$test->{name}'" );
+
+    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},
+    "$t_name    is ".($test->{is_valid} ? '' : 'not ').'valid' );
+
+    # Tables
+    if ( $test->{tables} ) {
+        is_deeply( [ map {$_->name}   $obj->get_tables ],
+                   [ map {$_->{name}} @{$test->{tables}} ],
+                   "${t_name}    table names match" );
+        foreach ( @{$test->{tables}} ) {
+            my $t_name = $_->{name} || die "Need a table name to test.";
+            table_ok( $obj->get_table($t_name), $_, $name );
+        }
+    }
+    else {
+        is(scalar($obj->get_tables), undef,
+            "${t_name}    has no tables.");
+    }
+
+    # Procedures, Triggers, Views
+    _test_kids($obj, $test, $name, {
+        procedure  => "procedures",
+        trigger    => "triggers",
+        view       => "views",
+    });
+}
+
+# maybe_plan($ntests, @modules)
+#
+# Calls plan $ntests if @modules can all be loaded; otherwise,
+# calls skip_all with an explanation of why the tests were skipped.
+sub maybe_plan {
+    my ($ntests, @modules) = @_;
+    my @errors;
+
+    for my $module (@modules) {
+        eval "use $module;";
+        if ($@ && $@ =~ /Can't locate (\S+)/) {
+            my $mod = $1;
+            $mod =~ s/\.pm$//;
+            $mod =~ s#/#::#g;
+            push @errors, $mod;
+        }
+    }
+
+    if (@errors) {
+        my $msg = sprintf "Missing dependenc%s: %s",
+            @errors == 1 ? 'y' : 'ies',
+            join ", ", @errors;
+        plan skip_all => $msg;
+    }
+    else {
+        plan tests => $ntests;
+    }
 }
 
 1; # compile please ===========================================================
@@ -382,11 +490,11 @@ __END__
  my $sqlt = SQL::Translator->new(
      parser => "Magic",
      filename => "$Bin/data/magic/test.magic",
-     ... 
+     ...
  );
  ...
  my $schema = $sqlt->schema;
+
  # Test the table it produced.
  table_ok( $schema->get_table("Customer"), {
      name => "Customer",
@@ -422,24 +530,25 @@ __END__
 
 =head1 DESCSIPTION
 
-Provides a set of Test::More tests for Schema objects. Tesing a parsed
+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.
+expect the schema to look. Also provides 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 
+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
 check that you your parser hasn't accidentally set schema values you didn't
-expect it to. (And makes tests look nice and long ;-)
+expect it to.
 
 For an example of the output run the t/16xml-parser.t test.
 
 =head1 Tests
 
-All the tests take a first arg of the schema object to test, followed by a 
+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 pre-pend to all the generated test
 names.
 
 =head2 table_ok
@@ -456,9 +565,25 @@ names.
 
 =head2 procedure_ok
 
+=head1 CONDITIONAL TESTS
+
+The C<maybe_plan> function handles conditionally running an individual
+test.  It is here to enable running the test suite even when dependencies
+are missing; not having (for example) GraphViz installed should not keep
+the test suite from passing.
+
+C<maybe_plan> takes the number of tests to (maybe) run, and a list of
+modules on which test execution depends:
+
+    maybe_plan(180, 'SQL::Translator::Parser::MySQL');
+
+If one of C<SQL::Translator::Parser::MySQL>'s dependencies does not exist,
+then the test will be skipped.
+
 =head1 EXPORTS
 
-table_ok, field_ok, constraint_ok, index_ok, view_ok, trigger_ok, procedure_ok
+table_ok, field_ok, constraint_ok, index_ok, view_ok, trigger_ok, procedure_ok,
+maybe_plan
 
 =head1 TODO
 
@@ -466,9 +591,10 @@ table_ok, field_ok, constraint_ok, index_ok, view_ok, trigger_ok, procedure_ok
 
 =item Test the tests!
 
-=item schema_ok()
+=item Test Count Constants
 
-Test whole schema.
+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.
 
 =item Test skipping
 
@@ -490,7 +616,7 @@ schema file and test yaml file to compare it against.
 
 =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
 his mysql test.