5 use Test::More qw/no_plan/;
7 use Test::SQL::Translator qw(maybe_plan);
12 ### Set $ENV{SQLTTEST_RT_DEBUG} = 1 for more output
14 # What tests to run - parser/producer name, and optional args
29 producer_args => { mysql_version => 5 },
30 parser_args => { mysql_parser_version => 5 },
35 producer_args => { mysql_version => '5.1' },
36 parser_args => { mysql_parser_version => '5.1' },
39 engine => 'PostgreSQL',
49 engine => 'SQLServer',
64 # There is no Access producer
67 # producer_args => {},
73 # This data file has the right mix of table/view/procedure/trigger
74 # definitions, and lists enough quirks to trip up most combos
75 # I am not sure if augmenting it will break other tests - experiment
76 my $base_file = "$Bin/data/xml/schema.xml";
78 my $base_t = SQL::Translator->new;
79 $base_t->$_ (1) for qw/add_drop_table no_comments/;
81 my $base_schema = $base_t->translate (
84 ) or die $base_t->error;
87 for my $args (@$plan) {
89 $args->{name} ||= $args->{engine};
92 sub { check_roundtrip ($args, $base_schema) },
93 "Round trip for $args->{name} did not throw an exception",
99 my ($args, $base_schema) = @_;
100 my $base_t = $base_schema->translator;
102 # create some sql from the submitted schema
103 my $base_sql = $base_t->translate (
104 data => $base_schema,
105 producer => $args->{engine},
106 producer_args => $args->{producer_args},
111 qr/^\s*CREATE TABLE/m, #assume there is at least one create table statement
112 "Received some meaningful output from the first $args->{name} production",
113 ) or diag ( _gen_diag ($base_t->error) );
116 my $parser_t = SQL::Translator->new;
117 $parser_t->$_ (1) for qw/add_drop_table no_comments/;
118 my $mid_schema = $parser_t->translate (
120 parser => $args->{engine},
121 parser_args => $args->{parser_args},
124 isa_ok ($mid_schema, 'SQL::Translator::Schema', "First $args->{name} parser pass produced a schema:")
125 or diag (_gen_diag ( $parser_t->error, $base_sql ) );
127 # schemas should be comparable at least as far as table/field numbers go
129 _get_table_info ($mid_schema->get_tables),
130 _get_table_info ($base_schema->get_tables),
131 "Schema tables generally match afer $args->{name} parser trip",
134 # and produce sql once again
136 # Producing a schema with a Translator different from the one the schema was generated
137 # from does not work. This is arguably a bug, 61translator_agnostic.t works with that
138 # my $producer_t = SQL::Translator->new;
139 # $producer_t->$_ (1) for qw/add_drop_table no_comments/;
141 # my $rt_sql = $producer_t->translate (
142 # data => $mid_schema,
143 # producer => $args->{engine},
144 # producer_args => $args->{producer_args},
147 my $rt_sql = $parser_t->translate (
149 producer => $args->{engine},
150 producer_args => $args->{producer_args},
155 qr/^\s*CREATE TABLE/m, #assume there is at least one create table statement
156 "Received some meaningful output from the second $args->{name} production",
157 ) or diag ( _gen_diag ( $parser_t->error ) );
159 # the two sql strings should be identical
160 my $msg = "$args->{name} SQL roundtrip successful - SQL statements match";
161 $ENV{SQLTTEST_RT_DEBUG}
163 [ split /\n/, $rt_sql ],
164 [ split /\n/, $base_sql ],
167 : ok ($rt_sql eq $base_sql, $msg)
171 sub _get_table_info {
176 for my $t (@tables) {
180 map { $_->name } ($t->get_fields),
188 # takes an error string and an optional SQL block
189 # returns the string conctenated with a line-numbered block for
192 my ($err, $sql) = @_;
194 return 'Unknown error' unless $err;
197 if ($sql and $ENV{SQLTTEST_RT_DEBUG}) {
199 for (split /\n/, $sql) {
200 push @sql_lines, sprintf ('%03d: %s',
201 scalar @sql_lines + 1,
206 return "$err\n\n" . join ("\n", @sql_lines);