5 use Test::More qw/no_plan/;
12 ### Set $ENV{SQLTTEST_RT_DEBUG} = 1 for more output
14 # What tests to run - parser/producer name, and optional args
18 # req => 'XML::LibXML',
25 # producer_args => {},
30 # producer_args => {},
35 # name => 'MySQL 5.0',
36 # producer_args => { mysql_version => 5 },
37 # parser_args => { mysql_parser_version => 5 },
41 # name => 'MySQL 5.1',
42 # producer_args => { mysql_version => '5.1' },
43 # parser_args => { mysql_parser_version => '5.1' },
46 engine => 'PostgreSQL',
51 # engine => 'SQLServer',
52 # producer_args => {},
58 # producer_args => {},
60 # todo => 'Needs volunteers',
64 # producer_args => {},
66 # todo => 'Needs volunteers',
70 # producer_args => {},
72 # todo => 'Needs volunteers',
75 # There is no Access producer
78 # producer_args => {},
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 my $base_file = "$Bin/data/roundtrip_autogen.yaml";
87 open (my $base_fh, '<', $base_file) or die "$base_file: $!";
89 my $base_t = SQL::Translator->new;
90 $base_t->$_ (1) for qw/add_drop_table no_comments/;
92 my $base_schema = $base_t->translate(
94 data => do { local $/; <$base_fh>; },
95 ) or die $base_t->error;
97 #assume there is at least one table
99 XML => qr/<tables>\s*<table/,
100 YAML => qr/\A---\n.+tables\:/s,
101 SQL => qr/^\s*CREATE TABLE/m,
104 for my $args (@$plan) {
106 $args->{name} ||= $args->{engine};
108 my @req = ref $args->{req} ? @{$args->{req}} : $args->{req}||();
112 push @missing, $_ if ($@);
115 skip sprintf ('Need %s for %s roundtrip test',
116 join (', ', @missing),
122 local $TODO = $args->{todo} if $args->{todo};
125 # sub { check_roundtrip ($args, $base_schema) },
126 sub { check_roundtrip ($args, $base_t, $base_schema) },
127 # sub { check_roundtrip ($args, $base_schema) },
128 "Round trip for $args->{name} did not throw an exception",
135 sub check_roundtrip {
136 # my ($args, $base_schema) = @_;
137 # my $base_t = $base_schema->translator;
138 my ($args, $base_t, $base_schema) = @_;
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; #({ parser => $args->{engine} });
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::Object::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);