9b9d8ac378e9cff914f8419a3024659d30c991bb
[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     ,"Output looks right";
86 }
87
88
89 __DATA__
90 Schema: 
91 Database: 
92
93 Foo: bar
94 Hello: world
95
96 Table: Basic
97 ==========================================================================
98
99 Fields
100     id
101         data_type:             int
102         size:                  10
103         is_nullable:           0
104         default_value:         
105         is_primary_key:        1
106         is_unique:             0
107         is_auto_increment:     1
108         is_foreign_key:        0
109         foreign_key_reference: 
110         is_valid:              1
111         order:                 1
112         table:                 Basic
113     
114     title
115         data_type:             varchar
116         size:                  100
117         is_nullable:           0
118         default_value:         hello
119         is_primary_key:        0
120         is_unique:             0
121         is_auto_increment:     0
122         is_foreign_key:        0
123         foreign_key_reference: 
124         is_valid:              1
125         order:                 2
126         table:                 Basic
127     
128     description
129         data_type:             text
130         size:                  0
131         is_nullable:           1
132         default_value:         
133         is_primary_key:        0
134         is_unique:             0
135         is_auto_increment:     0
136         is_foreign_key:        0
137         foreign_key_reference: 
138         is_valid:              1
139         order:                 3
140         table:                 Basic
141     
142     email
143         data_type:             varchar
144         size:                  255
145         is_nullable:           1
146         default_value:         
147         is_primary_key:        0
148         is_unique:             1
149         is_auto_increment:     0
150         is_foreign_key:        0
151         foreign_key_reference: 
152         is_valid:              1
153         order:                 4
154         table:                 Basic
155     
156     explicitnulldef
157         data_type:             varchar
158         size:                  0
159         is_nullable:           1
160         default_value:         
161         is_primary_key:        0
162         is_unique:             0
163         is_auto_increment:     0
164         is_foreign_key:        0
165         foreign_key_reference: 
166         is_valid:              1
167         order:                 5
168         table:                 Basic
169     
170     explicitemptystring
171         data_type:             varchar
172         size:                  0
173         is_nullable:           1
174         default_value:         
175         is_primary_key:        0
176         is_unique:             0
177         is_auto_increment:     0
178         is_foreign_key:        0
179         foreign_key_reference: 
180         is_valid:              1
181         order:                 6
182         table:                 Basic
183     
184     emptytagdef
185         data_type:             varchar
186         size:                  0
187         is_nullable:           1
188         default_value:         
189         is_primary_key:        0
190         is_unique:             0
191         is_auto_increment:     0
192         is_foreign_key:        0
193         foreign_key_reference: 
194         is_valid:              1
195         order:                 7
196         table:                 Basic
197     
198     timest
199         data_type:             timestamp
200         size:                  0
201         is_nullable:           1
202         default_value:         
203         is_primary_key:        0
204         is_unique:             0
205         is_auto_increment:     0
206         is_foreign_key:        0
207         foreign_key_reference: 
208         is_valid:              1
209         order:                 8
210         table:                 Basic
211     
212
213 Indices
214     titleindex
215         table:    Basic
216         fields:   title
217         type:     NORMAL
218         options:  
219         is_valid: 1
220     
221     
222 Constraints
223     ?
224         type:             PRIMARY KEY
225         fields:           id
226         expression:       
227         match_type:       
228         reference_fields: 
229         reference_table:  
230         deferrable:       1
231         on_delete:        
232         on_update:        
233         options:          
234         is_valid:         1
235     
236     emailuniqueindex
237         type:             UNIQUE
238         fields:           email
239         expression:       
240         match_type:       
241         reference_fields: 
242         reference_table:  
243         deferrable:       1
244         on_delete:        
245         on_update:        
246         options:          
247         is_valid:         1
248