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 | ); |
43 | |
44 | $cd = $hri_rs->find ({cdid => 1}); |
45 | is_deeply ( $cd, $datahashref1, 'first/find return the same thing (result_class attr propagates)'); |
46 | |
47 | $cd = $hri_rs->search({ cdid => 1 })->single; |
48 | is_deeply ( $cd, $datahashref1, 'first/search+single return the same thing (result_class attr propagates)'); |
331e41cf |
49 | |
60eb6547 |
50 | $hri_rs->result_class ('DBIx::Class::Row'); # something bogus |
331e41cf |
51 | is( |
60eb6547 |
52 | $hri_rs->search->result_class, 'DBIx::Class::ResultClass::HashRefInflator', |
53 | 'result_class set using accessor does not propagate over unused search' |
331e41cf |
54 | ); |
55 | |
60eb6547 |
56 | # test result class auto-loading |
57 | throws_ok ( |
58 | sub { $rs->result_class ('nonexsitant_bogus_class') }, |
59 | qr/Can't locate nonexsitant_bogus_class.pm/, |
60 | 'Attempt to load on accessor override', |
61 | ); |
62 | is ($rs->result_class, 'DBICTest::CDSubclass', 'class unchanged'); |
63 | |
64 | throws_ok ( |
65 | sub { $rs->search ({}, { result_class => 'nonexsitant_bogus_class' }) }, |
66 | qr/Can't locate nonexsitant_bogus_class.pm/, |
67 | 'Attempt to load on accessor override', |
68 | ); |
69 | is ($rs->result_class, 'DBICTest::CDSubclass', 'class unchanged'); |
0fcfe456 |
70 | } |
b0930c1e |
71 | |
72 | sub check_cols_of { |
73 | my ($dbic_obj, $datahashref) = @_; |
9f6555d3 |
74 | |
b0930c1e |
75 | foreach my $col (keys %$datahashref) { |
76 | # plain column |
77 | if (not ref ($datahashref->{$col}) ) { |
78 | is ($datahashref->{$col}, $dbic_obj->get_column($col), 'same value'); |
79 | } |
80 | # related table entry (belongs_to) |
81 | elsif (ref ($datahashref->{$col}) eq 'HASH') { |
82 | check_cols_of($dbic_obj->$col, $datahashref->{$col}); |
83 | } |
84 | # multiple related entries (has_many) |
85 | elsif (ref ($datahashref->{$col}) eq 'ARRAY') { |
86 | my @dbic_reltable = $dbic_obj->$col; |
87 | my @hashref_reltable = @{$datahashref->{$col}}; |
9f6555d3 |
88 | |
b0930c1e |
89 | is (scalar @hashref_reltable, scalar @dbic_reltable, 'number of related entries'); |
90 | |
91 | # for my $index (0..scalar @hashref_reltable) { |
92 | for my $index (0..scalar @dbic_reltable) { |
93 | my $dbic_reltable_obj = $dbic_reltable[$index]; |
94 | my $hashref_reltable_entry = $hashref_reltable[$index]; |
9f6555d3 |
95 | |
b0930c1e |
96 | check_cols_of($dbic_reltable_obj, $hashref_reltable_entry); |
97 | } |
98 | } |
99 | } |
100 | } |
101 | |
102 | # create a cd without tracks for testing empty has_many relationship |
103 | $schema->resultset('CD')->create({ title => 'Silence is golden', artist => 3, year => 2006 }); |
104 | |
105 | # order_by to ensure both resultsets have the rows in the same order |
c5bc9ba6 |
106 | # also check result_class-as-an-attribute syntax |
b0930c1e |
107 | my $rs_dbic = $schema->resultset('CD')->search(undef, |
108 | { |
109 | prefetch => [ qw/ artist tracks / ], |
110 | order_by => [ 'me.cdid', 'tracks.position' ], |
111 | } |
112 | ); |
113 | my $rs_hashrefinf = $schema->resultset('CD')->search(undef, |
114 | { |
115 | prefetch => [ qw/ artist tracks / ], |
116 | order_by => [ 'me.cdid', 'tracks.position' ], |
c5bc9ba6 |
117 | result_class => 'DBIx::Class::ResultClass::HashRefInflator', |
b0930c1e |
118 | } |
119 | ); |
b0930c1e |
120 | |
121 | my @dbic = $rs_dbic->all; |
122 | my @hashrefinf = $rs_hashrefinf->all; |
123 | |
2328814a |
124 | for my $index (0 .. $#hashrefinf) { |
b0930c1e |
125 | my $dbic_obj = $dbic[$index]; |
126 | my $datahashref = $hashrefinf[$index]; |
127 | |
128 | check_cols_of($dbic_obj, $datahashref); |
129 | } |
2328814a |
130 | |
131 | # sometimes for ultra-mega-speed you want to fetch columns in esoteric ways |
8273e845 |
132 | # check the inflator over a non-fetching join |
2328814a |
133 | $rs_dbic = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, { |
134 | prefetch => { cds => 'tracks' }, |
135 | order_by => [qw/cds.cdid tracks.trackid/], |
136 | }); |
137 | |
138 | $rs_hashrefinf = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, { |
139 | join => { cds => 'tracks' }, |
140 | select => [qw/name tracks.title tracks.cd /], |
141 | as => [qw/name cds.tracks.title cds.tracks.cd /], |
142 | order_by => [qw/cds.cdid tracks.trackid/], |
c5bc9ba6 |
143 | result_class => 'DBIx::Class::ResultClass::HashRefInflator', |
2328814a |
144 | }); |
2328814a |
145 | |
146 | @dbic = map { $_->tracks->all } ($rs_dbic->first->cds->all); |
147 | @hashrefinf = $rs_hashrefinf->all; |
148 | |
149 | is (scalar @dbic, scalar @hashrefinf, 'Equal number of tracks fetched'); |
150 | |
151 | for my $index (0 .. $#hashrefinf) { |
152 | my $track = $dbic[$index]; |
153 | my $datahashref = $hashrefinf[$index]; |
154 | |
155 | is ($track->cd->artist->name, $datahashref->{name}, 'Brought back correct artist'); |
9f6555d3 |
156 | for my $col (keys %{$datahashref->{cds}{tracks}}) { |
157 | is ($track->get_column ($col), $datahashref->{cds}{tracks}{$col}, "Correct track '$col'"); |
2328814a |
158 | } |
159 | } |
584e74ed |
160 | |
161 | # check for same query as above but using extended columns syntax |
162 | $rs_hashrefinf = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, { |
163 | join => { cds => 'tracks' }, |
164 | columns => {name => 'name', 'cds.tracks.title' => 'tracks.title', 'cds.tracks.cd' => 'tracks.cd'}, |
165 | order_by => [qw/cds.cdid tracks.trackid/], |
166 | }); |
167 | $rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator'); |
168 | is_deeply [$rs_hashrefinf->all], \@hashrefinf, 'Check query using extended columns syntax'; |
0f2aa8f2 |
169 | |
170 | # check nested prefetching of has_many relationships which return nothing |
171 | my $artist = $schema->resultset ('Artist')->create ({ name => 'unsuccessful artist without CDs'}); |
e8b32a6d |
172 | $artist->discard_changes; |
0f2aa8f2 |
173 | my $rs_artists = $schema->resultset ('Artist')->search ({ 'me.artistid' => $artist->id}, { |
e8b32a6d |
174 | prefetch => { cds => 'tracks' }, result_class => 'DBIx::Class::ResultClass::HashRefInflator', |
0f2aa8f2 |
175 | }); |
e8b32a6d |
176 | is_deeply( |
177 | [$rs_artists->all], |
178 | [{ $artist->get_columns, cds => [] }], |
179 | 'nested has_many prefetch without entries' |
180 | ); |
0fcfe456 |
181 | |
182 | done_testing; |
c9d29bb2 |
183 | |