Explicitly test that prefetched rels get inflated by HRI
[dbsrgits/DBIx-Class.git] / t / inflate / hri.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
b0930c1e 3use strict;
0fcfe456 4use warnings;
b0930c1e 5
0fcfe456 6use Test::More;
f073420b 7
d634850b 8use DBIx::Class::_Util 'modver_gt_or_eq_and_lt';
f073420b 9use base();
10BEGIN {
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 15use Test::Exception;
c0329273 16
b0930c1e 17use DBICTest;
b0930c1e 18my $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
83sub check_cols_of {
6b44e3b0 84 my ($dbic_obj, $datahashref, $prefetch, $prefix) = @_;
85
86 $prefetch ||= [];
87 $prefix ||= '';
88 my %prefetch; @prefetch{@$prefetch} = ();
9f6555d3 89
b0930c1e 90 foreach my $col (keys %$datahashref) {
91 # plain column
92 if (not ref ($datahashref->{$col}) ) {
6b44e3b0 93 is ($datahashref->{$col}, $dbic_obj->get_column($col), "value for $prefix$col");
b0930c1e 94 }
95 # related table entry (belongs_to)
96 elsif (ref ($datahashref->{$col}) eq 'HASH') {
6b44e3b0 97 delete $prefetch{$col};
98 check_cols_of($dbic_obj->$col, $datahashref->{$col}, [], "$col.");
b0930c1e 99 }
100 # multiple related entries (has_many)
101 elsif (ref ($datahashref->{$col}) eq 'ARRAY') {
6b44e3b0 102 delete $prefetch{$col};
b0930c1e 103 my @dbic_reltable = $dbic_obj->$col;
104 my @hashref_reltable = @{$datahashref->{$col}};
9f6555d3 105
6b44e3b0 106 is (scalar @hashref_reltable, scalar @dbic_reltable, "number of related $col");
b0930c1e 107
108 # for my $index (0..scalar @hashref_reltable) {
109 for my $index (0..scalar @dbic_reltable) {
110 my $dbic_reltable_obj = $dbic_reltable[$index];
111 my $hashref_reltable_entry = $hashref_reltable[$index];
9f6555d3 112
6b44e3b0 113 check_cols_of($dbic_reltable_obj, $hashref_reltable_entry, [], "$col\[$index\].");
b0930c1e 114 }
115 }
116 }
6b44e3b0 117 if (@$prefetch) {
118 is 0+(keys %prefetch), 0, "prefetched " . join ", ", @$prefetch
119 or diag "missed: " . join ", ", keys %prefetch;
120 }
b0930c1e 121}
122
123# create a cd without tracks for testing empty has_many relationship
124$schema->resultset('CD')->create({ title => 'Silence is golden', artist => 3, year => 2006 });
125
126# order_by to ensure both resultsets have the rows in the same order
c5bc9ba6 127# also check result_class-as-an-attribute syntax
b0930c1e 128my $rs_dbic = $schema->resultset('CD')->search(undef,
129 {
130 prefetch => [ qw/ artist tracks / ],
131 order_by => [ 'me.cdid', 'tracks.position' ],
132 }
133);
134my $rs_hashrefinf = $schema->resultset('CD')->search(undef,
135 {
136 prefetch => [ qw/ artist tracks / ],
137 order_by => [ 'me.cdid', 'tracks.position' ],
c5bc9ba6 138 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
b0930c1e 139 }
140);
b0930c1e 141
142my @dbic = $rs_dbic->all;
143my @hashrefinf = $rs_hashrefinf->all;
144
2328814a 145for my $index (0 .. $#hashrefinf) {
b0930c1e 146 my $dbic_obj = $dbic[$index];
147 my $datahashref = $hashrefinf[$index];
148
6b44e3b0 149 check_cols_of($dbic_obj, $datahashref, [qw(artist tracks)]);
b0930c1e 150}
2328814a 151
152# sometimes for ultra-mega-speed you want to fetch columns in esoteric ways
8273e845 153# check the inflator over a non-fetching join
2328814a 154$rs_dbic = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, {
155 prefetch => { cds => 'tracks' },
156 order_by => [qw/cds.cdid tracks.trackid/],
157});
158
159$rs_hashrefinf = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, {
160 join => { cds => 'tracks' },
161 select => [qw/name tracks.title tracks.cd /],
162 as => [qw/name cds.tracks.title cds.tracks.cd /],
163 order_by => [qw/cds.cdid tracks.trackid/],
c5bc9ba6 164 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
2328814a 165});
2328814a 166
167@dbic = map { $_->tracks->all } ($rs_dbic->first->cds->all);
168@hashrefinf = $rs_hashrefinf->all;
169
170is (scalar @dbic, scalar @hashrefinf, 'Equal number of tracks fetched');
171
172for my $index (0 .. $#hashrefinf) {
173 my $track = $dbic[$index];
174 my $datahashref = $hashrefinf[$index];
175
176 is ($track->cd->artist->name, $datahashref->{name}, 'Brought back correct artist');
9f6555d3 177 for my $col (keys %{$datahashref->{cds}{tracks}}) {
178 is ($track->get_column ($col), $datahashref->{cds}{tracks}{$col}, "Correct track '$col'");
2328814a 179 }
180}
584e74ed 181
182# check for same query as above but using extended columns syntax
183$rs_hashrefinf = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, {
184 join => { cds => 'tracks' },
185 columns => {name => 'name', 'cds.tracks.title' => 'tracks.title', 'cds.tracks.cd' => 'tracks.cd'},
186 order_by => [qw/cds.cdid tracks.trackid/],
187});
188$rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator');
189is_deeply [$rs_hashrefinf->all], \@hashrefinf, 'Check query using extended columns syntax';
0f2aa8f2 190
191# check nested prefetching of has_many relationships which return nothing
192my $artist = $schema->resultset ('Artist')->create ({ name => 'unsuccessful artist without CDs'});
e8b32a6d 193$artist->discard_changes;
0f2aa8f2 194my $rs_artists = $schema->resultset ('Artist')->search ({ 'me.artistid' => $artist->id}, {
e8b32a6d 195 prefetch => { cds => 'tracks' }, result_class => 'DBIx::Class::ResultClass::HashRefInflator',
0f2aa8f2 196});
e8b32a6d 197is_deeply(
198 [$rs_artists->all],
199 [{ $artist->get_columns, cds => [] }],
200 'nested has_many prefetch without entries'
201);
0fcfe456 202
203done_testing;
c9d29bb2 204