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