Update tests to use maybe_plan.
[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;
2d691ec1 22use Test::SQL::Translator qw(maybe_plan);
fd5abbd7 23use SQL::Translator;
24use SQL::Translator::Schema::Constants;
1223c9b2 25
26# Usefull test subs for the schema objs
27#=============================================================================
28
29my %ATTRIBUTES;
30$ATTRIBUTES{field} = [qw/
31name
32data_type
33default_value
34size
35is_primary_key
36is_unique
37is_nullable
38is_foreign_key
39is_auto_increment
40/];
41
42sub 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
65sub 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
2d691ec1 82maybe_plan(103,
83 'SQL::Translator::Parser::XML::XMI',
84 'SQL::Translator::Producer::MySQL');
1223c9b2 85
1223c9b2 86my $testschema = "$Bin/data/xmi/Foo.poseidon2.xmi";
ef2d7798 87die "Can't find test schema $testschema" unless -e $testschema;
ef2d7798 88
ef2d7798 89my $obj;
90$obj = SQL::Translator->new(
91 filename => $testschema,
1223c9b2 92 from => 'XML-XMI',
93 to => 'MySQL',
ef2d7798 94 debug => DEBUG,
95 show_warnings => 1,
1223c9b2 96);
ef2d7798 97my $sql = $obj->translate;
1223c9b2 98print $sql if DEBUG;
1223c9b2 99
100#
ef2d7798 101# Test the schema
1223c9b2 102#
103my $scma = $obj->schema;
104my @tblnames = map {$_->name} $scma->get_tables;
f42065cb 105is_deeply( \@tblnames, [qw/Foo PrivateFoo Recording CD Track ProtectedFoo/]
ef2d7798 106 ,"tables");
107
ef2d7798 108#
109# Tables
1223c9b2 110#
111# Foo
112#
113test_table( $scma->get_table("Foo"),
114 name => "Foo",
115 fields => [
ef2d7798 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 ],
1223c9b2 142);
143
144#
145# Recording
146#
147test_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#
173test_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);