Tab/WS crusade
[dbsrgits/SQL-Translator.git] / t / 33tt-table-producer.t
CommitLineData
aee4b66e 1#!/usr/bin/perl -w
0be5b227 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 {
3e1ed76b 24 maybe_plan(8, 'Template 2.20', 'Test::Differences')
0be5b227 25}
26use Test::Differences;
27
28use SQL::Translator;
29use SQL::Translator::Producer::TT::Table;
30
31# Setup a tmp directory we can output files to.
32my $tdir = tempdir( CLEANUP => 1 );
33
34# Parse the test XML schema
35my $obj;
36$obj = SQL::Translator->new(
37 debug => DEBUG, #$opt{d},
38 show_warnings => 1,
39 add_drop_table => 1,
40 from => "SQLite",
41 filename => "$Bin/data/sqlite/create.sql",
42 to => "TT-Table",
43 producer_args => {
44 tt_table => "$Bin/data/template/table.tt",
45 mk_files => 1,
46 mk_files_base => "$tdir",
47 mk_file_ext => "txt",
48 on_exists => "replace",
49 },
50);
51my $out;
52lives_ok { $out = $obj->translate; } "Translate ran";
53ok $out ne "" ,"Produced something!";
54warn $obj->error unless $out;
55
56# Normal output looks ok
57local $/ = undef; # slurp
aee4b66e 58eq_or_diff
59 $out,
60 do { local (@ARGV, $/) = "$Bin/data/template/testresult_table.txt"; <> },
61 "Output looks right"
62;
0be5b227 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
aee4b66e 73 Foreign Keys:\x20
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
aee4b66e 83 Foreign Keys:\x20
0be5b227 84 Data Fields: name, age
85
86}
41fa63fd 87, "pet.txt looks right";
88close(FILE);
0be5b227 89
0be5b227 90print $out if DEBUG;
91#print "Debug:", Dumper($obj) if DEBUG;