Add timestamp tests, make postgres produce timestamp(0) if asked
[dbsrgits/SQL-Translator.git] / t / 18ttschema-producer.t
index afe5025..5002d1e 100644 (file)
@@ -7,60 +7,90 @@
 use strict;
 use Test::More;
 use Test::Exception;
+use Test::SQL::Translator qw(maybe_plan);
 
 use Data::Dumper;
-use vars '%opt';
-BEGIN { map { $opt{$_}=1 if s/^-// } @ARGV; }
-use constant DEBUG => (exists $opt{d} ? 1 : 0);
-local $SIG{__WARN__} = sub { diag "[warn] ", @_; };
-
 use FindBin qw/$Bin/;
 
 # Testing 1,2,3,4...
 #=============================================================================
 
-eval { require Template; };
-if ($@ && $@ =~ m!locate Template.pm in!) {
-    plan skip_all => "You need Template Toolkit to run this test.";
-}
-eval { require Test::Differences; };
-if ($@ && $@ =~ m!locate Test/Differences.pm in!) {
-    plan skip_all => "You need Test::Differences for this test.";
+BEGIN {
+    maybe_plan(6, 
+        'XML::XPath', 
+        'SQL::Translator::Parser::XML::SQLFairy',
+        'Template', 
+        'Test::Differences'
+    )
 }
 use Test::Differences;
-plan tests => 3;
-    
+
 use SQL::Translator;
 use SQL::Translator::Producer::TTSchema;
 
-# Parse the test XML schema
-my $obj;
-$obj = SQL::Translator->new(
-    debug          => DEBUG, #$opt{d},
-    show_warnings  => 1,
-    add_drop_table => 1,
-    from           => "XML-SQLFairy",
-    filename       => "$Bin/data/xml/schema-basic.xml",
-    to             => "TTSchema",
-     producer_args  => {
-        ttfile => "$Bin/data/template/basic.tt",
-    },
-);
-my $out;
-lives_ok { $out = $obj->translate; }  "Translate ran";
-ok $out ne ""                        ,"Produced something!";
-local $/ = undef; # slurp
-eq_or_diff $out, <DATA>              ,"Output looks right";
-# I'm not sure if this diff is the best test, it is probaly too sensitive. But 
-# it at least it will blow up if anything changes!
+# Main test. Template whole schema and test tt_vars
+{
+    my $obj;
+    $obj = SQL::Translator->new(
+        show_warnings  => 1,
+        from           => "XML-SQLFairy",
+        filename       => "$Bin/data/xml/schema.xml",
+        to             => "TTSchema",
+        producer_args  => {
+            ttfile  => "$Bin/data/template/basic.tt",
+            tt_vars => {
+                foo   => 'bar',
+                hello => 'world',
+            },
+        },
+    );
+    my $out;
+    lives_ok { $out = $obj->translate; }  "Translate ran";
+    ok $out ne ""                        ,"Produced something!";
+    local $/ = undef; # slurp
+    eq_or_diff $out, <DATA>              ,"Output looks right";
+}
+
+# Test passing of Template config
+{
+    my $tmpl = q{
+    [%- FOREACH table = schema.get_tables %]
+    Table: $table
+    [%- END %]};
+    my $obj;
+    $obj = SQL::Translator->new(
+        show_warnings  => 1,
+        from           => "XML-SQLFairy",
+        filename       => "$Bin/data/xml/schema.xml",
+        to             => "TTSchema",
+        producer_args  => {
+            ttfile  => \$tmpl,
+            tt_conf => {
+                INTERPOLATE => 1,
+            },
+            tt_vars => {
+                foo   => 'bar',
+                hello => 'world',
+            },
+        },
+    );
+    my $out;
+    lives_ok { $out = $obj->translate; }  "Translate ran";
+    ok $out ne ""                        ,"Produced something!";
+    local $/ = undef; # slurp
+    eq_or_diff $out, q{
+    Table: Basic}              
+    ,"Output looks right";
+}
 
-print $out if DEBUG;
-#print "Debug:", Dumper($obj) if DEBUG;
 
 __DATA__
 Schema: 
 Database: 
 
+Foo: bar
+Hello: world
+
 Table: Basic
 ==========================================================================
 
@@ -77,7 +107,6 @@ Fields
         foreign_key_reference: 
         is_valid:              1
         order:                 1
-        extra:                 
         table:                 Basic
     
     title
@@ -92,7 +121,6 @@ Fields
         foreign_key_reference: 
         is_valid:              1
         order:                 2
-        extra:                 
         table:                 Basic
     
     description
@@ -107,7 +135,6 @@ Fields
         foreign_key_reference: 
         is_valid:              1
         order:                 3
-        extra:                 
         table:                 Basic
     
     email
@@ -122,7 +149,6 @@ Fields
         foreign_key_reference: 
         is_valid:              1
         order:                 4
-        extra:                 
         table:                 Basic
     
     explicitnulldef
@@ -137,7 +163,6 @@ Fields
         foreign_key_reference: 
         is_valid:              1
         order:                 5
-        extra:                 
         table:                 Basic
     
     explicitemptystring
@@ -152,7 +177,6 @@ Fields
         foreign_key_reference: 
         is_valid:              1
         order:                 6
-        extra:                 
         table:                 Basic
     
     emptytagdef
@@ -167,7 +191,20 @@ Fields
         foreign_key_reference: 
         is_valid:              1
         order:                 7
-        extra:                 
+        table:                 Basic
+    
+    timest
+        data_type:             timestamp
+        size:                  0
+        is_nullable:           1
+        default_value:         
+        is_primary_key:        0
+        is_unique:             0
+        is_auto_increment:     0
+        is_foreign_key:        0
+        foreign_key_reference: 
+        is_valid:              1
+        order:                 8
         table:                 Basic