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
32 producer_args => { mysql_version => 5 },
33 parser_args => { mysql_parser_version => 5 },
38 producer_args => { mysql_version => '5.1' },
39 parser_args => { mysql_parser_version => '5.1' },
42 engine => 'PostgreSQL',
52 engine => 'SQLServer',
67 # There is no Access producer
70 # producer_args => {},
76 # This data file has the right mix of table/view/procedure/trigger
77 # definitions, and lists enough quirks to trip up most combos
78 # I am not sure if augmenting it will break other tests - experiment
79 my $base_file = "$Bin/data/xml/schema.xml";
81 my $base_t = SQL::Translator->new;
82 $base_t->$_ (1) for qw/add_drop_table no_comments/;
84 my $base_schema = $base_t->translate (
87 ) or die $base_t->error;
90 for my $args (@$plan) {
92 $args->{name} ||= $args->{engine};
95 sub { check_roundtrip ($args, $base_schema) },
96 "Round trip for $args->{name} did not throw an exception",
101 sub check_roundtrip {
102 my ($args, $base_schema) = @_;
103 my $base_t = $base_schema->translator;
105 # create some output from the submitted schema
106 my $base_out = $base_t->translate (
107 data => $base_schema,
108 producer => $args->{engine},
109 producer_args => $args->{producer_args},
114 $args->{engine} eq 'XML' #assume there is at least one table
115 ? qr/<tables>\s*<table/m
116 : qr/^\s*CREATE TABLE/m
118 "Received some meaningful output from the first $args->{name} production",
120 diag ( _gen_diag ($base_t->error) );
125 my $parser_t = SQL::Translator->new;
126 $parser_t->$_ (1) for qw/add_drop_table no_comments/;
127 my $mid_schema = $parser_t->translate (
129 parser => $args->{engine},
130 parser_args => $args->{parser_args},
133 isa_ok ($mid_schema, 'SQL::Translator::Schema', "First $args->{name} parser pass produced a schema:")
135 diag (_gen_diag ( $parser_t->error, $base_out ) );
139 # schemas should be comparable at least as far as table/field numbers go
141 _get_table_info ($mid_schema->get_tables),
142 _get_table_info ($base_schema->get_tables),
143 "Schema tables generally match afer $args->{name} parser trip",
146 # and produce sql once again
148 # Producing a schema with a Translator different from the one the schema was generated
149 # from does not work. This is arguably a bug, 61translator_agnostic.t works with that
150 # my $producer_t = SQL::Translator->new;
151 # $producer_t->$_ (1) for qw/add_drop_table no_comments/;
153 # my $rt_sql = $producer_t->translate (
154 # data => $mid_schema,
155 # producer => $args->{engine},
156 # producer_args => $args->{producer_args},
159 my $rt_out = $parser_t->translate (
161 producer => $args->{engine},
162 producer_args => $args->{producer_args},
167 $args->{engine} eq 'XML' #assume there is at least one table
168 ? qr/<tables>\s*<table/m
169 : qr/^\s*CREATE TABLE/m
171 "Received some meaningful output from the second $args->{name} production",
173 diag ( _gen_diag ( $parser_t->error ) );
177 # the two sql strings should be identical
178 my $msg = "$args->{name} SQL roundtrip successful - SQL statements match";
179 $ENV{SQLTTEST_RT_DEBUG} #stringify below because IO::Scalar does not behave nice
180 ? eq_or_diff ("$rt_out", "$base_out", $msg)
181 : ok ("$rt_out" eq "$base_out", $msg)
185 sub _get_table_info {
190 for my $t (@tables) {
194 map { $_->name } ($t->get_fields),
202 # takes an error string and an optional output block
203 # returns the string conctenated with a line-numbered block for
206 my ($err, $out) = @_;
208 return 'Unknown error' unless $err;
211 if ($out and $ENV{SQLTTEST_RT_DEBUG}) {
213 for (split /\n/, $out) {
214 push @lines, sprintf ('%03d: %s',
220 return "$err\n\n" . join ("\n", @lines);