added more tests
Alexander Hartmaier [Fri, 16 Mar 2012 13:14:44 +0000 (14:14 +0100)]
t/row/has_relationship_loaded.t

index ec4a7e0..cd55430 100644 (file)
@@ -8,7 +8,7 @@ use DBICTest;
 
 my $schema = DBICTest->init_schema();
 my $rs = $schema->resultset('CD');
-my $row = $rs->first;
+my $row = $rs->new_result({});
 
 dies_ok { $row->has_relationship_loaded() }
   'has_relationship_loaded needs a relationship name';
@@ -16,10 +16,33 @@ dies_ok { $row->has_relationship_loaded() }
 ok !$row->has_relationship_loaded($_), "vanilla row has no loaded relationship '$_'"
   for $row->result_source->relationships;
 
-# Prefetch of single relationship
+# Prefetch of single belongs_to relationship
 {
-  my $prefetched = $rs->search_rs(undef, { prefetch => 'artist' })->first;
-  ok $prefetched->has_relationship_loaded('artist'), 'single prefetch detected by has_relationship_loaded';
+  my $prefetched_rs = $rs->search_rs(undef, { prefetch => 'artist' });
+  my $cd = $prefetched_rs->find(1);
+  ok $cd->has_relationship_loaded('artist'), 'belongs_to relationship with related row detected by has_relationship_loaded';
+}
+
+# Prefetch of single might_have relationship
+{
+  my $prefetched_rs = $rs->search_rs(undef, { prefetch => 'liner_notes' });
+  my $cd_without_liner_notes = $prefetched_rs->find(1);
+  ok $cd_without_liner_notes->has_relationship_loaded('liner_notes'), 'might_have relationship without related row detected by has_relationship_loaded';
+  my $cd_with_liner_notes = $prefetched_rs->find(2);
+  ok $cd_with_liner_notes->has_relationship_loaded('liner_notes'), 'might_have relationship with related row detected by has_relationship_loaded';
+}
+
+# Prefetch of single has_many relationship
+{
+  my $prefetched_rs = $rs->search_rs(undef, { prefetch => 'tracks' });
+  my $cd_without_tracks = $prefetched_rs->create({
+    artist => 1,
+    title  => 'Empty CD',
+    year   => 2012,
+  });
+  ok $cd_without_tracks->has_relationship_loaded('tracks'), 'has_many relationship without related row detected by has_relationship_loaded';
+  my $cd_with_tracks = $prefetched_rs->find(2);
+  ok $cd_with_tracks->has_relationship_loaded('tracks'), 'has_many relationship with related row detected by has_relationship_loaded';
 }
 
 # Prefetch of multiple relationships
@@ -31,9 +54,9 @@ ok !$row->has_relationship_loaded($_), "vanilla row has no loaded relationship '
 
 # 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';
+  my $prefetched = $schema->resultset('Artist')->search_rs(undef, { prefetch => {'cds' => 'tracks'} })->find(1);
+  ok $prefetched->has_relationship_loaded('cds'), 'direct prefetch detected by has_relationship_loaded';
+  ok $prefetched->cds->first->has_relationship_loaded('tracks'), 'nested prefetch detected by has_relationship_loaded';
 }
 
 done_testing;