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