Applied patch sent in by Daniel Westermann-Clark on Oct 11 2006.
[dbsrgits/SQL-Translator.git] / t / 18ttschema-producer.t
CommitLineData
2b2601b5 1#!/usr/bin/perl -w
2# vim:filetype=perl
3
4# Before `make install' is performed this script should be runnable with
5# `make test'. After `make install' it should work as `perl test.pl'
6
2e11379e 7use strict;
2b2601b5 8use Test::More;
9use Test::Exception;
2d691ec1 10use Test::SQL::Translator qw(maybe_plan);
2b2601b5 11
12use Data::Dumper;
2b2601b5 13use FindBin qw/$Bin/;
14
15# Testing 1,2,3,4...
16#=============================================================================
17
2d691ec1 18BEGIN {
9d6bd3c7 19 maybe_plan(6,
d2f0a9f6 20 'XML::XPath',
d55fe5ca 21 'SQL::Translator::Parser::XML::SQLFairy',
d2f0a9f6 22 'Template',
23 'Test::Differences'
24 )
2b2601b5 25}
26use Test::Differences;
a03aec42 27
2b2601b5 28use SQL::Translator;
29use SQL::Translator::Producer::TTSchema;
30
9d6bd3c7 31# Main test. Template whole schema and test tt_vars
32{
33 my $obj;
34 $obj = SQL::Translator->new(
35 show_warnings => 1,
36 from => "XML-SQLFairy",
37 filename => "$Bin/data/xml/schema.xml",
38 to => "TTSchema",
39 producer_args => {
40 ttfile => "$Bin/data/template/basic.tt",
bced3fa7 41# ttfile => "$Bin/data/template/test.tt",
9d6bd3c7 42 tt_vars => {
43 foo => 'bar',
44 hello => 'world',
45 },
046d668a 46 },
9d6bd3c7 47 );
48 my $out;
49 lives_ok { $out = $obj->translate; } "Translate ran";
bced3fa7 50# print STDERR "Output: $out\n";
9d6bd3c7 51 ok $out ne "" ,"Produced something!";
52 local $/ = undef; # slurp
53 eq_or_diff $out, <DATA> ,"Output looks right";
54}
55
56# Test passing of Template config
57{
58 my $tmpl = q{
59 [%- FOREACH table = schema.get_tables %]
60 Table: $table
61 [%- END %]};
62 my $obj;
63 $obj = SQL::Translator->new(
64 show_warnings => 1,
65 from => "XML-SQLFairy",
66 filename => "$Bin/data/xml/schema.xml",
67 to => "TTSchema",
68 producer_args => {
69 ttfile => \$tmpl,
70 tt_conf => {
71 INTERPOLATE => 1,
72 },
73 tt_vars => {
74 foo => 'bar',
75 hello => 'world',
76 },
77 },
78 );
79 my $out;
80 lives_ok { $out = $obj->translate; } "Translate ran";
81 ok $out ne "" ,"Produced something!";
82 local $/ = undef; # slurp
83 eq_or_diff $out, q{
b08b5416 84 Table: Basic
85 Table: Another}
9d6bd3c7 86 ,"Output looks right";
87}
2b2601b5 88
2b2601b5 89
90__DATA__
91Schema:
92Database:
93
046d668a 94Foo: bar
95Hello: world
96
2b2601b5 97Table: Basic
98==========================================================================
99
100Fields
101 id
102 data_type: int
103 size: 10
104 is_nullable: 0
105 default_value:
106 is_primary_key: 1
107 is_unique: 0
108 is_auto_increment: 1
109 is_foreign_key: 0
110 foreign_key_reference:
111 is_valid: 1
112 order: 1
2b2601b5 113 table: Basic
114
115 title
116 data_type: varchar
117 size: 100
118 is_nullable: 0
119 default_value: hello
120 is_primary_key: 0
121 is_unique: 0
122 is_auto_increment: 0
123 is_foreign_key: 0
124 foreign_key_reference:
125 is_valid: 1
126 order: 2
2b2601b5 127 table: Basic
128
129 description
130 data_type: text
131 size: 0
132 is_nullable: 1
133 default_value:
134 is_primary_key: 0
135 is_unique: 0
136 is_auto_increment: 0
137 is_foreign_key: 0
138 foreign_key_reference:
139 is_valid: 1
140 order: 3
2b2601b5 141 table: Basic
142
143 email
144 data_type: varchar
145 size: 255
146 is_nullable: 1
147 default_value:
148 is_primary_key: 0
149 is_unique: 1
150 is_auto_increment: 0
151 is_foreign_key: 0
152 foreign_key_reference:
153 is_valid: 1
154 order: 4
2b2601b5 155 table: Basic
156
157 explicitnulldef
158 data_type: varchar
159 size: 0
160 is_nullable: 1
161 default_value:
162 is_primary_key: 0
163 is_unique: 0
164 is_auto_increment: 0
165 is_foreign_key: 0
166 foreign_key_reference:
167 is_valid: 1
168 order: 5
2b2601b5 169 table: Basic
170
171 explicitemptystring
172 data_type: varchar
173 size: 0
174 is_nullable: 1
175 default_value:
176 is_primary_key: 0
177 is_unique: 0
178 is_auto_increment: 0
179 is_foreign_key: 0
180 foreign_key_reference:
181 is_valid: 1
182 order: 6
2b2601b5 183 table: Basic
184
5952d39c 185 emptytagdef
b3530353 186 data_type: varchar
187 size: 0
188 is_nullable: 1
189 default_value:
190 is_primary_key: 0
191 is_unique: 0
192 is_auto_increment: 0
193 is_foreign_key: 0
194 foreign_key_reference:
195 is_valid: 1
196 order: 7
b3530353 197 table: Basic
198
b08b5416 199 another_id
200 data_type: int
201 size: 10
202 is_nullable: 1
203 default_value: 2
204 is_primary_key: 0
205 is_unique: 0
206 is_auto_increment: 0
207 is_foreign_key: 1
208 foreign_key_reference: Another
209 is_valid: 1
210 order: 8
211 table: Basic
212
08d91aad 213 timest
214 data_type: timestamp
215 size: 0
216 is_nullable: 1
217 default_value:
218 is_primary_key: 0
219 is_unique: 0
220 is_auto_increment: 0
221 is_foreign_key: 0
222 foreign_key_reference:
223 is_valid: 1
b08b5416 224 order: 9
08d91aad 225 table: Basic
226
2b2601b5 227
228Indices
229 titleindex
230 table: Basic
231 fields: title
232 type: NORMAL
233 options:
234 is_valid: 1
235
236
237Constraints
238 ?
239 type: PRIMARY KEY
240 fields: id
241 expression:
242 match_type:
243 reference_fields:
244 reference_table:
245 deferrable: 1
246 on_delete:
247 on_update:
248 options:
249 is_valid: 1
250
251 emailuniqueindex
252 type: UNIQUE
253 fields: email
254 expression:
255 match_type:
256 reference_fields:
257 reference_table:
258 deferrable: 1
259 on_delete:
260 on_update:
261 options:
262 is_valid: 1
263
b08b5416 264 ?
265 type: FOREIGN KEY
266 fields: another_id
267 expression:
268 match_type:
269 reference_fields: id
270 reference_table: Another
271 deferrable: 1
272 on_delete:
273 on_update:
274 options:
275 is_valid: 1
276
277Table: Another
278==========================================================================
279
280Fields
281 id
282 data_type: int
283 size: 10
284 is_nullable: 0
285 default_value:
286 is_primary_key: 1
287 is_unique: 0
288 is_auto_increment: 1
289 is_foreign_key: 0
290 foreign_key_reference:
291 is_valid: 1
292 order: 10
293 table: Another
294
295
296Indices
297
298Constraints
299 ?
300 type: PRIMARY KEY
301 fields: id
302 expression:
303 match_type:
304 reference_fields:
305 reference_table:
306 deferrable: 1
307 on_delete:
308 on_update:
309 options:
310 is_valid: 1
311