Add parenthesis into the VIEW definition to make sure the pg parser still can deal...
[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(212, '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
43 my $sql;
44 {
45   my @w;
46   local $SIG{__WARN__} = sub { push @w, $_[0] if $_[0] =~ /The database_event tag is deprecated - please use database_events/ };
47
48   $sql = $sqlt->translate(
49     from     => 'XML-SQLFairy',
50     to       => 'MySQL',
51     filename => $testschema,
52   ) or die $sqlt->error;
53   print $sql if DEBUG;
54
55   ok (@w, 'database_event deprecation warning issued');
56 }
57
58 # Test the schema objs generted from the XML
59 #
60 my $scma = $sqlt->schema;
61
62 # Hmmm, when using schema_ok the field test data gets a bit too nested and
63 # fiddly to work with. (See 28xml-xmi-parser-sqlfairy.t for more a split out
64 # version)
65 schema_ok( $scma, {
66     tables => [
67         {
68             name => "Basic",
69             options => [ { ENGINE => 'InnoDB' } ],
70             extra => {
71                 foo => "bar",
72                 hello => "world",
73                 bar => "baz",
74             },
75             fields => [
76                 {
77                     name => "id",
78                     data_type => "int",
79                     default_value => undef,
80                     is_nullable => 0,
81                     size => 10,
82                     is_primary_key => 1,
83                     is_auto_increment => 1,
84                     extra => { ZEROFILL => 1 },
85                 },
86                 {
87                     name => "title",
88                     data_type => "varchar",
89                     is_nullable => 0,
90                     default_value => "hello",
91                     size => 100,
92                 },
93                 {
94                     name => "description",
95                     data_type => "text",
96                     is_nullable => 1,
97                     default_value => "",
98                 },
99                 {
100                     name => "email",
101                     data_type => "varchar",
102                     size => 500,
103                     is_unique => 1,
104                     default_value => undef,
105                     is_nullable => 1,
106                     extra => {
107                         foo => "bar",
108                         hello => "world",
109                         bar => "baz",
110                     }
111                 },
112                 {
113                     name => "explicitnulldef",
114                     data_type => "varchar",
115                     default_value => undef,
116                     is_nullable => 1,
117                     size => 255,
118                 },
119                 {
120                     name => "explicitemptystring",
121                     data_type => "varchar",
122                     default_value => "",
123                     is_nullable => 1,
124                     size => 255,
125                 },
126                 {
127                     name => "emptytagdef",
128                     data_type => "varchar",
129                     default_value => "",
130                     is_nullable => 1,
131                     comments => "Hello emptytagdef",
132                     size => 255,
133                 },
134                 {
135                     name => "another_id",
136                     data_type => "int",
137                     size => "10",
138                     default_value => 2,
139                     is_nullable => 1,
140                     is_foreign_key => 1,
141                 },
142                 {
143                     name => "timest",
144                     data_type => "timestamp",
145                     size => "0",
146                     is_nullable => 1,
147                 },
148             ],
149             constraints => [
150                 {
151                     type => PRIMARY_KEY,
152                     fields => ["id"],
153                     extra => {
154                         foo => "bar",
155                         hello => "world",
156                         bar => "baz",
157                     },
158                 },
159                 {
160                     name => 'emailuniqueindex',
161                     type => UNIQUE,
162                     fields => ["email"],
163                 },
164                 {
165                     type => FOREIGN_KEY,
166                     fields => ["another_id"],
167                     reference_table => "Another",
168                     reference_fields => ["id"],
169                     name => 'Basic_fk'
170                 },
171             ],
172             indices => [
173                 {
174                     name => "titleindex",
175                     fields => ["title"],
176                     extra => {
177                         foo => "bar",
178                         hello => "world",
179                         bar => "baz",
180                     },
181                 },
182             ],
183         }, # end table Basic
184         {
185             name => "Another",
186             extra => {
187                 foo => "bar",
188                 hello => "world",
189                 bar => "baz",
190             },
191             options => [ { ENGINE => 'InnoDB' } ],
192             fields => [
193                 {
194                     name => "id",
195                     data_type => "int",
196                     default_value => undef,
197                     is_nullable => 0,
198                     size => 10,
199                     is_primary_key => 1,
200                     is_auto_increment => 1,
201                 },
202             ],
203         }, # end table Another
204     ], # end tables
205
206     views => [
207         {
208             name => 'email_list',
209             sql => "SELECT email FROM Basic WHERE (email IS NOT NULL)",
210             fields => ['email'],
211             extra => {
212                 foo => "bar",
213                 hello => "world",
214                 bar => "baz",
215             },
216         },
217     ],
218
219     triggers => [
220         {
221             name                => 'foo_trigger',
222             perform_action_when => 'after',
223             database_events     => 'insert',
224             on_table            => 'Basic',
225             action              => 'update modified=timestamp();',
226             extra => {
227                 foo => "bar",
228                 hello => "world",
229                 bar => "baz",
230             },
231         },
232         {
233             name                => 'bar_trigger',
234             perform_action_when => 'before',
235             database_events     => 'insert,update',
236             on_table            => 'Basic',
237             action              => 'update modified2=timestamp();',
238             extra => {
239                 hello => "aliens",
240             },
241         },
242     ],
243
244     procedures => [
245         {
246             name       => 'foo_proc',
247             sql        => 'select foo from bar',
248             parameters => ['foo', 'bar'],
249             owner      => 'Nomar',
250             comments   => 'Go Sox!',
251             extra => {
252                 foo => "bar",
253                 hello => "world",
254                 bar => "baz",
255             },
256         },
257     ],
258
259 }); # end schema