From: Brian Cassidy Date: Fri, 9 Sep 2005 15:39:36 +0000 (+0000) Subject: added test for 2 relationships in the same class to the same other class X-Git-Tag: v0.03001~16 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ccae0573e39c15a598d9640aaf8e6d31fcadc01e;p=dbsrgits%2FDBIx-Class.git added test for 2 relationships in the same class to the same other class --- diff --git a/t/18self_referencial.t b/t/18self_referencial.t new file mode 100644 index 0000000..65af217 --- /dev/null +++ b/t/18self_referencial.t @@ -0,0 +1,38 @@ +use Test::More; + +# this test will check to see if you can have 2 columns +# in the same class pointing at the same other class +# +# example: +# +# +---------+ +--------------+ +# | SelfRef | | SelfRefAlias | +# +---------+ 1-M +--------------+ +# | id |-------| self_ref | --+ +# | name | | alias | --+ +# +---------+ +--------------+ | +# /|\ | +# | | +# +--------------------------------+ +# +# see http://use.perl.org/~LTjake/journal/24876 for the +# issue with CDBI + +plan tests => 5; + +use lib qw( t/lib ); + +use_ok( 'DBICTest' ); + +my $item = DBICTest::SelfRef->find( 1 ); +is( $item->name, 'First', 'proper start item' ); + +my @aliases = $item->aliases; + +is( scalar @aliases, 1, 'proper number of aliases' ); + +my $orig = $aliases[ 0 ]->self_ref; +my $alias = $aliases[ 0 ]->alias; + +is( $orig->name, 'First', 'proper original' ); +is( $alias->name, 'Second', 'proper alias' ); \ No newline at end of file diff --git a/t/lib/DBICTest.pm b/t/lib/DBICTest.pm index 525703e..09a05b6 100755 --- a/t/lib/DBICTest.pm +++ b/t/lib/DBICTest.pm @@ -36,6 +36,12 @@ CREATE TABLE fourkeys (foo INTEGER NOT NULL, bar INTEGER NOT NULL, CREATE TABLE onekey (id INTEGER NOT NULL PRIMARY KEY, artist INTEGER NOT NULL, cd INTEGER NOT NULL ); +CREATE TABLE self_ref (id INTEGER NOT NULL PRIMARY KEY, + name VARCHAR ); + +CREATE TABLE self_ref_alias (self_ref INTEGER NOT NULL, alias INTEGER NOT NULL, + PRIMARY KEY( self_ref, alias ) ); + INSERT INTO artist (artistid, name) VALUES (1, 'Caterwauler McCrae'); INSERT INTO artist (artistid, name) VALUES (2, 'Random Boy Band'); @@ -99,6 +105,12 @@ INSERT INTO onekey (id, artist, cd) VALUES (1, 1, 1); INSERT INTO onekey (id, artist, cd) VALUES (2, 1, 2); INSERT INTO onekey (id, artist, cd) VALUES (3, 2, 2); + +INSERT INTO self_ref(id, name) VALUES (1, 'First'); + +INSERT INTO self_ref(id, name) VALUES (2, 'Second'); + +INSERT INTO self_ref_alias(self_ref, alias) VALUES (1, 2); EOSQL $dbh->do($_) for split(/\n\n/, $sql); diff --git a/t/lib/DBICTest/Schema.pm b/t/lib/DBICTest/Schema.pm index 4c46a54..2a94ce9 100644 --- a/t/lib/DBICTest/Schema.pm +++ b/t/lib/DBICTest/Schema.pm @@ -3,6 +3,6 @@ package DBICTest::Schema; use base qw/DBIx::Class::Schema/; __PACKAGE__->load_classes(qw/ - Artist CD Track Tag LinerNotes OneKey TwoKeys FourKeys/); + Artist CD Track Tag LinerNotes OneKey TwoKeys FourKeys SelfRef SelfRefAlias /); 1; diff --git a/t/lib/DBICTest/Schema/SelfRef.pm b/t/lib/DBICTest/Schema/SelfRef.pm new file mode 100644 index 0000000..f51d97e --- /dev/null +++ b/t/lib/DBICTest/Schema/SelfRef.pm @@ -0,0 +1,14 @@ +package DBICTest::Schema::SelfRef; + +use base 'DBIx::Class::Core'; + +__PACKAGE__->table('self_ref'); +__PACKAGE__->add_columns(qw/id name/); +__PACKAGE__->set_primary_key('id'); +__PACKAGE__->add_relationship( + aliases => 'DBICTest::Schema::SelfRefAlias', + { 'foreign.self_ref' => 'self.id' }, + { accessor => 'multi' } +); + +1; diff --git a/t/lib/DBICTest/Schema/SelfRefAlias.pm b/t/lib/DBICTest/Schema/SelfRefAlias.pm new file mode 100644 index 0000000..785fcc6 --- /dev/null +++ b/t/lib/DBICTest/Schema/SelfRefAlias.pm @@ -0,0 +1,20 @@ +package DBICTest::Schema::SelfRefAlias; + +use base 'DBIx::Class::Core'; + +__PACKAGE__->table('self_ref_alias'); +__PACKAGE__->add_columns(qw/self_ref alias/); +__PACKAGE__->set_primary_key('self_ref alias'); +__PACKAGE__->add_relationship( + self_ref => 'DBICTest::Schema::SelfRef', + { 'foreign.id' => 'self.self_ref' }, + { accessor => 'single' } + +); +__PACKAGE__->add_relationship( + alias => 'DBICTest::Schema::SelfRef', + { 'foreign.id' => 'self.alias' }, + { accessor => 'single' } +); + +1;