File: 33tt-table-producer.t

package info (click to toggle)
libsql-translator-perl 0.11011-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 15,380 kB
  • sloc: perl: 251,748; sql: 3,805; xml: 233; makefile: 7
file content (91 lines) | stat: -rw-r--r-- 2,353 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/perl -w
# vim:filetype=perl

# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'

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);

use FindBin qw/$Bin/;
use File::Temp qw/tempdir/;

# Testing 1,2,3,4...
#=============================================================================

BEGIN {
    maybe_plan(8, 'Template 2.20', 'Test::Differences')
}
use Test::Differences;

use SQL::Translator;
use SQL::Translator::Producer::TT::Table;

# Setup a tmp directory we can output files to.
my $tdir = tempdir( CLEANUP => 1 );

# Parse the test XML schema
my $obj;
$obj = SQL::Translator->new(
    debug          => DEBUG, #$opt{d},
    show_warnings  => 1,
    add_drop_table => 1,
    from           => "SQLite",
    filename       => "$Bin/data/sqlite/create.sql",
    to             => "TT-Table",
    producer_args  => {
        tt_table => "$Bin/data/template/table.tt",
        mk_files      => 1,
        mk_files_base => "$tdir",
        mk_file_ext   => "txt",
        on_exists     => "replace",
    },
);
my $out;
lives_ok { $out = $obj->translate; }  "Translate ran";
ok $out ne ""                        ,"Produced something!";
warn $obj->error unless $out;

# Normal output looks ok
local $/ = undef; # slurp
eq_or_diff
  $out,
  do { local (@ARGV, $/) = "$Bin/data/template/testresult_table.txt"; <> },
  "Output looks right"
;

# File output
my @files = glob("$tdir/*.txt");
ok( @files == 2, "Wrote 2 files." );
is( $files[0], "$tdir/person.txt" , "Wrote person.txt" );
is( $files[1], "$tdir/pet.txt"    , "Wrote pet.txt" );

open(FILE, "$tdir/person.txt") || die "Couldn't open $tdir/person.txt : $!";
eq_or_diff <FILE>, qq{Table: person
  Primary Key:  person_id
  Foreign Keys:\x20
  Data Fields:  name, age, weight, iq, description

}
, "person.txt looks right";
close(FILE);

open(FILE, "$tdir/pet.txt") || die "Couldn't open $tdir/pet.txt : $!";
eq_or_diff <FILE>, qq{Table: pet
  Primary Key:  pet_id, person_id
  Foreign Keys:\x20
  Data Fields:  name, age

}
, "pet.txt looks right";
close(FILE);

print $out if DEBUG;
#print "Debug:", Dumper($obj) if DEBUG;