Commit | Line | Data |
38e29e8a |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use lib qw(t/lib); |
5 | use Test::More; |
6 | use Test::Exception; |
56b34078 |
7 | use Data::Dumper::Concise; |
8 | local $Data::Dumper::Maxdepth = 3; |
38e29e8a |
9 | use DBICTest; |
10 | |
11 | my $schema = DBICTest->init_schema(); |
12 | my $rs = $schema->resultset('CD'); |
164e5ebd |
13 | my $row = $rs->new_result({}); |
38e29e8a |
14 | |
15 | dies_ok { $row->has_relationship_loaded() } |
16 | 'has_relationship_loaded needs a relationship name'; |
17 | |
18 | ok !$row->has_relationship_loaded($_), "vanilla row has no loaded relationship '$_'" |
19 | for $row->result_source->relationships; |
20 | |
164e5ebd |
21 | # Prefetch of single belongs_to relationship |
38e29e8a |
22 | { |
164e5ebd |
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'; |
26 | } |
27 | |
28 | # Prefetch of single might_have relationship |
29 | { |
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'; |
35 | } |
36 | |
37 | # Prefetch of single has_many relationship |
38 | { |
39 | my $prefetched_rs = $rs->search_rs(undef, { prefetch => 'tracks' }); |
56b34078 |
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'; |
42 | |
43 | # New without related rows |
44 | my $new_cd_without_tracks = $rs->create({ |
164e5ebd |
45 | artist => 1, |
46 | title => 'Empty CD', |
47 | year => 2012, |
48 | }); |
56b34078 |
49 | ok !$new_cd_without_tracks->has_relationship_loaded('tracks'), 'has_many relationship without related rows for new object detected by has_relationship_loaded'; |
50 | |
51 | my $new_cd_with_tracks = $rs->create({ |
52 | artist => 1, |
53 | title => 'Non-empty CD', |
54 | year => 2012, |
55 | tracks => [ |
56 | { |
57 | position => 1, |
58 | title => 'first track', |
59 | }, |
60 | { |
61 | position => 2, |
62 | title => 'second track', |
63 | }, |
64 | ], |
65 | }); |
66 | |
67 | ok $new_cd_with_tracks->has_relationship_loaded('tracks'), 'has_many relationship with related rows for new object detected by has_relationship_loaded'; |
68 | |
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'; |
38e29e8a |
71 | } |
72 | |
73 | # Prefetch of multiple relationships |
74 | { |
c9b544e4 |
75 | my $prefetched = $rs->search_rs(undef, { prefetch => ['artist', 'tracks'] })->find(1); |
38e29e8a |
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'; |
c9b544e4 |
78 | ok !$prefetched->tracks->first->has_relationship_loaded('single_cd'), 'nested not prefetched rel detected by has_relationship_loaded'; |
38e29e8a |
79 | } |
80 | |
81 | # Prefetch of nested relationships |
82 | { |
164e5ebd |
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'; |
c9b544e4 |
86 | ok !$prefetched->cds->first->has_relationship_loaded('single_track'), 'nested not prefetched rel detected by has_relationship_loaded'; |
87 | } |
88 | |
89 | # Multinew |
90 | { |
91 | my $cd_with_tracks = $rs->new({ |
92 | artist => 1, |
93 | title => 'CD with tracks', |
94 | year => 2012, |
95 | tracks => [ |
96 | { |
97 | position => 1, |
98 | title => 'first track', |
99 | }, |
100 | { |
101 | position => 2, |
102 | title => 'second track', |
103 | }, |
104 | ], |
105 | }); |
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'; |
110 | } |
111 | |
112 | # Multicreate |
113 | { |
114 | my $cd_with_tracks = $rs->create({ |
115 | artist => 1, |
116 | title => 'CD with tracks', |
117 | year => 2012, |
118 | tracks => [ |
119 | { |
120 | position => 1, |
121 | title => 'first track', |
122 | }, |
123 | { |
124 | position => 2, |
125 | title => 'second track', |
126 | }, |
127 | ], |
128 | }); |
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'; |
38e29e8a |
132 | } |
133 | |
134 | done_testing; |