Fixed missing schema table comments
[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>
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 />
87c5565e 174 <tables></tables>
175 <views></views>
176 <triggers>
177 <trigger name="foo_trigger" database_event="insert" on_table="foo" perform_action_when="after" order="1">
178 <action>update modified=timestamp();</action>
0eebe059 179 <extra hello="world" />
87c5565e 180 </trigger>
181 </triggers>
182 <procedures></procedures>
983ed646 183</schema>
1e3867bf 184EOXML
185
186 $obj = SQL::Translator->new(
187 debug => DEBUG,
188 trace => TRACE,
189 show_warnings => 1,
190 add_drop_table => 1,
191 from => "MySQL",
192 to => "XML-SQLFairy",
193 );
194 my $s = $obj->schema;
195 my $name = 'foo_trigger';
196 my $perform_action_when = 'after';
197 my $database_event = 'insert';
198 my $on_table = 'foo';
199 my $action = 'update modified=timestamp();';
200 my $t = $s->add_trigger(
201 name => $name,
202 perform_action_when => $perform_action_when,
203 database_event => $database_event,
204 on_table => $on_table,
205 action => $action,
0eebe059 206 extra => { hello => "world" },
1e3867bf 207 ) or die $s->error;
ec791002 208
1e3867bf 209 # As we have created a Schema we give translate a dummy string so that
210 # it will run the produce.
211 lives_ok {$xml =$obj->translate("FOO");} "Translate (Trigger) ran";
212 ok("$xml" ne "" ,"Produced something!");
213 print "XML attrib_values=>1:\n$xml" if DEBUG;
214 # Strip sqlf header with its variable date so we diff safely
215 $xml =~ s/^([^\n]*\n){7}//m;
216 eq_or_diff $xml, $ans ,"XML looks right";
217} # end Trigger
218
219#
220# Procedure
221#
222# Thanks to Ken for the schema setup lifted from 13schema.t
223{
224my ($obj,$ans,$xml);
225
226$ans = <<EOXML;
983ed646 227<schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
0eebe059 228 <extra />
87c5565e 229 <tables></tables>
230 <views></views>
231 <triggers></triggers>
232 <procedures>
233 <procedure name="foo_proc" parameters="foo,bar" owner="Nomar" order="1">
234 <sql>select foo from bar</sql>
235 <comments>Go Sox!</comments>
0eebe059 236 <extra hello="world" />
87c5565e 237 </procedure>
238 </procedures>
983ed646 239</schema>
1e3867bf 240EOXML
241
242 $obj = SQL::Translator->new(
243 debug => DEBUG,
244 trace => TRACE,
245 show_warnings => 1,
246 add_drop_table => 1,
247 from => "MySQL",
248 to => "XML-SQLFairy",
249 );
250 my $s = $obj->schema;
251 my $name = 'foo_proc';
252 my $sql = 'select foo from bar';
253 my $parameters = 'foo, bar';
254 my $owner = 'Nomar';
255 my $comments = 'Go Sox!';
256 my $p = $s->add_procedure(
257 name => $name,
258 sql => $sql,
259 parameters => $parameters,
260 owner => $owner,
261 comments => $comments,
0eebe059 262 extra => { hello => "world" },
1e3867bf 263 ) or die $s->error;
d3422086 264
1e3867bf 265 # As we have created a Schema we give translate a dummy string so that
266 # it will run the produce.
267 lives_ok {$xml =$obj->translate("FOO");} "Translate (Procedure) ran";
268 ok("$xml" ne "" ,"Produced something!");
269 print "XML attrib_values=>1:\n$xml" if DEBUG;
270 # Strip sqlf header with its variable date so we diff safely
271 $xml =~ s/^([^\n]*\n){7}//m;
272 eq_or_diff $xml, $ans ,"XML looks right";
273} # end Procedure
e0a0c3e1 274
275#
276# Field.extra
277#
278{
279my ($obj,$ans,$xml);
280
281$ans = <<EOXML;
282<schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
0eebe059 283 <extra />
87c5565e 284 <tables>
285 <table name="Basic" order="2">
0eebe059 286 <extra />
87c5565e 287 <fields>
288 <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">
289 <extra ZEROFILL="1" />
290 <comments></comments>
291 </field>
292 </fields>
293 <indices></indices>
294 <constraints></constraints>
a4fb1ddc 295 <comments></comments>
87c5565e 296 </table>
297 </tables>
298 <views></views>
299 <triggers></triggers>
300 <procedures></procedures>
e0a0c3e1 301</schema>
302EOXML
303
304 $obj = SQL::Translator->new(
305 debug => DEBUG,
306 trace => TRACE,
307 show_warnings => 1,
308 add_drop_table => 1,
309 from => "MySQL",
310 to => "XML-SQLFairy",
311 );
312 my $s = $obj->schema;
313 my $t = $s->add_table( name => "Basic" ) or die $s->error;
314 my $f = $t->add_field(
315 name => "foo",
316 data_type => "integer",
317 size => "10",
318 ) or die $t->error;
319 $f->extra(ZEROFILL => "1");
320
321 # As we have created a Schema we give translate a dummy string so that
322 # it will run the produce.
323 lives_ok {$xml =$obj->translate("FOO");} "Translate (Field.extra) ran";
324 ok("$xml" ne "" ,"Produced something!");
325 print "XML:\n$xml" if DEBUG;
326 # Strip sqlf header with its variable date so we diff safely
327 $xml =~ s/^([^\n]*\n){7}//m;
328 eq_or_diff $xml, $ans ,"XML looks right";
329} # end extra