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