Added Views, Procedures and Triggers to bring it inline with the current Schema featu...
[dbsrgits/SQL-Translator.git] / t / 21xml-xmi-parser.t
CommitLineData
1223c9b2 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#
fd5abbd7 8# Tests basic functionality and the default xmi2schema
1223c9b2 9#
10
11use strict;
fd5abbd7 12use FindBin qw/$Bin/;
1223c9b2 13use Data::Dumper;
fd5abbd7 14
15# run test with -d for debug
1223c9b2 16my %opt;
17BEGIN { map { $opt{$_}=1 if s/^-// } @ARGV; }
18use constant DEBUG => (exists $opt{d} ? 1 : 0);
1223c9b2 19
fd5abbd7 20use Test::More;
21use Test::Exception;
22use SQL::Translator;
23use SQL::Translator::Schema::Constants;
1223c9b2 24
25# Usefull test subs for the schema objs
26#=============================================================================
27
28my %ATTRIBUTES;
29$ATTRIBUTES{field} = [qw/
30name
31data_type
32default_value
33size
34is_primary_key
35is_unique
36is_nullable
37is_foreign_key
38is_auto_increment
39/];
40
41sub 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
64sub 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
215c6c52 81plan tests => 103;
1223c9b2 82
1223c9b2 83my $testschema = "$Bin/data/xmi/Foo.poseidon2.xmi";
ef2d7798 84die "Can't find test schema $testschema" unless -e $testschema;
ef2d7798 85
ef2d7798 86my $obj;
87$obj = SQL::Translator->new(
88 filename => $testschema,
1223c9b2 89 from => 'XML-XMI',
90 to => 'MySQL',
ef2d7798 91 debug => DEBUG,
92 show_warnings => 1,
1223c9b2 93);
ef2d7798 94my $sql = $obj->translate;
1223c9b2 95print $sql if DEBUG;
1223c9b2 96
97#
ef2d7798 98# Test the schema
1223c9b2 99#
100my $scma = $obj->schema;
101my @tblnames = map {$_->name} $scma->get_tables;
f42065cb 102is_deeply( \@tblnames, [qw/Foo PrivateFoo Recording CD Track ProtectedFoo/]
ef2d7798 103 ,"tables");
104
ef2d7798 105#
106# Tables
1223c9b2 107#
108# Foo
109#
110test_table( $scma->get_table("Foo"),
111 name => "Foo",
112 fields => [
ef2d7798 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 ],
1223c9b2 139);
140
141#
142# Recording
143#
144test_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#
170test_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);