Improve trigger 'scope' attribute support (RT#119997)
[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(undef, '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                     is_unique => 1,
93                 },
94                 {
95                     name => "description",
96                     data_type => "text",
97                     is_nullable => 1,
98                     default_value => "",
99                 },
100                 {
101                     name => "email",
102                     data_type => "varchar",
103                     size => 500,
104                     is_unique => 1,
105                     default_value => undef,
106                     is_nullable => 1,
107                     extra => {
108                         foo => "bar",
109                         hello => "world",
110                         bar => "baz",
111                     }
112                 },
113                 {
114                     name => "explicitnulldef",
115                     data_type => "varchar",
116                     default_value => undef,
117                     is_nullable => 1,
118                     size => 255,
119                 },
120                 {
121                     name => "explicitemptystring",
122                     data_type => "varchar",
123                     default_value => "",
124                     is_nullable => 1,
125                     size => 255,
126                 },
127                 {
128                     name => "emptytagdef",
129                     data_type => "varchar",
130                     default_value => "",
131                     is_nullable => 1,
132                     comments => "Hello emptytagdef",
133                     size => 255,
134                 },
135                 {
136                     name => "another_id",
137                     data_type => "int",
138                     size => "10",
139                     default_value => 2,
140                     is_nullable => 1,
141                     is_foreign_key => 1,
142                 },
143                 {
144                     name => "timest",
145                     data_type => "timestamp",
146                     size => "0",
147                     is_nullable => 1,
148                 },
149             ],
150             constraints => [
151                 {
152                     type => PRIMARY_KEY,
153                     fields => ["id"],
154                     extra => {
155                         foo => "bar",
156                         hello => "world",
157                         bar => "baz",
158                     },
159                 },
160                 {
161                     name => 'emailuniqueindex',
162                     type => UNIQUE,
163                     fields => ["email"],
164                 },
165                 {
166                     name => 'very_long_index_name_on_title_field_which_should_be_truncated_for_various_rdbms',
167                     type => UNIQUE,
168                     fields => ["title"],
169                 },
170                 {
171                     type => FOREIGN_KEY,
172                     fields => ["another_id"],
173                     reference_table => "Another",
174                     reference_fields => ["id"],
175                     name => 'Basic_fk'
176                 },
177             ],
178             indices => [
179                 {
180                     name => "titleindex",
181                     fields => ["title"],
182                     extra => {
183                         foo => "bar",
184                         hello => "world",
185                         bar => "baz",
186                     },
187                 },
188             ],
189         }, # end table Basic
190         {
191             name => "Another",
192             extra => {
193                 foo => "bar",
194                 hello => "world",
195                 bar => "baz",
196             },
197             options => [ { ENGINE => 'InnoDB' } ],
198             fields => [
199                 {
200                     name => "id",
201                     data_type => "int",
202                     default_value => undef,
203                     is_nullable => 0,
204                     size => 10,
205                     is_primary_key => 1,
206                     is_auto_increment => 1,
207                 },
208                 {
209                     name => "num",
210                     data_type => "numeric",
211                     default_value => undef,
212                     size => '10,2',
213                 },
214             ],
215         }, # end table Another
216     ], # end tables
217
218     views => [
219         {
220             name => 'email_list',
221             sql => "SELECT email FROM Basic WHERE (email IS NOT NULL)",
222             fields => ['email'],
223             extra => {
224                 foo => "bar",
225                 hello => "world",
226                 bar => "baz",
227             },
228         },
229     ],
230
231     triggers => [
232         {
233             name                => 'foo_trigger',
234             perform_action_when => 'after',
235             database_events     => 'insert',
236             on_table            => 'Basic',
237             action              => 'update modified=timestamp();',
238             scope               => 'row',
239             extra => {
240                 foo => "bar",
241                 hello => "world",
242                 bar => "baz",
243             },
244         },
245         {
246             name                => 'bar_trigger',
247             perform_action_when => 'before',
248             database_events     => 'insert,update',
249             on_table            => 'Basic',
250             action              => 'update modified2=timestamp();',
251             scope               => 'row',
252             extra => {
253                 hello => "aliens",
254             },
255         },
256     ],
257
258     procedures => [
259         {
260             name       => 'foo_proc',
261             sql        => 'select foo from bar',
262             parameters => ['foo', 'bar'],
263             owner      => 'Nomar',
264             comments   => 'Go Sox!',
265             extra => {
266                 foo => "bar",
267                 hello => "world",
268                 bar => "baz",
269             },
270         },
271     ],
272
273 }); # end schema
274
275 done_testing;