Commit | Line | Data |
b0930c1e |
1 | use strict; |
0fcfe456 |
2 | use warnings; |
b0930c1e |
3 | |
0fcfe456 |
4 | use Test::More; |
f073420b |
5 | |
6 | use DBIx::Class::_Util 'modver_gt_or_eq'; |
7 | use base(); |
8 | BEGIN { |
9 | plan skip_all => 'base.pm 2.20 (only present in perl 5.19.7) is known to break this test' |
10 | if modver_gt_or_eq(base => '2.19_01') and ! modver_gt_or_eq(base => '2.21'); |
11 | } |
12 | |
60eb6547 |
13 | use Test::Exception; |
b0930c1e |
14 | use lib qw(t/lib); |
15 | use DBICTest; |
b0930c1e |
16 | my $schema = DBICTest->init_schema(); |
17 | |
b0930c1e |
18 | # Under some versions of SQLite if the $rs is left hanging around it will lock |
19 | # So we create a scope here cos I'm lazy |
20 | { |
331e41cf |
21 | my $rs = $schema->resultset('CD')->search ({}, { |
22 | order_by => 'cdid', |
331e41cf |
23 | }); |
b0930c1e |
24 | |
60eb6547 |
25 | my $orig_resclass = $rs->result_class; |
26 | eval "package DBICTest::CDSubclass; use base '$orig_resclass'"; |
b0930c1e |
27 | |
60eb6547 |
28 | # override on a specific $rs object, should not chain |
29 | $rs->result_class ('DBICTest::CDSubclass'); |
b0930c1e |
30 | |
60eb6547 |
31 | my $cd = $rs->find ({cdid => 1}); |
32 | is (ref $cd, 'DBICTest::CDSubclass', 'result_class override propagates to find'); |
b0930c1e |
33 | |
60eb6547 |
34 | $cd = $rs->search({ cdid => 1 })->single; |
35 | is (ref $cd, $orig_resclass, 'result_class override does not propagate over seach+single'); |
b0930c1e |
36 | |
60eb6547 |
37 | $cd = $rs->search()->find ({ cdid => 1 }); |
38 | is (ref $cd, $orig_resclass, 'result_class override does not propagate over seach+find'); |
efc8ae6e |
39 | |
60eb6547 |
40 | # set as attr - should propagate |
41 | my $hri_rs = $rs->search ({}, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' }); |
42 | is ($rs->result_class, 'DBICTest::CDSubclass', 'original class unchanged'); |
43 | is ($hri_rs->result_class, 'DBIx::Class::ResultClass::HashRefInflator', 'result_class accessor pre-set via attribute'); |
331e41cf |
44 | |
60eb6547 |
45 | my $datahashref1 = $hri_rs->next; |
46 | is_deeply( |
47 | [ sort keys %$datahashref1 ], |
48 | [ sort $rs->result_source->columns ], |
49 | 'returned correct columns', |
50 | ); |
704a0f8e |
51 | $hri_rs->reset; |
60eb6547 |
52 | |
53 | $cd = $hri_rs->find ({cdid => 1}); |
54 | is_deeply ( $cd, $datahashref1, 'first/find return the same thing (result_class attr propagates)'); |
55 | |
56 | $cd = $hri_rs->search({ cdid => 1 })->single; |
57 | is_deeply ( $cd, $datahashref1, 'first/search+single return the same thing (result_class attr propagates)'); |
331e41cf |
58 | |
60eb6547 |
59 | $hri_rs->result_class ('DBIx::Class::Row'); # something bogus |
331e41cf |
60 | is( |
60eb6547 |
61 | $hri_rs->search->result_class, 'DBIx::Class::ResultClass::HashRefInflator', |
62 | 'result_class set using accessor does not propagate over unused search' |
331e41cf |
63 | ); |
64 | |
60eb6547 |
65 | # test result class auto-loading |
66 | throws_ok ( |
67 | sub { $rs->result_class ('nonexsitant_bogus_class') }, |
68 | qr/Can't locate nonexsitant_bogus_class.pm/, |
69 | 'Attempt to load on accessor override', |
70 | ); |
71 | is ($rs->result_class, 'DBICTest::CDSubclass', 'class unchanged'); |
72 | |
73 | throws_ok ( |
74 | sub { $rs->search ({}, { result_class => 'nonexsitant_bogus_class' }) }, |
75 | qr/Can't locate nonexsitant_bogus_class.pm/, |
76 | 'Attempt to load on accessor override', |
77 | ); |
78 | is ($rs->result_class, 'DBICTest::CDSubclass', 'class unchanged'); |
0fcfe456 |
79 | } |
b0930c1e |
80 | |
81 | sub check_cols_of { |
82 | my ($dbic_obj, $datahashref) = @_; |
9f6555d3 |
83 | |
b0930c1e |
84 | foreach my $col (keys %$datahashref) { |
85 | # plain column |
86 | if (not ref ($datahashref->{$col}) ) { |
87 | is ($datahashref->{$col}, $dbic_obj->get_column($col), 'same value'); |
88 | } |
89 | # related table entry (belongs_to) |
90 | elsif (ref ($datahashref->{$col}) eq 'HASH') { |
91 | check_cols_of($dbic_obj->$col, $datahashref->{$col}); |
92 | } |
93 | # multiple related entries (has_many) |
94 | elsif (ref ($datahashref->{$col}) eq 'ARRAY') { |
95 | my @dbic_reltable = $dbic_obj->$col; |
96 | my @hashref_reltable = @{$datahashref->{$col}}; |
9f6555d3 |
97 | |
b0930c1e |
98 | is (scalar @hashref_reltable, scalar @dbic_reltable, 'number of related entries'); |
99 | |
100 | # for my $index (0..scalar @hashref_reltable) { |
101 | for my $index (0..scalar @dbic_reltable) { |
102 | my $dbic_reltable_obj = $dbic_reltable[$index]; |
103 | my $hashref_reltable_entry = $hashref_reltable[$index]; |
9f6555d3 |
104 | |
b0930c1e |
105 | check_cols_of($dbic_reltable_obj, $hashref_reltable_entry); |
106 | } |
107 | } |
108 | } |
109 | } |
110 | |
111 | # create a cd without tracks for testing empty has_many relationship |
112 | $schema->resultset('CD')->create({ title => 'Silence is golden', artist => 3, year => 2006 }); |
113 | |
114 | # order_by to ensure both resultsets have the rows in the same order |
c5bc9ba6 |
115 | # also check result_class-as-an-attribute syntax |
b0930c1e |
116 | my $rs_dbic = $schema->resultset('CD')->search(undef, |
117 | { |
118 | prefetch => [ qw/ artist tracks / ], |
119 | order_by => [ 'me.cdid', 'tracks.position' ], |
120 | } |
121 | ); |
122 | my $rs_hashrefinf = $schema->resultset('CD')->search(undef, |
123 | { |
124 | prefetch => [ qw/ artist tracks / ], |
125 | order_by => [ 'me.cdid', 'tracks.position' ], |
c5bc9ba6 |
126 | result_class => 'DBIx::Class::ResultClass::HashRefInflator', |
b0930c1e |
127 | } |
128 | ); |
b0930c1e |
129 | |
130 | my @dbic = $rs_dbic->all; |
131 | my @hashrefinf = $rs_hashrefinf->all; |
132 | |
2328814a |
133 | for my $index (0 .. $#hashrefinf) { |
b0930c1e |
134 | my $dbic_obj = $dbic[$index]; |
135 | my $datahashref = $hashrefinf[$index]; |
136 | |
137 | check_cols_of($dbic_obj, $datahashref); |
138 | } |
2328814a |
139 | |
140 | # sometimes for ultra-mega-speed you want to fetch columns in esoteric ways |
8273e845 |
141 | # check the inflator over a non-fetching join |
2328814a |
142 | $rs_dbic = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, { |
143 | prefetch => { cds => 'tracks' }, |
144 | order_by => [qw/cds.cdid tracks.trackid/], |
145 | }); |
146 | |
147 | $rs_hashrefinf = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, { |
148 | join => { cds => 'tracks' }, |
149 | select => [qw/name tracks.title tracks.cd /], |
150 | as => [qw/name cds.tracks.title cds.tracks.cd /], |
151 | order_by => [qw/cds.cdid tracks.trackid/], |
c5bc9ba6 |
152 | result_class => 'DBIx::Class::ResultClass::HashRefInflator', |
2328814a |
153 | }); |
2328814a |
154 | |
155 | @dbic = map { $_->tracks->all } ($rs_dbic->first->cds->all); |
156 | @hashrefinf = $rs_hashrefinf->all; |
157 | |
158 | is (scalar @dbic, scalar @hashrefinf, 'Equal number of tracks fetched'); |
159 | |
160 | for my $index (0 .. $#hashrefinf) { |
161 | my $track = $dbic[$index]; |
162 | my $datahashref = $hashrefinf[$index]; |
163 | |
164 | is ($track->cd->artist->name, $datahashref->{name}, 'Brought back correct artist'); |
9f6555d3 |
165 | for my $col (keys %{$datahashref->{cds}{tracks}}) { |
166 | is ($track->get_column ($col), $datahashref->{cds}{tracks}{$col}, "Correct track '$col'"); |
2328814a |
167 | } |
168 | } |
584e74ed |
169 | |
170 | # check for same query as above but using extended columns syntax |
171 | $rs_hashrefinf = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, { |
172 | join => { cds => 'tracks' }, |
173 | columns => {name => 'name', 'cds.tracks.title' => 'tracks.title', 'cds.tracks.cd' => 'tracks.cd'}, |
174 | order_by => [qw/cds.cdid tracks.trackid/], |
175 | }); |
176 | $rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator'); |
177 | is_deeply [$rs_hashrefinf->all], \@hashrefinf, 'Check query using extended columns syntax'; |
0f2aa8f2 |
178 | |
179 | # check nested prefetching of has_many relationships which return nothing |
180 | my $artist = $schema->resultset ('Artist')->create ({ name => 'unsuccessful artist without CDs'}); |
e8b32a6d |
181 | $artist->discard_changes; |
0f2aa8f2 |
182 | my $rs_artists = $schema->resultset ('Artist')->search ({ 'me.artistid' => $artist->id}, { |
e8b32a6d |
183 | prefetch => { cds => 'tracks' }, result_class => 'DBIx::Class::ResultClass::HashRefInflator', |
0f2aa8f2 |
184 | }); |
e8b32a6d |
185 | is_deeply( |
186 | [$rs_artists->all], |
187 | [{ $artist->get_columns, cds => [] }], |
188 | 'nested has_many prefetch without entries' |
189 | ); |
0fcfe456 |
190 | |
191 | done_testing; |
c9d29bb2 |
192 | |