Fixed missing schema table comments
[dbsrgits/SQL-Translator.git] / t / 17sqlfxml-producer.t
1 #!/usr/bin/perl -w
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
7 local $^W = 0;
8
9 use strict;
10 use Test::More;
11 use Test::Exception;
12 use Test::SQL::Translator qw(maybe_plan);
13
14 use Data::Dumper;
15 my %opt;
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);
19
20 use FindBin qw/$Bin/;
21
22 my $file = "$Bin/data/mysql/sqlfxml-producer-basic.sql";
23
24 local $SIG{__WARN__} = sub {
25     CORE::warn(@_)
26         unless $_[0] =~ m#XML/Writer#;
27 };
28
29 # Testing 1,2,3,4...
30 #=============================================================================
31
32 BEGIN {
33     maybe_plan(14,
34         'XML::Writer',
35         'Test::Differences',
36         'SQL::Translator::Producer::XML::SQLFairy');
37 }
38
39 use Test::Differences;
40 use SQL::Translator;
41 use SQL::Translator::Producer::XML::SQLFairy;
42
43 #
44 # basic stuff
45 #
46 {
47 my ($obj,$ans,$xml);
48
49 $ans = <<EOXML;
50 <schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
51   <extra />
52   <tables>
53     <table name="Basic" order="1">
54       <extra />
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>
74         <index name="titleindex" type="NORMAL" fields="title" options="">
75           <extra />
76         </index>
77       </indices>
78       <constraints>
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>
85       </constraints>
86       <comments></comments>
87     </table>
88   </tables>
89   <views></views>
90   <triggers></triggers>
91   <procedures></procedures>
92 </schema>
93 EOXML
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",
102 );
103 $xml = $obj->translate($file) or die $obj->error;
104 ok("$xml" ne ""                             ,"Produced something!");
105 print "XML:\n$xml" if DEBUG;
106 # Strip sqlf header with its variable date so we diff safely
107 $xml =~ s/^([^\n]*\n){7}//m;
108 eq_or_diff $xml, $ans, "XML looks right";
109
110 } # end basic stuff
111
112 #
113 # View
114 #
115 # Thanks to Ken for the schema setup lifted from 13schema.t
116 {
117 my ($obj,$ans,$xml);
118
119 $ans = <<EOXML;
120 <schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
121   <extra />
122   <tables></tables>
123   <views>
124     <view name="foo_view" fields="name,age" order="1">
125       <sql>select name, age from person</sql>
126       <extra hello="world" />
127     </view>
128   </views>
129   <triggers></triggers>
130   <procedures></procedures>
131 </schema>
132 EOXML
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,
150         extra  => { hello => "world" },
151         schema => $s,
152     ) or die $s->error;
153
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 {
169 my ($obj,$ans,$xml);
170
171 $ans = <<EOXML;
172 <schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
173   <extra />
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>
179       <extra hello="world" />
180     </trigger>
181   </triggers>
182   <procedures></procedures>
183 </schema>
184 EOXML
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,
206         extra               => { hello => "world" },
207     ) or die $s->error;
208
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 {
224 my ($obj,$ans,$xml);
225
226 $ans = <<EOXML;
227 <schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
228   <extra />
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>
236       <extra hello="world" />
237     </procedure>
238   </procedures>
239 </schema>
240 EOXML
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,
262         extra      => { hello => "world" },
263     ) or die $s->error;
264
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
274
275 #
276 # Field.extra
277 #
278 {
279 my ($obj,$ans,$xml);
280
281 $ans = <<EOXML;
282 <schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
283   <extra />
284   <tables>
285     <table name="Basic" order="2">
286       <extra />
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>
295       <comments></comments>
296     </table>
297   </tables>
298   <views></views>
299   <triggers></triggers>
300   <procedures></procedures>
301 </schema>
302 EOXML
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