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'
11 # run with -d for debug
13 BEGIN { map { $opt{$_}=1 if s/^-// } @ARGV; }
14 use constant DEBUG => (exists $opt{d} ? 1 : 0);
19 use SQL::Translator::Schema::Constants;
21 # Usefull test subs for the schema objs
22 #=============================================================================
27 fail " Field '$test->{name}' doesn't exist!";
28 # TODO Do a skip on the following tests
32 is( $f1->name, $test->{name}, " Field name '$test->{name}'" );
34 is( $f1->data_type, $test->{data_type}, " Type is '$test->{data_type}'" )
35 if exists $test->{data_type};
37 is( $f1->size, $test->{size}, " Size is '$test->{size}'" )
38 if exists $test->{size};
40 is( $f1->default_value, $test->{default_value},
41 " Default value is ".(defined($test->{default_value}) ? "'$test->{default_value}'" : "UNDEF" ) )
42 if exists $test->{default_value};
44 is( $f1->is_nullable, $test->{is_nullable},
45 " ".($test->{is_nullable} ? 'can' : 'cannot').' be null' )
46 if exists $test->{is_nullable};
48 is( $f1->is_unique, $test->{is_unique},
49 " ".($test->{is_unique} ? 'can' : 'cannot').' be unique' )
50 if exists $test->{is_unique};
52 is( $f1->is_primary_key, $test->{is_primary_key},
53 " is ".($test->{is_primary_key} ? '' : 'not').' a primary_key' )
54 if exists $test->{is_primary_key};
56 is( $f1->is_foreign_key, $test->{is_foreign_key},
57 " is ".($test->{is_foreign_key} ? '' : 'not').' a foreign_key' )
58 if exists $test->{is_foreign_key};
60 is( $f1->is_auto_increment, $test->{is_auto_increment},
61 " is ".($test->{is_auto_increment} ? '' : 'not').' an auto_increment' )
62 if exists $test->{is_auto_increment};
67 #$test->{name} ||= "<anon>";
69 if ( exists $test->{name} ) {
70 is( $con->name, $test->{name}, " Constraint '$test->{name}'" );
73 ok( $con, " Constraint" );
76 is( $con->type, $test->{type}, " type is '$test->{type}'" )
77 if exists $test->{type};
79 is( $con->table->name, $test->{table}, " table is '$test->{table}'" )
80 if exists $test->{table};
82 is( join(",",$con->fields), $test->{fields},
83 " fields is '$test->{fields}'" )
84 if exists $test->{fields};
86 is( $con->reference_table, $test->{reference_table},
87 " reference_table is '$test->{reference_table}'" )
88 if exists $test->{reference_table};
90 is( join(",",$con->reference_fields), $test->{reference_fields},
91 " reference_fields is '$test->{reference_fields}'" )
92 if exists $test->{reference_fields};
94 is( $con->match_type, $test->{match_type},
95 " match_type is '$test->{match_type}'" )
96 if exists $test->{match_type};
98 is( $con->on_delete_do, $test->{on_delete_do},
99 " on_delete_do is '$test->{on_delete_do}'" )
100 if exists $test->{on_delete_do};
102 is( $con->on_update_do, $test->{on_update_do},
103 " on_update_do is '$test->{on_update_do}'" )
104 if exists $test->{on_update_do};
110 $arg{constraints} ||= [];
111 my $name = $arg{name} || die "Need a table name to test.";
113 my @fldnames = map { $_->{name} } @{$arg{fields}};
114 is_deeply( [ map {$_->name} $tbl->get_fields ],
115 [ map {$_->{name}} @{$arg{fields}} ],
116 "Table $name\'s fields" );
117 foreach ( @{$arg{fields}} ) {
118 my $name = $_->{name} || die "Need a field name to test.";
119 test_field( $tbl->get_field($name), $_ );
122 if ( my @tcons = @{$arg{constraints}} ) {
123 my @cons = $tbl->get_constraints;
124 is(scalar(@cons), scalar(@tcons),
125 "Table $name has ".scalar(@tcons)." Constraints");
127 my $ans = { table => $tbl->name, %{shift @tcons}};
128 constraint_ok( $_, $ans );
134 #=============================================================================
138 my $testschema = "$Bin/data/xmi/OrderDB.sqlfairy.poseidon2.xmi";
139 die "Can't find test schema $testschema" unless -e $testschema;
142 $obj = SQL::Translator->new(
143 filename => $testschema,
144 from => 'XML-XMI-SQLFairy',
149 my $sql = $obj->translate;
150 ok( $sql, "Got some SQL");
157 my $scma = $obj->schema;
158 is( $scma->is_valid, 1, 'Schema is valid' );
159 my @tblnames = map {$_->name} $scma->get_tables;
160 is(scalar(@{$scma->get_tables}), scalar(@tblnames), "Right number of tables");
161 is_deeply( \@tblnames,
162 [qw/Order OrderLine Customer ContactDetails ContactDetails_Customer/]
165 test_table( $scma->get_table("Customer"),
170 data_type => "VARCHAR",
172 default_value => undef,
178 data_type => "VARCHAR",
180 default_value => undef,
185 name => "CustomerID",
188 default_value => undef,
195 type => "PRIMARY KEY",
196 fields => "CustomerID",
199 # name => "UniqueEmail",
206 test_table( $scma->get_table("ContactDetails_Customer"),
207 name => "ContactDetails_Customer",
210 name => "ContactDetailsID",
213 default_value => undef,
216 is_auto_increment => 0,
219 name => "CustomerID",
222 default_value => undef,
225 is_auto_increment => 0,
230 type => "FOREIGN KEY",
231 fields => "ContactDetailsID",
232 reference_table => "ContactDetails",
233 reference_fields => "ContactDetailsID",
236 type => "FOREIGN KEY",
237 fields => "CustomerID",
238 reference_table => "Customer",
239 reference_fields => "CustomerID",
242 type => "PRIMARY KEY",
243 fields => "ContactDetailsID,CustomerID",
248 test_table( $scma->get_table("ContactDetails"),
249 name => "ContactDetails",
253 data_type => "VARCHAR",
255 default_value => undef,
261 data_type => "VARCHAR",
263 default_value => undef,
268 name => "ContactDetailsID",
271 default_value => undef,
274 is_auto_increment => 1,
279 type => "PRIMARY KEY",
280 fields => "ContactDetailsID",
285 test_table( $scma->get_table("Order"),
289 name => "invoiceNumber",
292 default_value => undef,
299 default_value => undef,
304 name => "CustomerID",
307 default_value => undef,
315 type => "PRIMARY KEY",
316 fields => "invoiceNumber",
319 type => "FOREIGN KEY",
320 fields => "CustomerID",
321 reference_table => "Customer",
322 reference_fields => "CustomerID",
328 # name => "idxOrderDate",
330 # fields => "orderDate",
336 test_table( $scma->get_table("OrderLine"),
340 name => "lineNumber",
356 name => "OrderLineID",
359 default_value => undef,
364 name => "invoiceNumber",
367 default_value => undef,
374 type => "PRIMARY KEY",
375 fields => "OrderLineID,invoiceNumber",
378 type => "FOREIGN KEY",
379 fields => "invoiceNumber",
380 reference_table => "Order",
381 reference_fields => "invoiceNumber",