Revert r482[45], by implementing a better version of r4760
[dbsrgits/DBIx-Class.git] / t / 68inflate_resultclass_hashrefinflator.t
CommitLineData
b0930c1e 1use strict;
2use warnings;
3
4use Test::More qw(no_plan);
5use lib qw(t/lib);
2328814a 6use Scalar::Util qw/blessed/;
7use DateTime;
b0930c1e 8use DBICTest;
9use DBIx::Class::ResultClass::HashRefInflator;
10my $schema = DBICTest->init_schema();
11
12
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
15{
16 my $rs = $schema->resultset('CD');
17
18 # get the defined columns
19 my @dbic_cols = sort $rs->result_source->columns;
20
21 # use the hashref inflator class as result class
22 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
23
24 # fetch first record
25 my $datahashref1 = $rs->first;
26
27 my @hashref_cols = sort keys %$datahashref1;
28
29 is_deeply( \@dbic_cols, \@hashref_cols, 'returned columns' );
30}
31
32
33sub check_cols_of {
34 my ($dbic_obj, $datahashref) = @_;
35
36 foreach my $col (keys %$datahashref) {
37 # plain column
38 if (not ref ($datahashref->{$col}) ) {
39 is ($datahashref->{$col}, $dbic_obj->get_column($col), 'same value');
40 }
41 # related table entry (belongs_to)
42 elsif (ref ($datahashref->{$col}) eq 'HASH') {
43 check_cols_of($dbic_obj->$col, $datahashref->{$col});
44 }
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}};
49
50 is (scalar @hashref_reltable, scalar @dbic_reltable, 'number of related entries');
51
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];
56
57 check_cols_of($dbic_reltable_obj, $hashref_reltable_entry);
58 }
59 }
60 }
61}
62
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 });
65
66# order_by to ensure both resultsets have the rows in the same order
67my $rs_dbic = $schema->resultset('CD')->search(undef,
68 {
69 prefetch => [ qw/ artist tracks / ],
70 order_by => [ 'me.cdid', 'tracks.position' ],
71 }
72);
73my $rs_hashrefinf = $schema->resultset('CD')->search(undef,
74 {
75 prefetch => [ qw/ artist tracks / ],
76 order_by => [ 'me.cdid', 'tracks.position' ],
77 }
78);
79$rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator');
80
81my @dbic = $rs_dbic->all;
82my @hashrefinf = $rs_hashrefinf->all;
83
2328814a 84for my $index (0 .. $#hashrefinf) {
b0930c1e 85 my $dbic_obj = $dbic[$index];
86 my $datahashref = $hashrefinf[$index];
87
88 check_cols_of($dbic_obj, $datahashref);
89}
2328814a 90
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/],
96});
97
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/],
103});
104$rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator');
105
106@dbic = map { $_->tracks->all } ($rs_dbic->first->cds->all);
107@hashrefinf = $rs_hashrefinf->all;
108
109is (scalar @dbic, scalar @hashrefinf, 'Equal number of tracks fetched');
110
111for my $index (0 .. $#hashrefinf) {
112 my $track = $dbic[$index];
113 my $datahashref = $hashrefinf[$index];
114
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'");
118 }
119}
120
121# Test the data inflator
122
a5b29361 123is_deeply (
124 DBIx::Class::ResultClass::HashRefInflator->new (inflate_columns => 1),
125 DBIx::Class::ResultClass::HashRefInflator->new ({inflate_columns => 1}),
126 'Make sure arguments as list and as hashref work identically'
127);
128
2328814a 129$schema->class('CD')->inflate_column( 'year',
130 { inflate => sub { DateTime->new( year => shift ) },
131 deflate => sub { shift->year } }
132);
133
134my $cd_rs = $schema->resultset("CD")->search ({cdid => 3});
135$cd_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
136
137my $cd = $cd_rs->first;
138ok ( (not blessed $cd->{year}), "Plain string returned for year");
139is ( $cd->{year}, '1997', "We are looking at the right year");
a5b29361 140
141# try again with a HRI instance
142$cd_rs->reset;
143$cd_rs->result_class(DBIx::Class::ResultClass::HashRefInflator->new);
144my $cd2 = $cd_rs->first;
145is_deeply ($cd, $cd2, "HRI used as instance returns the same hashref as the old result_class ('class')");
146
147# try it again with inflation requested
148$cd_rs->reset;
149$cd_rs->result_class(DBIx::Class::ResultClass::HashRefInflator->new (inflate_columns => 1));
150my $cd3 = $cd_rs->first;
151isa_ok ($cd3->{year}, 'DateTime', "Inflated object");
152is ($cd3->{year}, DateTime->new ( year => 1997 ), "Correct year was inflated");