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