X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F17sqlfxml-producer.t;h=54ccf8a515110ad1dc29e5a387dff21fa896964a;hb=ebe497908ca605f223483e859c3dbc724f3d48a0;hp=1a7ced0abc31c3f5585f395394d34b46415a80f7;hpb=2e11379ef0451e54acca5dee655b0c33272008ed;p=dbsrgits%2FSQL-Translator.git diff --git a/t/17sqlfxml-producer.t b/t/17sqlfxml-producer.t index 1a7ced0..54ccf8a 100644 --- a/t/17sqlfxml-producer.t +++ b/t/17sqlfxml-producer.t @@ -9,6 +9,7 @@ local $^W = 0; use strict; use Test::More; use Test::Exception; +use Test::SQL::Translator qw(maybe_plan); use Data::Dumper; my %opt; @@ -20,32 +21,35 @@ use FindBin qw/$Bin/; my $file = "$Bin/data/mysql/sqlfxml-producer-basic.sql"; +local $SIG{__WARN__} = sub { + CORE::warn(@_) + unless $_[0] =~ m#XML/Writer#; +}; # Testing 1,2,3,4... #============================================================================= -eval { require XML::Writer; }; -if ($@ && $@ =~ m!locate XML::Writer.pm in!) { - plan skip_all => "You need XML::Writer to use XML::SQLFairy."; -} -eval { require Test::Differences; }; -if ($@ && $@ =~ m!locate Test/Differences.pm in!) { - plan skip_all => "You need Test::Differences for this test."; +BEGIN { + maybe_plan(18, + 'XML::Writer', + 'Test::Differences', + 'SQL::Translator::Producer::XML::SQLFairy'); } + use Test::Differences; -plan tests => 6; - use SQL::Translator; use SQL::Translator::Producer::XML::SQLFairy; -my ($obj,$ans,$xml); - # # emit_empty_tags => 0 # +{ +my ($obj,$ans,$xml); $ans = < + + Basic 1 @@ -53,78 +57,84 @@ $ans = < id integer + 10 + 0 1 1 - 0 0 + comment on id field 1 - 10 title varchar + 100 + 0 hello 0 0 - 0 0 + 2 - 100 description text + 65535 + 1 0 0 - 1 0 + 3 - 65535 email varchar + 255 + 1 0 0 - 1 0 + 4 - 255 - title titleindex - NORMAL + title + - 1 - - id - - + PRIMARY KEY + id + + - - PRIMARY KEY + + + + 1 - 1 - - email - - + UNIQUE + email + + - - UNIQUE + + + + 1 @@ -139,19 +149,25 @@ $obj = SQL::Translator->new( from => 'MySQL', to => 'XML-SQLFairy', ); -lives_ok { $xml = $obj->translate($file); } "Translate ran"; +lives_ok {$xml = $obj->translate($file);} "Translate (emit_empty_tags=>0) ran"; ok("$xml" ne "" ,"Produced something!"); print "XML:\n$xml" if DEBUG; # Strip sqlf header with its variable date so we diff safely $xml =~ s/^([^\n]*\n){7}//m; eq_or_diff $xml, $ans ,"XML looks right"; +} # end emit_empty_tags=>0 + # # emit_empty_tags => 1 # +{ +my ($obj,$ans,$xml); $ans = < + + Basic 2 @@ -159,89 +175,92 @@ $ans = < id integer + 10 + 0 1 1 - 0 0 + comment on id field 5 - 10 title varchar + 100 + 0 hello 0 0 - 0 0 + 6 - 100 description text + 65535 + 1 0 0 - 1 0 + 7 - 65535 email varchar + 255 + 1 0 0 - 1 0 + 8 - 255 - title titleindex - NORMAL + title + - 1 - - id - - + PRIMARY KEY + id + + - - - PRIMARY KEY + + + + 1 - 1 - - email - - + UNIQUE + email + + - - - UNIQUE + + + + 1 EOXML -undef $obj; $obj = SQL::Translator->new( debug => DEBUG, trace => TRACE, @@ -251,23 +270,212 @@ $obj = SQL::Translator->new( to => 'XML-SQLFairy', producer_args => { emit_empty_tags => 1 }, ); -lives_ok { $xml = $obj->translate($file); } "Translate ran"; +lives_ok { $xml=$obj->translate($file); } "Translate (emit_empty_tags=>1) ran"; ok("$xml" ne "" ,"Produced something!"); print "XML emit_empty_tags=>1:\n$xml" if DEBUG; # Strip sqlf header with its variable date so we diff safely $xml =~ s/^([^\n]*\n){7}//m; eq_or_diff $xml, $ans ,"XML looks right"; - # This diff probably isn't a very good test! Should really check the - # result with XPath or something, but that would take ages to write ;-) - -# TODO Make this a real test of attrib_values -# $obj = SQL::Translator->new( -# debug => DEBUG, -# trace => TRACE, -# show_warnings => 1, -# add_drop_table => 1, -# from => "MySQL", -# to => "XML-SQLFairy", -# producer_args => { attrib_values => 1 }, -# ); -# print $obj->translate($file); + +} # end emit_empty_tags => 1 + +# +# attrib_values => 1 +# +{ +my ($obj,$ans,$xml); + +$ans = < + + + + + + + + + + + + + + + + +EOXML + +$obj = SQL::Translator->new( + debug => DEBUG, + trace => TRACE, + show_warnings => 1, + add_drop_table => 1, + from => "MySQL", + to => "XML-SQLFairy", + producer_args => { attrib_values => 1 }, +); +lives_ok {$xml = $obj->translate($file);} "Translate (attrib_values=>1) ran"; +ok("$xml" ne "" ,"Produced something!"); +print "XML attrib_values=>1:\n$xml" if DEBUG; +# Strip sqlf header with its variable date so we diff safely +$xml =~ s/^([^\n]*\n){7}//m; +eq_or_diff $xml, $ans ,"XML looks right"; + +} # end attrib_values => 1 + +# +# View +# +# Thanks to Ken for the schema setup lifted from 13schema.t +{ +my ($obj,$ans,$xml); + +$ans = < + + + + foo_view + select name, age from person + name,age + 1 + + +EOXML + + $obj = SQL::Translator->new( + debug => DEBUG, + trace => TRACE, + show_warnings => 1, + add_drop_table => 1, + from => "MySQL", + to => "XML-SQLFairy", + ); + my $s = $obj->schema; + my $name = 'foo_view'; + my $sql = 'select name, age from person'; + my $fields = 'name, age'; + my $v = $s->add_view( + name => $name, + sql => $sql, + fields => $fields, + schema => $s, + ) or die $s->error; + + # As we have created a Schema we give translate a dummy string so that + # it will run the produce. + lives_ok {$xml =$obj->translate("FOO");} "Translate (View) ran"; + ok("$xml" ne "" ,"Produced something!"); + print "XML attrib_values=>1:\n$xml" if DEBUG; + # Strip sqlf header with its variable date so we diff safely + $xml =~ s/^([^\n]*\n){7}//m; + eq_or_diff $xml, $ans ,"XML looks right"; +} # end View + +# +# Trigger +# +# Thanks to Ken for the schema setup lifted from 13schema.t +{ +my ($obj,$ans,$xml); + +$ans = < + + + + foo_trigger + insert + update modified=timestamp(); + foo + after + 1 + + +EOXML + + $obj = SQL::Translator->new( + debug => DEBUG, + trace => TRACE, + show_warnings => 1, + add_drop_table => 1, + from => "MySQL", + to => "XML-SQLFairy", + ); + my $s = $obj->schema; + my $name = 'foo_trigger'; + my $perform_action_when = 'after'; + my $database_event = 'insert'; + my $on_table = 'foo'; + my $action = 'update modified=timestamp();'; + my $t = $s->add_trigger( + name => $name, + perform_action_when => $perform_action_when, + database_event => $database_event, + on_table => $on_table, + action => $action, + ) or die $s->error; + + # As we have created a Schema we give translate a dummy string so that + # it will run the produce. + lives_ok {$xml =$obj->translate("FOO");} "Translate (Trigger) ran"; + ok("$xml" ne "" ,"Produced something!"); + print "XML attrib_values=>1:\n$xml" if DEBUG; + # Strip sqlf header with its variable date so we diff safely + $xml =~ s/^([^\n]*\n){7}//m; + eq_or_diff $xml, $ans ,"XML looks right"; +} # end Trigger + +# +# Procedure +# +# Thanks to Ken for the schema setup lifted from 13schema.t +{ +my ($obj,$ans,$xml); + +$ans = < + + + + foo_proc + select foo from bar + foo,bar + Nomar + Go Sox! + 1 + + +EOXML + + $obj = SQL::Translator->new( + debug => DEBUG, + trace => TRACE, + show_warnings => 1, + add_drop_table => 1, + from => "MySQL", + to => "XML-SQLFairy", + ); + my $s = $obj->schema; + my $name = 'foo_proc'; + my $sql = 'select foo from bar'; + my $parameters = 'foo, bar'; + my $owner = 'Nomar'; + my $comments = 'Go Sox!'; + my $p = $s->add_procedure( + name => $name, + sql => $sql, + parameters => $parameters, + owner => $owner, + comments => $comments, + ) or die $s->error; + + # As we have created a Schema we give translate a dummy string so that + # it will run the produce. + lives_ok {$xml =$obj->translate("FOO");} "Translate (Procedure) ran"; + ok("$xml" ne "" ,"Produced something!"); + print "XML attrib_values=>1:\n$xml" if DEBUG; + # Strip sqlf header with its variable date so we diff safely + $xml =~ s/^([^\n]*\n){7}//m; + eq_or_diff $xml, $ans ,"XML looks right"; +} # end Procedure