Better debug output
[dbsrgits/SQL-Translator.git] / t / 33tt-table-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 vars '%opt';
14 BEGIN { map { $opt{$_}=1 if s/^-// } @ARGV; }
15 use constant DEBUG => (exists $opt{d} ? 1 : 0);
16
17 use FindBin qw/$Bin/;
18 use File::Temp qw/tempdir/;
19
20 # Testing 1,2,3,4...
21 #=============================================================================
22
23 BEGIN {
24     eval {require Template;};
25
26     maybe_plan(8, 'Template', 'Test::Differences')
27 }
28 use Test::Differences;
29
30 use SQL::Translator;
31 use SQL::Translator::Producer::TT::Table;
32
33 # Setup a tmp directory we can output files to.
34 my $tdir = tempdir( CLEANUP => 1 );
35
36 # Parse the test XML schema
37 my $obj;
38 $obj = SQL::Translator->new(
39     debug          => DEBUG, #$opt{d},
40     show_warnings  => 1,
41     add_drop_table => 1,
42     from           => "SQLite",
43     filename       => "$Bin/data/sqlite/create.sql",
44     to             => "TT-Table",
45     producer_args  => {
46         tt_table => "$Bin/data/template/table.tt",
47         mk_files      => 1,
48         mk_files_base => "$tdir",
49         mk_file_ext   => "txt",
50         on_exists     => "replace",
51     },
52 );
53 my $out;
54 lives_ok { $out = $obj->translate; }  "Translate ran";
55 ok $out ne ""                        ,"Produced something!";
56 warn $obj->error unless $out;
57
58 # Normal output looks ok
59 local $/ = undef; # slurp
60 eq_or_diff $out, <DATA>              ,"Output looks right";
61
62 # File output
63 my @files = glob("$tdir/*.txt");
64 ok( @files == 2, "Wrote 2 files." );
65 is( $files[0], "$tdir/person.txt" , "Wrote person.txt" );
66 is( $files[1], "$tdir/pet.txt"    , "Wrote pet.txt" );
67
68 open(FILE, "$tdir/person.txt") || die "Couldn't open $tdir/person.txt : $!";
69 eq_or_diff <FILE>, qq{Table: person
70   Primary Key:  person_id
71   Foreign Keys: 
72   Data Fields:  name, age, weight, iq, description
73
74 }
75 , "person.txt looks right";
76 close(FILE);
77
78 open(FILE, "$tdir/pet.txt") || die "Couldn't open $tdir/pet.txt : $!";
79 eq_or_diff <FILE>, qq{Table: pet
80   Primary Key:  pet_id, person_id
81   Foreign Keys: 
82   Data Fields:  name, age
83
84 }
85 , "pet.txt looks right";
86 close(FILE);
87
88
89 print $out if DEBUG;
90 #print "Debug:", Dumper($obj) if DEBUG;
91
92 __DATA__
93 Table: person
94   Primary Key:  person_id
95   Foreign Keys: 
96   Data Fields:  name, age, weight, iq, description
97
98 Table: pet
99   Primary Key:  pet_id, person_id
100   Foreign Keys: 
101   Data Fields:  name, age
102