Update Trigger to insist on a valid table for on_table
[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(150, '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             extra => {
61                 foo => "bar",
62                 hello => "world",
63                 bar => "baz",
64                 mysql_table_type => "InnoDB",
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             constraints => [
124                 {
125                     type => PRIMARY_KEY,
126                     fields => ["id"],
127                     extra => {
128                         foo => "bar",
129                         hello => "world",
130                         bar => "baz",
131                     },
132                 },
133                 {
134                     name => 'emailuniqueindex',
135                     type => UNIQUE,
136                     fields => ["email"],
137                 }
138             ],
139             indices => [
140                 {
141                     name => "titleindex",
142                     fields => ["title"],
143                     extra => {
144                         foo => "bar",
145                         hello => "world",
146                         bar => "baz",
147                     },
148                 },
149             ],
150         } # end table Basic
151     ], # end tables
152
153     views => [
154         {
155             name => 'email_list',
156             sql => "SELECT email FROM Basic WHERE email IS NOT NULL",
157             fields => ['email'],
158             extra => {
159                 foo => "bar",
160                 hello => "world",
161                 bar => "baz",
162             },
163         },
164     ],
165
166     triggers => [
167         {
168             name                => 'foo_trigger',
169             perform_action_when => 'after',
170             database_event      => 'insert',
171             on_table            => 'Basic',
172             action              => 'update modified=timestamp();',
173             extra => {
174                 foo => "bar",
175                 hello => "world",
176                 bar => "baz",
177             },
178         },
179     ],
180
181     procedures => [
182         {
183             name       => 'foo_proc',
184             sql        => 'select foo from bar',
185             parameters => ['foo', 'bar'],
186             owner      => 'Nomar',
187             comments   => 'Go Sox!',
188             extra => {
189                 foo => "bar",
190                 hello => "world",
191                 bar => "baz",
192             },
193         },
194     ],
195
196 }); # end schema