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