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();
my @dbic = $rs_dbic->all;
my @hashrefinf = $rs_hashrefinf->all;
-for my $index (0..scalar @hashrefinf) {
+for my $index (0 .. $#hashrefinf) {
my $dbic_obj = $dbic[$index];
my $datahashref = $hashrefinf[$index];
check_cols_of($dbic_obj, $datahashref);
}
+
+# sometimes for ultra-mega-speed you want to fetch columns in esoteric ways
+# check the inflator over a non-fetching join
+$rs_dbic = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, {
+ prefetch => { cds => 'tracks' },
+ order_by => [qw/cds.cdid tracks.trackid/],
+});
+
+$rs_hashrefinf = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, {
+ join => { cds => 'tracks' },
+ select => [qw/name tracks.title tracks.cd /],
+ as => [qw/name cds.tracks.title cds.tracks.cd /],
+ order_by => [qw/cds.cdid tracks.trackid/],
+});
+$rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator');
+
+@dbic = map { $_->tracks->all } ($rs_dbic->first->cds->all);
+@hashrefinf = $rs_hashrefinf->all;
+
+is (scalar @dbic, scalar @hashrefinf, 'Equal number of tracks fetched');
+
+for my $index (0 .. $#hashrefinf) {
+ my $track = $dbic[$index];
+ my $datahashref = $hashrefinf[$index];
+
+ is ($track->cd->artist->name, $datahashref->{name}, 'Brought back correct artist');
+ for my $col (keys %{$datahashref->{cds}{tracks}}) {
+ 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");