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