From: Dagfinn Ilmari Mannsåker Date: Sat, 26 Jul 2014 12:32:53 +0000 (+0100) Subject: Fix and extend link table tests X-Git-Tag: v0.11019~13 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=21e3be0cd37c1904b15904eb4ae3258f4f08fb9e;p=dbsrgits%2FSQL-Translator.git Fix and extend link table tests --- diff --git a/t/13schema.t b/t/13schema.t index 3cbd633..c42dd74 100644 --- a/t/13schema.t +++ b/t/13schema.t @@ -746,7 +746,7 @@ require_ok( 'SQL::Translator::Schema' ); } # -# Test table dataness/triviality +# Test link tables # { @@ -756,31 +756,45 @@ require_ok( 'SQL::Translator::Schema' ); $t1->primary_key( 'id' ); $t1->add_field( name => 'name' ); + ok( $t1->is_data, 'Person table has data' ); + ok( !$t1->is_trivial_link, 'Person table is not trivial' ); + my $t2 = $s->add_table( name => 'pet' ); $t2->add_field( name => 'id' ); $t2->primary_key( 'id' ); $t2->add_field( name => 'name' ); + ok( $t2->is_data, 'Pet table has data' ); + ok( !$t1->is_trivial_link, 'Pet table is trivial' ); + my $t3 = $s->add_table( name => 'person_pet' ); - $t3->add_field( name => 'person_id' ); - $t3->add_field( name => 'pet_id' ); - $t3->primary_key( 'person_id', 'pet_id' ); + $t3->add_field( name => 'id' ); + my $f1 = $t3->add_field( name => 'person_id' ); + my $f2 = $t3->add_field( name => 'pet_id' ); + $t3->primary_key( 'id' ); $t3->add_constraint( type => FOREIGN_KEY, - field => 'person_id', + fields => 'person_id', reference_table => $t1, ); $t3->add_constraint( type => FOREIGN_KEY, - field => 'pet_id', + fields => 'pet_id', reference_table => $t2, ); + ok( $f1->is_foreign_key, "person_id is foreign key" ); + ok( $f2->is_foreign_key, "pet_id is foreign key" ); + + ok( !$t3->is_data, 'Link table has no data' ); + ok( $t3->is_trivial_link, 'Link table is trivial' ); + is( $t3->can_link($t1, $t2)->[0], 'one2one', 'Link table can link' ); + my $t4 = $s->add_table( name => 'fans' ); - my $f1 = $t4->add_field( name => 'fan_id' ); - my $f2 = $t4->add_field( name => 'idol_id' ); + my $f3 = $t4->add_field( name => 'fan_id' ); + my $f4 = $t4->add_field( name => 'idol_id' ); $t4->primary_key( 'fan_id', 'idol_id' ); $t4->add_constraint( @@ -797,17 +811,13 @@ require_ok( 'SQL::Translator::Schema' ); reference_table => $t1, ); - ok( $t1->is_data, 'Person table has data' ); - ok( !$t1->is_trivial_link, 'Person table is not trivial' ); - - ok( $t2->is_data, 'Pet table has data' ); - ok( !$t1->is_trivial_link, 'Pet table is trivial' ); - - ok( !$t3->is_data, 'Link table has no data' ); - ok( $t3->is_trivial_link, 'Link table is trivial' ); + ok( $f3->is_foreign_key, "fan_id is foreign key" ); + ok( $f4->is_foreign_key, "idol_id is foreign key" ); ok( !$t4->is_data, 'Self-link table has no data' ); ok( !$t4->is_trivial_link, 'Self-link table is not trivial' ); + is( $t4->can_link($t1, $t1)->[0], 'many2many', 'Self-link table can link' ); + ok( !$t4->can_link($t1, $t2)->[0], 'Self-link table can\'t link other' ); } done_testing;