Commit | Line | Data |
6c9e9546 |
1 | #!/usr/bin/perl |
2 | |
3 | use warnings; |
4 | use strict; |
5 | use Test::More qw/no_plan/; |
6 | use Test::Exception; |
ce6c267a |
7 | use Test::Differences; |
6c9e9546 |
8 | use FindBin qw/$Bin/; |
9 | |
10 | use SQL::Translator; |
11 | |
12 | ### Set $ENV{SQLTTEST_RT_DEBUG} = 1 for more output |
13 | |
14 | # What tests to run - parser/producer name, and optional args |
15 | my $plan = [ |
16 | { |
3a328a0a |
17 | engine => 'XML', |
18 | }, |
19 | { |
6c9e9546 |
20 | engine => 'SQLite', |
21 | producer_args => {}, |
22 | parser_args => {}, |
23 | }, |
24 | { |
25 | engine => 'MySQL', |
26 | producer_args => {}, |
27 | parser_args => {}, |
28 | }, |
29 | { |
30 | engine => 'MySQL', |
31 | name => 'MySQL 5.0', |
32 | producer_args => { mysql_version => 5 }, |
33 | parser_args => { mysql_parser_version => 5 }, |
34 | }, |
35 | { |
36 | engine => 'MySQL', |
37 | name => 'MySQL 5.1', |
38 | producer_args => { mysql_version => '5.1' }, |
39 | parser_args => { mysql_parser_version => '5.1' }, |
40 | }, |
41 | { |
42 | engine => 'PostgreSQL', |
43 | producer_args => {}, |
44 | parser_args => {}, |
45 | }, |
e2fb9ad3 |
46 | { |
47 | engine => 'SQLServer', |
48 | producer_args => {}, |
49 | parser_args => {}, |
50 | }, |
0a2d7cf1 |
51 | { |
52 | engine => 'Oracle', |
53 | producer_args => {}, |
54 | parser_args => {}, |
55 | todo => 'Needs volunteers', |
56 | }, |
57 | { |
58 | engine => 'Sybase', |
59 | producer_args => {}, |
60 | parser_args => {}, |
61 | todo => 'Needs volunteers', |
62 | }, |
63 | { |
64 | engine => 'DB2', |
65 | producer_args => {}, |
66 | parser_args => {}, |
67 | todo => 'Needs volunteers', |
68 | }, |
0e0ca612 |
69 | |
474910ee |
70 | # YAML parsing/producing cycles result in some weird self referencing structure |
71 | # { |
72 | # engine => 'YAML', |
73 | # }, |
74 | |
0e0ca612 |
75 | # There is no Access producer |
76 | # { |
77 | # engine => 'Access', |
78 | # producer_args => {}, |
79 | # parser_args => {}, |
80 | # }, |
6c9e9546 |
81 | ]; |
82 | |
83 | |
84 | # This data file has the right mix of table/view/procedure/trigger |
85 | # definitions, and lists enough quirks to trip up most combos |
86 | # I am not sure if augmenting it will break other tests - experiment |
87 | my $base_file = "$Bin/data/xml/schema.xml"; |
88 | |
89 | my $base_t = SQL::Translator->new; |
0e0ca612 |
90 | $base_t->$_ (1) for qw/add_drop_table no_comments/; |
6c9e9546 |
91 | |
92 | my $base_schema = $base_t->translate ( |
93 | parser => 'XML', |
94 | file => $base_file, |
95 | ) or die $base_t->error; |
96 | |
474910ee |
97 | #assume there is at least one table |
98 | my $string_re = { |
99 | XML => qr/<tables>\s*<table/, |
100 | YAML => qr/\A---\n.+tables\:/s, |
101 | SQL => qr/^\s*CREATE TABLE/m, |
102 | }; |
6c9e9546 |
103 | |
104 | for my $args (@$plan) { |
0a2d7cf1 |
105 | TODO: { |
106 | local $TODO = $args->{todo} if $args->{todo}; |
6c9e9546 |
107 | |
0a2d7cf1 |
108 | $args->{name} ||= $args->{engine}; |
6c9e9546 |
109 | |
0a2d7cf1 |
110 | lives_ok ( |
111 | sub { check_roundtrip ($args, $base_schema) }, |
112 | "Round trip for $args->{name} did not throw an exception", |
113 | ); |
114 | } |
6c9e9546 |
115 | } |
116 | |
117 | |
118 | sub check_roundtrip { |
119 | my ($args, $base_schema) = @_; |
120 | my $base_t = $base_schema->translator; |
121 | |
3a328a0a |
122 | # create some output from the submitted schema |
123 | my $base_out = $base_t->translate ( |
6c9e9546 |
124 | data => $base_schema, |
125 | producer => $args->{engine}, |
126 | producer_args => $args->{producer_args}, |
127 | ); |
128 | |
129 | like ( |
3a328a0a |
130 | $base_out, |
474910ee |
131 | $string_re->{$args->{engine}} || $string_re->{SQL}, |
6c9e9546 |
132 | "Received some meaningful output from the first $args->{name} production", |
4c549812 |
133 | ) or do { |
134 | diag ( _gen_diag ($base_t->error) ); |
135 | return; |
136 | }; |
6c9e9546 |
137 | |
138 | # parse the sql back |
139 | my $parser_t = SQL::Translator->new; |
0e0ca612 |
140 | $parser_t->$_ (1) for qw/add_drop_table no_comments/; |
6c9e9546 |
141 | my $mid_schema = $parser_t->translate ( |
3a328a0a |
142 | data => $base_out, |
6c9e9546 |
143 | parser => $args->{engine}, |
144 | parser_args => $args->{parser_args}, |
145 | ); |
146 | |
147 | isa_ok ($mid_schema, 'SQL::Translator::Schema', "First $args->{name} parser pass produced a schema:") |
4c549812 |
148 | or do { |
3a328a0a |
149 | diag (_gen_diag ( $parser_t->error, $base_out ) ); |
4c549812 |
150 | return; |
151 | }; |
6c9e9546 |
152 | |
153 | # schemas should be comparable at least as far as table/field numbers go |
154 | is_deeply ( |
155 | _get_table_info ($mid_schema->get_tables), |
156 | _get_table_info ($base_schema->get_tables), |
157 | "Schema tables generally match afer $args->{name} parser trip", |
4c549812 |
158 | ) or return; |
6c9e9546 |
159 | |
160 | # and produce sql once again |
161 | |
162 | # Producing a schema with a Translator different from the one the schema was generated |
163 | # from does not work. This is arguably a bug, 61translator_agnostic.t works with that |
164 | # my $producer_t = SQL::Translator->new; |
0e0ca612 |
165 | # $producer_t->$_ (1) for qw/add_drop_table no_comments/; |
6c9e9546 |
166 | |
167 | # my $rt_sql = $producer_t->translate ( |
168 | # data => $mid_schema, |
169 | # producer => $args->{engine}, |
170 | # producer_args => $args->{producer_args}, |
171 | # ); |
172 | |
3a328a0a |
173 | my $rt_out = $parser_t->translate ( |
6c9e9546 |
174 | data => $mid_schema, |
175 | producer => $args->{engine}, |
176 | producer_args => $args->{producer_args}, |
177 | ); |
178 | |
179 | like ( |
3a328a0a |
180 | $rt_out, |
474910ee |
181 | $string_re->{$args->{engine}} || $string_re->{SQL}, |
6c9e9546 |
182 | "Received some meaningful output from the second $args->{name} production", |
4c549812 |
183 | ) or do { |
184 | diag ( _gen_diag ( $parser_t->error ) ); |
185 | return; |
186 | }; |
6c9e9546 |
187 | |
188 | # the two sql strings should be identical |
189 | my $msg = "$args->{name} SQL roundtrip successful - SQL statements match"; |
ce6c267a |
190 | $ENV{SQLTTEST_RT_DEBUG} #stringify below because IO::Scalar does not behave nice |
191 | ? eq_or_diff ("$rt_out", "$base_out", $msg) |
192 | : ok ("$rt_out" eq "$base_out", $msg) |
6c9e9546 |
193 | ; |
194 | } |
195 | |
196 | sub _get_table_info { |
197 | my @tables = @_; |
198 | |
199 | my @info; |
200 | |
201 | for my $t (@tables) { |
202 | push @info, { |
203 | name => $t->name, |
204 | fields => [ |
205 | map { $_->name } ($t->get_fields), |
206 | ], |
207 | }; |
208 | } |
209 | |
210 | return \@info; |
211 | } |
212 | |
3a328a0a |
213 | # takes an error string and an optional output block |
6c9e9546 |
214 | # returns the string conctenated with a line-numbered block for |
215 | # easier reading |
216 | sub _gen_diag { |
3a328a0a |
217 | my ($err, $out) = @_; |
6c9e9546 |
218 | |
219 | return 'Unknown error' unless $err; |
220 | |
221 | |
3a328a0a |
222 | if ($out and $ENV{SQLTTEST_RT_DEBUG}) { |
223 | my @lines; |
224 | for (split /\n/, $out) { |
225 | push @lines, sprintf ('%03d: %s', |
226 | scalar @lines + 1, |
6c9e9546 |
227 | $_, |
228 | ); |
229 | } |
230 | |
3a328a0a |
231 | return "$err\n\n" . join ("\n", @lines); |
6c9e9546 |
232 | } |
233 | |
234 | return $err; |
235 | } |