5 use Test::More qw/no_plan/;
13 ### Set $ENV{SQLTTEST_RT_DEBUG} = 1 for more output
15 # What tests to run - parser/producer name, and optional args
38 producer_args => { mysql_version => 5 },
39 parser_args => { mysql_parser_version => 5 },
44 producer_args => { mysql_version => '5.1' },
45 parser_args => { mysql_parser_version => '5.1' },
48 engine => 'PostgreSQL',
53 engine => 'SQLServer',
60 # producer_args => {},
62 # todo => 'Needs volunteers',
66 # producer_args => {},
68 # todo => 'Needs volunteers',
72 # producer_args => {},
74 # todo => 'Needs volunteers',
77 # There is no Access producer
80 # producer_args => {},
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: $!";
91 my $base_t = SQL::Translator->new;
92 $base_t->$_ (1) for qw/add_drop_table no_comments/;
94 my $base_schema = $base_t->translate (
96 data => do { local $/; <$base_fh>; },
97 ) or die $base_t->error;
100 #assume there is at least one table
102 XML => qr/<tables>\s*<table/,
103 YAML => qr/\A---\n.+tables\:/s,
104 SQL => qr/^\s*CREATE TABLE/m,
107 for my $args (@$plan) {
109 $args->{name} ||= $args->{engine};
111 my @req = ref $args->{req} ? @{$args->{req}} : $args->{req}||();
115 push @missing, $_ if ($@);
118 skip sprintf ('Need %s for %s roundtrip test',
119 join (', ', @missing),
125 local $TODO = $args->{todo} if $args->{todo};
128 sub { check_roundtrip ($args, $base_schema) },
129 "Round trip for $args->{name} did not throw an exception",
136 sub check_roundtrip {
137 my ($args, $base_schema) = @_;
138 my $base_t = $base_schema->translator;
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},
149 $string_re->{$args->{engine}} || $string_re->{SQL},
150 "Received some meaningful output from the first $args->{name} production",
152 diag ( _gen_diag ($base_t->error) );
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 (
161 parser => $args->{engine},
162 parser_args => $args->{parser_args},
165 isa_ok ($mid_schema, 'SQL::Translator::Schema', "First $args->{name} parser pass produced a schema:")
167 diag (_gen_diag ( $parser_t->error, $base_out ) );
171 # schemas should be comparable at least as far as table/field numbers go
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",
178 # and produce sql once again
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/;
185 # my $rt_sql = $producer_t->translate (
186 # data => $mid_schema,
187 # producer => $args->{engine},
188 # producer_args => $args->{producer_args},
191 my $rt_out = $parser_t->translate (
193 producer => $args->{engine},
194 producer_args => $args->{producer_args},
199 $string_re->{$args->{engine}} || $string_re->{SQL},
200 "Received some meaningful output from the second $args->{name} production",
202 diag ( _gen_diag ( $parser_t->error ) );
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)
214 sub _get_table_info {
219 for my $t (@tables) {
223 map { $_->name } ($t->get_fields),
231 # takes an error string and an optional output block
232 # returns the string conctenated with a line-numbered block for
235 my ($err, $out) = @_;
237 return 'Unknown error' unless $err;
240 if ($out and $ENV{SQLTTEST_RT_DEBUG}) {
242 for (split /\n/, $out) {
243 push @lines, sprintf ('%03d: %s',
249 return "$err\n\n" . join ("\n", @lines);