997065abd4ffe6018e8183069765f072fba95ce0
[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             tt_vars => {
42                 foo   => 'bar',
43                 hello => 'world',
44             },
45         },
46     );
47     my $out;
48     lives_ok { $out = $obj->translate; }  "Translate ran";
49     ok $out ne ""                        ,"Produced something!";
50     local $/ = undef; # slurp
51     eq_or_diff $out, <DATA>              ,"Output looks right";
52 }
53
54 # Test passing of Template config
55 {
56     my $tmpl = q{
57     [%- FOREACH table = schema.get_tables %]
58     Table: $table
59     [%- END %]};
60     my $obj;
61     $obj = SQL::Translator->new(
62         show_warnings  => 1,
63         from           => "XML-SQLFairy",
64         filename       => "$Bin/data/xml/schema.xml",
65         to             => "TTSchema",
66         producer_args  => {
67             ttfile  => \$tmpl,
68             tt_conf => {
69                 INTERPOLATE => 1,
70             },
71             tt_vars => {
72                 foo   => 'bar',
73                 hello => 'world',
74             },
75         },
76     );
77     my $out;
78     lives_ok { $out = $obj->translate; }  "Translate ran";
79     ok $out ne ""                        ,"Produced something!";
80     local $/ = undef; # slurp
81     eq_or_diff $out, q{
82     Table: Basic}              
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:                  255
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
197 Indices
198     titleindex
199         table:    Basic
200         fields:   title
201         type:     NORMAL
202         options:  
203         is_valid: 1
204     
205     
206 Constraints
207     ?
208         type:             PRIMARY KEY
209         fields:           id
210         expression:       
211         match_type:       
212         reference_fields: 
213         reference_table:  
214         deferrable:       1
215         on_delete:        
216         on_update:        
217         options:          
218         is_valid:         1
219     
220     emailuniqueindex
221         type:             UNIQUE
222         fields:           email
223         expression:       
224         match_type:       
225         reference_fields: 
226         reference_table:  
227         deferrable:       1
228         on_delete:        
229         on_update:        
230         options:          
231         is_valid:         1
232