Update tests to use maybe_plan.
[dbsrgits/SQL-Translator.git] / t / 21xml-xmi-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 # Tests basic functionality and the default xmi2schema
9 #
10
11 use strict;
12 use FindBin qw/$Bin/;
13 use Data::Dumper;
14
15 # run test with -d for debug
16 my %opt;
17 BEGIN { map { $opt{$_}=1 if s/^-// } @ARGV; }
18 use constant DEBUG => (exists $opt{d} ? 1 : 0);
19
20 use Test::More;
21 use Test::Exception;
22 use Test::SQL::Translator qw(maybe_plan);
23 use SQL::Translator;
24 use SQL::Translator::Schema::Constants;
25
26 # Usefull test subs for the schema objs
27 #=============================================================================
28
29 my %ATTRIBUTES;
30 $ATTRIBUTES{field} = [qw/
31 name
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
47     foreach my $attr ( @{$ATTRIBUTES{field}} ) {
48         if ( exists $test->{$attr} ) {
49             my $ans = $test->{$attr};
50             if ( $attr =~ m/^is_/ ) {
51                 if ($ans) { ok $fld->$attr,  " $name - $attr true"; }
52                 else      { ok !$fld->$attr, " $name - $attr false"; }
53             }
54             else {
55                 is $fld->$attr, $ans, " $name - $attr = '"
56                                      .(defined $ans ? $ans : "NULL" )."'";
57             }
58         }
59         else {
60             ok !$fld->$attr, "$name - $attr not set";
61         }
62     }
63 }
64
65 sub test_table {
66     my $tbl = shift;
67     my %arg = @_;
68     my $name = $arg{name} || die "Need a table name to test.";
69     my @fldnames = map { $_->{name} } @{$arg{fields}};
70     is_deeply( [ map {$_->name}   $tbl->get_fields ],
71                [ map {$_->{name}} @{$arg{fields}} ],
72                "Table $name\'s fields" );
73     foreach ( @{$arg{fields}} ) {
74         my $name = $_->{name} || die "Need a field name to test.";
75         test_field( $tbl->get_field($name), $_ );
76     }
77 }
78
79 # Testing 1,2,3,..
80 #=============================================================================
81
82 maybe_plan(103,
83     'SQL::Translator::Parser::XML::XMI',
84     'SQL::Translator::Producer::MySQL');
85
86 my $testschema = "$Bin/data/xmi/Foo.poseidon2.xmi";
87 die "Can't find test schema $testschema" unless -e $testschema;
88
89 my $obj;
90 $obj = SQL::Translator->new(
91     filename => $testschema,
92     from     => 'XML-XMI',
93     to       => 'MySQL',
94     debug          => DEBUG,
95     show_warnings  => 1,
96 );
97 my $sql = $obj->translate;
98 print $sql if DEBUG;
99
100 #
101 # Test the schema
102 #
103 my $scma = $obj->schema;
104 my @tblnames = map {$_->name} $scma->get_tables;
105 is_deeply( \@tblnames, [qw/Foo PrivateFoo Recording CD Track ProtectedFoo/]
106     ,"tables");
107
108 #
109 # Tables
110 #
111 # Foo
112 #
113 test_table( $scma->get_table("Foo"),
114     name => "Foo",
115     fields => [
116         {
117             name => "fooid",
118             data_type => "int",
119             default_value => undef,
120             is_nullable => 1,
121             is_primary_key => 1,
122         },
123         {
124             name => "name",
125             data_type => "varchar",
126             default_value => "",
127             is_nullable => 1,
128         },
129         {
130             name => "protectedname",
131             data_type => "varchar",
132             default_value => undef,
133             is_nullable => 1,
134         },
135         {
136             name => "privatename",
137             data_type => "varchar",
138             default_value => undef,
139             is_nullable => 1,
140         },
141     ],
142 );
143
144 #
145 # Recording
146 #
147 test_table( $scma->get_table("Recording"),
148     name => "Recording",
149     fields => [
150     {
151         name => "recordingid",
152         data_type => "int",
153         default_value => undef,
154         is_nullable => 1,
155         is_primary_key => 1,
156     },
157     {
158         name => "title",
159         data_type => "varchar",
160         is_nullable => 1,
161     },
162     {
163         name => "type",
164         data_type => "varchar",
165         is_nullable => 1,
166     },
167     ],
168 );
169
170 #
171 # Track
172 #
173 test_table( $scma->get_table("Track"),
174     name => "Track",
175     fields => [
176     {
177         name => "trackid",
178         data_type => "int",
179         default_value => undef,
180         is_nullable => 1,
181         is_primary_key => 1,
182     },
183     {
184         name => "recordingid",
185         data_type => "int",
186         default_value => undef,
187         is_nullable => 1,
188         is_primary_key => 0,
189         #is_foreign_key => 1,
190     },
191     {
192         name => "number",
193         data_type => "int",
194         default_value => "1",
195         is_nullable => 1,
196     },
197     {
198         name => "name",
199         data_type => "varchar",
200         is_nullable => 1,
201     },
202     ],
203 );