4 use Test::More qw(no_plan);
6 use Scalar::Util qw/blessed/;
9 use DBIx::Class::ResultClass::HashRefInflator;
10 my $schema = DBICTest->init_schema();
13 # Under some versions of SQLite if the $rs is left hanging around it will lock
14 # So we create a scope here cos I'm lazy
16 my $rs = $schema->resultset('CD');
18 # get the defined columns
19 my @dbic_cols = sort $rs->result_source->columns;
21 # use the hashref inflator class as result class
22 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
25 my $datahashref1 = $rs->first;
27 my @hashref_cols = sort keys %$datahashref1;
29 is_deeply( \@dbic_cols, \@hashref_cols, 'returned columns' );
34 my ($dbic_obj, $datahashref) = @_;
36 foreach my $col (keys %$datahashref) {
38 if (not ref ($datahashref->{$col}) ) {
39 is ($datahashref->{$col}, $dbic_obj->get_column($col), 'same value');
41 # related table entry (belongs_to)
42 elsif (ref ($datahashref->{$col}) eq 'HASH') {
43 check_cols_of($dbic_obj->$col, $datahashref->{$col});
45 # multiple related entries (has_many)
46 elsif (ref ($datahashref->{$col}) eq 'ARRAY') {
47 my @dbic_reltable = $dbic_obj->$col;
48 my @hashref_reltable = @{$datahashref->{$col}};
50 is (scalar @hashref_reltable, scalar @dbic_reltable, 'number of related entries');
52 # for my $index (0..scalar @hashref_reltable) {
53 for my $index (0..scalar @dbic_reltable) {
54 my $dbic_reltable_obj = $dbic_reltable[$index];
55 my $hashref_reltable_entry = $hashref_reltable[$index];
57 check_cols_of($dbic_reltable_obj, $hashref_reltable_entry);
63 # create a cd without tracks for testing empty has_many relationship
64 $schema->resultset('CD')->create({ title => 'Silence is golden', artist => 3, year => 2006 });
66 # order_by to ensure both resultsets have the rows in the same order
67 my $rs_dbic = $schema->resultset('CD')->search(undef,
69 prefetch => [ qw/ artist tracks / ],
70 order_by => [ 'me.cdid', 'tracks.position' ],
73 my $rs_hashrefinf = $schema->resultset('CD')->search(undef,
75 prefetch => [ qw/ artist tracks / ],
76 order_by => [ 'me.cdid', 'tracks.position' ],
79 $rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator');
81 my @dbic = $rs_dbic->all;
82 my @hashrefinf = $rs_hashrefinf->all;
84 for my $index (0 .. $#hashrefinf) {
85 my $dbic_obj = $dbic[$index];
86 my $datahashref = $hashrefinf[$index];
88 check_cols_of($dbic_obj, $datahashref);
91 # sometimes for ultra-mega-speed you want to fetch columns in esoteric ways
92 # check the inflator over a non-fetching join
93 $rs_dbic = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, {
94 prefetch => { cds => 'tracks' },
95 order_by => [qw/cds.cdid tracks.trackid/],
98 $rs_hashrefinf = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, {
99 join => { cds => 'tracks' },
100 select => [qw/name tracks.title tracks.cd /],
101 as => [qw/name cds.tracks.title cds.tracks.cd /],
102 order_by => [qw/cds.cdid tracks.trackid/],
104 $rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator');
106 @dbic = map { $_->tracks->all } ($rs_dbic->first->cds->all);
107 @hashrefinf = $rs_hashrefinf->all;
109 is (scalar @dbic, scalar @hashrefinf, 'Equal number of tracks fetched');
111 for my $index (0 .. $#hashrefinf) {
112 my $track = $dbic[$index];
113 my $datahashref = $hashrefinf[$index];
115 is ($track->cd->artist->name, $datahashref->{name}, 'Brought back correct artist');
116 for my $col (keys %{$datahashref->{cds}{tracks}}) {
117 is ($track->get_column ($col), $datahashref->{cds}{tracks}{$col}, "Correct track '$col'");
121 # Test the data inflator
123 $schema->class('CD')->inflate_column( 'year',
124 { inflate => sub { DateTime->new( year => shift ) },
125 deflate => sub { shift->year } }
128 my $cd_rs = $schema->resultset("CD")->search ({cdid => 3});
129 $cd_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
131 my $cd = $cd_rs->first;
132 ok ( (not blessed $cd->{year}), "Plain string returned for year");
133 is ( $cd->{year}, '1997', "We are looking at the right year");