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