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