4 use Test::More qw(no_plan);
7 use DBIx::Class::ResultClass::HashRefInflator;
8 my $schema = DBICTest->init_schema();
11 # Under some versions of SQLite if the $rs is left hanging around it will lock
12 # So we create a scope here cos I'm lazy
14 my $rs = $schema->resultset('CD');
16 # get the defined columns
17 my @dbic_cols = sort $rs->result_source->columns;
19 # use the hashref inflator class as result class
20 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
23 my $datahashref1 = $rs->first;
25 my @hashref_cols = sort keys %$datahashref1;
27 is_deeply( \@dbic_cols, \@hashref_cols, 'returned columns' );
32 my ($dbic_obj, $datahashref) = @_;
34 foreach my $col (keys %$datahashref) {
36 if (not ref ($datahashref->{$col}) ) {
37 is ($datahashref->{$col}, $dbic_obj->get_column($col), 'same value');
39 # related table entry (belongs_to)
40 elsif (ref ($datahashref->{$col}) eq 'HASH') {
41 check_cols_of($dbic_obj->$col, $datahashref->{$col});
43 # multiple related entries (has_many)
44 elsif (ref ($datahashref->{$col}) eq 'ARRAY') {
45 my @dbic_reltable = $dbic_obj->$col;
46 my @hashref_reltable = @{$datahashref->{$col}};
48 is (scalar @hashref_reltable, scalar @dbic_reltable, 'number of related entries');
50 # for my $index (0..scalar @hashref_reltable) {
51 for my $index (0..scalar @dbic_reltable) {
52 my $dbic_reltable_obj = $dbic_reltable[$index];
53 my $hashref_reltable_entry = $hashref_reltable[$index];
55 check_cols_of($dbic_reltable_obj, $hashref_reltable_entry);
61 # create a cd without tracks for testing empty has_many relationship
62 $schema->resultset('CD')->create({ title => 'Silence is golden', artist => 3, year => 2006 });
64 # order_by to ensure both resultsets have the rows in the same order
65 my $rs_dbic = $schema->resultset('CD')->search(undef,
67 prefetch => [ qw/ artist tracks / ],
68 order_by => [ 'me.cdid', 'tracks.position' ],
71 my $rs_hashrefinf = $schema->resultset('CD')->search(undef,
73 prefetch => [ qw/ artist tracks / ],
74 order_by => [ 'me.cdid', 'tracks.position' ],
77 $rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator');
79 my @dbic = $rs_dbic->all;
80 my @hashrefinf = $rs_hashrefinf->all;
82 for my $index (0 .. $#hashrefinf) {
83 my $dbic_obj = $dbic[$index];
84 my $datahashref = $hashrefinf[$index];
86 check_cols_of($dbic_obj, $datahashref);
89 # sometimes for ultra-mega-speed you want to fetch columns in esoteric ways
90 # check the inflator over a non-fetching join
91 $rs_dbic = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, {
92 prefetch => { cds => 'tracks' },
93 order_by => [qw/cds.cdid tracks.trackid/],
96 $rs_hashrefinf = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, {
97 join => { cds => 'tracks' },
98 select => [qw/name tracks.title tracks.cd /],
99 as => [qw/name cds.tracks.title cds.tracks.cd /],
100 order_by => [qw/cds.cdid tracks.trackid/],
102 $rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator');
104 @dbic = map { $_->tracks->all } ($rs_dbic->first->cds->all);
105 @hashrefinf = $rs_hashrefinf->all;
107 is (scalar @dbic, scalar @hashrefinf, 'Equal number of tracks fetched');
109 for my $index (0 .. $#hashrefinf) {
110 my $track = $dbic[$index];
111 my $datahashref = $hashrefinf[$index];
113 is ($track->cd->artist->name, $datahashref->{name}, 'Brought back correct artist');
114 for my $col (keys %{$datahashref->{cds}{tracks}}) {
115 is ($track->get_column ($col), $datahashref->{cds}{tracks}{$col}, "Correct track '$col'");