Merge 'trunk' into 'roundtrip'
[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     eval {require Template;};
20
21     if ( $@ ) {
22         plan skip_all => 'Template not installed?'
23     }
24
25     maybe_plan(6, 
26         'XML::XPath', 
27         'SQL::Translator::Parser::XML::SQLFairy',
28         'Template', 
29         'Test::Differences'
30     );
31 }
32 use Test::Differences;
33
34 use SQL::Translator;
35 use SQL::Translator::Producer::TTSchema;
36
37 # Main test. Template whole schema and test tt_vars
38 {
39     my $obj;
40     $obj = SQL::Translator->new(
41         show_warnings  => 0,
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             },
51         },
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  => 0,
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{
88     Table: Basic
89     Table: Another}
90     ,"Output looks right";
91 }
92
93
94 __DATA__
95 Schema: 
96 Database: 
97
98 Foo: bar
99 Hello: world
100
101 Table: Basic
102 ==========================================================================
103
104 Fields
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
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
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
145         table:                 Basic
146     
147     email
148         data_type:             varchar
149         size:                  500
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
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
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
187         table:                 Basic
188     
189     emptytagdef
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
201         table:                 Basic
202     
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     
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
228         order:                 9
229         table:                 Basic
230     
231
232 Indices
233     titleindex
234         table:    Basic
235         fields:   title
236         type:     NORMAL
237         options:  
238         is_valid: 1
239     
240     
241 Constraints
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     
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     
281 Table: Another
282 ==========================================================================
283
284 Fields
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
300 Indices
301     
302 Constraints
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