6 use DBIx::Class::_Util 'modver_gt_or_eq';
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');
16 my $schema = DBICTest->init_schema();
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
21 my $rs = $schema->resultset('CD')->search ({}, {
25 my $orig_resclass = $rs->result_class;
26 eval "package DBICTest::CDSubclass; use base '$orig_resclass'";
28 # override on a specific $rs object, should not chain
29 $rs->result_class ('DBICTest::CDSubclass');
31 my $cd = $rs->find ({cdid => 1});
32 is (ref $cd, 'DBICTest::CDSubclass', 'result_class override propagates to find');
34 $cd = $rs->search({ cdid => 1 })->single;
35 is (ref $cd, $orig_resclass, 'result_class override does not propagate over seach+single');
37 $cd = $rs->search()->find ({ cdid => 1 });
38 is (ref $cd, $orig_resclass, 'result_class override does not propagate over seach+find');
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');
45 my $datahashref1 = $hri_rs->next;
47 [ sort keys %$datahashref1 ],
48 [ sort $rs->result_source->columns ],
49 'returned correct columns',
53 $cd = $hri_rs->find ({cdid => 1});
54 is_deeply ( $cd, $datahashref1, 'first/find return the same thing (result_class attr propagates)');
56 $cd = $hri_rs->search({ cdid => 1 })->single;
57 is_deeply ( $cd, $datahashref1, 'first/search+single return the same thing (result_class attr propagates)');
59 $hri_rs->result_class ('DBIx::Class::Row'); # something bogus
61 $hri_rs->search->result_class, 'DBIx::Class::ResultClass::HashRefInflator',
62 'result_class set using accessor does not propagate over unused search'
65 # test result class auto-loading
67 sub { $rs->result_class ('nonexsitant_bogus_class') },
68 qr/Can't locate nonexsitant_bogus_class.pm/,
69 'Attempt to load on accessor override',
71 is ($rs->result_class, 'DBICTest::CDSubclass', 'class unchanged');
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',
78 is ($rs->result_class, 'DBICTest::CDSubclass', 'class unchanged');
82 my ($dbic_obj, $datahashref) = @_;
84 foreach my $col (keys %$datahashref) {
86 if (not ref ($datahashref->{$col}) ) {
87 is ($datahashref->{$col}, $dbic_obj->get_column($col), 'same value');
89 # related table entry (belongs_to)
90 elsif (ref ($datahashref->{$col}) eq 'HASH') {
91 check_cols_of($dbic_obj->$col, $datahashref->{$col});
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}};
98 is (scalar @hashref_reltable, scalar @dbic_reltable, 'number of related entries');
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];
105 check_cols_of($dbic_reltable_obj, $hashref_reltable_entry);
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 });
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,
118 prefetch => [ qw/ artist tracks / ],
119 order_by => [ 'me.cdid', 'tracks.position' ],
122 my $rs_hashrefinf = $schema->resultset('CD')->search(undef,
124 prefetch => [ qw/ artist tracks / ],
125 order_by => [ 'me.cdid', 'tracks.position' ],
126 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
130 my @dbic = $rs_dbic->all;
131 my @hashrefinf = $rs_hashrefinf->all;
133 for my $index (0 .. $#hashrefinf) {
134 my $dbic_obj = $dbic[$index];
135 my $datahashref = $hashrefinf[$index];
137 check_cols_of($dbic_obj, $datahashref);
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/],
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',
155 @dbic = map { $_->tracks->all } ($rs_dbic->first->cds->all);
156 @hashrefinf = $rs_hashrefinf->all;
158 is (scalar @dbic, scalar @hashrefinf, 'Equal number of tracks fetched');
160 for my $index (0 .. $#hashrefinf) {
161 my $track = $dbic[$index];
162 my $datahashref = $hashrefinf[$index];
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'");
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/],
176 $rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator');
177 is_deeply [$rs_hashrefinf->all], \@hashrefinf, 'Check query using extended columns syntax';
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',
187 [{ $artist->get_columns, cds => [] }],
188 'nested has_many prefetch without entries'