X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F86sqlt.t;h=ad9c4803ab268f912dc3a199c795d1a08d5df485;hb=26ddc864d0c6b21a64c1c390f4c4b5e031aed2b2;hp=92d90f269600fb8fa739e49ff768936544873ef7;hpb=4481a67cae76f7700df22a79deb481a21c8062e2;p=dbsrgits%2FDBIx-Class-Historic.git diff --git a/t/86sqlt.t b/t/86sqlt.t index 92d90f2..ad9c480 100644 --- a/t/86sqlt.t +++ b/t/86sqlt.t @@ -10,7 +10,7 @@ plan skip_all => 'SQL::Translator required' if $@; my $schema = DBICTest->init_schema; -plan tests => 53; +plan tests => 60; my $translator = SQL::Translator->new( parser_args => { @@ -24,6 +24,10 @@ $translator->producer('SQLite'); my $output = $translator->translate(); + +ok($output, "SQLT produced someoutput") + or diag($translator->error); + # Note that the constraints listed here are the only ones that are tested -- if # more exist in the Schema than are listed here and all listed constraints are # correct, the test will still pass. If you add a class with UNIQUE or FOREIGN @@ -172,6 +176,16 @@ my %fk_constraints = ( on_delete => '', on_update => '', }, ], + # ForceForeign + forceforeign => [ + { + 'display' => 'forceforeign->artist', + 'selftable' => 'forceforeign', 'foreigntable' => 'artist', + 'selfcols' => ['artist'], 'foreigncols' => ['artist_id'], + on_delete => '', on_update => '', + }, + ], + ); my %unique_constraints = ( @@ -209,13 +223,26 @@ my %unique_constraints = ( # ], ); +my %indexes = ( + artist => [ + { + 'fields' => ['name'] + }, + ] +); + my $tschema = $translator->schema(); +# Test that the $schema->sqlt_deploy_hook was called okay and that it removed +# the 'link' table +ok( !defined($tschema->get_table('link')), "Link table was removed by hook"); # Test that nonexistent constraints are not found my $constraint = get_constraint('FOREIGN KEY', 'cd', ['title'], 'cd', ['year']); ok( !defined($constraint), 'nonexistent FOREIGN KEY constraint not found' ); $constraint = get_constraint('UNIQUE', 'cd', ['artist']); ok( !defined($constraint), 'nonexistent UNIQUE constraint not found' ); +$constraint = get_constraint('FOREIGN KEY', 'forceforeign', ['cd'], 'cd', ['cdid']); +ok( !defined($constraint), 'forced nonexistent FOREIGN KEY constraint not found' ); for my $expected_constraints (keys %fk_constraints) { for my $expected_constraint (@{ $fk_constraints{$expected_constraints} }) { @@ -240,6 +267,13 @@ for my $expected_constraints (keys %unique_constraints) { } } +for my $table_index (keys %indexes) { + for my $expected_index ( @{ $indexes{$table_index} } ) { + + ok ( get_index($table_index, $expected_index), "Got a matching index on $table_index table"); + } +} + # Returns the Constraint object for the specified constraint type, table and # columns from the SQL::Translator schema, or undef if no matching constraint # is found. @@ -289,6 +323,34 @@ sub get_constraint { return undef; # didn't find a matching constraint } +sub get_index { + my ($table_name, $index) = @_; + + my $table = $tschema->get_table($table_name); + + CAND_INDEX: + for my $cand_index ( $table->get_indices ) { + + next CAND_INDEX if $index->{name} && $cand_index->name ne $index->{name} + || $index->{type} && $cand_index->type ne $index->{type}; + + my %idx_fields = map { $_ => 1 } $cand_index->fields; + + for my $field ( @{ $index->{fields} } ) { + next CAND_INDEX unless $idx_fields{$field}; + } + + %idx_fields = map { $_ => 1 } @{$index->{fields}}; + for my $field ( $cand_index->fields) { + next CAND_INDEX unless $idx_fields{$field}; + } + + return $cand_index; + } + + return undef; # No matching idx +} + # Test parameters in a FOREIGN KEY constraint other than columns sub test_fk { my ($expected, $got) = @_;