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',
47 engine => 'SQLServer',
53 # producer_args => {},
58 # producer_args => {},
63 # producer_args => {},
67 # YAML parsing/producing cycles result in some weird self referencing structure
72 # There is no Access producer
75 # producer_args => {},
81 # This data file has the right mix of table/view/procedure/trigger
82 # definitions, and lists enough quirks to trip up most combos
83 # I am not sure if augmenting it will break other tests - experiment
84 my $base_file = "$Bin/data/xml/schema.xml";
86 my $base_t = SQL::Translator->new;
87 $base_t->$_ (1) for qw/add_drop_table no_comments/;
89 my $base_schema = $base_t->translate (
92 ) or die $base_t->error;
94 #assume there is at least one table
96 XML => qr/<tables>\s*<table/,
97 YAML => qr/\A---\n.+tables\:/s,
98 SQL => qr/^\s*CREATE TABLE/m,
101 for my $args (@$plan) {
103 $args->{name} ||= $args->{engine};
106 sub { check_roundtrip ($args, $base_schema) },
107 "Round trip for $args->{name} did not throw an exception",
112 sub check_roundtrip {
113 my ($args, $base_schema) = @_;
114 my $base_t = $base_schema->translator;
116 # create some output from the submitted schema
117 my $base_out = $base_t->translate (
118 data => $base_schema,
119 producer => $args->{engine},
120 producer_args => $args->{producer_args},
125 $string_re->{$args->{engine}} || $string_re->{SQL},
126 "Received some meaningful output from the first $args->{name} production",
128 diag ( _gen_diag ($base_t->error) );
133 my $parser_t = SQL::Translator->new;
134 $parser_t->$_ (1) for qw/add_drop_table no_comments/;
135 my $mid_schema = $parser_t->translate (
137 parser => $args->{engine},
138 parser_args => $args->{parser_args},
141 isa_ok ($mid_schema, 'SQL::Translator::Schema', "First $args->{name} parser pass produced a schema:")
143 diag (_gen_diag ( $parser_t->error, $base_out ) );
147 # schemas should be comparable at least as far as table/field numbers go
149 _get_table_info ($mid_schema->get_tables),
150 _get_table_info ($base_schema->get_tables),
151 "Schema tables generally match afer $args->{name} parser trip",
154 # and produce sql once again
156 # Producing a schema with a Translator different from the one the schema was generated
157 # from does not work. This is arguably a bug, 61translator_agnostic.t works with that
158 # my $producer_t = SQL::Translator->new;
159 # $producer_t->$_ (1) for qw/add_drop_table no_comments/;
161 # my $rt_sql = $producer_t->translate (
162 # data => $mid_schema,
163 # producer => $args->{engine},
164 # producer_args => $args->{producer_args},
167 my $rt_out = $parser_t->translate (
169 producer => $args->{engine},
170 producer_args => $args->{producer_args},
175 $string_re->{$args->{engine}} || $string_re->{SQL},
176 "Received some meaningful output from the second $args->{name} production",
178 diag ( _gen_diag ( $parser_t->error ) );
182 # the two sql strings should be identical
183 my $msg = "$args->{name} SQL roundtrip successful - SQL statements match";
184 $ENV{SQLTTEST_RT_DEBUG} #stringify below because IO::Scalar does not behave nice
185 ? eq_or_diff ("$rt_out", "$base_out", $msg)
186 : ok ("$rt_out" eq "$base_out", $msg)
190 sub _get_table_info {
195 for my $t (@tables) {
199 map { $_->name } ($t->get_fields),
207 # takes an error string and an optional output block
208 # returns the string conctenated with a line-numbered block for
211 my ($err, $out) = @_;
213 return 'Unknown error' unless $err;
216 if ($out and $ENV{SQLTTEST_RT_DEBUG}) {
218 for (split /\n/, $out) {
219 push @lines, sprintf ('%03d: %s',
225 return "$err\n\n" . join ("\n", @lines);