From: Alexander Hartmaier Date: Fri, 16 Mar 2012 11:39:04 +0000 (+0100) Subject: added has_relationship_loaded skeleton method and some tests X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=38e29e8a4f9dc755af31c591292cb5a2ab72d015;p=dbsrgits%2FDBIx-Class.git added has_relationship_loaded skeleton method and some tests --- diff --git a/lib/DBIx/Class/Row.pm b/lib/DBIx/Class/Row.pm index 000498a..2cc5739 100644 --- a/lib/DBIx/Class/Row.pm +++ b/lib/DBIx/Class/Row.pm @@ -696,6 +696,27 @@ sub has_column_loaded { return exists $self->{_column_data}{$column}; } +=head2 has_relationship_loaded + +=over 4 + +=item Arguments: $relationship_name + +=item Return Value: true, if the relationship has been loaded. + +=back + +=cut + +sub has_relationship_loaded { + my ($self, $rel) = @_; + + $self->throw_exception( "has_relationship_loaded needs a relationship name" ) + unless defined $rel; + + return 0; +} + =head2 get_columns my %data = $result->get_columns; diff --git a/t/row/has_relationship_loaded.t b/t/row/has_relationship_loaded.t new file mode 100644 index 0000000..ec4a7e0 --- /dev/null +++ b/t/row/has_relationship_loaded.t @@ -0,0 +1,39 @@ +use strict; +use warnings; + +use lib qw(t/lib); +use Test::More; +use Test::Exception; +use DBICTest; + +my $schema = DBICTest->init_schema(); +my $rs = $schema->resultset('CD'); +my $row = $rs->first; + +dies_ok { $row->has_relationship_loaded() } + 'has_relationship_loaded needs a relationship name'; + +ok !$row->has_relationship_loaded($_), "vanilla row has no loaded relationship '$_'" + for $row->result_source->relationships; + +# Prefetch of single relationship +{ + my $prefetched = $rs->search_rs(undef, { prefetch => 'artist' })->first; + ok $prefetched->has_relationship_loaded('artist'), 'single prefetch detected by has_relationship_loaded'; +} + +# Prefetch of multiple relationships +{ + my $prefetched = $rs->search_rs(undef, { prefetch => ['artist', 'tracks'] })->first; + ok $prefetched->has_relationship_loaded('artist'), 'first prefetch detected by has_relationship_loaded'; + ok $prefetched->has_relationship_loaded('tracks'), 'second prefetch detected by has_relationship_loaded'; +} + +# Prefetch of nested relationships +{ + my $prefetched = $rs->search_rs(undef, { prefetch => {'artist' => 'artwork_to_artist'} })->first; + ok $prefetched->has_relationship_loaded('artist'), 'direct prefetch detected by has_relationship_loaded'; + ok $prefetched->artist->has_relationship_loaded('artwork_to_artist'), 'nested prefetch detected by has_relationship_loaded'; +} + +done_testing;