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