Release 0.11003
[dbsrgits/SQL-Translator.git] / t / 17sqlfxml-producer.t
CommitLineData
ec791002 1#!/usr/bin/perl -w
d0c12b9f 2# vim:filetype=perl
3
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'
6
1ea530d4 7local $^W = 0;
8
d0c12b9f 9use strict;
10use Test::More;
11use Test::Exception;
2d691ec1 12use Test::SQL::Translator qw(maybe_plan);
d0c12b9f 13
14use Data::Dumper;
2e11379e 15my %opt;
d0c12b9f 16BEGIN { map { $opt{$_}=1 if s/^-// } @ARGV; }
17use constant DEBUG => (exists $opt{d} ? 1 : 0);
18use constant TRACE => (exists $opt{t} ? 1 : 0);
19
20use FindBin qw/$Bin/;
21
22my $file = "$Bin/data/mysql/sqlfxml-producer-basic.sql";
23
fbc0552f 24local $SIG{__WARN__} = sub {
25 CORE::warn(@_)
8ce5d615 26 unless $_[0] =~ m!XML/Writer!;
fbc0552f 27};
d0c12b9f 28
29# Testing 1,2,3,4...
30#=============================================================================
31
2d691ec1 32BEGIN {
87c5565e 33 maybe_plan(14,
2d691ec1 34 'XML::Writer',
35 'Test::Differences',
36 'SQL::Translator::Producer::XML::SQLFairy');
d0c12b9f 37}
d3422086 38
2d691ec1 39use Test::Differences;
d0c12b9f 40use SQL::Translator;
ac62dff1 41use SQL::Translator::Producer::XML::SQLFairy;
d0c12b9f 42
d0c12b9f 43#
ec791002 44# basic stuff
d0c12b9f 45#
1e3867bf 46{
47my ($obj,$ans,$xml);
d0c12b9f 48
49$ans = <<EOXML;
983ed646 50<schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
0eebe059 51 <extra />
87c5565e 52 <tables>
53 <table name="Basic" order="1">
0eebe059 54 <extra />
87c5565e 55 <fields>
56 <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">
57 <extra />
58 <comments>comment on id field</comments>
59 </field>
60 <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">
61 <extra />
62 <comments></comments>
63 </field>
64 <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">
65 <extra />
66 <comments></comments>
67 </field>
68 <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">
69 <extra />
70 <comments></comments>
71 </field>
72 </fields>
73 <indices>
0eebe059 74 <index name="titleindex" type="NORMAL" fields="title" options="">
75 <extra />
76 </index>
87c5565e 77 </indices>
78 <constraints>
0eebe059 79 <constraint name="" type="PRIMARY KEY" fields="id" reference_table="" reference_fields="" on_delete="" on_update="" match_type="" expression="" options="" deferrable="1">
80 <extra />
81 </constraint>
82 <constraint name="" type="UNIQUE" fields="email" reference_table="" reference_fields="" on_delete="" on_update="" match_type="" expression="" options="" deferrable="1">
83 <extra />
84 </constraint>
87c5565e 85 </constraints>
a4fb1ddc 86 <comments></comments>
87c5565e 87 </table>
88 </tables>
89 <views></views>
90 <triggers></triggers>
91 <procedures></procedures>
983ed646 92</schema>
f11724ad 93EOXML
94
95$obj = SQL::Translator->new(
96 debug => DEBUG,
97 trace => TRACE,
98 show_warnings => 1,
99 add_drop_table => 1,
100 from => "MySQL",
101 to => "XML-SQLFairy",
f11724ad 102);
87c5565e 103$xml = $obj->translate($file) or die $obj->error;
f11724ad 104ok("$xml" ne "" ,"Produced something!");
ec791002 105print "XML:\n$xml" if DEBUG;
f11724ad 106# Strip sqlf header with its variable date so we diff safely
ec791002 107$xml =~ s/^([^\n]*\n){7}//m;
108eq_or_diff $xml, $ans, "XML looks right";
f11724ad 109
ec791002 110} # end basic stuff
1e3867bf 111
112#
113# View
114#
115# Thanks to Ken for the schema setup lifted from 13schema.t
116{
117my ($obj,$ans,$xml);
118
119$ans = <<EOXML;
983ed646 120<schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
0eebe059 121 <extra />
87c5565e 122 <tables></tables>
123 <views>
124 <view name="foo_view" fields="name,age" order="1">
125 <sql>select name, age from person</sql>
0eebe059 126 <extra hello="world" />
87c5565e 127 </view>
128 </views>
129 <triggers></triggers>
130 <procedures></procedures>
983ed646 131</schema>
1e3867bf 132EOXML
133
134 $obj = SQL::Translator->new(
135 debug => DEBUG,
136 trace => TRACE,
137 show_warnings => 1,
138 add_drop_table => 1,
139 from => "MySQL",
140 to => "XML-SQLFairy",
141 );
142 my $s = $obj->schema;
143 my $name = 'foo_view';
144 my $sql = 'select name, age from person';
145 my $fields = 'name, age';
146 my $v = $s->add_view(
147 name => $name,
148 sql => $sql,
149 fields => $fields,
0eebe059 150 extra => { hello => "world" },
1e3867bf 151 schema => $s,
152 ) or die $s->error;
ec791002 153
1e3867bf 154 # As we have created a Schema we give translate a dummy string so that
155 # it will run the produce.
156 lives_ok {$xml =$obj->translate("FOO");} "Translate (View) ran";
157 ok("$xml" ne "" ,"Produced something!");
158 print "XML attrib_values=>1:\n$xml" if DEBUG;
159 # Strip sqlf header with its variable date so we diff safely
160 $xml =~ s/^([^\n]*\n){7}//m;
161 eq_or_diff $xml, $ans ,"XML looks right";
162} # end View
163
164#
165# Trigger
166#
167# Thanks to Ken for the schema setup lifted from 13schema.t
168{
169my ($obj,$ans,$xml);
170
171$ans = <<EOXML;
983ed646 172<schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
0eebe059 173 <extra />
8ce5d615 174 <tables>
d37416fd 175 <table name="Basic" order="1">
8ce5d615 176 <extra />
177 <fields></fields>
178 <indices></indices>
179 <constraints></constraints>
180 <comments></comments>
181 </table>
182 </tables>
87c5565e 183 <views></views>
184 <triggers>
34541850 185 <trigger name="foo_trigger" database_events="insert" on_table="Basic" perform_action_when="after" order="1">
87c5565e 186 <action>update modified=timestamp();</action>
0eebe059 187 <extra hello="world" />
87c5565e 188 </trigger>
189 </triggers>
190 <procedures></procedures>
983ed646 191</schema>
1e3867bf 192EOXML
193
194 $obj = SQL::Translator->new(
195 debug => DEBUG,
196 trace => TRACE,
197 show_warnings => 1,
198 add_drop_table => 1,
199 from => "MySQL",
200 to => "XML-SQLFairy",
201 );
202 my $s = $obj->schema;
203 my $name = 'foo_trigger';
204 my $perform_action_when = 'after';
205 my $database_event = 'insert';
1e3867bf 206 my $action = 'update modified=timestamp();';
8ce5d615 207 my $table = $s->add_table( name => "Basic" ) or die $s->error;
1e3867bf 208 my $t = $s->add_trigger(
209 name => $name,
210 perform_action_when => $perform_action_when,
f9c96971 211 database_events => [$database_event],
8ce5d615 212 table => $table,
1e3867bf 213 action => $action,
0eebe059 214 extra => { hello => "world" },
1e3867bf 215 ) or die $s->error;
ec791002 216
1e3867bf 217 # As we have created a Schema we give translate a dummy string so that
218 # it will run the produce.
219 lives_ok {$xml =$obj->translate("FOO");} "Translate (Trigger) ran";
220 ok("$xml" ne "" ,"Produced something!");
221 print "XML attrib_values=>1:\n$xml" if DEBUG;
222 # Strip sqlf header with its variable date so we diff safely
223 $xml =~ s/^([^\n]*\n){7}//m;
224 eq_or_diff $xml, $ans ,"XML looks right";
225} # end Trigger
226
227#
228# Procedure
229#
230# Thanks to Ken for the schema setup lifted from 13schema.t
231{
232my ($obj,$ans,$xml);
233
234$ans = <<EOXML;
983ed646 235<schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
0eebe059 236 <extra />
87c5565e 237 <tables></tables>
238 <views></views>
239 <triggers></triggers>
240 <procedures>
241 <procedure name="foo_proc" parameters="foo,bar" owner="Nomar" order="1">
242 <sql>select foo from bar</sql>
243 <comments>Go Sox!</comments>
0eebe059 244 <extra hello="world" />
87c5565e 245 </procedure>
246 </procedures>
983ed646 247</schema>
1e3867bf 248EOXML
249
250 $obj = SQL::Translator->new(
251 debug => DEBUG,
252 trace => TRACE,
253 show_warnings => 1,
254 add_drop_table => 1,
255 from => "MySQL",
256 to => "XML-SQLFairy",
257 );
258 my $s = $obj->schema;
259 my $name = 'foo_proc';
260 my $sql = 'select foo from bar';
261 my $parameters = 'foo, bar';
262 my $owner = 'Nomar';
263 my $comments = 'Go Sox!';
264 my $p = $s->add_procedure(
265 name => $name,
266 sql => $sql,
267 parameters => $parameters,
268 owner => $owner,
269 comments => $comments,
0eebe059 270 extra => { hello => "world" },
1e3867bf 271 ) or die $s->error;
d3422086 272
1e3867bf 273 # As we have created a Schema we give translate a dummy string so that
274 # it will run the produce.
275 lives_ok {$xml =$obj->translate("FOO");} "Translate (Procedure) ran";
276 ok("$xml" ne "" ,"Produced something!");
277 print "XML attrib_values=>1:\n$xml" if DEBUG;
278 # Strip sqlf header with its variable date so we diff safely
279 $xml =~ s/^([^\n]*\n){7}//m;
280 eq_or_diff $xml, $ans ,"XML looks right";
281} # end Procedure
e0a0c3e1 282
283#
284# Field.extra
285#
286{
287my ($obj,$ans,$xml);
288
289$ans = <<EOXML;
290<schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
0eebe059 291 <extra />
87c5565e 292 <tables>
d37416fd 293 <table name="Basic" order="1">
0eebe059 294 <extra />
87c5565e 295 <fields>
d37416fd 296 <field name="foo" data_type="integer" size="10" is_nullable="1" is_auto_increment="0" is_primary_key="0" is_foreign_key="0" order="1">
87c5565e 297 <extra ZEROFILL="1" />
298 <comments></comments>
299 </field>
300 </fields>
301 <indices></indices>
302 <constraints></constraints>
a4fb1ddc 303 <comments></comments>
87c5565e 304 </table>
305 </tables>
306 <views></views>
307 <triggers></triggers>
308 <procedures></procedures>
e0a0c3e1 309</schema>
310EOXML
311
312 $obj = SQL::Translator->new(
313 debug => DEBUG,
314 trace => TRACE,
315 show_warnings => 1,
316 add_drop_table => 1,
317 from => "MySQL",
318 to => "XML-SQLFairy",
319 );
320 my $s = $obj->schema;
321 my $t = $s->add_table( name => "Basic" ) or die $s->error;
322 my $f = $t->add_field(
323 name => "foo",
324 data_type => "integer",
325 size => "10",
326 ) or die $t->error;
327 $f->extra(ZEROFILL => "1");
328
329 # As we have created a Schema we give translate a dummy string so that
330 # it will run the produce.
331 lives_ok {$xml =$obj->translate("FOO");} "Translate (Field.extra) ran";
332 ok("$xml" ne "" ,"Produced something!");
333 print "XML:\n$xml" if DEBUG;
334 # Strip sqlf header with its variable date so we diff safely
335 $xml =~ s/^([^\n]*\n){7}//m;
336 eq_or_diff $xml, $ans ,"XML looks right";
337} # end extra