4 # Before `make install' is performed this script should be runnable with
5 # `make test'. After `make install' it should work as `perl test.pl'
12 use Test::SQL::Translator qw(maybe_plan);
16 BEGIN { map { $opt{$_}=1 if s/^-// } @ARGV; }
17 use constant DEBUG => (exists $opt{d} ? 1 : 0);
18 use constant TRACE => (exists $opt{t} ? 1 : 0);
22 my $file = "$Bin/data/mysql/sqlfxml-producer-basic.sql";
24 local $SIG{__WARN__} = sub {
26 unless $_[0] =~ m#XML/Writer#;
30 #=============================================================================
36 'SQL::Translator::Producer::XML::SQLFairy');
39 use Test::Differences;
41 use SQL::Translator::Producer::XML::SQLFairy;
50 <schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
52 <table name="Basic" order="1">
54 <field name="id" data_type="integer" size="10" is_nullable="0" is_auto_increment="1" is_primary_key="1" is_foreign_key="0" order="1">
56 <comments>comment on id field</comments>
58 <field name="title" data_type="varchar" size="100" is_nullable="0" default_value="hello" is_auto_increment="0" is_primary_key="0" is_foreign_key="0" order="2">
62 <field name="description" data_type="text" size="65535" is_nullable="1" default_value="" is_auto_increment="0" is_primary_key="0" is_foreign_key="0" order="3">
66 <field name="email" data_type="varchar" size="255" is_nullable="1" is_auto_increment="0" is_primary_key="0" is_foreign_key="0" order="4">
72 <index name="titleindex" type="NORMAL" fields="title" options="" />
75 <constraint name="" type="PRIMARY KEY" fields="id" reference_table="" reference_fields="" on_delete="" on_update="" match_type="" expression="" options="" deferrable="1" />
76 <constraint name="" type="UNIQUE" fields="email" reference_table="" reference_fields="" on_delete="" on_update="" match_type="" expression="" options="" deferrable="1" />
82 <procedures></procedures>
86 $obj = SQL::Translator->new(
94 $xml = $obj->translate($file) or die $obj->error;
95 ok("$xml" ne "" ,"Produced something!");
96 print "XML:\n$xml" if DEBUG;
97 # Strip sqlf header with its variable date so we diff safely
98 $xml =~ s/^([^\n]*\n){7}//m;
99 eq_or_diff $xml, $ans, "XML looks right";
106 # Thanks to Ken for the schema setup lifted from 13schema.t
111 <schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
114 <view name="foo_view" fields="name,age" order="1">
115 <sql>select name, age from person</sql>
118 <triggers></triggers>
119 <procedures></procedures>
123 $obj = SQL::Translator->new(
129 to => "XML-SQLFairy",
131 my $s = $obj->schema;
132 my $name = 'foo_view';
133 my $sql = 'select name, age from person';
134 my $fields = 'name, age';
135 my $v = $s->add_view(
142 # As we have created a Schema we give translate a dummy string so that
143 # it will run the produce.
144 lives_ok {$xml =$obj->translate("FOO");} "Translate (View) ran";
145 ok("$xml" ne "" ,"Produced something!");
146 print "XML attrib_values=>1:\n$xml" if DEBUG;
147 # Strip sqlf header with its variable date so we diff safely
148 $xml =~ s/^([^\n]*\n){7}//m;
149 eq_or_diff $xml, $ans ,"XML looks right";
155 # Thanks to Ken for the schema setup lifted from 13schema.t
160 <schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
164 <trigger name="foo_trigger" database_event="insert" on_table="foo" perform_action_when="after" order="1">
165 <action>update modified=timestamp();</action>
168 <procedures></procedures>
172 $obj = SQL::Translator->new(
178 to => "XML-SQLFairy",
180 my $s = $obj->schema;
181 my $name = 'foo_trigger';
182 my $perform_action_when = 'after';
183 my $database_event = 'insert';
184 my $on_table = 'foo';
185 my $action = 'update modified=timestamp();';
186 my $t = $s->add_trigger(
188 perform_action_when => $perform_action_when,
189 database_event => $database_event,
190 on_table => $on_table,
194 # As we have created a Schema we give translate a dummy string so that
195 # it will run the produce.
196 lives_ok {$xml =$obj->translate("FOO");} "Translate (Trigger) ran";
197 ok("$xml" ne "" ,"Produced something!");
198 print "XML attrib_values=>1:\n$xml" if DEBUG;
199 # Strip sqlf header with its variable date so we diff safely
200 $xml =~ s/^([^\n]*\n){7}//m;
201 eq_or_diff $xml, $ans ,"XML looks right";
207 # Thanks to Ken for the schema setup lifted from 13schema.t
212 <schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
215 <triggers></triggers>
217 <procedure name="foo_proc" parameters="foo,bar" owner="Nomar" order="1">
218 <sql>select foo from bar</sql>
219 <comments>Go Sox!</comments>
225 $obj = SQL::Translator->new(
231 to => "XML-SQLFairy",
233 my $s = $obj->schema;
234 my $name = 'foo_proc';
235 my $sql = 'select foo from bar';
236 my $parameters = 'foo, bar';
238 my $comments = 'Go Sox!';
239 my $p = $s->add_procedure(
242 parameters => $parameters,
244 comments => $comments,
247 # As we have created a Schema we give translate a dummy string so that
248 # it will run the produce.
249 lives_ok {$xml =$obj->translate("FOO");} "Translate (Procedure) ran";
250 ok("$xml" ne "" ,"Produced something!");
251 print "XML attrib_values=>1:\n$xml" if DEBUG;
252 # Strip sqlf header with its variable date so we diff safely
253 $xml =~ s/^([^\n]*\n){7}//m;
254 eq_or_diff $xml, $ans ,"XML looks right";
264 <schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
266 <table name="Basic" order="2">
268 <field name="foo" data_type="integer" size="10" is_nullable="1" is_auto_increment="0" is_primary_key="0" is_foreign_key="0" order="5">
269 <extra ZEROFILL="1" />
270 <comments></comments>
274 <constraints></constraints>
278 <triggers></triggers>
279 <procedures></procedures>
283 $obj = SQL::Translator->new(
289 to => "XML-SQLFairy",
291 my $s = $obj->schema;
292 my $t = $s->add_table( name => "Basic" ) or die $s->error;
293 my $f = $t->add_field(
295 data_type => "integer",
298 $f->extra(ZEROFILL => "1");
300 # As we have created a Schema we give translate a dummy string so that
301 # it will run the produce.
302 lives_ok {$xml =$obj->translate("FOO");} "Translate (Field.extra) ran";
303 ok("$xml" ne "" ,"Produced something!");
304 print "XML:\n$xml" if DEBUG;
305 # Strip sqlf header with its variable date so we diff safely
306 $xml =~ s/^([^\n]*\n){7}//m;
307 eq_or_diff $xml, $ans ,"XML looks right";