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