Now uses schema_ok
[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 plan tests => 284;
30
31 foreach (
32     "$Bin/data/xml/schema-basic.xml",
33     "$Bin/data/xml/schema-basic-attribs.xml"
34 ) {
35     do_file($_);
36 }
37
38 sub do_file {
39     my $testschema = shift;
40     # Parse the test XML schema
41     my $obj;
42     $obj = SQL::Translator->new(
43         debug          => DEBUG,
44         show_warnings  => 1,
45         add_drop_table => 1,
46     );
47     die "Can't find test schema $testschema" unless -e $testschema;
48     my $sql = $obj->translate(
49         from     => 'XML-SQLFairy',
50         to       => 'MySQL',
51         filename => $testschema,
52     );
53     print $sql if DEBUG;
54
55     # Test the schema objs generted from the XML
56     #
57     my $scma = $obj->schema;
58
59     # Hmmm, when using schema_ok the field test data gets a bit too nested and
60     # fiddly to work with. (See 28xml-xmi-parser-sqlfairy.t for more split out
61     # version)
62     schema_ok( $scma, {
63         tables => [
64             {
65                 name => "Basic",
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                     },
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                     },
97                     {
98                         name => "explicitnulldef",
99                         data_type => "varchar",
100                         default_value => undef,
101                         is_nullable => 1,
102                     },
103                     {
104                         name => "explicitemptystring",
105                         data_type => "varchar",
106                         default_value => "",
107                         is_nullable => 1,
108                     },
109                     {
110                         name => "emptytagdef",
111                         data_type => "varchar",
112                         default_value => "",
113                         is_nullable => 1,
114                     },
115                 ],
116                 constraints => [
117                     {
118                         type => PRIMARY_KEY,
119                         fields => ["id"],
120                     },
121                     {
122                         name => 'emailuniqueindex',
123                         type => UNIQUE,
124                         fields => ["email"],
125                     }
126                 ],
127                 indices => [
128                     {
129                         name => "titleindex",
130                         fields => ["title"],
131                     },
132                 ],
133             } # end table Basic
134         ], # end tables
135
136         views => [
137             {
138                 name => 'email_list',
139                 sql => "SELECT email FROM Basic WHERE email IS NOT NULL",
140                 fields => ['email'],
141             },
142         ],
143
144         triggers => [
145             {
146                 name                => 'foo_trigger',
147                 perform_action_when => 'after',
148                 database_event      => 'insert',
149                 on_table            => 'foo',
150                 action              => 'update modified=timestamp();',
151             },
152         ],
153
154         procedures => [
155             {
156                 name       => 'foo_proc',
157                 sql        => 'select foo from bar',
158                 parameters => ['foo', 'bar'],
159                 owner      => 'Nomar',
160                 comments   => 'Go Sox!',
161             },
162         ],
163
164     }); # end schema
165
166 } # end do_file()