X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F68inflate_resultclass_hashrefinflator.t;h=7138989786a0a21d51992d4558369b4584421873;hb=9f2d17e9dc71dabc3263cc7c2ad794f7eb24d223;hp=e8fe25bcd6f8ecc1ba7dffd691d6eb600651b5c7;hpb=a5b293612996cda25ce7e7bf1a5a5a23249c7b01;p=dbsrgits%2FDBIx-Class.git diff --git a/t/68inflate_resultclass_hashrefinflator.t b/t/68inflate_resultclass_hashrefinflator.t index e8fe25b..7138989 100644 --- a/t/68inflate_resultclass_hashrefinflator.t +++ b/t/68inflate_resultclass_hashrefinflator.t @@ -3,10 +3,7 @@ use warnings; use Test::More qw(no_plan); use lib qw(t/lib); -use Scalar::Util qw/blessed/; -use DateTime; use DBICTest; -use DBIx::Class::ResultClass::HashRefInflator; my $schema = DBICTest->init_schema(); @@ -64,6 +61,7 @@ sub check_cols_of { $schema->resultset('CD')->create({ title => 'Silence is golden', artist => 3, year => 2006 }); # order_by to ensure both resultsets have the rows in the same order +# also check result_class-as-an-attribute syntax my $rs_dbic = $schema->resultset('CD')->search(undef, { prefetch => [ qw/ artist tracks / ], @@ -74,9 +72,9 @@ my $rs_hashrefinf = $schema->resultset('CD')->search(undef, { prefetch => [ qw/ artist tracks / ], order_by => [ 'me.cdid', 'tracks.position' ], + result_class => 'DBIx::Class::ResultClass::HashRefInflator', } ); -$rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator'); my @dbic = $rs_dbic->all; my @hashrefinf = $rs_hashrefinf->all; @@ -100,8 +98,8 @@ $rs_hashrefinf = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, { select => [qw/name tracks.title tracks.cd /], as => [qw/name cds.tracks.title cds.tracks.cd /], order_by => [qw/cds.cdid tracks.trackid/], + result_class => 'DBIx::Class::ResultClass::HashRefInflator', }); -$rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator'); @dbic = map { $_->tracks->all } ($rs_dbic->first->cds->all); @hashrefinf = $rs_hashrefinf->all; @@ -117,36 +115,3 @@ for my $index (0 .. $#hashrefinf) { is ($track->get_column ($col), $datahashref->{cds}{tracks}{$col}, "Correct track '$col'"); } } - -# Test the data inflator - -is_deeply ( - DBIx::Class::ResultClass::HashRefInflator->new (inflate_columns => 1), - DBIx::Class::ResultClass::HashRefInflator->new ({inflate_columns => 1}), - 'Make sure arguments as list and as hashref work identically' -); - -$schema->class('CD')->inflate_column( 'year', - { inflate => sub { DateTime->new( year => shift ) }, - deflate => sub { shift->year } } -); - -my $cd_rs = $schema->resultset("CD")->search ({cdid => 3}); -$cd_rs->result_class('DBIx::Class::ResultClass::HashRefInflator'); - -my $cd = $cd_rs->first; -ok ( (not blessed $cd->{year}), "Plain string returned for year"); -is ( $cd->{year}, '1997', "We are looking at the right year"); - -# try again with a HRI instance -$cd_rs->reset; -$cd_rs->result_class(DBIx::Class::ResultClass::HashRefInflator->new); -my $cd2 = $cd_rs->first; -is_deeply ($cd, $cd2, "HRI used as instance returns the same hashref as the old result_class ('class')"); - -# try it again with inflation requested -$cd_rs->reset; -$cd_rs->result_class(DBIx::Class::ResultClass::HashRefInflator->new (inflate_columns => 1)); -my $cd3 = $cd_rs->first; -isa_ok ($cd3->{year}, 'DateTime', "Inflated object"); -is ($cd3->{year}, DateTime->new ( year => 1997 ), "Correct year was inflated");