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