7 use Data::Dumper::Concise;
8 local $Data::Dumper::Maxdepth = 3;
11 my $schema = DBICTest->init_schema();
12 my $rs = $schema->resultset('CD');
13 my $row = $rs->new_result({});
15 dies_ok { $row->has_relationship_loaded() }
16 'has_relationship_loaded needs a relationship name';
18 ok !$row->has_relationship_loaded($_), "vanilla row has no loaded relationship '$_'"
19 for $row->result_source->relationships;
21 # Prefetch of single belongs_to relationship
23 my $prefetched_rs = $rs->search_rs(undef, { prefetch => 'artist' });
24 my $cd = $prefetched_rs->find(1);
25 ok $cd->has_relationship_loaded('artist'), 'belongs_to relationship with related row detected by has_relationship_loaded';
28 # Prefetch of single might_have relationship
30 my $prefetched_rs = $rs->search_rs(undef, { prefetch => 'liner_notes' });
31 my $cd_without_liner_notes = $prefetched_rs->find(1);
32 ok $cd_without_liner_notes->has_relationship_loaded('liner_notes'), 'might_have relationship without related row detected by has_relationship_loaded';
33 my $cd_with_liner_notes = $prefetched_rs->find(2);
34 ok $cd_with_liner_notes->has_relationship_loaded('liner_notes'), 'might_have relationship with related row detected by has_relationship_loaded';
37 # Prefetch of single has_many relationship
39 my $prefetched_rs = $rs->search_rs(undef, { prefetch => 'tracks' });
40 my $cd_with_tracks = $prefetched_rs->find(2);
41 ok $cd_with_tracks->has_relationship_loaded('tracks'), 'has_many relationship with related rows detected by has_relationship_loaded';
43 # New without related rows
44 my $new_cd_without_tracks = $rs->create({
49 ok !$new_cd_without_tracks->has_relationship_loaded('tracks'), 'has_many relationship without related rows for new object detected by has_relationship_loaded';
51 my $new_cd_with_tracks = $rs->create({
53 title => 'Non-empty CD',
58 title => 'first track',
62 title => 'second track',
67 ok $new_cd_with_tracks->has_relationship_loaded('tracks'), 'has_many relationship with related rows for new object detected by has_relationship_loaded';
69 my $cd_without_tracks = $prefetched_rs->find($new_cd_without_tracks->id);
70 ok $cd_without_tracks->has_relationship_loaded('tracks'), 'has_many relationship without related rows detected by has_relationship_loaded';
73 # Prefetch of multiple relationships
75 my $prefetched = $rs->search_rs(undef, { prefetch => ['artist', 'tracks'] })->find(1);
76 ok $prefetched->has_relationship_loaded('artist'), 'first prefetch detected by has_relationship_loaded';
77 ok $prefetched->has_relationship_loaded('tracks'), 'second prefetch detected by has_relationship_loaded';
78 ok !$prefetched->tracks->first->has_relationship_loaded('single_cd'), 'nested not prefetched rel detected by has_relationship_loaded';
81 # Prefetch of nested relationships
83 my $prefetched = $schema->resultset('Artist')->search_rs(undef, { prefetch => {'cds' => 'tracks'} })->find(1);
84 ok $prefetched->has_relationship_loaded('cds'), 'direct prefetch detected by has_relationship_loaded';
85 ok $prefetched->cds->first->has_relationship_loaded('tracks'), 'nested prefetch detected by has_relationship_loaded';
86 ok !$prefetched->cds->first->has_relationship_loaded('single_track'), 'nested not prefetched rel detected by has_relationship_loaded';
91 my $cd_with_tracks = $rs->new({
93 title => 'CD with tracks',
98 title => 'first track',
102 title => 'second track',
106 ok !$cd_with_tracks->has_relationship_loaded('artist'), 'multinew: not created rel detected by has_relationship_loaded';
107 ok $cd_with_tracks->has_relationship_loaded('tracks'), 'multinew: created rel detected by has_relationship_loaded';
108 # fails because $cd_with_tracks->tracks->first returns undef
109 # ok !$cd_with_tracks->tracks->first->has_relationship_loaded('cd'), 'multinew: nested not created rel detected by has_relationship_loaded';
114 my $cd_with_tracks = $rs->create({
116 title => 'CD with tracks',
121 title => 'first track',
125 title => 'second track',
129 ok !$cd_with_tracks->has_relationship_loaded('artist'), 'multicreate: not created rel detected by has_relationship_loaded';
130 ok $cd_with_tracks->has_relationship_loaded('tracks'), 'multicreate: created rel detected by has_relationship_loaded';
131 ok !$cd_with_tracks->tracks->first->has_relationship_loaded('cd'), 'multicreate: nested not created rel detected by has_relationship_loaded';