Fix find on resultset with custom result_class
[dbsrgits/DBIx-Class.git] / t / inflate / hri.t
CommitLineData
b0930c1e 1use strict;
0fcfe456 2use warnings;
b0930c1e 3
0fcfe456 4use Test::More;
b0930c1e 5use lib qw(t/lib);
6use DBICTest;
b0930c1e 7my $schema = DBICTest->init_schema();
8
b0930c1e 9# Under some versions of SQLite if the $rs is left hanging around it will lock
10# So we create a scope here cos I'm lazy
11{
0fcfe456 12 my $rs = $schema->resultset('CD')->search ({}, { order_by => 'cdid' });
b0930c1e 13
14 # get the defined columns
15 my @dbic_cols = sort $rs->result_source->columns;
16
17 # use the hashref inflator class as result class
18 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
19
20 # fetch first record
21 my $datahashref1 = $rs->first;
22
23 my @hashref_cols = sort keys %$datahashref1;
24
25 is_deeply( \@dbic_cols, \@hashref_cols, 'returned columns' );
b0930c1e 26
0fcfe456 27 my $cd1 = $rs->find ({cdid => 1});
28 is_deeply ( $cd1, $datahashref1, 'first/find return the same thing');
29}
b0930c1e 30
31sub check_cols_of {
32 my ($dbic_obj, $datahashref) = @_;
33
34 foreach my $col (keys %$datahashref) {
35 # plain column
36 if (not ref ($datahashref->{$col}) ) {
37 is ($datahashref->{$col}, $dbic_obj->get_column($col), 'same value');
38 }
39 # related table entry (belongs_to)
40 elsif (ref ($datahashref->{$col}) eq 'HASH') {
41 check_cols_of($dbic_obj->$col, $datahashref->{$col});
42 }
43 # multiple related entries (has_many)
44 elsif (ref ($datahashref->{$col}) eq 'ARRAY') {
45 my @dbic_reltable = $dbic_obj->$col;
46 my @hashref_reltable = @{$datahashref->{$col}};
47
48 is (scalar @hashref_reltable, scalar @dbic_reltable, 'number of related entries');
49
50 # for my $index (0..scalar @hashref_reltable) {
51 for my $index (0..scalar @dbic_reltable) {
52 my $dbic_reltable_obj = $dbic_reltable[$index];
53 my $hashref_reltable_entry = $hashref_reltable[$index];
54
55 check_cols_of($dbic_reltable_obj, $hashref_reltable_entry);
56 }
57 }
58 }
59}
60
61# create a cd without tracks for testing empty has_many relationship
62$schema->resultset('CD')->create({ title => 'Silence is golden', artist => 3, year => 2006 });
63
64# order_by to ensure both resultsets have the rows in the same order
c5bc9ba6 65# also check result_class-as-an-attribute syntax
b0930c1e 66my $rs_dbic = $schema->resultset('CD')->search(undef,
67 {
68 prefetch => [ qw/ artist tracks / ],
69 order_by => [ 'me.cdid', 'tracks.position' ],
70 }
71);
72my $rs_hashrefinf = $schema->resultset('CD')->search(undef,
73 {
74 prefetch => [ qw/ artist tracks / ],
75 order_by => [ 'me.cdid', 'tracks.position' ],
c5bc9ba6 76 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
b0930c1e 77 }
78);
b0930c1e 79
80my @dbic = $rs_dbic->all;
81my @hashrefinf = $rs_hashrefinf->all;
82
2328814a 83for my $index (0 .. $#hashrefinf) {
b0930c1e 84 my $dbic_obj = $dbic[$index];
85 my $datahashref = $hashrefinf[$index];
86
87 check_cols_of($dbic_obj, $datahashref);
88}
2328814a 89
90# sometimes for ultra-mega-speed you want to fetch columns in esoteric ways
91# check the inflator over a non-fetching join
92$rs_dbic = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, {
93 prefetch => { cds => 'tracks' },
94 order_by => [qw/cds.cdid tracks.trackid/],
95});
96
97$rs_hashrefinf = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, {
98 join => { cds => 'tracks' },
99 select => [qw/name tracks.title tracks.cd /],
100 as => [qw/name cds.tracks.title cds.tracks.cd /],
101 order_by => [qw/cds.cdid tracks.trackid/],
c5bc9ba6 102 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
2328814a 103});
2328814a 104
105@dbic = map { $_->tracks->all } ($rs_dbic->first->cds->all);
106@hashrefinf = $rs_hashrefinf->all;
107
108is (scalar @dbic, scalar @hashrefinf, 'Equal number of tracks fetched');
109
110for my $index (0 .. $#hashrefinf) {
111 my $track = $dbic[$index];
112 my $datahashref = $hashrefinf[$index];
113
114 is ($track->cd->artist->name, $datahashref->{name}, 'Brought back correct artist');
115 for my $col (keys %{$datahashref->{cds}{tracks}}) {
116 is ($track->get_column ($col), $datahashref->{cds}{tracks}{$col}, "Correct track '$col'");
117 }
118}
584e74ed 119
120# check for same query as above but using extended columns syntax
121$rs_hashrefinf = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, {
122 join => { cds => 'tracks' },
123 columns => {name => 'name', 'cds.tracks.title' => 'tracks.title', 'cds.tracks.cd' => 'tracks.cd'},
124 order_by => [qw/cds.cdid tracks.trackid/],
125});
126$rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator');
127is_deeply [$rs_hashrefinf->all], \@hashrefinf, 'Check query using extended columns syntax';
0f2aa8f2 128
129# check nested prefetching of has_many relationships which return nothing
130my $artist = $schema->resultset ('Artist')->create ({ name => 'unsuccessful artist without CDs'});
e8b32a6d 131$artist->discard_changes;
0f2aa8f2 132my $rs_artists = $schema->resultset ('Artist')->search ({ 'me.artistid' => $artist->id}, {
e8b32a6d 133 prefetch => { cds => 'tracks' }, result_class => 'DBIx::Class::ResultClass::HashRefInflator',
0f2aa8f2 134});
e8b32a6d 135is_deeply(
136 [$rs_artists->all],
137 [{ $artist->get_columns, cds => [] }],
138 'nested has_many prefetch without entries'
139);
0fcfe456 140
141done_testing;