X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FTest%2FSQL%2FTranslator.pm;h=7a9a47528717481d638d17ae4f2bec7051b5685c;hb=b178940934ec79968ed16511ec2644f3736c92f2;hp=2f4b040d6ae14a152348ede8cf3c2eb6e2e12b76;hpb=49133ae73acfa89f849086f5c28fc9395cd2bebe;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/Test/SQL/Translator.pm b/lib/Test/SQL/Translator.pm index 2f4b040..7a9a475 100644 --- a/lib/Test/SQL/Translator.pm +++ b/lib/Test/SQL/Translator.pm @@ -1,7 +1,7 @@ package Test::SQL::Translator; # ---------------------------------------------------------------------- -# $Id: Translator.pm,v 1.4 2004-03-04 14:41:49 dlc Exp $ +# $Id: Translator.pm,v 1.7 2004-11-05 15:03:10 grommit Exp $ # ---------------------------------------------------------------------- # Copyright (C) 2003 The SQLFairy Authors # @@ -34,8 +34,8 @@ use warnings; use base qw(Exporter); use vars qw($VERSION @EXPORT @EXPORT_OK); -$VERSION = sprintf "%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/; -@EXPORT = qw( +$VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/; +@EXPORT = qw( schema_ok table_ok field_ok @@ -48,7 +48,6 @@ $VERSION = sprintf "%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/; ); use Test::More; -use Test::Exception; use SQL::Translator::Schema::Constants; # $ATTRIBUTES{ } = { => , ... } @@ -82,6 +81,7 @@ $ATTRIBUTES{constraint} = { on_update => '', reference_fields => [], reference_table => '', + extra => {}, }; $ATTRIBUTES{'index'} = { fields => [], @@ -89,12 +89,14 @@ $ATTRIBUTES{'index'} = { name => "", options => [], type => NORMAL, + extra => {}, }; $ATTRIBUTES{'view'} = { name => "", sql => "", fields => [], is_valid => 1, + extra => {}, }; $ATTRIBUTES{'trigger'} = { name => '', @@ -103,6 +105,7 @@ $ATTRIBUTES{'trigger'} = { on_table => undef, action => undef, is_valid => 1, + extra => {}, }; $ATTRIBUTES{'procedure'} = { name => '', @@ -110,6 +113,7 @@ $ATTRIBUTES{'procedure'} = { parameters => [], owner => '', comments => '', + extra => {}, }; $ATTRIBUTES{table} = { comments => undef, @@ -121,6 +125,7 @@ $ATTRIBUTES{table} = { constraints => undef, indices => undef, is_valid => 1, + extra => {}, }; $ATTRIBUTES{schema} = { name => '', @@ -130,6 +135,7 @@ $ATTRIBUTES{schema} = { triggers => undef, # [] when set views => undef, # [] when set is_valid => 1, + extra => {}, }; @@ -244,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 { @@ -263,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 { @@ -285,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 { @@ -303,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 { @@ -323,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 { @@ -337,6 +353,8 @@ sub table_ok { 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}}; @@ -388,8 +406,6 @@ sub _test_kids { } } - - sub schema_ok { my ($obj,$test,$name) = @_; my $t_name = t_name($name); @@ -399,6 +415,8 @@ 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}, "$t_name is ".($test->{is_valid} ? '' : 'not ').'valid' ); @@ -472,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", @@ -512,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 @@ -572,13 +591,9 @@ maybe_plan =item Test the tests! -=item schema_ok() - -Test whole schema. - =item Test Count Constants -Constants to give the number of tests each *_ok sub uses. e.g. How many tests +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 @@ -601,7 +616,7 @@ schema file and test yaml file to compare it against. =head1 AUTHOR -Mark D. Addison Emark.addison@itn.co.ukE. +Mark D. Addison Emark.addison@itn.co.ukE, Darren Chamberlain . Thanks to Ken Y. Clark for the original table and field test code taken from his mysql test.