VIEW support for Pg parser, also some cleanups
[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 {
f38b7673 30 maybe_plan(212, '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 },
202 ],
203 }, # end table Another
ec791002 204 ], # end tables
205
206 views => [
207 {
208 name => 'email_list',
209 sql => "SELECT email FROM Basic WHERE email IS NOT NULL",
210 fields => ['email'],
b1789409 211 extra => {
212 foo => "bar",
213 hello => "world",
214 bar => "baz",
215 },
ec791002 216 },
217 ],
218
219 triggers => [
220 {
221 name => 'foo_trigger',
222 perform_action_when => 'after',
1f4dd26c 223 database_events => 'insert',
8ce5d615 224 on_table => 'Basic',
ec791002 225 action => 'update modified=timestamp();',
b1789409 226 extra => {
227 foo => "bar",
228 hello => "world",
229 bar => "baz",
230 },
ec791002 231 },
f38b7673 232 {
233 name => 'bar_trigger',
234 perform_action_when => 'before',
235 database_events => 'insert,update',
236 on_table => 'Basic',
237 action => 'update modified2=timestamp();',
238 extra => {
239 hello => "aliens",
240 },
241 },
ec791002 242 ],
243
244 procedures => [
245 {
246 name => 'foo_proc',
247 sql => 'select foo from bar',
248 parameters => ['foo', 'bar'],
249 owner => 'Nomar',
250 comments => 'Go Sox!',
b1789409 251 extra => {
252 foo => "bar",
253 hello => "world",
254 bar => "baz",
255 },
ec791002 256 },
257 ],
258
259}); # end schema