All Schema objects now have an extra attribute. Added parsing support (and
[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             },
65             fields => [
66                 {
67                     name => "id",
68                     data_type => "int",
69                     default_value => undef,
70                     is_nullable => 0,
71                     size => 10,
72                     is_primary_key => 1,
73                     is_auto_increment => 1,
74                     extra => { ZEROFILL => 1 },
75                 },
76                 {
77                     name => "title",
78                     data_type => "varchar",
79                     is_nullable => 0,
80                     default_value => "hello",
81                     size => 100,
82                 },
83                 {
84                     name => "description",
85                     data_type => "text",
86                     is_nullable => 1,
87                     default_value => "",
88                 },
89                 {
90                     name => "email",
91                     data_type => "varchar",
92                     size => 255,
93                     is_unique => 1,
94                     default_value => undef,
95                     is_nullable => 1,
96                     extra => {
97                         foo => "bar",
98                         hello => "world",
99                         bar => "baz",
100                     }
101                 },
102                 {
103                     name => "explicitnulldef",
104                     data_type => "varchar",
105                     default_value => undef,
106                     is_nullable => 1,
107                 },
108                 {
109                     name => "explicitemptystring",
110                     data_type => "varchar",
111                     default_value => "",
112                     is_nullable => 1,
113                 },
114                 {
115                     name => "emptytagdef",
116                     data_type => "varchar",
117                     default_value => "",
118                     is_nullable => 1,
119                     comments => "Hello emptytagdef",
120                 },
121             ],
122             constraints => [
123                 {
124                     type => PRIMARY_KEY,
125                     fields => ["id"],
126                     extra => {
127                         foo => "bar",
128                         hello => "world",
129                         bar => "baz",
130                     },
131                 },
132                 {
133                     name => 'emailuniqueindex',
134                     type => UNIQUE,
135                     fields => ["email"],
136                 }
137             ],
138             indices => [
139                 {
140                     name => "titleindex",
141                     fields => ["title"],
142                     extra => {
143                         foo => "bar",
144                         hello => "world",
145                         bar => "baz",
146                     },
147                 },
148             ],
149         } # end table Basic
150     ], # end tables
151
152     views => [
153         {
154             name => 'email_list',
155             sql => "SELECT email FROM Basic WHERE email IS NOT NULL",
156             fields => ['email'],
157             extra => {
158                 foo => "bar",
159                 hello => "world",
160                 bar => "baz",
161             },
162         },
163     ],
164
165     triggers => [
166         {
167             name                => 'foo_trigger',
168             perform_action_when => 'after',
169             database_event      => 'insert',
170             on_table            => 'foo',
171             action              => 'update modified=timestamp();',
172             extra => {
173                 foo => "bar",
174                 hello => "world",
175                 bar => "baz",
176             },
177         },
178     ],
179
180     procedures => [
181         {
182             name       => 'foo_proc',
183             sql        => 'select foo from bar',
184             parameters => ['foo', 'bar'],
185             owner      => 'Nomar',
186             comments   => 'Go Sox!',
187             extra => {
188                 foo => "bar",
189                 hello => "world",
190                 bar => "baz",
191             },
192         },
193     ],
194
195 }); # end schema