Experimental filters
[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(@_)
26 unless $_[0] =~ m#XML/Writer#;
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>
86 </table>
87 </tables>
88 <views></views>
89 <triggers></triggers>
90 <procedures></procedures>
983ed646 91</schema>
f11724ad 92EOXML
93
94$obj = SQL::Translator->new(
95 debug => DEBUG,
96 trace => TRACE,
97 show_warnings => 1,
98 add_drop_table => 1,
99 from => "MySQL",
100 to => "XML-SQLFairy",
f11724ad 101);
87c5565e 102$xml = $obj->translate($file) or die $obj->error;
f11724ad 103ok("$xml" ne "" ,"Produced something!");
ec791002 104print "XML:\n$xml" if DEBUG;
f11724ad 105# Strip sqlf header with its variable date so we diff safely
ec791002 106$xml =~ s/^([^\n]*\n){7}//m;
107eq_or_diff $xml, $ans, "XML looks right";
f11724ad 108
ec791002 109} # end basic stuff
1e3867bf 110
111#
112# View
113#
114# Thanks to Ken for the schema setup lifted from 13schema.t
115{
116my ($obj,$ans,$xml);
117
118$ans = <<EOXML;
983ed646 119<schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
0eebe059 120 <extra />
87c5565e 121 <tables></tables>
122 <views>
123 <view name="foo_view" fields="name,age" order="1">
124 <sql>select name, age from person</sql>
0eebe059 125 <extra hello="world" />
87c5565e 126 </view>
127 </views>
128 <triggers></triggers>
129 <procedures></procedures>
983ed646 130</schema>
1e3867bf 131EOXML
132
133 $obj = SQL::Translator->new(
134 debug => DEBUG,
135 trace => TRACE,
136 show_warnings => 1,
137 add_drop_table => 1,
138 from => "MySQL",
139 to => "XML-SQLFairy",
140 );
141 my $s = $obj->schema;
142 my $name = 'foo_view';
143 my $sql = 'select name, age from person';
144 my $fields = 'name, age';
145 my $v = $s->add_view(
146 name => $name,
147 sql => $sql,
148 fields => $fields,
0eebe059 149 extra => { hello => "world" },
1e3867bf 150 schema => $s,
151 ) or die $s->error;
ec791002 152
1e3867bf 153 # As we have created a Schema we give translate a dummy string so that
154 # it will run the produce.
155 lives_ok {$xml =$obj->translate("FOO");} "Translate (View) ran";
156 ok("$xml" ne "" ,"Produced something!");
157 print "XML attrib_values=>1:\n$xml" if DEBUG;
158 # Strip sqlf header with its variable date so we diff safely
159 $xml =~ s/^([^\n]*\n){7}//m;
160 eq_or_diff $xml, $ans ,"XML looks right";
161} # end View
162
163#
164# Trigger
165#
166# Thanks to Ken for the schema setup lifted from 13schema.t
167{
168my ($obj,$ans,$xml);
169
170$ans = <<EOXML;
983ed646 171<schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
0eebe059 172 <extra />
87c5565e 173 <tables></tables>
174 <views></views>
175 <triggers>
176 <trigger name="foo_trigger" database_event="insert" on_table="foo" perform_action_when="after" order="1">
177 <action>update modified=timestamp();</action>
0eebe059 178 <extra hello="world" />
87c5565e 179 </trigger>
180 </triggers>
181 <procedures></procedures>
983ed646 182</schema>
1e3867bf 183EOXML
184
185 $obj = SQL::Translator->new(
186 debug => DEBUG,
187 trace => TRACE,
188 show_warnings => 1,
189 add_drop_table => 1,
190 from => "MySQL",
191 to => "XML-SQLFairy",
192 );
193 my $s = $obj->schema;
194 my $name = 'foo_trigger';
195 my $perform_action_when = 'after';
196 my $database_event = 'insert';
197 my $on_table = 'foo';
198 my $action = 'update modified=timestamp();';
199 my $t = $s->add_trigger(
200 name => $name,
201 perform_action_when => $perform_action_when,
202 database_event => $database_event,
203 on_table => $on_table,
204 action => $action,
0eebe059 205 extra => { hello => "world" },
1e3867bf 206 ) or die $s->error;
ec791002 207
1e3867bf 208 # As we have created a Schema we give translate a dummy string so that
209 # it will run the produce.
210 lives_ok {$xml =$obj->translate("FOO");} "Translate (Trigger) ran";
211 ok("$xml" ne "" ,"Produced something!");
212 print "XML attrib_values=>1:\n$xml" if DEBUG;
213 # Strip sqlf header with its variable date so we diff safely
214 $xml =~ s/^([^\n]*\n){7}//m;
215 eq_or_diff $xml, $ans ,"XML looks right";
216} # end Trigger
217
218#
219# Procedure
220#
221# Thanks to Ken for the schema setup lifted from 13schema.t
222{
223my ($obj,$ans,$xml);
224
225$ans = <<EOXML;
983ed646 226<schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
0eebe059 227 <extra />
87c5565e 228 <tables></tables>
229 <views></views>
230 <triggers></triggers>
231 <procedures>
232 <procedure name="foo_proc" parameters="foo,bar" owner="Nomar" order="1">
233 <sql>select foo from bar</sql>
234 <comments>Go Sox!</comments>
0eebe059 235 <extra hello="world" />
87c5565e 236 </procedure>
237 </procedures>
983ed646 238</schema>
1e3867bf 239EOXML
240
241 $obj = SQL::Translator->new(
242 debug => DEBUG,
243 trace => TRACE,
244 show_warnings => 1,
245 add_drop_table => 1,
246 from => "MySQL",
247 to => "XML-SQLFairy",
248 );
249 my $s = $obj->schema;
250 my $name = 'foo_proc';
251 my $sql = 'select foo from bar';
252 my $parameters = 'foo, bar';
253 my $owner = 'Nomar';
254 my $comments = 'Go Sox!';
255 my $p = $s->add_procedure(
256 name => $name,
257 sql => $sql,
258 parameters => $parameters,
259 owner => $owner,
260 comments => $comments,
0eebe059 261 extra => { hello => "world" },
1e3867bf 262 ) or die $s->error;
d3422086 263
1e3867bf 264 # As we have created a Schema we give translate a dummy string so that
265 # it will run the produce.
266 lives_ok {$xml =$obj->translate("FOO");} "Translate (Procedure) ran";
267 ok("$xml" ne "" ,"Produced something!");
268 print "XML attrib_values=>1:\n$xml" if DEBUG;
269 # Strip sqlf header with its variable date so we diff safely
270 $xml =~ s/^([^\n]*\n){7}//m;
271 eq_or_diff $xml, $ans ,"XML looks right";
272} # end Procedure
e0a0c3e1 273
274#
275# Field.extra
276#
277{
278my ($obj,$ans,$xml);
279
280$ans = <<EOXML;
281<schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
0eebe059 282 <extra />
87c5565e 283 <tables>
284 <table name="Basic" order="2">
0eebe059 285 <extra />
87c5565e 286 <fields>
287 <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">
288 <extra ZEROFILL="1" />
289 <comments></comments>
290 </field>
291 </fields>
292 <indices></indices>
293 <constraints></constraints>
294 </table>
295 </tables>
296 <views></views>
297 <triggers></triggers>
298 <procedures></procedures>
e0a0c3e1 299</schema>
300EOXML
301
302 $obj = SQL::Translator->new(
303 debug => DEBUG,
304 trace => TRACE,
305 show_warnings => 1,
306 add_drop_table => 1,
307 from => "MySQL",
308 to => "XML-SQLFairy",
309 );
310 my $s = $obj->schema;
311 my $t = $s->add_table( name => "Basic" ) or die $s->error;
312 my $f = $t->add_field(
313 name => "foo",
314 data_type => "integer",
315 size => "10",
316 ) or die $t->error;
317 $f->extra(ZEROFILL => "1");
318
319 # As we have created a Schema we give translate a dummy string so that
320 # it will run the produce.
321 lives_ok {$xml =$obj->translate("FOO");} "Translate (Field.extra) ran";
322 ok("$xml" ne "" ,"Produced something!");
323 print "XML:\n$xml" if DEBUG;
324 # Strip sqlf header with its variable date so we diff safely
325 $xml =~ s/^([^\n]*\n){7}//m;
326 eq_or_diff $xml, $ans ,"XML looks right";
327} # end extra