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