From: Peter Rabbitson Date: Wed, 2 Jun 2010 13:11:09 +0000 (+0000) Subject: Codify result_class accessor/attribute behavior X-Git-Tag: v0.08122~16 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=60eb65471a0585681e161641ee0c6f6495d0a6e3;p=dbsrgits%2FDBIx-Class.git Codify result_class accessor/attribute behavior --- diff --git a/t/inflate/hri.t b/t/inflate/hri.t index c3549d8..1a497d4 100644 --- a/t/inflate/hri.t +++ b/t/inflate/hri.t @@ -2,6 +2,7 @@ use strict; use warnings; use Test::More; +use Test::Exception; use lib qw(t/lib); use DBICTest; my $schema = DBICTest->init_schema(); @@ -11,35 +12,62 @@ my $schema = DBICTest->init_schema(); { my $rs = $schema->resultset('CD')->search ({}, { order_by => 'cdid', - # use the hashref inflator class as result class - result_class => 'DBIx::Class::ResultClass::HashRefInflator', }); - # get the defined columns - my @dbic_cols = sort $rs->result_source->columns; + my $orig_resclass = $rs->result_class; + eval "package DBICTest::CDSubclass; use base '$orig_resclass'"; + +# override on a specific $rs object, should not chain + $rs->result_class ('DBICTest::CDSubclass'); - # fetch first record - my $datahashref1 = $rs->first; + my $cd = $rs->find ({cdid => 1}); + is (ref $cd, 'DBICTest::CDSubclass', 'result_class override propagates to find'); - my @hashref_cols = sort keys %$datahashref1; + $cd = $rs->search({ cdid => 1 })->single; + is (ref $cd, $orig_resclass, 'result_class override does not propagate over seach+single'); - is_deeply( \@dbic_cols, \@hashref_cols, 'returned columns' ); + $cd = $rs->search()->find ({ cdid => 1 }); + is (ref $cd, $orig_resclass, 'result_class override does not propagate over seach+find'); - my $cd1 = $rs->find ({cdid => 1}); - is_deeply ( $cd1, $datahashref1, 'first/find return the same thing'); +# set as attr - should propagate + my $hri_rs = $rs->search ({}, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' }); + is ($rs->result_class, 'DBICTest::CDSubclass', 'original class unchanged'); + is ($hri_rs->result_class, 'DBIx::Class::ResultClass::HashRefInflator', 'result_class accessor pre-set via attribute'); - my $cd2 = $rs->search({ cdid => 1 })->single; - is_deeply ( $cd2, $datahashref1, 'first/search+single return the same thing'); - $rs->result_class('DBIx::Class::Row'); + my $datahashref1 = $hri_rs->next; + is_deeply( + [ sort keys %$datahashref1 ], + [ sort $rs->result_source->columns ], + 'returned correct columns', + ); + + $cd = $hri_rs->find ({cdid => 1}); + is_deeply ( $cd, $datahashref1, 'first/find return the same thing (result_class attr propagates)'); - is( $rs->result_class, 'DBIx::Class::Row', 'result_class set' ); + $cd = $hri_rs->search({ cdid => 1 })->single; + is_deeply ( $cd, $datahashref1, 'first/search+single return the same thing (result_class attr propagates)'); + $hri_rs->result_class ('DBIx::Class::Row'); # something bogus is( - $rs->search->result_class, 'DBIx::Class::ResultClass::HashRefInflator', - 'result_class set using accessor does not propagate over search' + $hri_rs->search->result_class, 'DBIx::Class::ResultClass::HashRefInflator', + 'result_class set using accessor does not propagate over unused search' ); +# test result class auto-loading + throws_ok ( + sub { $rs->result_class ('nonexsitant_bogus_class') }, + qr/Can't locate nonexsitant_bogus_class.pm/, + 'Attempt to load on accessor override', + ); + is ($rs->result_class, 'DBICTest::CDSubclass', 'class unchanged'); + + throws_ok ( + sub { $rs->search ({}, { result_class => 'nonexsitant_bogus_class' }) }, + qr/Can't locate nonexsitant_bogus_class.pm/, + 'Attempt to load on accessor override', + ); + is ($rs->result_class, 'DBICTest::CDSubclass', 'class unchanged'); } sub check_cols_of {