be7372aa4f1d2f3e3e3bb77340bfc7a647a8ce85
[dbsrgits/SQL-Translator.git] / t / 16xml-parser.t
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 #
8 # basic.t
9 # -------
10 # Tests that;
11 #
12
13 use Test::More tests => 78;
14 use Test::Exception;
15
16 use strict;
17 use Data::Dumper;
18 our %opt;
19 BEGIN { map { $opt{$_}=1 if s/^-// } @ARGV; }
20 use constant DEBUG => (exists $opt{d} ? 1 : 0);
21 local $SIG{__WARN__} = sub { diag "[warn] ", @_; };
22
23 use FindBin qw/$Bin/;
24
25 # Usefull test subs for the schema objs
26 #=============================================================================
27
28 our %ATTRIBUTES;
29 $ATTRIBUTES{field} = [qw/
30 name
31 order
32 data_type
33 default_value
34 size
35 is_primary_key
36 is_unique
37 is_nullable
38 is_foreign_key
39 is_auto_increment
40 /];
41
42 sub test_field {
43     my ($fld,$test) = @_;
44     die "test_field needs a least a name!" unless $test->{name};
45     my $name = $test->{name};
46     is $fld->name, $name, "$name - Name right";
47
48     foreach my $attr ( @{$ATTRIBUTES{field}} ) {
49         if ( exists $test->{$attr} ) {
50             my $ans = $test->{$attr};
51             if ( $attr =~ m/^is_/ ) {
52                 if ($ans) { ok $fld->$attr,  " $name - $attr true"; }
53                 else      { ok !$fld->$attr, " $name - $attr false"; }
54             }
55             else {
56                 is $fld->$attr, $ans, " $name - $attr = '"
57                                      .(defined $ans ? $ans : "NULL" )."'";
58             }
59         }
60         else {
61             ok !$fld->$attr, "$name - $attr not set";
62         }
63     }
64 }
65
66 # TODO test_constraint, test_index
67
68 # Testing 1,2,3,4...
69 #=============================================================================
70
71 use SQL::Translator;
72 use SQL::Translator::Schema::Constants;
73
74 # Parse the test XML schema
75 our $obj;
76 $obj = SQL::Translator->new(
77     debug          => DEBUG,
78     show_warnings  => 1,
79     add_drop_table => 1,
80 );
81 my $testschema = "$Bin/data/xml/schema-basic.xml";
82 die "Can't find test schema $testschema" unless -e $testschema;
83 my $sql = $obj->translate(
84     from     => "SqlfXML",
85     to       =>"MySQL",
86     filename => $testschema,
87 );
88 print $sql if DEBUG;
89 #print "Debug:", Dumper($obj) if DEBUG;
90
91 # Test the schema objs generted from the XML
92 #
93 my $scma = $obj->schema;
94 my @tblnames = map {$_->name} $scma->get_tables;
95 is_deeply( \@tblnames, [qw/Basic/], "tables");
96
97 # Basic
98 my $tbl = $scma->get_table("Basic");
99 is $tbl->order, 1, "Basic->order";
100 is_deeply( [map {$_->name} $tbl->get_fields],
101     [qw/id title description email explicitnulldef explicitemptystring/] , 
102     "Table Basic's fields");
103 test_field($tbl->get_field("id"),{
104     name => "id",
105     order => 1,
106     data_type => "int",
107     default_value => undef,
108     is_nullable => 0,
109     size => 10,
110     is_primary_key => 1,
111     is_auto_increment => 1,
112 });
113 test_field($tbl->get_field("title"),{
114     name => "title",
115     order => 2,
116     data_type => "varchar",
117     is_nullable => 0,
118     default_value => "hello",
119     size => 100,
120 });
121 test_field($tbl->get_field("description"),{
122     name => "description",
123     order => 3,
124     data_type => "text",
125     is_nullable => 1,
126     default_value => "",
127 });
128 test_field($tbl->get_field("email"),{
129     name => "email",
130     order => 4,
131     data_type => "varchar",
132     size => 255,
133     is_unique => 1,
134     default_value => undef,
135     is_nullable => 1,
136 });
137 test_field($tbl->get_field("explicitnulldef"),{
138     name => "explicitnulldef",
139     order => 5,
140     data_type => "varchar",
141     default_value => undef,
142     is_nullable => 1,
143 });
144 test_field($tbl->get_field("explicitemptystring"),{
145     name => "explicitemptystring",
146     order => 6,
147     data_type => "varchar",
148     default_value => "",
149     is_nullable => 1,
150 });
151
152 my @indices = $tbl->get_indices;
153 is scalar(@indices), 1, "Table basic has 1 index";
154
155 my @constraints = $tbl->get_constraints;
156 is scalar(@constraints), 2, "Table basic has 2 constraints";
157 my $con = shift @constraints;
158 is $con->table, $tbl, "Constaints table right";
159 is $con->name, "", "Constaints table right";
160 is $con->type, PRIMARY_KEY, "Constaint is primary key";
161 is_deeply [$con->fields], ["id"], "Constaint fields";
162 $con = shift @constraints;
163 is $con->table, $tbl, "Constaints table right";
164 is $con->type, UNIQUE, "Constaint UNIQUE";
165 is_deeply [$con->fields], ["email"], "Constaint fields";