Commit | Line | Data |
b0930c1e |
1 | use strict; |
0fcfe456 |
2 | use warnings; |
b0930c1e |
3 | |
0fcfe456 |
4 | use Test::More; |
60eb6547 |
5 | use Test::Exception; |
b0930c1e |
6 | use lib qw(t/lib); |
7 | use DBICTest; |
b0930c1e |
8 | my $schema = DBICTest->init_schema(); |
9 | |
b0930c1e |
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 | { |
331e41cf |
13 | my $rs = $schema->resultset('CD')->search ({}, { |
14 | order_by => 'cdid', |
331e41cf |
15 | }); |
b0930c1e |
16 | |
60eb6547 |
17 | my $orig_resclass = $rs->result_class; |
18 | eval "package DBICTest::CDSubclass; use base '$orig_resclass'"; |
b0930c1e |
19 | |
60eb6547 |
20 | # override on a specific $rs object, should not chain |
21 | $rs->result_class ('DBICTest::CDSubclass'); |
b0930c1e |
22 | |
60eb6547 |
23 | my $cd = $rs->find ({cdid => 1}); |
24 | is (ref $cd, 'DBICTest::CDSubclass', 'result_class override propagates to find'); |
b0930c1e |
25 | |
60eb6547 |
26 | $cd = $rs->search({ cdid => 1 })->single; |
27 | is (ref $cd, $orig_resclass, 'result_class override does not propagate over seach+single'); |
b0930c1e |
28 | |
60eb6547 |
29 | $cd = $rs->search()->find ({ cdid => 1 }); |
30 | is (ref $cd, $orig_resclass, 'result_class override does not propagate over seach+find'); |
efc8ae6e |
31 | |
60eb6547 |
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'); |
331e41cf |
36 | |
60eb6547 |
37 | my $datahashref1 = $hri_rs->next; |
38 | is_deeply( |
39 | [ sort keys %$datahashref1 ], |
40 | [ sort $rs->result_source->columns ], |
41 | 'returned correct columns', |
42 | ); |
704a0f8e |
43 | $hri_rs->reset; |
60eb6547 |
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)'); |
331e41cf |
50 | |
60eb6547 |
51 | $hri_rs->result_class ('DBIx::Class::Row'); # something bogus |
331e41cf |
52 | is( |
60eb6547 |
53 | $hri_rs->search->result_class, 'DBIx::Class::ResultClass::HashRefInflator', |
54 | 'result_class set using accessor does not propagate over unused search' |
331e41cf |
55 | ); |
56 | |
60eb6547 |
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'); |
0fcfe456 |
71 | } |
b0930c1e |
72 | |
73 | sub check_cols_of { |
74 | my ($dbic_obj, $datahashref) = @_; |
9f6555d3 |
75 | |
b0930c1e |
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}}; |
9f6555d3 |
89 | |
b0930c1e |
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]; |
9f6555d3 |
96 | |
b0930c1e |
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 |
c5bc9ba6 |
107 | # also check result_class-as-an-attribute syntax |
b0930c1e |
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' ], |
c5bc9ba6 |
118 | result_class => 'DBIx::Class::ResultClass::HashRefInflator', |
b0930c1e |
119 | } |
120 | ); |
b0930c1e |
121 | |
122 | my @dbic = $rs_dbic->all; |
123 | my @hashrefinf = $rs_hashrefinf->all; |
124 | |
2328814a |
125 | for my $index (0 .. $#hashrefinf) { |
b0930c1e |
126 | my $dbic_obj = $dbic[$index]; |
127 | my $datahashref = $hashrefinf[$index]; |
128 | |
129 | check_cols_of($dbic_obj, $datahashref); |
130 | } |
2328814a |
131 | |
132 | # sometimes for ultra-mega-speed you want to fetch columns in esoteric ways |
8273e845 |
133 | # check the inflator over a non-fetching join |
2328814a |
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/], |
c5bc9ba6 |
144 | result_class => 'DBIx::Class::ResultClass::HashRefInflator', |
2328814a |
145 | }); |
2328814a |
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'); |
9f6555d3 |
157 | for my $col (keys %{$datahashref->{cds}{tracks}}) { |
158 | is ($track->get_column ($col), $datahashref->{cds}{tracks}{$col}, "Correct track '$col'"); |
2328814a |
159 | } |
160 | } |
584e74ed |
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'; |
0f2aa8f2 |
170 | |
171 | # check nested prefetching of has_many relationships which return nothing |
172 | my $artist = $schema->resultset ('Artist')->create ({ name => 'unsuccessful artist without CDs'}); |
e8b32a6d |
173 | $artist->discard_changes; |
0f2aa8f2 |
174 | my $rs_artists = $schema->resultset ('Artist')->search ({ 'me.artistid' => $artist->id}, { |
e8b32a6d |
175 | prefetch => { cds => 'tracks' }, result_class => 'DBIx::Class::ResultClass::HashRefInflator', |
0f2aa8f2 |
176 | }); |
e8b32a6d |
177 | is_deeply( |
178 | [$rs_artists->all], |
179 | [{ $artist->get_columns, cds => [] }], |
180 | 'nested has_many prefetch without entries' |
181 | ); |
0fcfe456 |
182 | |
183 | done_testing; |
c9d29bb2 |
184 | |