Enforce XML::LibXML version requirements
[dbsrgits/SQL-Translator.git] / t / 60roundtrip.t
CommitLineData
6c9e9546 1#!/usr/bin/perl
2
3use warnings;
4use strict;
5use Test::More qw/no_plan/;
6use Test::Exception;
ce6c267a 7use Test::Differences;
6c9e9546 8use FindBin qw/$Bin/;
9
10use SQL::Translator;
11
05603c3a 12
6c9e9546 13### Set $ENV{SQLTTEST_RT_DEBUG} = 1 for more output
14
15# What tests to run - parser/producer name, and optional args
16my $plan = [
17 {
3a328a0a 18 engine => 'XML',
89e19730 19 req => 'XML::LibXML 1.69',
05603c3a 20 },
21 {
22 engine => 'YAML',
3a328a0a 23 },
05603c3a 24
3a328a0a 25 {
6c9e9546 26 engine => 'SQLite',
27 producer_args => {},
28 parser_args => {},
29 },
30 {
31 engine => 'MySQL',
32 producer_args => {},
33 parser_args => {},
34 },
35 {
36 engine => 'MySQL',
37 name => 'MySQL 5.0',
38 producer_args => { mysql_version => 5 },
39 parser_args => { mysql_parser_version => 5 },
40 },
41 {
42 engine => 'MySQL',
43 name => 'MySQL 5.1',
44 producer_args => { mysql_version => '5.1' },
45 parser_args => { mysql_parser_version => '5.1' },
46 },
47 {
48 engine => 'PostgreSQL',
49 producer_args => {},
50 parser_args => {},
51 },
e2fb9ad3 52 {
53 engine => 'SQLServer',
54 producer_args => {},
55 parser_args => {},
56 },
05603c3a 57
c1e2579f 58# {
59# engine => 'Oracle',
60# producer_args => {},
61# parser_args => {},
62# todo => 'Needs volunteers',
63# },
64# {
65# engine => 'Sybase',
66# producer_args => {},
67# parser_args => {},
68# todo => 'Needs volunteers',
69# },
70# {
71# engine => 'DB2',
72# producer_args => {},
73# parser_args => {},
74# todo => 'Needs volunteers',
75# },
0e0ca612 76
77# There is no Access producer
78# {
79# engine => 'Access',
80# producer_args => {},
81# parser_args => {},
82# },
6c9e9546 83];
84
85
86# This data file has the right mix of table/view/procedure/trigger
87# definitions, and lists enough quirks to trip up most combos
05603c3a 88my $base_file = "$Bin/data/roundtrip_autogen.yaml";
89open (my $base_fh, '<', $base_file) or die "$base_file: $!";
6c9e9546 90
91my $base_t = SQL::Translator->new;
0e0ca612 92$base_t->$_ (1) for qw/add_drop_table no_comments/;
6c9e9546 93
94my $base_schema = $base_t->translate (
05603c3a 95 parser => 'YAML',
96 data => do { local $/; <$base_fh>; },
6c9e9546 97) or die $base_t->error;
98
05603c3a 99
474910ee 100#assume there is at least one table
101my $string_re = {
102 XML => qr/<tables>\s*<table/,
103 YAML => qr/\A---\n.+tables\:/s,
104 SQL => qr/^\s*CREATE TABLE/m,
105};
6c9e9546 106
107for my $args (@$plan) {
05603c3a 108 SKIP: {
0a2d7cf1 109 $args->{name} ||= $args->{engine};
6c9e9546 110
05603c3a 111 my @req = ref $args->{req} ? @{$args->{req}} : $args->{req}||();
112 my @missing;
113 for (@req) {
89e19730 114 eval "use $_ ()";
05603c3a 115 push @missing, $_ if ($@);
116 }
117 if (@missing) {
118 skip sprintf ('Need %s for %s roundtrip test',
119 join (', ', @missing),
120 $args->{name},
121 );
122 }
123
124 TODO: {
125 local $TODO = $args->{todo} if $args->{todo};
126
127 lives_ok (
128 sub { check_roundtrip ($args, $base_schema) },
129 "Round trip for $args->{name} did not throw an exception",
130 );
131 }
0a2d7cf1 132 }
6c9e9546 133}
134
135
136sub check_roundtrip {
137 my ($args, $base_schema) = @_;
138 my $base_t = $base_schema->translator;
139
3a328a0a 140# create some output from the submitted schema
141 my $base_out = $base_t->translate (
6c9e9546 142 data => $base_schema,
143 producer => $args->{engine},
144 producer_args => $args->{producer_args},
145 );
146
147 like (
3a328a0a 148 $base_out,
474910ee 149 $string_re->{$args->{engine}} || $string_re->{SQL},
6c9e9546 150 "Received some meaningful output from the first $args->{name} production",
4c549812 151 ) or do {
152 diag ( _gen_diag ($base_t->error) );
153 return;
154 };
6c9e9546 155
156# parse the sql back
157 my $parser_t = SQL::Translator->new;
0e0ca612 158 $parser_t->$_ (1) for qw/add_drop_table no_comments/;
6c9e9546 159 my $mid_schema = $parser_t->translate (
3a328a0a 160 data => $base_out,
6c9e9546 161 parser => $args->{engine},
162 parser_args => $args->{parser_args},
163 );
164
165 isa_ok ($mid_schema, 'SQL::Translator::Schema', "First $args->{name} parser pass produced a schema:")
4c549812 166 or do {
3a328a0a 167 diag (_gen_diag ( $parser_t->error, $base_out ) );
4c549812 168 return;
169 };
6c9e9546 170
171# schemas should be comparable at least as far as table/field numbers go
172 is_deeply (
173 _get_table_info ($mid_schema->get_tables),
174 _get_table_info ($base_schema->get_tables),
175 "Schema tables generally match afer $args->{name} parser trip",
4c549812 176 ) or return;
6c9e9546 177
178# and produce sql once again
179
180# Producing a schema with a Translator different from the one the schema was generated
181# from does not work. This is arguably a bug, 61translator_agnostic.t works with that
182# my $producer_t = SQL::Translator->new;
0e0ca612 183# $producer_t->$_ (1) for qw/add_drop_table no_comments/;
6c9e9546 184
185# my $rt_sql = $producer_t->translate (
186# data => $mid_schema,
187# producer => $args->{engine},
188# producer_args => $args->{producer_args},
189# );
190
3a328a0a 191 my $rt_out = $parser_t->translate (
6c9e9546 192 data => $mid_schema,
193 producer => $args->{engine},
194 producer_args => $args->{producer_args},
195 );
196
197 like (
3a328a0a 198 $rt_out,
474910ee 199 $string_re->{$args->{engine}} || $string_re->{SQL},
6c9e9546 200 "Received some meaningful output from the second $args->{name} production",
4c549812 201 ) or do {
202 diag ( _gen_diag ( $parser_t->error ) );
203 return;
204 };
6c9e9546 205
206# the two sql strings should be identical
207 my $msg = "$args->{name} SQL roundtrip successful - SQL statements match";
ce6c267a 208 $ENV{SQLTTEST_RT_DEBUG} #stringify below because IO::Scalar does not behave nice
209 ? eq_or_diff ("$rt_out", "$base_out", $msg)
210 : ok ("$rt_out" eq "$base_out", $msg)
6c9e9546 211 ;
212}
213
214sub _get_table_info {
215 my @tables = @_;
216
217 my @info;
218
219 for my $t (@tables) {
220 push @info, {
221 name => $t->name,
222 fields => [
223 map { $_->name } ($t->get_fields),
224 ],
225 };
226 }
227
228 return \@info;
229}
230
3a328a0a 231# takes an error string and an optional output block
6c9e9546 232# returns the string conctenated with a line-numbered block for
233# easier reading
234sub _gen_diag {
3a328a0a 235 my ($err, $out) = @_;
6c9e9546 236
237 return 'Unknown error' unless $err;
238
239
3a328a0a 240 if ($out and $ENV{SQLTTEST_RT_DEBUG}) {
241 my @lines;
242 for (split /\n/, $out) {
243 push @lines, sprintf ('%03d: %s',
244 scalar @lines + 1,
6c9e9546 245 $_,
246 );
247 }
248
3a328a0a 249 return "$err\n\n" . join ("\n", @lines);
6c9e9546 250 }
251
252 return $err;
253}