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