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", |
b1789409 |
60 | extra => { |
61 | foo => "bar", |
62 | hello => "world", |
63 | bar => "baz", |
5a0c7b43 |
64 | mysql_table_type => "InnoDB", |
b1789409 |
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, |
108 | }, |
109 | { |
110 | name => "explicitemptystring", |
111 | data_type => "varchar", |
112 | default_value => "", |
113 | is_nullable => 1, |
114 | }, |
115 | { |
116 | name => "emptytagdef", |
117 | data_type => "varchar", |
118 | default_value => "", |
119 | is_nullable => 1, |
94ed484b |
120 | comments => "Hello emptytagdef", |
ec791002 |
121 | }, |
08d91aad |
122 | { |
b08b5416 |
123 | name => "another_id", |
124 | data_type => "int", |
125 | size => "10", |
126 | default_value => 2, |
127 | is_nullable => 1, |
128 | is_foreign_key => 1, |
129 | }, |
130 | { |
08d91aad |
131 | name => "timest", |
132 | data_type => "timestamp", |
133 | size => "0", |
134 | is_nullable => 1, |
135 | }, |
ec791002 |
136 | ], |
137 | constraints => [ |
138 | { |
139 | type => PRIMARY_KEY, |
140 | fields => ["id"], |
b1789409 |
141 | extra => { |
142 | foo => "bar", |
143 | hello => "world", |
144 | bar => "baz", |
145 | }, |
ec791002 |
146 | }, |
147 | { |
148 | name => 'emailuniqueindex', |
149 | type => UNIQUE, |
150 | fields => ["email"], |
b08b5416 |
151 | }, |
152 | { |
153 | type => FOREIGN_KEY, |
154 | fields => ["another_id"], |
155 | reference_table => "Another", |
156 | reference_fields => ["id"], |
157 | }, |
ec791002 |
158 | ], |
159 | indices => [ |
160 | { |
161 | name => "titleindex", |
162 | fields => ["title"], |
b1789409 |
163 | extra => { |
164 | foo => "bar", |
165 | hello => "world", |
166 | bar => "baz", |
167 | }, |
ec791002 |
168 | }, |
169 | ], |
b08b5416 |
170 | }, # end table Basic |
171 | { |
172 | name => "Another", |
173 | extra => { |
174 | foo => "bar", |
175 | hello => "world", |
176 | bar => "baz", |
177 | mysql_table_type => "InnoDB", |
178 | }, |
179 | fields => [ |
180 | { |
181 | name => "id", |
182 | data_type => "int", |
183 | default_value => undef, |
184 | is_nullable => 0, |
185 | size => 10, |
186 | is_primary_key => 1, |
187 | is_auto_increment => 1, |
188 | }, |
189 | ], |
190 | }, # end table Another |
ec791002 |
191 | ], # end tables |
192 | |
193 | views => [ |
194 | { |
195 | name => 'email_list', |
196 | sql => "SELECT email FROM Basic WHERE email IS NOT NULL", |
197 | fields => ['email'], |
b1789409 |
198 | extra => { |
199 | foo => "bar", |
200 | hello => "world", |
201 | bar => "baz", |
202 | }, |
ec791002 |
203 | }, |
204 | ], |
205 | |
206 | triggers => [ |
207 | { |
208 | name => 'foo_trigger', |
209 | perform_action_when => 'after', |
210 | database_event => 'insert', |
8ce5d615 |
211 | on_table => 'Basic', |
ec791002 |
212 | action => 'update modified=timestamp();', |
b1789409 |
213 | extra => { |
214 | foo => "bar", |
215 | hello => "world", |
216 | bar => "baz", |
217 | }, |
ec791002 |
218 | }, |
219 | ], |
220 | |
221 | procedures => [ |
222 | { |
223 | name => 'foo_proc', |
224 | sql => 'select foo from bar', |
225 | parameters => ['foo', 'bar'], |
226 | owner => 'Nomar', |
227 | comments => 'Go Sox!', |
b1789409 |
228 | extra => { |
229 | foo => "bar", |
230 | hello => "world", |
231 | bar => "baz", |
232 | }, |
ec791002 |
233 | }, |
234 | ], |
235 | |
236 | }); # end schema |