8 my $schema = DBICTest->init_schema();
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
13 my $rs = $schema->resultset('CD')->search ({}, {
17 my $orig_resclass = $rs->result_class;
18 eval "package DBICTest::CDSubclass; use base '$orig_resclass'";
20 # override on a specific $rs object, should not chain
21 $rs->result_class ('DBICTest::CDSubclass');
23 my $cd = $rs->find ({cdid => 1});
24 is (ref $cd, 'DBICTest::CDSubclass', 'result_class override propagates to find');
26 $cd = $rs->search({ cdid => 1 })->single;
27 is (ref $cd, $orig_resclass, 'result_class override does not propagate over seach+single');
29 $cd = $rs->search()->find ({ cdid => 1 });
30 is (ref $cd, $orig_resclass, 'result_class override does not propagate over seach+find');
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');
37 my $datahashref1 = $hri_rs->next;
39 [ sort keys %$datahashref1 ],
40 [ sort $rs->result_source->columns ],
41 'returned correct columns',
45 $cd = $hri_rs->find ({cdid => 1});
46 is_deeply ( $cd, $datahashref1, 'first/find return the same thing (result_class attr propagates)');
48 $cd = $hri_rs->search({ cdid => 1 })->single;
49 is_deeply ( $cd, $datahashref1, 'first/search+single return the same thing (result_class attr propagates)');
51 $hri_rs->result_class ('DBIx::Class::Row'); # something bogus
53 $hri_rs->search->result_class, 'DBIx::Class::ResultClass::HashRefInflator',
54 'result_class set using accessor does not propagate over unused search'
57 # test result class auto-loading
59 sub { $rs->result_class ('nonexsitant_bogus_class') },
60 qr/Can't locate nonexsitant_bogus_class.pm/,
61 'Attempt to load on accessor override',
63 is ($rs->result_class, 'DBICTest::CDSubclass', 'class unchanged');
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',
70 is ($rs->result_class, 'DBICTest::CDSubclass', 'class unchanged');
74 my ($dbic_obj, $datahashref) = @_;
76 foreach my $col (keys %$datahashref) {
78 if (not ref ($datahashref->{$col}) ) {
79 is ($datahashref->{$col}, $dbic_obj->get_column($col), 'same value');
81 # related table entry (belongs_to)
82 elsif (ref ($datahashref->{$col}) eq 'HASH') {
83 check_cols_of($dbic_obj->$col, $datahashref->{$col});
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}};
90 is (scalar @hashref_reltable, scalar @dbic_reltable, 'number of related entries');
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];
97 check_cols_of($dbic_reltable_obj, $hashref_reltable_entry);
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 });
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,
110 prefetch => [ qw/ artist tracks / ],
111 order_by => [ 'me.cdid', 'tracks.position' ],
114 my $rs_hashrefinf = $schema->resultset('CD')->search(undef,
116 prefetch => [ qw/ artist tracks / ],
117 order_by => [ 'me.cdid', 'tracks.position' ],
118 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
122 my @dbic = $rs_dbic->all;
123 my @hashrefinf = $rs_hashrefinf->all;
125 for my $index (0 .. $#hashrefinf) {
126 my $dbic_obj = $dbic[$index];
127 my $datahashref = $hashrefinf[$index];
129 check_cols_of($dbic_obj, $datahashref);
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/],
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',
147 @dbic = map { $_->tracks->all } ($rs_dbic->first->cds->all);
148 @hashrefinf = $rs_hashrefinf->all;
150 is (scalar @dbic, scalar @hashrefinf, 'Equal number of tracks fetched');
152 for my $index (0 .. $#hashrefinf) {
153 my $track = $dbic[$index];
154 my $datahashref = $hashrefinf[$index];
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'");
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/],
168 $rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator');
169 is_deeply [$rs_hashrefinf->all], \@hashrefinf, 'Check query using extended columns syntax';
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',
179 [{ $artist->get_columns, cds => [] }],
180 'nested has_many prefetch without entries'