1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
8 use DBIx::Class::_Util 'modver_gt_or_eq_and_lt';
11 plan skip_all => 'base.pm 2.20 (only present in perl 5.19.7) is known to break this test'
12 if modver_gt_or_eq_and_lt( 'base', '2.19_01', '2.21' );
18 my $schema = DBICTest->init_schema();
20 # Under some versions of SQLite if the $rs is left hanging around it will lock
21 # So we create a scope here cos I'm lazy
23 my $rs = $schema->resultset('CD')->search ({}, {
27 my $orig_resclass = $rs->result_class;
28 eval "package DBICTest::CDSubclass; use base '$orig_resclass'";
30 # override on a specific $rs object, should not chain
31 $rs->result_class ('DBICTest::CDSubclass');
33 my $cd = $rs->find ({cdid => 1});
34 is (ref $cd, 'DBICTest::CDSubclass', 'result_class override propagates to find');
36 $cd = $rs->search({ cdid => 1 })->single;
37 is (ref $cd, $orig_resclass, 'result_class override does not propagate over seach+single');
39 $cd = $rs->search()->find ({ cdid => 1 });
40 is (ref $cd, $orig_resclass, 'result_class override does not propagate over seach+find');
42 # set as attr - should propagate
43 my $hri_rs = $rs->search ({}, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' });
44 is ($rs->result_class, 'DBICTest::CDSubclass', 'original class unchanged');
45 is ($hri_rs->result_class, 'DBIx::Class::ResultClass::HashRefInflator', 'result_class accessor pre-set via attribute');
47 my $datahashref1 = $hri_rs->next;
49 [ sort keys %$datahashref1 ],
50 [ sort $rs->result_source->columns ],
51 'returned correct columns',
55 $cd = $hri_rs->find ({cdid => 1});
56 is_deeply ( $cd, $datahashref1, 'first/find return the same thing (result_class attr propagates)');
58 $cd = $hri_rs->search({ cdid => 1 })->single;
59 is_deeply ( $cd, $datahashref1, 'first/search+single return the same thing (result_class attr propagates)');
61 $hri_rs->result_class ('DBIx::Class::Row'); # something bogus
63 $hri_rs->search->result_class, 'DBIx::Class::ResultClass::HashRefInflator',
64 'result_class set using accessor does not propagate over unused search'
67 # test result class auto-loading
69 sub { $rs->result_class ('nonexsitant_bogus_class') },
70 qr/Can't locate nonexsitant_bogus_class.pm/,
71 'Attempt to load on accessor override',
73 is ($rs->result_class, 'DBICTest::CDSubclass', 'class unchanged');
76 sub { $rs->search ({}, { result_class => 'nonexsitant_bogus_class' }) },
77 qr/Can't locate nonexsitant_bogus_class.pm/,
78 'Attempt to load on accessor override',
80 is ($rs->result_class, 'DBICTest::CDSubclass', 'class unchanged');
84 my ($dbic_obj, $datahashref) = @_;
86 foreach my $col (keys %$datahashref) {
88 if (not ref ($datahashref->{$col}) ) {
89 is ($datahashref->{$col}, $dbic_obj->get_column($col), 'same value');
91 # related table entry (belongs_to)
92 elsif (ref ($datahashref->{$col}) eq 'HASH') {
93 check_cols_of($dbic_obj->$col, $datahashref->{$col});
95 # multiple related entries (has_many)
96 elsif (ref ($datahashref->{$col}) eq 'ARRAY') {
97 my @dbic_reltable = $dbic_obj->$col;
98 my @hashref_reltable = @{$datahashref->{$col}};
100 is (scalar @hashref_reltable, scalar @dbic_reltable, 'number of related entries');
102 # for my $index (0..scalar @hashref_reltable) {
103 for my $index (0..scalar @dbic_reltable) {
104 my $dbic_reltable_obj = $dbic_reltable[$index];
105 my $hashref_reltable_entry = $hashref_reltable[$index];
107 check_cols_of($dbic_reltable_obj, $hashref_reltable_entry);
113 # create a cd without tracks for testing empty has_many relationship
114 $schema->resultset('CD')->create({ title => 'Silence is golden', artist => 3, year => 2006 });
116 # order_by to ensure both resultsets have the rows in the same order
117 # also check result_class-as-an-attribute syntax
118 my $rs_dbic = $schema->resultset('CD')->search(undef,
120 prefetch => [ qw/ artist tracks / ],
121 order_by => [ 'me.cdid', 'tracks.position' ],
124 my $rs_hashrefinf = $schema->resultset('CD')->search(undef,
126 prefetch => [ qw/ artist tracks / ],
127 order_by => [ 'me.cdid', 'tracks.position' ],
128 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
132 my @dbic = $rs_dbic->all;
133 my @hashrefinf = $rs_hashrefinf->all;
135 for my $index (0 .. $#hashrefinf) {
136 my $dbic_obj = $dbic[$index];
137 my $datahashref = $hashrefinf[$index];
139 check_cols_of($dbic_obj, $datahashref);
142 # sometimes for ultra-mega-speed you want to fetch columns in esoteric ways
143 # check the inflator over a non-fetching join
144 $rs_dbic = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, {
145 prefetch => { cds => 'tracks' },
146 order_by => [qw/cds.cdid tracks.trackid/],
149 $rs_hashrefinf = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, {
150 join => { cds => 'tracks' },
151 select => [qw/name tracks.title tracks.cd /],
152 as => [qw/name cds.tracks.title cds.tracks.cd /],
153 order_by => [qw/cds.cdid tracks.trackid/],
154 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
157 @dbic = map { $_->tracks->all } ($rs_dbic->first->cds->all);
158 @hashrefinf = $rs_hashrefinf->all;
160 is (scalar @dbic, scalar @hashrefinf, 'Equal number of tracks fetched');
162 for my $index (0 .. $#hashrefinf) {
163 my $track = $dbic[$index];
164 my $datahashref = $hashrefinf[$index];
166 is ($track->cd->artist->name, $datahashref->{name}, 'Brought back correct artist');
167 for my $col (keys %{$datahashref->{cds}{tracks}}) {
168 is ($track->get_column ($col), $datahashref->{cds}{tracks}{$col}, "Correct track '$col'");
172 # check for same query as above but using extended columns syntax
173 $rs_hashrefinf = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, {
174 join => { cds => 'tracks' },
175 columns => {name => 'name', 'cds.tracks.title' => 'tracks.title', 'cds.tracks.cd' => 'tracks.cd'},
176 order_by => [qw/cds.cdid tracks.trackid/],
178 $rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator');
179 is_deeply [$rs_hashrefinf->all], \@hashrefinf, 'Check query using extended columns syntax';
181 # check nested prefetching of has_many relationships which return nothing
182 my $artist = $schema->resultset ('Artist')->create ({ name => 'unsuccessful artist without CDs'});
183 $artist->discard_changes;
184 my $rs_artists = $schema->resultset ('Artist')->search ({ 'me.artistid' => $artist->id}, {
185 prefetch => { cds => 'tracks' }, result_class => 'DBIx::Class::ResultClass::HashRefInflator',
189 [{ $artist->get_columns, cds => [] }],
190 'nested has_many prefetch without entries'