Test table and field names with quote characters in them
[dbsrgits/SQL-Translator.git] / t / 60roundtrip.t
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5 use Test::More qw/no_plan/;
6 use Test::Exception;
7 use Test::Differences;
8 use FindBin qw/$Bin/;
9
10 use SQL::Translator;
11 use SQL::Translator::Utils qw/ddl_parser_instance/;
12
13
14 ### Set $ENV{SQLTTEST_RT_DEBUG} = 1 for more output
15
16 # What tests to run - parser/producer name, and optional args
17 my $plan = [
18   {
19     engine => 'XML',
20     req => 'XML::LibXML 1.69',
21     no_grammar => 1,
22   },
23   {
24     engine => 'YAML',
25     no_grammar => 1,
26   },
27
28   {
29     engine => 'SQLite',
30     producer_args => {},
31     parser_args => {},
32   },
33   {
34     engine => 'MySQL',
35     producer_args => {},
36     parser_args => {},
37   },
38   {
39     engine => 'MySQL',
40     name => 'MySQL 5.0',
41     producer_args => { mysql_version => 5 },
42     parser_args => { mysql_parser_version => 5 },
43   },
44   {
45     engine => 'MySQL',
46     name => 'MySQL 5.1',
47     producer_args => { mysql_version => '5.1' },
48     parser_args => { mysql_parser_version => '5.1' },
49   },
50   {
51     engine => 'PostgreSQL',
52     producer_args => {},
53     parser_args => {},
54   },
55   {
56     engine => 'SQLServer',
57     producer_args => {},
58     parser_args => {},
59   },
60
61   {
62     engine => 'Oracle',
63     producer_args => {},
64     parser_args => {},
65     todo => 'Needs volunteers',
66   },
67   {
68     engine => 'Sybase',
69     producer_args => {},
70     parser_args => {},
71     todo => 'Needs volunteers',
72   },
73   {
74     engine => 'DB2',
75     producer_args => {},
76     parser_args => {},
77     todo => 'Needs volunteers',
78   },
79
80 # There is no Access producer
81 #  {
82 #    engine => 'Access',
83 #    producer_args => {},
84 #    parser_args => {},
85 #  },
86 ];
87
88
89 # This data file has the right mix of table/view/procedure/trigger
90 # definitions, and lists enough quirks to trip up most combos
91 my $base_file = "$Bin/data/roundtrip_autogen.yaml";
92 open (my $base_fh, '<', $base_file) or die "$base_file: $!";
93
94 my $base_t = SQL::Translator->new;
95 $base_t->$_ (1) for qw/add_drop_table no_comments quote_identifiers/;
96
97 my $base_schema = $base_t->translate (
98   parser => 'YAML',
99   data => do { local $/; <$base_fh>; },
100 ) or die $base_t->error;
101
102
103 #assume there is at least one table
104 my $string_re = {
105   XML => qr/<tables>\s*<table/,
106   YAML => qr/\A---\n.+tables\:/s,
107   SQL => qr/^\s*CREATE TABLE/m,
108 };
109
110 for my $args (@$plan) {
111   SKIP: {
112     $args->{name} ||= $args->{engine};
113
114     my @req = ref $args->{req} ? @{$args->{req}} : $args->{req}||();
115     my @missing;
116     for (@req) {
117       eval "use $_ ()";
118       push @missing, $_ if ($@);
119     }
120     if (@missing) {
121       skip sprintf ('Need %s for %s roundtrip test',
122         join (', ', @missing),
123         $args->{name},
124       );
125     }
126
127     use_ok("SQL::Translator::Producer::$args->{engine}");
128     use_ok("SQL::Translator::Parser::$args->{engine}");
129
130     ok(ddl_parser_instance($args->{engine}), 'Got proper parser instance')
131       unless $args->{no_grammar};
132
133     TODO: {
134       local $TODO = $args->{todo} if $args->{todo};
135
136       no warnings 'once';
137       # silence PR::D from spewing on STDERR
138       local $::RD_ERRORS = 0 if $args->{todo};
139       local $::RD_WARN = 0 if $args->{todo};
140       local $::RD_HINT = 0 if $args->{todo};
141
142       lives_ok (
143         sub { check_roundtrip ($args, $base_schema) },
144         "Round trip for $args->{name} did not throw an exception",
145       );
146     }
147   }
148 }
149
150
151 sub check_roundtrip {
152   my ($args, $base_schema) = @_;
153   my $base_t = $base_schema->translator;
154
155 # create some output from the submitted schema
156   my $base_out = $base_t->translate (
157     data => $base_schema,
158     producer => $args->{engine},
159     producer_args => $args->{producer_args},
160   );
161
162   like (
163     $base_out,
164     $string_re->{$args->{engine}} || $string_re->{SQL},
165     "Received some meaningful output from the first $args->{name} production",
166   ) or do {
167     diag ( _gen_diag ($base_t->error) );
168     return;
169   };
170
171 # parse the sql back
172   my $parser_t = SQL::Translator->new;
173   $parser_t->$_ (1) for qw/add_drop_table no_comments quote_identifiers/;
174   my $mid_schema = $parser_t->translate (
175     data => $base_out,
176     parser => $args->{engine},
177     parser_args => $args->{parser_args},
178   );
179
180   isa_ok ($mid_schema, 'SQL::Translator::Schema', "First $args->{name} parser pass produced a schema:")
181     or do {
182       diag (_gen_diag ( $parser_t->error, $base_out ) );
183       my $i;
184       note join ("\n" . ( '=' x 76) . "\n",
185         'Unparseable DDL:',
186         (join ("\n", map { ++$i . ":\t$_" } split /\n/, $base_out) ),
187         ''
188       );
189       return;
190     };
191
192 # schemas should be comparable at least as far as table/field numbers go
193   is_deeply (
194     _get_table_info ($mid_schema->get_tables),
195     _get_table_info ($base_schema->get_tables),
196     "Schema tables generally match afer $args->{name} parser trip",
197   ) or (diag(explain _get_table_info($mid_schema->get_tables)), return);
198
199 # and produce sql once again
200
201 # Producing a schema with a Translator different from the one the schema was generated
202 # from does not work. This is arguably a bug, 61translator_agnostic.t works with that
203 #  my $producer_t = SQL::Translator->new;
204 #  $producer_t->$_ (1) for qw/add_drop_table no_comments/;
205
206 #  my $rt_sql = $producer_t->translate (
207 #    data => $mid_schema,
208 #    producer => $args->{engine},
209 #    producer_args => $args->{producer_args},
210 #  );
211
212   my $rt_out = $parser_t->translate (
213     data => $mid_schema,
214     producer => $args->{engine},
215     producer_args => $args->{producer_args},
216   );
217
218   like (
219     $rt_out,
220     $string_re->{$args->{engine}} || $string_re->{SQL},
221     "Received some meaningful output from the second $args->{name} production",
222   ) or do {
223     diag ( _gen_diag ( $parser_t->error ) );
224     return;
225   };
226
227 # the two sql strings should be identical
228   my $msg = "$args->{name} SQL roundtrip successful - SQL statements match";
229   $ENV{SQLTTEST_RT_DEBUG}
230     ? eq_or_diff ($rt_out, $base_out, $msg)
231     : ok ($rt_out eq $base_out, $msg)
232   ;
233 }
234
235 sub _get_table_info {
236   my @tables = @_;
237
238   my @info;
239
240   for my $t (@tables) {
241     push @info, {
242       name => $t->name,
243       fields => [
244         map { $_->name } ($t->get_fields),
245       ],
246     };
247   }
248
249   return \@info;
250 }
251
252 # takes an error string and an optional output block
253 # returns the string conctenated with a line-numbered block for
254 # easier reading
255 sub _gen_diag {
256   my ($err, $out) = @_;
257
258   return 'Unknown error' unless $err;
259
260
261   if ($out and $ENV{SQLTTEST_RT_DEBUG}) {
262     my @lines;
263     for (split /\n/, $out) {
264       push @lines, sprintf ('%03d: %s',
265         scalar @lines + 1,
266         $_,
267       );
268     }
269
270     return "$err\n\n" . join ("\n", @lines);
271   }
272
273   return $err;
274 }