Added parsing of field.extra
[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                     extra => { ZEROFILL => 1 },
70                 },
71                 {
72                     name => "title",
73                     data_type => "varchar",
74                     is_nullable => 0,
75                     default_value => "hello",
76                     size => 100,
77                 },
78                 {
79                     name => "description",
80                     data_type => "text",
81                     is_nullable => 1,
82                     default_value => "",
83                 },
84                 {
85                     name => "email",
86                     data_type => "varchar",
87                     size => 255,
88                     is_unique => 1,
89                     default_value => undef,
90                     is_nullable => 1,
91                     extra => {
92                         foo => "bar",
93                         hello => "world",
94                         bar => "baz",
95                     }
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                     comments => "Hello emptytagdef",
115                 },
116             ],
117             constraints => [
118                 {
119                     type => PRIMARY_KEY,
120                     fields => ["id"],
121                 },
122                 {
123                     name => 'emailuniqueindex',
124                     type => UNIQUE,
125                     fields => ["email"],
126                 }
127             ],
128             indices => [
129                 {
130                     name => "titleindex",
131                     fields => ["title"],
132                 },
133             ],
134         } # end table Basic
135     ], # end tables
136
137     views => [
138         {
139             name => 'email_list',
140             sql => "SELECT email FROM Basic WHERE email IS NOT NULL",
141             fields => ['email'],
142         },
143     ],
144
145     triggers => [
146         {
147             name                => 'foo_trigger',
148             perform_action_when => 'after',
149             database_event      => 'insert',
150             on_table            => 'foo',
151             action              => 'update modified=timestamp();',
152         },
153     ],
154
155     procedures => [
156         {
157             name       => 'foo_proc',
158             sql        => 'select foo from bar',
159             parameters => ['foo', 'bar'],
160             owner      => 'Nomar',
161             comments   => 'Go Sox!',
162         },
163     ],
164
165 }); # end schema