Move more normalization changes to preprocess_schema
[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                     size => 255,
109                 },
110                 {
111                     name => "explicitemptystring",
112                     data_type => "varchar",
113                     default_value => "",
114                     is_nullable => 1,
115                     size => 255,
116                 },
117                 {
118                     name => "emptytagdef",
119                     data_type => "varchar",
120                     default_value => "",
121                     is_nullable => 1,
122                     comments => "Hello emptytagdef",
123                     size => 255,
124                 },
125                 {
126                     name => "another_id",
127                     data_type => "int",
128                     size => "10",
129                     default_value => 2,
130                     is_nullable => 1,
131                     is_foreign_key => 1,
132                 },
133                 {
134                     name => "timest",
135                     data_type => "timestamp",
136                     size => "0",
137                     is_nullable => 1,
138                 },
139             ],
140             constraints => [
141                 {
142                     type => PRIMARY_KEY,
143                     fields => ["id"],
144                     extra => {
145                         foo => "bar",
146                         hello => "world",
147                         bar => "baz",
148                     },
149                 },
150                 {
151                     name => 'emailuniqueindex',
152                     type => UNIQUE,
153                     fields => ["email"],
154                 },
155                 {
156                     type => FOREIGN_KEY,
157                     fields => ["another_id"],
158                     reference_table => "Another",
159                     reference_fields => ["id"],
160                     name => 'Basic_fk'
161                 },
162             ],
163             indices => [
164                 {
165                     name => "titleindex",
166                     fields => ["title"],
167                     extra => {
168                         foo => "bar",
169                         hello => "world",
170                         bar => "baz",
171                     },
172                 },
173             ],
174         }, # end table Basic
175         {
176             name => "Another",
177             extra => {
178                 foo => "bar",
179                 hello => "world",
180                 bar => "baz",
181             },
182             options => [ { ENGINE => 'InnoDB' } ],
183             fields => [
184                 {
185                     name => "id",
186                     data_type => "int",
187                     default_value => undef,
188                     is_nullable => 0,
189                     size => 10,
190                     is_primary_key => 1,
191                     is_auto_increment => 1,
192                 },
193             ],
194         }, # end table Another
195     ], # end tables
196
197     views => [
198         {
199             name => 'email_list',
200             sql => "SELECT email FROM Basic WHERE email IS NOT NULL",
201             fields => ['email'],
202             extra => {
203                 foo => "bar",
204                 hello => "world",
205                 bar => "baz",
206             },
207         },
208     ],
209
210     triggers => [
211         {
212             name                => 'foo_trigger',
213             perform_action_when => 'after',
214             database_event      => 'insert',
215             on_table            => 'Basic',
216             action              => 'update modified=timestamp();',
217             extra => {
218                 foo => "bar",
219                 hello => "world",
220                 bar => "baz",
221             },
222         },
223     ],
224
225     procedures => [
226         {
227             name       => 'foo_proc',
228             sql        => 'select foo from bar',
229             parameters => ['foo', 'bar'],
230             owner      => 'Nomar',
231             comments   => 'Go Sox!',
232             extra => {
233                 foo => "bar",
234                 hello => "world",
235                 bar => "baz",
236             },
237         },
238     ],
239
240 }); # end schema