Correct constraint names in preprocess for MySQL producer
[dbsrgits/SQL-Translator.git] / t / 16xml-parser.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 # Run script with -d for debug.
8
9 use strict;
10
11 use FindBin qw/$Bin/;
12
13 use Test::More;
14 use Test::SQL::Translator;
15 use Test::Exception;
16 use Data::Dumper;
17 use SQL::Translator;
18 use SQL::Translator::Schema::Constants;
19
20 # Simple options. -d for debug
21 my %opt;
22 BEGIN { map { $opt{$_}=1 if s/^-// } @ARGV; }
23 use constant DEBUG => (exists $opt{d} ? 1 : 0);
24
25
26 # Testing 1,2,3,4...
27 #=============================================================================
28
29 BEGIN {
30     maybe_plan(204, 'SQL::Translator::Parser::XML::SQLFairy');
31 }
32
33 my $testschema = "$Bin/data/xml/schema.xml";
34
35 my $sqlt;
36 $sqlt = SQL::Translator->new(
37     debug          => DEBUG,
38     show_warnings  => 1,
39     add_drop_table => 1,
40 );
41 die "Can't find test schema $testschema" unless -e $testschema;
42 my $sql = $sqlt->translate(
43     from     => 'XML-SQLFairy',
44     to       => 'MySQL',
45     filename => $testschema,
46 ) or die $sqlt->error;
47 print $sql if DEBUG;
48
49 # Test the schema objs generted from the XML
50 #
51 my $scma = $sqlt->schema;
52
53 # Hmmm, when using schema_ok the field test data gets a bit too nested and
54 # fiddly to work with. (See 28xml-xmi-parser-sqlfairy.t for more a split out
55 # version)
56 schema_ok( $scma, {
57     tables => [
58         {
59             name => "Basic",
60             options => [ { ENGINE => 'InnoDB' } ],
61             extra => {
62                 foo => "bar",
63                 hello => "world",
64                 bar => "baz",
65             },
66             fields => [
67                 {
68                     name => "id",
69                     data_type => "int",
70                     default_value => undef,
71                     is_nullable => 0,
72                     size => 10,
73                     is_primary_key => 1,
74                     is_auto_increment => 1,
75                     extra => { ZEROFILL => 1 },
76                 },
77                 {
78                     name => "title",
79                     data_type => "varchar",
80                     is_nullable => 0,
81                     default_value => "hello",
82                     size => 100,
83                 },
84                 {
85                     name => "description",
86                     data_type => "text",
87                     is_nullable => 1,
88                     default_value => "",
89                 },
90                 {
91                     name => "email",
92                     data_type => "varchar",
93                     size => 255,
94                     is_unique => 1,
95                     default_value => undef,
96                     is_nullable => 1,
97                     extra => {
98                         foo => "bar",
99                         hello => "world",
100                         bar => "baz",
101                     }
102                 },
103                 {
104                     name => "explicitnulldef",
105                     data_type => "varchar",
106                     default_value => undef,
107                     is_nullable => 1,
108                 },
109                 {
110                     name => "explicitemptystring",
111                     data_type => "varchar",
112                     default_value => "",
113                     is_nullable => 1,
114                 },
115                 {
116                     name => "emptytagdef",
117                     data_type => "varchar",
118                     default_value => "",
119                     is_nullable => 1,
120                     comments => "Hello emptytagdef",
121                 },
122                 {
123                     name => "another_id",
124                     data_type => "int",
125                     size => "10",
126                     default_value => 2,
127                     is_nullable => 1,
128                     is_foreign_key => 1,
129                 },
130                 {
131                     name => "timest",
132                     data_type => "timestamp",
133                     size => "0",
134                     is_nullable => 1,
135                 },
136             ],
137             constraints => [
138                 {
139                     type => PRIMARY_KEY,
140                     fields => ["id"],
141                     extra => {
142                         foo => "bar",
143                         hello => "world",
144                         bar => "baz",
145                     },
146                 },
147                 {
148                     name => 'emailuniqueindex',
149                     type => UNIQUE,
150                     fields => ["email"],
151                 },
152                 {
153                     type => FOREIGN_KEY,
154                     fields => ["another_id"],
155                     reference_table => "Another",
156                     reference_fields => ["id"],
157                     name => 'Basic_fk'
158                 },
159             ],
160             indices => [
161                 {
162                     name => "titleindex",
163                     fields => ["title"],
164                     extra => {
165                         foo => "bar",
166                         hello => "world",
167                         bar => "baz",
168                     },
169                 },
170             ],
171         }, # end table Basic
172         {
173             name => "Another",
174             extra => {
175                 foo => "bar",
176                 hello => "world",
177                 bar => "baz",
178             },
179             options => [ { ENGINE => 'InnoDB' } ],
180             fields => [
181                 {
182                     name => "id",
183                     data_type => "int",
184                     default_value => undef,
185                     is_nullable => 0,
186                     size => 10,
187                     is_primary_key => 1,
188                     is_auto_increment => 1,
189                 },
190             ],
191         }, # end table Another
192     ], # end tables
193
194     views => [
195         {
196             name => 'email_list',
197             sql => "SELECT email FROM Basic WHERE email IS NOT NULL",
198             fields => ['email'],
199             extra => {
200                 foo => "bar",
201                 hello => "world",
202                 bar => "baz",
203             },
204         },
205     ],
206
207     triggers => [
208         {
209             name                => 'foo_trigger',
210             perform_action_when => 'after',
211             database_event      => 'insert',
212             on_table            => 'Basic',
213             action              => 'update modified=timestamp();',
214             extra => {
215                 foo => "bar",
216                 hello => "world",
217                 bar => "baz",
218             },
219         },
220     ],
221
222     procedures => [
223         {
224             name       => 'foo_proc',
225             sql        => 'select foo from bar',
226             parameters => ['foo', 'bar'],
227             owner      => 'Nomar',
228             comments   => 'Go Sox!',
229             extra => {
230                 foo => "bar",
231                 hello => "world",
232                 bar => "baz",
233             },
234         },
235     ],
236
237 }); # end schema