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'
15 BEGIN { map { $opt{$_}=1 if s/^-// } @ARGV; }
16 use constant DEBUG => (exists $opt{d} ? 1 : 0);
17 use constant TRACE => (exists $opt{t} ? 1 : 0);
21 my $file = "$Bin/data/mysql/sqlfxml-producer-basic.sql";
23 local $SIG{__WARN__} = sub {
25 unless $_[0] =~ m#XML/Writer#;
29 #=============================================================================
31 eval { require XML::Writer; };
32 if ($@ && $@ =~ m!locate XML::Writer.pm in!) {
33 plan skip_all => "You need XML::Writer to use XML::SQLFairy.";
35 eval { require Test::Differences; };
36 if ($@ && $@ =~ m!locate Test/Differences.pm in!) {
37 plan skip_all => "You need Test::Differences for this test.";
39 use Test::Differences;
43 use SQL::Translator::Producer::XML::SQLFairy;
46 # emit_empty_tags => 0
52 <sqlt:schema xmlns:sqlt="http://sqlfairy.sourceforge.net/sqlfairy.xml">
53 <sqlt:name></sqlt:name>
54 <sqlt:database></sqlt:database>
56 <sqlt:name>Basic</sqlt:name>
57 <sqlt:order>1</sqlt:order>
60 <sqlt:name>id</sqlt:name>
61 <sqlt:data_type>integer</sqlt:data_type>
62 <sqlt:size>10</sqlt:size>
63 <sqlt:is_nullable>0</sqlt:is_nullable>
64 <sqlt:is_auto_increment>1</sqlt:is_auto_increment>
65 <sqlt:is_primary_key>1</sqlt:is_primary_key>
66 <sqlt:is_foreign_key>0</sqlt:is_foreign_key>
67 <sqlt:comments>comment on id field</sqlt:comments>
68 <sqlt:order>1</sqlt:order>
71 <sqlt:name>title</sqlt:name>
72 <sqlt:data_type>varchar</sqlt:data_type>
73 <sqlt:size>100</sqlt:size>
74 <sqlt:is_nullable>0</sqlt:is_nullable>
75 <sqlt:default_value>hello</sqlt:default_value>
76 <sqlt:is_auto_increment>0</sqlt:is_auto_increment>
77 <sqlt:is_primary_key>0</sqlt:is_primary_key>
78 <sqlt:is_foreign_key>0</sqlt:is_foreign_key>
79 <sqlt:comments></sqlt:comments>
80 <sqlt:order>2</sqlt:order>
83 <sqlt:name>description</sqlt:name>
84 <sqlt:data_type>text</sqlt:data_type>
85 <sqlt:size>65535</sqlt:size>
86 <sqlt:is_nullable>1</sqlt:is_nullable>
87 <sqlt:default_value></sqlt:default_value>
88 <sqlt:is_auto_increment>0</sqlt:is_auto_increment>
89 <sqlt:is_primary_key>0</sqlt:is_primary_key>
90 <sqlt:is_foreign_key>0</sqlt:is_foreign_key>
91 <sqlt:comments></sqlt:comments>
92 <sqlt:order>3</sqlt:order>
95 <sqlt:name>email</sqlt:name>
96 <sqlt:data_type>varchar</sqlt:data_type>
97 <sqlt:size>255</sqlt:size>
98 <sqlt:is_nullable>1</sqlt:is_nullable>
99 <sqlt:is_auto_increment>0</sqlt:is_auto_increment>
100 <sqlt:is_primary_key>0</sqlt:is_primary_key>
101 <sqlt:is_foreign_key>0</sqlt:is_foreign_key>
102 <sqlt:comments></sqlt:comments>
103 <sqlt:order>4</sqlt:order>
108 <sqlt:name>titleindex</sqlt:name>
109 <sqlt:type>NORMAL</sqlt:type>
110 <sqlt:fields>title</sqlt:fields>
111 <sqlt:options></sqlt:options>
116 <sqlt:name></sqlt:name>
117 <sqlt:type>PRIMARY KEY</sqlt:type>
118 <sqlt:fields>id</sqlt:fields>
119 <sqlt:reference_table></sqlt:reference_table>
120 <sqlt:on_delete></sqlt:on_delete>
121 <sqlt:on_update></sqlt:on_update>
122 <sqlt:match_type></sqlt:match_type>
123 <sqlt:expression></sqlt:expression>
124 <sqlt:options></sqlt:options>
125 <sqlt:deferrable>1</sqlt:deferrable>
128 <sqlt:name></sqlt:name>
129 <sqlt:type>UNIQUE</sqlt:type>
130 <sqlt:fields>email</sqlt:fields>
131 <sqlt:reference_table></sqlt:reference_table>
132 <sqlt:on_delete></sqlt:on_delete>
133 <sqlt:on_update></sqlt:on_update>
134 <sqlt:match_type></sqlt:match_type>
135 <sqlt:expression></sqlt:expression>
136 <sqlt:options></sqlt:options>
137 <sqlt:deferrable>1</sqlt:deferrable>
144 $obj = SQL::Translator->new(
150 to => 'XML-SQLFairy',
152 lives_ok {$xml = $obj->translate($file);} "Translate (emit_empty_tags=>0) ran";
153 ok("$xml" ne "" ,"Produced something!");
154 print "XML:\n$xml" if DEBUG;
155 # Strip sqlf header with its variable date so we diff safely
156 $xml =~ s/^([^\n]*\n){7}//m;
157 eq_or_diff $xml, $ans ,"XML looks right";
159 } # end emit_empty_tags=>0
162 # emit_empty_tags => 1
168 <sqlt:schema xmlns:sqlt="http://sqlfairy.sourceforge.net/sqlfairy.xml">
169 <sqlt:name></sqlt:name>
170 <sqlt:database></sqlt:database>
172 <sqlt:name>Basic</sqlt:name>
173 <sqlt:order>2</sqlt:order>
176 <sqlt:name>id</sqlt:name>
177 <sqlt:data_type>integer</sqlt:data_type>
178 <sqlt:size>10</sqlt:size>
179 <sqlt:is_nullable>0</sqlt:is_nullable>
180 <sqlt:default_value></sqlt:default_value>
181 <sqlt:is_auto_increment>1</sqlt:is_auto_increment>
182 <sqlt:is_primary_key>1</sqlt:is_primary_key>
183 <sqlt:is_foreign_key>0</sqlt:is_foreign_key>
184 <sqlt:comments>comment on id field</sqlt:comments>
185 <sqlt:order>5</sqlt:order>
188 <sqlt:name>title</sqlt:name>
189 <sqlt:data_type>varchar</sqlt:data_type>
190 <sqlt:size>100</sqlt:size>
191 <sqlt:is_nullable>0</sqlt:is_nullable>
192 <sqlt:default_value>hello</sqlt:default_value>
193 <sqlt:is_auto_increment>0</sqlt:is_auto_increment>
194 <sqlt:is_primary_key>0</sqlt:is_primary_key>
195 <sqlt:is_foreign_key>0</sqlt:is_foreign_key>
196 <sqlt:comments></sqlt:comments>
197 <sqlt:order>6</sqlt:order>
200 <sqlt:name>description</sqlt:name>
201 <sqlt:data_type>text</sqlt:data_type>
202 <sqlt:size>65535</sqlt:size>
203 <sqlt:is_nullable>1</sqlt:is_nullable>
204 <sqlt:default_value></sqlt:default_value>
205 <sqlt:is_auto_increment>0</sqlt:is_auto_increment>
206 <sqlt:is_primary_key>0</sqlt:is_primary_key>
207 <sqlt:is_foreign_key>0</sqlt:is_foreign_key>
208 <sqlt:comments></sqlt:comments>
209 <sqlt:order>7</sqlt:order>
212 <sqlt:name>email</sqlt:name>
213 <sqlt:data_type>varchar</sqlt:data_type>
214 <sqlt:size>255</sqlt:size>
215 <sqlt:is_nullable>1</sqlt:is_nullable>
216 <sqlt:default_value></sqlt:default_value>
217 <sqlt:is_auto_increment>0</sqlt:is_auto_increment>
218 <sqlt:is_primary_key>0</sqlt:is_primary_key>
219 <sqlt:is_foreign_key>0</sqlt:is_foreign_key>
220 <sqlt:comments></sqlt:comments>
221 <sqlt:order>8</sqlt:order>
226 <sqlt:name>titleindex</sqlt:name>
227 <sqlt:type>NORMAL</sqlt:type>
228 <sqlt:fields>title</sqlt:fields>
229 <sqlt:options></sqlt:options>
234 <sqlt:name></sqlt:name>
235 <sqlt:type>PRIMARY KEY</sqlt:type>
236 <sqlt:fields>id</sqlt:fields>
237 <sqlt:reference_table></sqlt:reference_table>
238 <sqlt:reference_fields></sqlt:reference_fields>
239 <sqlt:on_delete></sqlt:on_delete>
240 <sqlt:on_update></sqlt:on_update>
241 <sqlt:match_type></sqlt:match_type>
242 <sqlt:expression></sqlt:expression>
243 <sqlt:options></sqlt:options>
244 <sqlt:deferrable>1</sqlt:deferrable>
247 <sqlt:name></sqlt:name>
248 <sqlt:type>UNIQUE</sqlt:type>
249 <sqlt:fields>email</sqlt:fields>
250 <sqlt:reference_table></sqlt:reference_table>
251 <sqlt:reference_fields></sqlt:reference_fields>
252 <sqlt:on_delete></sqlt:on_delete>
253 <sqlt:on_update></sqlt:on_update>
254 <sqlt:match_type></sqlt:match_type>
255 <sqlt:expression></sqlt:expression>
256 <sqlt:options></sqlt:options>
257 <sqlt:deferrable>1</sqlt:deferrable>
264 $obj = SQL::Translator->new(
270 to => 'XML-SQLFairy',
271 producer_args => { emit_empty_tags => 1 },
273 lives_ok { $xml=$obj->translate($file); } "Translate (emit_empty_tags=>1) ran";
274 ok("$xml" ne "" ,"Produced something!");
275 print "XML emit_empty_tags=>1:\n$xml" if DEBUG;
276 # Strip sqlf header with its variable date so we diff safely
277 $xml =~ s/^([^\n]*\n){7}//m;
278 eq_or_diff $xml, $ans ,"XML looks right";
280 } # end emit_empty_tags => 1
289 <sqlt:schema name="" database="" xmlns:sqlt="http://sqlfairy.sourceforge.net/sqlfairy.xml">
290 <sqlt:table name="Basic" order="3">
292 <sqlt:field name="id" data_type="integer" size="10" is_nullable="0" is_auto_increment="1" is_primary_key="1" is_foreign_key="0" comments="comment on id field" order="9" />
293 <sqlt: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" comments="" order="10" />
294 <sqlt: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" comments="" order="11" />
295 <sqlt:field name="email" data_type="varchar" size="255" is_nullable="1" is_auto_increment="0" is_primary_key="0" is_foreign_key="0" comments="" order="12" />
298 <sqlt:index name="titleindex" type="NORMAL" fields="title" options="" />
301 <sqlt:constraint name="" type="PRIMARY KEY" fields="id" reference_table="" on_delete="" on_update="" match_type="" expression="" options="" deferrable="1" />
302 <sqlt:constraint name="" type="UNIQUE" fields="email" reference_table="" on_delete="" on_update="" match_type="" expression="" options="" deferrable="1" />
308 $obj = SQL::Translator->new(
314 to => "XML-SQLFairy",
315 producer_args => { attrib_values => 1 },
317 lives_ok {$xml = $obj->translate($file);} "Translate (attrib_values=>1) ran";
318 ok("$xml" ne "" ,"Produced something!");
319 print "XML attrib_values=>1:\n$xml" if DEBUG;
320 # Strip sqlf header with its variable date so we diff safely
321 $xml =~ s/^([^\n]*\n){7}//m;
322 eq_or_diff $xml, $ans ,"XML looks right";
324 } # end attrib_values => 1
329 # Thanks to Ken for the schema setup lifted from 13schema.t
334 <sqlt:schema xmlns:sqlt="http://sqlfairy.sourceforge.net/sqlfairy.xml">
335 <sqlt:name></sqlt:name>
336 <sqlt:database></sqlt:database>
338 <sqlt:name>foo_view</sqlt:name>
339 <sqlt:sql>select name, age from person</sqlt:sql>
340 <sqlt:fields>name,age</sqlt:fields>
341 <sqlt:order>1</sqlt:order>
346 $obj = SQL::Translator->new(
352 to => "XML-SQLFairy",
354 my $s = $obj->schema;
355 my $name = 'foo_view';
356 my $sql = 'select name, age from person';
357 my $fields = 'name, age';
358 my $v = $s->add_view(
365 # As we have created a Schema we give translate a dummy string so that
366 # it will run the produce.
367 lives_ok {$xml =$obj->translate("FOO");} "Translate (View) ran";
368 ok("$xml" ne "" ,"Produced something!");
369 print "XML attrib_values=>1:\n$xml" if DEBUG;
370 # Strip sqlf header with its variable date so we diff safely
371 $xml =~ s/^([^\n]*\n){7}//m;
372 eq_or_diff $xml, $ans ,"XML looks right";
378 # Thanks to Ken for the schema setup lifted from 13schema.t
383 <sqlt:schema xmlns:sqlt="http://sqlfairy.sourceforge.net/sqlfairy.xml">
384 <sqlt:name></sqlt:name>
385 <sqlt:database></sqlt:database>
387 <sqlt:name>foo_trigger</sqlt:name>
388 <sqlt:database_event>insert</sqlt:database_event>
389 <sqlt:action>update modified=timestamp();</sqlt:action>
390 <sqlt:on_table>foo</sqlt:on_table>
391 <sqlt:perform_action_when>after</sqlt:perform_action_when>
392 <sqlt:order>1</sqlt:order>
397 $obj = SQL::Translator->new(
403 to => "XML-SQLFairy",
405 my $s = $obj->schema;
406 my $name = 'foo_trigger';
407 my $perform_action_when = 'after';
408 my $database_event = 'insert';
409 my $on_table = 'foo';
410 my $action = 'update modified=timestamp();';
411 my $t = $s->add_trigger(
413 perform_action_when => $perform_action_when,
414 database_event => $database_event,
415 on_table => $on_table,
419 # As we have created a Schema we give translate a dummy string so that
420 # it will run the produce.
421 lives_ok {$xml =$obj->translate("FOO");} "Translate (Trigger) ran";
422 ok("$xml" ne "" ,"Produced something!");
423 print "XML attrib_values=>1:\n$xml" if DEBUG;
424 # Strip sqlf header with its variable date so we diff safely
425 $xml =~ s/^([^\n]*\n){7}//m;
426 eq_or_diff $xml, $ans ,"XML looks right";
432 # Thanks to Ken for the schema setup lifted from 13schema.t
437 <sqlt:schema xmlns:sqlt="http://sqlfairy.sourceforge.net/sqlfairy.xml">
438 <sqlt:name></sqlt:name>
439 <sqlt:database></sqlt:database>
441 <sqlt:name>foo_proc</sqlt:name>
442 <sqlt:sql>select foo from bar</sqlt:sql>
443 <sqlt:parameters>foo,bar</sqlt:parameters>
444 <sqlt:owner>Nomar</sqlt:owner>
445 <sqlt:comments>Go Sox!</sqlt:comments>
446 <sqlt:order>1</sqlt:order>
451 $obj = SQL::Translator->new(
457 to => "XML-SQLFairy",
459 my $s = $obj->schema;
460 my $name = 'foo_proc';
461 my $sql = 'select foo from bar';
462 my $parameters = 'foo, bar';
464 my $comments = 'Go Sox!';
465 my $p = $s->add_procedure(
468 parameters => $parameters,
470 comments => $comments,
473 # As we have created a Schema we give translate a dummy string so that
474 # it will run the produce.
475 lives_ok {$xml =$obj->translate("FOO");} "Translate (Procedure) ran";
476 ok("$xml" ne "" ,"Produced something!");
477 print "XML attrib_values=>1:\n$xml" if DEBUG;
478 # Strip sqlf header with its variable date so we diff safely
479 $xml =~ s/^([^\n]*\n){7}//m;
480 eq_or_diff $xml, $ans ,"XML looks right";