Improve trigger 'scope' attribute support (RT#119997)
[dbsrgits/SQL-Translator.git] / t / 16xml-parser.t
CommitLineData
aee4b66e 1#!/usr/bin/perl -w
c957e92d 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 {
c0ec0e22 30 maybe_plan(undef, '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,
3b9249fb 92 is_unique => 1,
ec791002 93 },
94 {
95 name => "description",
96 data_type => "text",
97 is_nullable => 1,
98 default_value => "",
99 },
100 {
101 name => "email",
102 data_type => "varchar",
21d62b63 103 size => 500,
ec791002 104 is_unique => 1,
105 default_value => undef,
106 is_nullable => 1,
94ed484b 107 extra => {
108 foo => "bar",
109 hello => "world",
110 bar => "baz",
111 }
ec791002 112 },
113 {
114 name => "explicitnulldef",
115 data_type => "varchar",
116 default_value => undef,
117 is_nullable => 1,
929ef265 118 size => 255,
ec791002 119 },
120 {
121 name => "explicitemptystring",
122 data_type => "varchar",
123 default_value => "",
124 is_nullable => 1,
929ef265 125 size => 255,
ec791002 126 },
127 {
128 name => "emptytagdef",
129 data_type => "varchar",
130 default_value => "",
131 is_nullable => 1,
94ed484b 132 comments => "Hello emptytagdef",
929ef265 133 size => 255,
ec791002 134 },
08d91aad 135 {
b08b5416 136 name => "another_id",
137 data_type => "int",
138 size => "10",
139 default_value => 2,
140 is_nullable => 1,
141 is_foreign_key => 1,
142 },
143 {
08d91aad 144 name => "timest",
145 data_type => "timestamp",
146 size => "0",
147 is_nullable => 1,
148 },
ec791002 149 ],
150 constraints => [
151 {
152 type => PRIMARY_KEY,
153 fields => ["id"],
b1789409 154 extra => {
155 foo => "bar",
156 hello => "world",
157 bar => "baz",
158 },
ec791002 159 },
160 {
161 name => 'emailuniqueindex',
162 type => UNIQUE,
163 fields => ["email"],
b08b5416 164 },
165 {
3b9249fb 166 name => 'very_long_index_name_on_title_field_which_should_be_truncated_for_various_rdbms',
167 type => UNIQUE,
168 fields => ["title"],
169 },
170 {
b08b5416 171 type => FOREIGN_KEY,
172 fields => ["another_id"],
173 reference_table => "Another",
174 reference_fields => ["id"],
1c680eb9 175 name => 'Basic_fk'
b08b5416 176 },
ec791002 177 ],
178 indices => [
179 {
180 name => "titleindex",
181 fields => ["title"],
b1789409 182 extra => {
183 foo => "bar",
184 hello => "world",
185 bar => "baz",
186 },
ec791002 187 },
188 ],
b08b5416 189 }, # end table Basic
190 {
191 name => "Another",
192 extra => {
193 foo => "bar",
194 hello => "world",
195 bar => "baz",
b08b5416 196 },
ace08c3d 197 options => [ { ENGINE => 'InnoDB' } ],
b08b5416 198 fields => [
199 {
200 name => "id",
201 data_type => "int",
202 default_value => undef,
203 is_nullable => 0,
204 size => 10,
205 is_primary_key => 1,
206 is_auto_increment => 1,
207 },
0a0aab7b 208 {
209 name => "num",
210 data_type => "numeric",
211 default_value => undef,
212 size => '10,2',
213 },
b08b5416 214 ],
215 }, # end table Another
ec791002 216 ], # end tables
217
218 views => [
219 {
220 name => 'email_list',
3910f248 221 sql => "SELECT email FROM Basic WHERE (email IS NOT NULL)",
ec791002 222 fields => ['email'],
b1789409 223 extra => {
224 foo => "bar",
225 hello => "world",
226 bar => "baz",
227 },
ec791002 228 },
229 ],
230
231 triggers => [
232 {
233 name => 'foo_trigger',
234 perform_action_when => 'after',
1f4dd26c 235 database_events => 'insert',
8ce5d615 236 on_table => 'Basic',
ec791002 237 action => 'update modified=timestamp();',
c0ec0e22 238 scope => 'row',
b1789409 239 extra => {
240 foo => "bar",
241 hello => "world",
242 bar => "baz",
243 },
ec791002 244 },
f38b7673 245 {
246 name => 'bar_trigger',
247 perform_action_when => 'before',
248 database_events => 'insert,update',
249 on_table => 'Basic',
250 action => 'update modified2=timestamp();',
c0ec0e22 251 scope => 'row',
f38b7673 252 extra => {
253 hello => "aliens",
254 },
255 },
ec791002 256 ],
257
258 procedures => [
259 {
260 name => 'foo_proc',
261 sql => 'select foo from bar',
262 parameters => ['foo', 'bar'],
263 owner => 'Nomar',
264 comments => 'Go Sox!',
b1789409 265 extra => {
266 foo => "bar",
267 hello => "world",
268 bar => "baz",
269 },
ec791002 270 },
271 ],
272
273}); # end schema
c0ec0e22 274
275done_testing;