Template version doesn't matter.
[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;};
f51d8e46 25
0be5b227 26 maybe_plan(8, 'Template', 'Test::Differences')
27}
28use Test::Differences;
29
30use SQL::Translator;
31use SQL::Translator::Producer::TT::Table;
32
33# Setup a tmp directory we can output files to.
34my $tdir = tempdir( CLEANUP => 1 );
35
36# Parse the test XML schema
37my $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);
53my $out;
54lives_ok { $out = $obj->translate; } "Translate ran";
55ok $out ne "" ,"Produced something!";
56warn $obj->error unless $out;
57
58# Normal output looks ok
59local $/ = undef; # slurp
60eq_or_diff $out, <DATA> ,"Output looks right";
61
62# File output
63my @files = glob("$tdir/*.txt");
64ok( @files == 2, "Wrote 2 files." );
65is( $files[0], "$tdir/person.txt" , "Wrote person.txt" );
66is( $files[1], "$tdir/pet.txt" , "Wrote pet.txt" );
67
68open(FILE, "$tdir/person.txt") || die "Couldn't open $tdir/person.txt : $!";
69eq_or_diff <FILE>, qq{Table: person
70 Primary Key: person_id
41fa63fd 71 Foreign Keys:
0be5b227 72 Data Fields: name, age, weight, iq, description
73
74}
75, "person.txt looks right";
41fa63fd 76close(FILE);
0be5b227 77
78open(FILE, "$tdir/pet.txt") || die "Couldn't open $tdir/pet.txt : $!";
79eq_or_diff <FILE>, qq{Table: pet
80 Primary Key: pet_id, person_id
41fa63fd 81 Foreign Keys:
0be5b227 82 Data Fields: name, age
83
84}
41fa63fd 85, "pet.txt looks right";
86close(FILE);
0be5b227 87
88
89print $out if DEBUG;
90#print "Debug:", Dumper($obj) if DEBUG;
91
92__DATA__
93Table: person
94 Primary Key: person_id
41fa63fd 95 Foreign Keys:
0be5b227 96 Data Fields: name, age, weight, iq, description
97
98Table: pet
99 Primary Key: pet_id, person_id
41fa63fd 100 Foreign Keys:
0be5b227 101 Data Fields: name, age
102