added has_relationship_loaded skeleton method and some tests
Alexander Hartmaier [Fri, 16 Mar 2012 11:39:04 +0000 (12:39 +0100)]
lib/DBIx/Class/Row.pm
t/row/has_relationship_loaded.t [new file with mode: 0644]

index 000498a..2cc5739 100644 (file)
@@ -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 (file)
index 0000000..ec4a7e0
--- /dev/null
@@ -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;