X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F18ttschema-producer.t;h=d4c1e935da810fddb15959b105cb2e677a35da27;hb=1c680eb9aac43c469260f89be06027951722402e;hp=4f5f9d341507455407206fff7c183c839c6472bd;hpb=b35303534de4c377a4c9dea7a5efb8dfcf759537;p=dbsrgits%2FSQL-Translator.git diff --git a/t/18ttschema-producer.t b/t/18ttschema-producer.t index 4f5f9d3..d4c1e93 100644 --- a/t/18ttschema-producer.t +++ b/t/18ttschema-producer.t @@ -4,61 +4,100 @@ # Before `make install' is performed this script should be runnable with # `make test'. After `make install' it should work as `perl test.pl' +use strict; use Test::More; use Test::Exception; +use Test::SQL::Translator qw(maybe_plan); use Data::Dumper; -BEGIN { our %opt; map { $opt{$_}=1 if s/^-// } @ARGV; } -use constant DEBUG => (exists $opt{d} ? 1 : 0); -local $SIG{__WARN__} = sub { diag "[warn] ", @_; }; - use FindBin qw/$Bin/; # Testing 1,2,3,4... #============================================================================= -eval { require Template; }; -if ($@ && $@ =~ m!locate Template.pm in!) { - plan skip_all => "You need Template Toolkit to run this test."; -} -eval { require Test::Differences; }; -if ($@ && $@ =~ m!locate Test/Differences.pm in!) { - plan skip_all => "You need Test::Differences for this test."; +BEGIN { + eval {require Template;}; + plan skip_all => "Template v2.15 is is incompatible with SQL::Translator 0.08+" + if !$@ && Template->VERSION >= 2.15; + + maybe_plan(6, + 'XML::XPath', + 'SQL::Translator::Parser::XML::SQLFairy', + 'Template', + 'Test::Differences' + ); } use Test::Differences; -plan tests => 3; - + use SQL::Translator; use SQL::Translator::Producer::TTSchema; -# Parse the test XML schema -our $obj; -$obj = SQL::Translator->new( - debug => DEBUG, #$opt{d}, - show_warnings => 1, - add_drop_table => 1, - from => "SqlfXML", - filename => "$Bin/data/xml/schema-basic.xml", - to => "TTSchema", - producer_args => { - ttfile => "$Bin/data/template/basic.tt", - }, -); -my $out; -lives_ok { $out = $obj->translate; } "Produced template"; -ok $out ne "" ,"Output has some content"; -local $/ = undef; # slurp -eq_or_diff $out, ,"Output looks right"; -# I'm sure if this diff is the best test, it is probaly too sensitive. But it -# at least it will blow up if anything changes! - -print $out if DEBUG; -#print "Debug:", Dumper($obj) if DEBUG; +# Main test. Template whole schema and test tt_vars +{ + my $obj; + $obj = SQL::Translator->new( + show_warnings => 1, + from => "XML-SQLFairy", + filename => "$Bin/data/xml/schema.xml", + to => "TTSchema", + producer_args => { + ttfile => "$Bin/data/template/basic.tt", +# ttfile => "$Bin/data/template/test.tt", + tt_vars => { + foo => 'bar', + hello => 'world', + }, + }, + ); + my $out; + lives_ok { $out = $obj->translate; } "Translate ran"; +# print STDERR "Output: $out\n"; + ok $out ne "" ,"Produced something!"; + local $/ = undef; # slurp + eq_or_diff $out, ,"Output looks right"; +} + +# Test passing of Template config +{ + my $tmpl = q{ + [%- FOREACH table = schema.get_tables %] + Table: $table + [%- END %]}; + my $obj; + $obj = SQL::Translator->new( + show_warnings => 1, + from => "XML-SQLFairy", + filename => "$Bin/data/xml/schema.xml", + to => "TTSchema", + producer_args => { + ttfile => \$tmpl, + tt_conf => { + INTERPOLATE => 1, + }, + tt_vars => { + foo => 'bar', + hello => 'world', + }, + }, + ); + my $out; + lives_ok { $out = $obj->translate; } "Translate ran"; + ok $out ne "" ,"Produced something!"; + local $/ = undef; # slurp + eq_or_diff $out, q{ + Table: Basic + Table: Another} + ,"Output looks right"; +} + __DATA__ Schema: Database: +Foo: bar +Hello: world + Table: Basic ========================================================================== @@ -75,7 +114,6 @@ Fields foreign_key_reference: is_valid: 1 order: 1 - extra: table: Basic title @@ -90,7 +128,6 @@ Fields foreign_key_reference: is_valid: 1 order: 2 - extra: table: Basic description @@ -105,7 +142,6 @@ Fields foreign_key_reference: is_valid: 1 order: 3 - extra: table: Basic email @@ -120,7 +156,6 @@ Fields foreign_key_reference: is_valid: 1 order: 4 - extra: table: Basic explicitnulldef @@ -135,7 +170,6 @@ Fields foreign_key_reference: is_valid: 1 order: 5 - extra: table: Basic explicitemptystring @@ -150,10 +184,9 @@ Fields foreign_key_reference: is_valid: 1 order: 6 - extra: table: Basic - singletagdef + emptytagdef data_type: varchar size: 0 is_nullable: 1 @@ -165,7 +198,34 @@ Fields foreign_key_reference: is_valid: 1 order: 7 - extra: + table: Basic + + another_id + data_type: int + size: 10 + is_nullable: 1 + default_value: 2 + is_primary_key: 0 + is_unique: 0 + is_auto_increment: 0 + is_foreign_key: 1 + foreign_key_reference: Another + is_valid: 1 + order: 8 + table: Basic + + timest + data_type: timestamp + size: 0 + is_nullable: 1 + default_value: + is_primary_key: 0 + is_unique: 0 + is_auto_increment: 0 + is_foreign_key: 0 + foreign_key_reference: + is_valid: 1 + order: 9 table: Basic @@ -205,3 +265,51 @@ Constraints options: is_valid: 1 + ? + type: FOREIGN KEY + fields: another_id + expression: + match_type: + reference_fields: id + reference_table: Another + deferrable: 1 + on_delete: + on_update: + options: + is_valid: 1 + +Table: Another +========================================================================== + +Fields + id + data_type: int + size: 10 + is_nullable: 0 + default_value: + is_primary_key: 1 + is_unique: 0 + is_auto_increment: 1 + is_foreign_key: 0 + foreign_key_reference: + is_valid: 1 + order: 10 + table: Another + + +Indices + +Constraints + ? + type: PRIMARY KEY + fields: id + expression: + match_type: + reference_fields: + reference_table: + deferrable: 1 + on_delete: + on_update: + options: + is_valid: 1 +