From: Andy Grundman Date: Mon, 1 Aug 2005 19:12:46 +0000 (+0000) Subject: Added tests for add_relationship, search_related, create_related X-Git-Tag: v0.03001~110 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=256c505cfce8ca987ea281f69aca379681c03aa4;p=dbsrgits%2FDBIx-Class.git Added tests for add_relationship, search_related, create_related --- diff --git a/t/06relationship.t b/t/06relationship.t new file mode 100644 index 0000000..7a28841 --- /dev/null +++ b/t/06relationship.t @@ -0,0 +1,42 @@ +use Test::More; + +plan tests => 7; + +use lib qw(t/lib); + +use_ok('DBICTest'); + +# has_a test +my $cd = DBICTest::CD->retrieve(4); +my ($artist) = $cd->search_related('artist'); +is($artist->name, 'Random Boy Band', 'has_a search_related ok'); + +# has_many test with an order_by clause defined +$artist = DBICTest::Artist->retrieve(1); +is( ($artist->search_related('cds'))[1]->title, 'Spoonful of bees', 'has_many search_related with order_by ok' ); + +# search_related with additional abstract query +my @cds = $artist->search_related('cds', { title => { like => '%of%' } } ); +is( $cds[1]->title, 'Forkful of bees', 'search_related with abstract query ok' ); + +# creating a related object +$artist->create_related( 'cds', { + title => 'Big Flop', + year => 2005, +} ); +is( ($artist->search_related('cds'))[3]->title, 'Big Flop', 'create_related ok' ); + +# try to add a bogus relationship using the wrong cols +eval { + $artist->add_relationship( + tracks => 'DBICTest::Track', + { 'foreign.cd' => 'self.cdid' } + ); +}; +like($@, qr/no such accessor/, 'failed when creating a rel with invalid key, ok'); + +# another bogus relationship using no join condition +eval { + $artist->add_relationship( tracks => 'DBICTest::Track' ); +}; +like($@, qr/no join condition/, 'failed when creating a rel without join condition, ok'); diff --git a/t/lib/DBICTest.pm b/t/lib/DBICTest.pm index a7ba0cb..f3418c4 100755 --- a/t/lib/DBICTest.pm +++ b/t/lib/DBICTest.pm @@ -120,7 +120,10 @@ use base 'DBICTest'; DBICTest::Tag->table('tags'); DBICTest::Tag->add_columns(qw/tagid cd tag/); DBICTest::Tag->set_primary_key('tagid'); -#DBICTest::Tag->has_a(cd => 'SweetTest::CD'); +DBICTest::Tag->add_relationship( + cd => 'DBICTest::CD', + { 'foreign.cdid' => 'self.cd' } +); package DBICTest::Track; @@ -129,7 +132,10 @@ use base 'DBICTest'; DBICTest::Track->table('track'); DBICTest::Track->add_columns(qw/trackid cd position title/); DBICTest::Track->set_primary_key('trackid'); -#DBICTest::Track->has_a(cd => 'SweetTest::CD'); +DBICTest::Track->add_relationship( + cd => 'DBICTest::CD', + { 'foreign.cdid' => 'self.cd' } +); package DBICTest::CD; @@ -137,13 +143,20 @@ use base 'DBICTest'; DBICTest::CD->table('cd'); DBICTest::CD->add_columns(qw/cdid artist title year/); -DBICTest::CD->set_primary_key('trackid'); - -#DBICTest::CD->has_many(tracks => 'SweetTest::Track'); -#DBICTest::CD->has_many(tags => 'SweetTest::Tag'); -#DBICTest::CD->has_a(artist => 'SweetTest::Artist'); - -#DBICTest::CD->might_have(liner_notes => 'SweetTest::LinerNotes' => qw/notes/); +DBICTest::CD->set_primary_key('cdid'); +DBICTest::CD->add_relationship( + artist => 'DBICTest::Artist', + { 'foreign.artistid' => 'self.artist' } +); +DBICTest::CD->add_relationship( + tracks => 'DBICTest::Track', + { 'foreign.cd' => 'self.cdid' } +); +DBICTest::CD->add_relationship( + tags => 'DBICTest::Tag', + { 'foreign.cd' => 'self.cdid' } +); +#DBICTest::CD->might_have(liner_notes => 'DBICTest::LinerNotes' => qw/notes/); package DBICTest::Artist; @@ -152,9 +165,27 @@ use base 'DBICTest'; DBICTest::Artist->table('artist'); DBICTest::Artist->add_columns(qw/artistid name/); DBICTest::Artist->set_primary_key('artistid'); -#DBICTest::Artist->has_many(cds => 'SweetTest::CD'); -#DBICTest::Artist->has_many(twokeys => 'SweetTest::TwoKeys'); -#DBICTest::Artist->has_many(onekeys => 'SweetTest::OneKey'); +DBICTest::Artist->add_relationship( + cds => 'DBICTest::CD', + { 'foreign.artist' => 'self.artistid' }, + { order_by => 'year' } +); +DBICTest::Artist->add_relationship( + twokeys => 'DBICTest::TwoKeys', + { 'foreign.artist' => 'self.artistid' } +); +DBICTest::Artist->add_relationship( + onekeys => 'DBICTest::OneKey', + { 'foreign.artist' => 'self.artistid' } +); + +package DBICTest::OneKey; + +use base 'DBICTest'; + +DBICTest::OneKey->table('onekey'); +DBICTest::OneKey->add_columns(qw/id artist cd/); +DBICTest::OneKey->set_primary_key('id'); package DBICTest::TwoKeys; @@ -163,8 +194,14 @@ use base 'DBICTest'; DBICTest::TwoKeys->table('twokeys'); DBICTest::TwoKeys->add_columns(qw/artist cd/); DBICTest::TwoKeys->set_primary_key(qw/artist cd/); -#DBICTest::TwoKeys->has_a(artist => 'SweetTest::Artist'); -#DBICTest::TwoKeys->has_a(cd => 'SweetTest::CD'); +DBICTest::TwoKeys->add_relationship( + artist => 'DBICTest::Artist', + { 'foreign.artistid' => 'self.artist' } +); +DBICTest::TwoKeys->add_relationship( + cd => 'DBICTest::CD', + { 'foreign.cdid' => 'self.cd' } +); package DBICTest::FourKeys; @@ -174,12 +211,4 @@ DBICTest::FourKeys->table('fourkeys'); DBICTest::FourKeys->add_columns(qw/foo bar hello goodbye/); DBICTest::FourKeys->set_primary_key(qw/foo bar hello goodbye/); -package DBICTest::OneKey; - -use base 'DBICTest'; - -DBICTest::OneKey->table('onekey'); -DBICTest::OneKey->add_columns(qw/id artist cd/); -DBICTest::OneKey->set_primary_key('id'); - 1;