Updated to test the new, single format sqlf xml.
[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(142, '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             fields => [
61                 {
62                     name => "id",
63                     data_type => "int",
64                     default_value => undef,
65                     is_nullable => 0,
66                     size => 10,
67                     is_primary_key => 1,
68                     is_auto_increment => 1,
69                 },
70                 {
71                     name => "title",
72                     data_type => "varchar",
73                     is_nullable => 0,
74                     default_value => "hello",
75                     size => 100,
76                 },
77                 {
78                     name => "description",
79                     data_type => "text",
80                     is_nullable => 1,
81                     default_value => "",
82                 },
83                 {
84                     name => "email",
85                     data_type => "varchar",
86                     size => 255,
87                     is_unique => 1,
88                     default_value => undef,
89                     is_nullable => 1,
90                 },
91                 {
92                     name => "explicitnulldef",
93                     data_type => "varchar",
94                     default_value => undef,
95                     is_nullable => 1,
96                 },
97                 {
98                     name => "explicitemptystring",
99                     data_type => "varchar",
100                     default_value => "",
101                     is_nullable => 1,
102                 },
103                 {
104                     name => "emptytagdef",
105                     data_type => "varchar",
106                     default_value => "",
107                     is_nullable => 1,
108                 },
109             ],
110             constraints => [
111                 {
112                     type => PRIMARY_KEY,
113                     fields => ["id"],
114                 },
115                 {
116                     name => 'emailuniqueindex',
117                     type => UNIQUE,
118                     fields => ["email"],
119                 }
120             ],
121             indices => [
122                 {
123                     name => "titleindex",
124                     fields => ["title"],
125                 },
126             ],
127         } # end table Basic
128     ], # end tables
129
130     views => [
131         {
132             name => 'email_list',
133             sql => "SELECT email FROM Basic WHERE email IS NOT NULL",
134             fields => ['email'],
135         },
136     ],
137
138     triggers => [
139         {
140             name                => 'foo_trigger',
141             perform_action_when => 'after',
142             database_event      => 'insert',
143             on_table            => 'foo',
144             action              => 'update modified=timestamp();',
145         },
146     ],
147
148     procedures => [
149         {
150             name       => 'foo_proc',
151             sql        => 'select foo from bar',
152             parameters => ['foo', 'bar'],
153             owner      => 'Nomar',
154             comments   => 'Go Sox!',
155         },
156     ],
157
158 }); # end schema