5 use Test::More qw/no_plan/;
11 ### Set $ENV{SQLTTEST_RT_DEBUG} = 1 for more output
13 # What tests to run - parser/producer name, and optional args
31 producer_args => { mysql_version => 5 },
32 parser_args => { mysql_parser_version => 5 },
37 producer_args => { mysql_version => '5.1' },
38 parser_args => { mysql_parser_version => '5.1' },
41 engine => 'PostgreSQL',
51 engine => 'SQLServer',
66 # There is no Access producer
69 # producer_args => {},
75 # This data file has the right mix of table/view/procedure/trigger
76 # definitions, and lists enough quirks to trip up most combos
77 # I am not sure if augmenting it will break other tests - experiment
78 my $base_file = "$Bin/data/xml/schema.xml";
80 my $base_t = SQL::Translator->new;
81 $base_t->$_ (1) for qw/add_drop_table no_comments/;
83 my $base_schema = $base_t->translate (
86 ) or die $base_t->error;
89 for my $args (@$plan) {
91 $args->{name} ||= $args->{engine};
94 sub { check_roundtrip ($args, $base_schema) },
95 "Round trip for $args->{name} did not throw an exception",
100 sub check_roundtrip {
101 my ($args, $base_schema) = @_;
102 my $base_t = $base_schema->translator;
104 # create some output from the submitted schema
105 my $base_out = $base_t->translate (
106 data => $base_schema,
107 producer => $args->{engine},
108 producer_args => $args->{producer_args},
113 $args->{engine} eq 'XML' #assume there is at least one table
114 ? qr/<tables>\s*<table/m
115 : qr/^\s*CREATE TABLE/m
117 "Received some meaningful output from the first $args->{name} production",
119 diag ( _gen_diag ($base_t->error) );
124 my $parser_t = SQL::Translator->new;
125 $parser_t->$_ (1) for qw/add_drop_table no_comments/;
126 my $mid_schema = $parser_t->translate (
128 parser => $args->{engine},
129 parser_args => $args->{parser_args},
132 isa_ok ($mid_schema, 'SQL::Translator::Schema', "First $args->{name} parser pass produced a schema:")
134 diag (_gen_diag ( $parser_t->error, $base_out ) );
138 # schemas should be comparable at least as far as table/field numbers go
140 _get_table_info ($mid_schema->get_tables),
141 _get_table_info ($base_schema->get_tables),
142 "Schema tables generally match afer $args->{name} parser trip",
145 # and produce sql once again
147 # Producing a schema with a Translator different from the one the schema was generated
148 # from does not work. This is arguably a bug, 61translator_agnostic.t works with that
149 # my $producer_t = SQL::Translator->new;
150 # $producer_t->$_ (1) for qw/add_drop_table no_comments/;
152 # my $rt_sql = $producer_t->translate (
153 # data => $mid_schema,
154 # producer => $args->{engine},
155 # producer_args => $args->{producer_args},
158 my $rt_out = $parser_t->translate (
160 producer => $args->{engine},
161 producer_args => $args->{producer_args},
166 $args->{engine} eq 'XML' #assume there is at least one table
167 ? qr/<tables>\s*<table/m
168 : qr/^\s*CREATE TABLE/m
170 "Received some meaningful output from the second $args->{name} production",
172 diag ( _gen_diag ( $parser_t->error ) );
176 # the two sql strings should be identical
177 my $msg = "$args->{name} SQL roundtrip successful - SQL statements match";
178 $ENV{SQLTTEST_RT_DEBUG}
180 [ split /\n/, $rt_out ],
181 [ split /\n/, $base_out ],
184 : ok ($rt_out eq $base_out, $msg)
188 sub _get_table_info {
193 for my $t (@tables) {
197 map { $_->name } ($t->get_fields),
205 # takes an error string and an optional output block
206 # returns the string conctenated with a line-numbered block for
209 my ($err, $out) = @_;
211 return 'Unknown error' unless $err;
214 if ($out and $ENV{SQLTTEST_RT_DEBUG}) {
216 for (split /\n/, $out) {
217 push @lines, sprintf ('%03d: %s',
223 return "$err\n\n" . join ("\n", @lines);