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
28 producer_args => { mysql_version => 5 },
29 parser_args => { mysql_parser_version => 5 },
34 producer_args => { mysql_version => '5.1' },
35 parser_args => { mysql_parser_version => '5.1' },
38 engine => 'PostgreSQL',
48 engine => 'SQLServer',
63 # There is no Access producer
66 # producer_args => {},
72 # This data file has the right mix of table/view/procedure/trigger
73 # definitions, and lists enough quirks to trip up most combos
74 # I am not sure if augmenting it will break other tests - experiment
75 my $base_file = "$Bin/data/xml/schema.xml";
77 my $base_t = SQL::Translator->new;
78 $base_t->$_ (1) for qw/add_drop_table no_comments/;
80 my $base_schema = $base_t->translate (
83 ) or die $base_t->error;
86 for my $args (@$plan) {
88 $args->{name} ||= $args->{engine};
91 sub { check_roundtrip ($args, $base_schema) },
92 "Round trip for $args->{name} did not throw an exception",
98 my ($args, $base_schema) = @_;
99 my $base_t = $base_schema->translator;
101 # create some sql from the submitted schema
102 my $base_sql = $base_t->translate (
103 data => $base_schema,
104 producer => $args->{engine},
105 producer_args => $args->{producer_args},
110 qr/^\s*CREATE TABLE/m, #assume there is at least one create table statement
111 "Received some meaningful output from the first $args->{name} production",
113 diag ( _gen_diag ($base_t->error) );
118 my $parser_t = SQL::Translator->new;
119 $parser_t->$_ (1) for qw/add_drop_table no_comments/;
120 my $mid_schema = $parser_t->translate (
122 parser => $args->{engine},
123 parser_args => $args->{parser_args},
126 isa_ok ($mid_schema, 'SQL::Translator::Schema', "First $args->{name} parser pass produced a schema:")
128 diag (_gen_diag ( $parser_t->error, $base_sql ) );
132 # schemas should be comparable at least as far as table/field numbers go
134 _get_table_info ($mid_schema->get_tables),
135 _get_table_info ($base_schema->get_tables),
136 "Schema tables generally match afer $args->{name} parser trip",
139 # and produce sql once again
141 # Producing a schema with a Translator different from the one the schema was generated
142 # from does not work. This is arguably a bug, 61translator_agnostic.t works with that
143 # my $producer_t = SQL::Translator->new;
144 # $producer_t->$_ (1) for qw/add_drop_table no_comments/;
146 # my $rt_sql = $producer_t->translate (
147 # data => $mid_schema,
148 # producer => $args->{engine},
149 # producer_args => $args->{producer_args},
152 my $rt_sql = $parser_t->translate (
154 producer => $args->{engine},
155 producer_args => $args->{producer_args},
160 qr/^\s*CREATE TABLE/m, #assume there is at least one create table statement
161 "Received some meaningful output from the second $args->{name} production",
163 diag ( _gen_diag ( $parser_t->error ) );
167 # the two sql strings should be identical
168 my $msg = "$args->{name} SQL roundtrip successful - SQL statements match";
169 $ENV{SQLTTEST_RT_DEBUG}
171 [ split /\n/, $rt_sql ],
172 [ split /\n/, $base_sql ],
175 : ok ($rt_sql eq $base_sql, $msg)
179 sub _get_table_info {
184 for my $t (@tables) {
188 map { $_->name } ($t->get_fields),
196 # takes an error string and an optional SQL block
197 # returns the string conctenated with a line-numbered block for
200 my ($err, $sql) = @_;
202 return 'Unknown error' unless $err;
205 if ($sql and $ENV{SQLTTEST_RT_DEBUG}) {
207 for (split /\n/, $sql) {
208 push @sql_lines, sprintf ('%03d: %s',
209 scalar @sql_lines + 1,
214 return "$err\n\n" . join ("\n", @sql_lines);