Commit | Line | Data |
6c9e9546 |
1 | #!/usr/bin/perl |
2 | |
3 | use warnings; |
4 | use strict; |
5 | use Test::More qw/no_plan/; |
6 | use Test::Exception; |
7 | use Test::SQL::Translator qw(maybe_plan); |
8 | use FindBin qw/$Bin/; |
9 | |
10 | use SQL::Translator; |
11 | |
12 | ### Set $ENV{SQLTTEST_RT_DEBUG} = 1 for more output |
13 | |
14 | # What tests to run - parser/producer name, and optional args |
15 | my $plan = [ |
16 | { |
17 | engine => 'SQLite', |
18 | producer_args => {}, |
19 | parser_args => {}, |
20 | }, |
21 | { |
22 | engine => 'MySQL', |
23 | producer_args => {}, |
24 | parser_args => {}, |
25 | }, |
26 | { |
27 | engine => 'MySQL', |
28 | name => 'MySQL 5.0', |
29 | producer_args => { mysql_version => 5 }, |
30 | parser_args => { mysql_parser_version => 5 }, |
31 | }, |
32 | { |
33 | engine => 'MySQL', |
34 | name => 'MySQL 5.1', |
35 | producer_args => { mysql_version => '5.1' }, |
36 | parser_args => { mysql_parser_version => '5.1' }, |
37 | }, |
38 | { |
39 | engine => 'PostgreSQL', |
40 | producer_args => {}, |
41 | parser_args => {}, |
42 | }, |
43 | ]; |
44 | |
45 | |
46 | # This data file has the right mix of table/view/procedure/trigger |
47 | # definitions, and lists enough quirks to trip up most combos |
48 | # I am not sure if augmenting it will break other tests - experiment |
49 | my $base_file = "$Bin/data/xml/schema.xml"; |
50 | |
51 | my $base_t = SQL::Translator->new; |
52 | $base_t->$_ (1) for qw/validate add_drop_table no_comments/; |
53 | |
54 | my $base_schema = $base_t->translate ( |
55 | parser => 'XML', |
56 | file => $base_file, |
57 | ) or die $base_t->error; |
58 | |
59 | |
60 | for my $args (@$plan) { |
61 | |
62 | $args->{name} ||= $args->{engine}; |
63 | |
64 | lives_ok ( |
65 | sub { check_roundtrip ($args, $base_schema) }, |
66 | "Round trip for $args->{name} did not throw an exception", |
67 | ); |
68 | } |
69 | |
70 | |
71 | sub check_roundtrip { |
72 | my ($args, $base_schema) = @_; |
73 | my $base_t = $base_schema->translator; |
74 | |
75 | # create some sql from the submitted schema |
76 | my $base_sql = $base_t->translate ( |
77 | data => $base_schema, |
78 | producer => $args->{engine}, |
79 | producer_args => $args->{producer_args}, |
80 | ); |
81 | |
82 | like ( |
83 | $base_sql, |
84 | qr/^\s*CREATE TABLE/m, #assume there is at least one create table statement |
85 | "Received some meaningful output from the first $args->{name} production", |
86 | ) or diag ( _gen_diag ($base_t->error) ); |
87 | |
88 | # parse the sql back |
89 | my $parser_t = SQL::Translator->new; |
90 | $parser_t->$_ (1) for qw/validate add_drop_table no_comments/; |
91 | my $mid_schema = $parser_t->translate ( |
92 | data => $base_sql, |
93 | parser => $args->{engine}, |
94 | parser_args => $args->{parser_args}, |
95 | ); |
96 | |
97 | isa_ok ($mid_schema, 'SQL::Translator::Schema', "First $args->{name} parser pass produced a schema:") |
98 | or diag (_gen_diag ( $parser_t->error, $base_sql ) ); |
99 | |
100 | # schemas should be comparable at least as far as table/field numbers go |
101 | is_deeply ( |
102 | _get_table_info ($mid_schema->get_tables), |
103 | _get_table_info ($base_schema->get_tables), |
104 | "Schema tables generally match afer $args->{name} parser trip", |
105 | ); |
106 | |
107 | # and produce sql once again |
108 | |
109 | # Producing a schema with a Translator different from the one the schema was generated |
110 | # from does not work. This is arguably a bug, 61translator_agnostic.t works with that |
111 | # my $producer_t = SQL::Translator->new; |
112 | # $producer_t->$_ (1) for qw/validate add_drop_table no_comments/; |
113 | |
114 | # my $rt_sql = $producer_t->translate ( |
115 | # data => $mid_schema, |
116 | # producer => $args->{engine}, |
117 | # producer_args => $args->{producer_args}, |
118 | # ); |
119 | |
120 | my $rt_sql = $parser_t->translate ( |
121 | data => $mid_schema, |
122 | producer => $args->{engine}, |
123 | producer_args => $args->{producer_args}, |
124 | ); |
125 | |
126 | like ( |
127 | $rt_sql, |
128 | qr/^\s*CREATE TABLE/m, #assume there is at least one create table statement |
129 | "Received some meaningful output from the second $args->{name} production", |
130 | ) or diag ( _gen_diag ( $parser_t->error ) ); |
131 | |
132 | # the two sql strings should be identical |
133 | my $msg = "$args->{name} SQL roundtrip successful - SQL statements match"; |
134 | $ENV{SQLTTEST_RT_DEBUG} |
135 | ? is_deeply ( |
136 | [ split /\n/, $rt_sql ], |
137 | [ split /\n/, $base_sql ], |
138 | $msg, |
139 | ) |
140 | : ok ($rt_sql eq $base_sql, $msg) |
141 | ; |
142 | } |
143 | |
144 | sub _get_table_info { |
145 | my @tables = @_; |
146 | |
147 | my @info; |
148 | |
149 | for my $t (@tables) { |
150 | push @info, { |
151 | name => $t->name, |
152 | fields => [ |
153 | map { $_->name } ($t->get_fields), |
154 | ], |
155 | }; |
156 | } |
157 | |
158 | return \@info; |
159 | } |
160 | |
161 | # takes an error string and an optional SQL block |
162 | # returns the string conctenated with a line-numbered block for |
163 | # easier reading |
164 | sub _gen_diag { |
165 | my ($err, $sql) = @_; |
166 | |
167 | return 'Unknown error' unless $err; |
168 | |
169 | |
170 | if ($sql and $ENV{SQLTTEST_RT_DEBUG}) { |
171 | my @sql_lines; |
172 | for (split /\n/, $sql) { |
173 | push @sql_lines, sprintf ('%03d: %s', |
174 | scalar @sql_lines + 1, |
175 | $_, |
176 | ); |
177 | } |
178 | |
179 | return "$err\n\n" . join ("\n", @sql_lines); |
180 | } |
181 | |
182 | return $err; |
183 | } |