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 { |
b1789409 |
30 | maybe_plan(150, '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", |
64 | }, |
ec791002 |
65 | fields => [ |
66 | { |
67 | name => "id", |
68 | data_type => "int", |
69 | default_value => undef, |
70 | is_nullable => 0, |
71 | size => 10, |
72 | is_primary_key => 1, |
73 | is_auto_increment => 1, |
94ed484b |
74 | extra => { ZEROFILL => 1 }, |
ec791002 |
75 | }, |
76 | { |
77 | name => "title", |
78 | data_type => "varchar", |
79 | is_nullable => 0, |
80 | default_value => "hello", |
81 | size => 100, |
82 | }, |
83 | { |
84 | name => "description", |
85 | data_type => "text", |
86 | is_nullable => 1, |
87 | default_value => "", |
88 | }, |
89 | { |
90 | name => "email", |
91 | data_type => "varchar", |
92 | size => 255, |
93 | is_unique => 1, |
94 | default_value => undef, |
95 | is_nullable => 1, |
94ed484b |
96 | extra => { |
97 | foo => "bar", |
98 | hello => "world", |
99 | bar => "baz", |
100 | } |
ec791002 |
101 | }, |
102 | { |
103 | name => "explicitnulldef", |
104 | data_type => "varchar", |
105 | default_value => undef, |
106 | is_nullable => 1, |
107 | }, |
108 | { |
109 | name => "explicitemptystring", |
110 | data_type => "varchar", |
111 | default_value => "", |
112 | is_nullable => 1, |
113 | }, |
114 | { |
115 | name => "emptytagdef", |
116 | data_type => "varchar", |
117 | default_value => "", |
118 | is_nullable => 1, |
94ed484b |
119 | comments => "Hello emptytagdef", |
ec791002 |
120 | }, |
121 | ], |
122 | constraints => [ |
123 | { |
124 | type => PRIMARY_KEY, |
125 | fields => ["id"], |
b1789409 |
126 | extra => { |
127 | foo => "bar", |
128 | hello => "world", |
129 | bar => "baz", |
130 | }, |
ec791002 |
131 | }, |
132 | { |
133 | name => 'emailuniqueindex', |
134 | type => UNIQUE, |
135 | fields => ["email"], |
136 | } |
137 | ], |
138 | indices => [ |
139 | { |
140 | name => "titleindex", |
141 | fields => ["title"], |
b1789409 |
142 | extra => { |
143 | foo => "bar", |
144 | hello => "world", |
145 | bar => "baz", |
146 | }, |
ec791002 |
147 | }, |
148 | ], |
149 | } # end table Basic |
150 | ], # end tables |
151 | |
152 | views => [ |
153 | { |
154 | name => 'email_list', |
155 | sql => "SELECT email FROM Basic WHERE email IS NOT NULL", |
156 | fields => ['email'], |
b1789409 |
157 | extra => { |
158 | foo => "bar", |
159 | hello => "world", |
160 | bar => "baz", |
161 | }, |
ec791002 |
162 | }, |
163 | ], |
164 | |
165 | triggers => [ |
166 | { |
167 | name => 'foo_trigger', |
168 | perform_action_when => 'after', |
169 | database_event => 'insert', |
170 | on_table => 'foo', |
171 | action => 'update modified=timestamp();', |
b1789409 |
172 | extra => { |
173 | foo => "bar", |
174 | hello => "world", |
175 | bar => "baz", |
176 | }, |
ec791002 |
177 | }, |
178 | ], |
179 | |
180 | procedures => [ |
181 | { |
182 | name => 'foo_proc', |
183 | sql => 'select foo from bar', |
184 | parameters => ['foo', 'bar'], |
185 | owner => 'Nomar', |
186 | comments => 'Go Sox!', |
b1789409 |
187 | extra => { |
188 | foo => "bar", |
189 | hello => "world", |
190 | bar => "baz", |
191 | }, |
ec791002 |
192 | }, |
193 | ], |
194 | |
195 | }); # end schema |