add to changes
[dbsrgits/SQL-Translator.git] / t / 16xml-parser.t
CommitLineData
c957e92d 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'
c957e92d 6#
1c375f48 7# Run script with -d for debug.
c957e92d 8
2e11379e 9use strict;
1c375f48 10
11use FindBin qw/$Bin/;
12
b3530353 13use Test::More;
1c375f48 14use Test::SQL::Translator;
c957e92d 15use Test::Exception;
c957e92d 16use Data::Dumper;
1c375f48 17use SQL::Translator;
18use SQL::Translator::Schema::Constants;
19
20# Simple options. -d for debug
2e11379e 21my %opt;
c957e92d 22BEGIN { map { $opt{$_}=1 if s/^-// } @ARGV; }
23use constant DEBUG => (exists $opt{d} ? 1 : 0);
c957e92d 24
c957e92d 25
26# Testing 1,2,3,4...
27#=============================================================================
c957e92d 28
ec791002 29BEGIN {
0a0aab7b 30 maybe_plan(224, 'SQL::Translator::Parser::XML::SQLFairy');
07a82527 31}
32
ec791002 33my $testschema = "$Bin/data/xml/schema.xml";
34
35my $sqlt;
36$sqlt = SQL::Translator->new(
37 debug => DEBUG,
38 show_warnings => 1,
39 add_drop_table => 1,
40);
41die "Can't find test schema $testschema" unless -e $testschema;
f38b7673 42
43my $sql;
44{
45 my @w;
46 local $SIG{__WARN__} = sub { push @w, $_[0] if $_[0] =~ /The database_event tag is deprecated - please use database_events/ };
47
48 $sql = $sqlt->translate(
ec791002 49 from => 'XML-SQLFairy',
50 to => 'MySQL',
51 filename => $testschema,
f38b7673 52 ) or die $sqlt->error;
53 print $sql if DEBUG;
54
55 ok (@w, 'database_event deprecation warning issued');
56}
ec791002 57
58# Test the schema objs generted from the XML
59#
60my $scma = $sqlt->schema;
61
62# Hmmm, when using schema_ok the field test data gets a bit too nested and
63# fiddly to work with. (See 28xml-xmi-parser-sqlfairy.t for more a split out
64# version)
65schema_ok( $scma, {
66 tables => [
67 {
68 name => "Basic",
ace08c3d 69 options => [ { ENGINE => 'InnoDB' } ],
b1789409 70 extra => {
71 foo => "bar",
72 hello => "world",
73 bar => "baz",
74 },
ec791002 75 fields => [
76 {
77 name => "id",
78 data_type => "int",
79 default_value => undef,
80 is_nullable => 0,
81 size => 10,
82 is_primary_key => 1,
83 is_auto_increment => 1,
94ed484b 84 extra => { ZEROFILL => 1 },
ec791002 85 },
86 {
87 name => "title",
88 data_type => "varchar",
89 is_nullable => 0,
90 default_value => "hello",
91 size => 100,
92 },
93 {
94 name => "description",
95 data_type => "text",
96 is_nullable => 1,
97 default_value => "",
98 },
99 {
100 name => "email",
101 data_type => "varchar",
21d62b63 102 size => 500,
ec791002 103 is_unique => 1,
104 default_value => undef,
105 is_nullable => 1,
94ed484b 106 extra => {
107 foo => "bar",
108 hello => "world",
109 bar => "baz",
110 }
ec791002 111 },
112 {
113 name => "explicitnulldef",
114 data_type => "varchar",
115 default_value => undef,
116 is_nullable => 1,
929ef265 117 size => 255,
ec791002 118 },
119 {
120 name => "explicitemptystring",
121 data_type => "varchar",
122 default_value => "",
123 is_nullable => 1,
929ef265 124 size => 255,
ec791002 125 },
126 {
127 name => "emptytagdef",
128 data_type => "varchar",
129 default_value => "",
130 is_nullable => 1,
94ed484b 131 comments => "Hello emptytagdef",
929ef265 132 size => 255,
ec791002 133 },
08d91aad 134 {
b08b5416 135 name => "another_id",
136 data_type => "int",
137 size => "10",
138 default_value => 2,
139 is_nullable => 1,
140 is_foreign_key => 1,
141 },
142 {
08d91aad 143 name => "timest",
144 data_type => "timestamp",
145 size => "0",
146 is_nullable => 1,
147 },
ec791002 148 ],
149 constraints => [
150 {
151 type => PRIMARY_KEY,
152 fields => ["id"],
b1789409 153 extra => {
154 foo => "bar",
155 hello => "world",
156 bar => "baz",
157 },
ec791002 158 },
159 {
160 name => 'emailuniqueindex',
161 type => UNIQUE,
162 fields => ["email"],
b08b5416 163 },
164 {
165 type => FOREIGN_KEY,
166 fields => ["another_id"],
167 reference_table => "Another",
168 reference_fields => ["id"],
1c680eb9 169 name => 'Basic_fk'
b08b5416 170 },
ec791002 171 ],
172 indices => [
173 {
174 name => "titleindex",
175 fields => ["title"],
b1789409 176 extra => {
177 foo => "bar",
178 hello => "world",
179 bar => "baz",
180 },
ec791002 181 },
182 ],
b08b5416 183 }, # end table Basic
184 {
185 name => "Another",
186 extra => {
187 foo => "bar",
188 hello => "world",
189 bar => "baz",
b08b5416 190 },
ace08c3d 191 options => [ { ENGINE => 'InnoDB' } ],
b08b5416 192 fields => [
193 {
194 name => "id",
195 data_type => "int",
196 default_value => undef,
197 is_nullable => 0,
198 size => 10,
199 is_primary_key => 1,
200 is_auto_increment => 1,
201 },
0a0aab7b 202 {
203 name => "num",
204 data_type => "numeric",
205 default_value => undef,
206 size => '10,2',
207 },
b08b5416 208 ],
209 }, # end table Another
ec791002 210 ], # end tables
211
212 views => [
213 {
214 name => 'email_list',
3910f248 215 sql => "SELECT email FROM Basic WHERE (email IS NOT NULL)",
ec791002 216 fields => ['email'],
b1789409 217 extra => {
218 foo => "bar",
219 hello => "world",
220 bar => "baz",
221 },
ec791002 222 },
223 ],
224
225 triggers => [
226 {
227 name => 'foo_trigger',
228 perform_action_when => 'after',
1f4dd26c 229 database_events => 'insert',
8ce5d615 230 on_table => 'Basic',
ec791002 231 action => 'update modified=timestamp();',
b1789409 232 extra => {
233 foo => "bar",
234 hello => "world",
235 bar => "baz",
236 },
ec791002 237 },
f38b7673 238 {
239 name => 'bar_trigger',
240 perform_action_when => 'before',
241 database_events => 'insert,update',
242 on_table => 'Basic',
243 action => 'update modified2=timestamp();',
244 extra => {
245 hello => "aliens",
246 },
247 },
ec791002 248 ],
249
250 procedures => [
251 {
252 name => 'foo_proc',
253 sql => 'select foo from bar',
254 parameters => ['foo', 'bar'],
255 owner => 'Nomar',
256 comments => 'Go Sox!',
b1789409 257 extra => {
258 foo => "bar",
259 hello => "world",
260 bar => "baz",
261 },
ec791002 262 },
263 ],
264
265}); # end schema