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