Added writing of field.extra
[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(15,
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   <table name="Basic" order="1">
52     <fields>
53       <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">
54         <extra />
55         <comments>comment on id field</comments>
56       </field>
57       <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">
58         <extra />
59         <comments></comments>
60       </field>
61       <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">
62         <extra />
63         <comments></comments>
64       </field>
65       <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">
66         <extra />
67         <comments></comments>
68       </field>
69     </fields>
70     <indices>
71       <index name="titleindex" type="NORMAL" fields="title" options="" />
72     </indices>
73     <constraints>
74       <constraint name="" type="PRIMARY KEY" fields="id" reference_table="" reference_fields="" on_delete="" on_update="" match_type="" expression="" options="" deferrable="1" />
75       <constraint name="" type="UNIQUE" fields="email" reference_table="" reference_fields="" on_delete="" on_update="" match_type="" expression="" options="" deferrable="1" />
76     </constraints>
77   </table>
78 </schema>
79 EOXML
80
81 $obj = SQL::Translator->new(
82     debug          => DEBUG,
83     trace          => TRACE,
84     show_warnings  => 1,
85     add_drop_table => 1,
86     from           => "MySQL",
87     to             => "XML-SQLFairy",
88 );
89 lives_ok {$xml = $obj->translate($file);} "Translate (attrib_values=>1) ran";
90 ok("$xml" ne ""                             ,"Produced something!");
91 print "XML:\n$xml" if DEBUG;
92 # Strip sqlf header with its variable date so we diff safely
93 $xml =~ s/^([^\n]*\n){7}//m;
94 eq_or_diff $xml, $ans, "XML looks right";
95
96 } # end basic stuff
97
98 #
99 # View
100 #
101 # Thanks to Ken for the schema setup lifted from 13schema.t
102 {
103 my ($obj,$ans,$xml);
104
105 $ans = <<EOXML;
106 <schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
107   <view name="foo_view" fields="name,age" order="1">
108     <sql>select name, age from person</sql>
109   </view>
110 </schema>
111 EOXML
112
113     $obj = SQL::Translator->new(
114         debug          => DEBUG,
115         trace          => TRACE,
116         show_warnings  => 1,
117         add_drop_table => 1,
118         from           => "MySQL",
119         to             => "XML-SQLFairy",
120     );
121     my $s      = $obj->schema;
122     my $name   = 'foo_view';
123     my $sql    = 'select name, age from person';
124     my $fields = 'name, age';
125     my $v      = $s->add_view(
126         name   => $name,
127         sql    => $sql,
128         fields => $fields,
129         schema => $s,
130     ) or die $s->error;
131
132     # As we have created a Schema we give translate a dummy string so that
133     # it will run the produce.
134     lives_ok {$xml =$obj->translate("FOO");} "Translate (View) ran";
135     ok("$xml" ne ""                             ,"Produced something!");
136     print "XML attrib_values=>1:\n$xml" if DEBUG;
137     # Strip sqlf header with its variable date so we diff safely
138     $xml =~ s/^([^\n]*\n){7}//m; 
139     eq_or_diff $xml, $ans                       ,"XML looks right";
140 } # end View
141
142 #
143 # Trigger
144 #
145 # Thanks to Ken for the schema setup lifted from 13schema.t
146 {
147 my ($obj,$ans,$xml);
148
149 $ans = <<EOXML;
150 <schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
151   <trigger name="foo_trigger" database_event="insert" on_table="foo" perform_action_when="after" order="1">
152     <action>update modified=timestamp();</action>
153   </trigger>
154 </schema>
155 EOXML
156
157     $obj = SQL::Translator->new(
158         debug          => DEBUG,
159         trace          => TRACE,
160         show_warnings  => 1,
161         add_drop_table => 1,
162         from           => "MySQL",
163         to             => "XML-SQLFairy",
164     );
165     my $s                   = $obj->schema;
166     my $name                = 'foo_trigger';
167     my $perform_action_when = 'after';
168     my $database_event      = 'insert';
169     my $on_table            = 'foo';
170     my $action              = 'update modified=timestamp();';
171     my $t                   = $s->add_trigger(
172         name                => $name,
173         perform_action_when => $perform_action_when,
174         database_event      => $database_event,
175         on_table            => $on_table,
176         action              => $action,
177     ) or die $s->error;
178
179     # As we have created a Schema we give translate a dummy string so that
180     # it will run the produce.
181     lives_ok {$xml =$obj->translate("FOO");} "Translate (Trigger) ran";
182     ok("$xml" ne ""                             ,"Produced something!");
183     print "XML attrib_values=>1:\n$xml" if DEBUG;
184     # Strip sqlf header with its variable date so we diff safely
185     $xml =~ s/^([^\n]*\n){7}//m; 
186     eq_or_diff $xml, $ans                       ,"XML looks right";
187 } # end Trigger
188
189 #
190 # Procedure
191 #
192 # Thanks to Ken for the schema setup lifted from 13schema.t
193 {
194 my ($obj,$ans,$xml);
195
196 $ans = <<EOXML;
197 <schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
198   <procedure name="foo_proc" parameters="foo,bar" owner="Nomar" order="1">
199     <sql>select foo from bar</sql>
200     <comments>Go Sox!</comments>
201   </procedure>
202 </schema>
203 EOXML
204
205     $obj = SQL::Translator->new(
206         debug          => DEBUG,
207         trace          => TRACE,
208         show_warnings  => 1,
209         add_drop_table => 1,
210         from           => "MySQL",
211         to             => "XML-SQLFairy",
212     );
213     my $s          = $obj->schema;
214     my $name       = 'foo_proc';
215     my $sql        = 'select foo from bar';
216     my $parameters = 'foo, bar';
217     my $owner      = 'Nomar';
218     my $comments   = 'Go Sox!';
219     my $p          = $s->add_procedure(
220         name       => $name,
221         sql        => $sql,
222         parameters => $parameters,
223         owner      => $owner,
224         comments   => $comments,
225     ) or die $s->error;
226
227     # As we have created a Schema we give translate a dummy string so that
228     # it will run the produce.
229     lives_ok {$xml =$obj->translate("FOO");} "Translate (Procedure) ran";
230     ok("$xml" ne ""                             ,"Produced something!");
231     print "XML attrib_values=>1:\n$xml" if DEBUG;
232     # Strip sqlf header with its variable date so we diff safely
233     $xml =~ s/^([^\n]*\n){7}//m; 
234     eq_or_diff $xml, $ans                       ,"XML looks right";
235 } # end Procedure
236
237 #
238 # Field.extra
239 #
240 {
241 my ($obj,$ans,$xml);
242
243 $ans = <<EOXML;
244 <schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
245   <table name="Basic" order="2">
246     <fields>
247       <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">
248         <extra ZEROFILL="1" />
249         <comments></comments>
250       </field>
251     </fields>
252     <indices></indices>
253     <constraints></constraints>
254   </table>
255 </schema>
256 EOXML
257
258     $obj = SQL::Translator->new(
259         debug          => DEBUG,
260         trace          => TRACE,
261         show_warnings  => 1,
262         add_drop_table => 1,
263         from           => "MySQL",
264         to             => "XML-SQLFairy",
265     );
266     my $s = $obj->schema;
267     my $t = $s->add_table( name => "Basic" ) or die $s->error;
268     my $f = $t->add_field(
269         name      => "foo",
270         data_type => "integer",
271         size      => "10",
272     ) or die $t->error;
273     $f->extra(ZEROFILL => "1");
274
275     # As we have created a Schema we give translate a dummy string so that
276     # it will run the produce.
277     lives_ok {$xml =$obj->translate("FOO");} "Translate (Field.extra) ran";
278     ok("$xml" ne ""                             ,"Produced something!");
279     print "XML:\n$xml" if DEBUG;
280     # Strip sqlf header with its variable date so we diff safely
281     $xml =~ s/^([^\n]*\n){7}//m;
282     eq_or_diff $xml, $ans                       ,"XML looks right";
283 } # end extra