X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F18ttschema-producer.t;h=7b5d5b93a41b444510f608e9391440b650cb6412;hb=5991fad4ab740ce45140eab7c6fe930a3d57241c;hp=8bf46c6d34a726bc3da41ae4e8b4f45e1777806d;hpb=046d668a030d767eb367fe2045a395d7bffd482b;p=dbsrgits%2FSQL-Translator.git diff --git a/t/18ttschema-producer.t b/t/18ttschema-producer.t index 8bf46c6..7b5d5b9 100644 --- a/t/18ttschema-producer.t +++ b/t/18ttschema-producer.t @@ -10,53 +10,86 @@ use Test::Exception; use Test::SQL::Translator qw(maybe_plan); use Data::Dumper; -use vars '%opt'; -BEGIN { map { $opt{$_}=1 if s/^-// } @ARGV; } -use constant DEBUG => (exists $opt{d} ? 1 : 0); - use FindBin qw/$Bin/; # Testing 1,2,3,4... #============================================================================= BEGIN { - maybe_plan(3, + eval {require Template;}; + + if ( $@ ) { + plan skip_all => 'Template not installed?' + } + + maybe_plan(6, 'XML::XPath', 'SQL::Translator::Parser::XML::SQLFairy', 'Template', 'Test::Differences' - ) + ); } use Test::Differences; use SQL::Translator; use SQL::Translator::Producer::TTSchema; -# Parse the test XML schema -my $obj; -$obj = SQL::Translator->new( - debug => DEBUG, #$opt{d}, - show_warnings => 1, - add_drop_table => 1, - from => "XML-SQLFairy", - filename => "$Bin/data/xml/schema.xml", - to => "TTSchema", - producer_args => { - ttfile => "$Bin/data/template/basic.tt", - ttargs => { - foo => 'bar', - hello => 'world', +# Main test. Template whole schema and test tt_vars +{ + my $obj; + $obj = SQL::Translator->new( + show_warnings => 0, + from => "XML-SQLFairy", + filename => "$Bin/data/xml/schema.xml", + to => "TTSchema", + producer_args => { + ttfile => "$Bin/data/template/basic.tt", + 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, ,"Output looks right"; + ); + my $out; + lives_ok { $out = $obj->translate; } "Translate ran"; + 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 => 0, + 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"; +} -print $out if DEBUG; -#print "Debug:", Dumper($obj) if DEBUG; __DATA__ Schema: @@ -113,7 +146,7 @@ Fields email data_type: varchar - size: 255 + size: 500 is_nullable: 1 default_value: is_primary_key: 0 @@ -167,6 +200,34 @@ Fields order: 7 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 + Indices titleindex @@ -204,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 +