Applied patch sent in by Daniel Westermann-Clark on Oct 11 2006.
[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         'XML::XPath', 
21         'SQL::Translator::Parser::XML::SQLFairy',
22         'Template', 
23         'Test::Differences'
24     )
25 }
26 use Test::Differences;
27
28 use SQL::Translator;
29 use SQL::Translator::Producer::TTSchema;
30
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",
41 #            ttfile  => "$Bin/data/template/test.tt",
42             tt_vars => {
43                 foo   => 'bar',
44                 hello => 'world',
45             },
46         },
47     );
48     my $out;
49     lives_ok { $out = $obj->translate; }  "Translate ran";
50 #    print STDERR "Output: $out\n";
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{
84     Table: Basic
85     Table: Another}
86     ,"Output looks right";
87 }
88
89
90 __DATA__
91 Schema: 
92 Database: 
93
94 Foo: bar
95 Hello: world
96
97 Table: Basic
98 ==========================================================================
99
100 Fields
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
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
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
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
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
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
183         table:                 Basic
184     
185     emptytagdef
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
197         table:                 Basic
198     
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     
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
224         order:                 9
225         table:                 Basic
226     
227
228 Indices
229     titleindex
230         table:    Basic
231         fields:   title
232         type:     NORMAL
233         options:  
234         is_valid: 1
235     
236     
237 Constraints
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     
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     
277 Table: Another
278 ==========================================================================
279
280 Fields
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
296 Indices
297     
298 Constraints
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