forgot to commit fixes
[dbsrgits/DBIx-Class.git] / t / inflate / hri.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7 my $schema = DBICTest->init_schema();
8
9 # Under some versions of SQLite if the $rs is left hanging around it will lock
10 # So we create a scope here cos I'm lazy
11 {
12     my $rs = $schema->resultset('CD')->search ({}, {
13         order_by => 'cdid',
14         # use the hashref inflator class as result class
15         result_class => 'DBIx::Class::ResultClass::HashRefInflator',
16     });
17
18     # get the defined columns
19     my @dbic_cols = sort $rs->result_source->columns;
20
21     # fetch first record
22     my $datahashref1 = $rs->first;
23
24     my @hashref_cols = sort keys %$datahashref1;
25
26     is_deeply( \@dbic_cols, \@hashref_cols, 'returned columns' );
27
28     my $cd1 = $rs->find ({cdid => 1});
29     is_deeply ( $cd1, $datahashref1, 'first/find return the same thing');
30
31     my $cd2 = $rs->search({ cdid => 1 })->single;
32     is_deeply ( $cd2, $datahashref1, 'first/search+single return the same thing');
33
34     $rs->result_class('DBIx::Class::Row');
35
36     is( $rs->result_class, 'DBIx::Class::Row', 'result_class set' );
37
38     is(
39         $rs->search->result_class, 'DBIx::Class::ResultClass::HashRefInflator',
40         'result_class set using accessor does not propagate over search'
41     );
42
43 }
44
45 sub check_cols_of {
46     my ($dbic_obj, $datahashref) = @_;
47
48     foreach my $col (keys %$datahashref) {
49         # plain column
50         if (not ref ($datahashref->{$col}) ) {
51             is ($datahashref->{$col}, $dbic_obj->get_column($col), 'same value');
52         }
53         # related table entry (belongs_to)
54         elsif (ref ($datahashref->{$col}) eq 'HASH') {
55             check_cols_of($dbic_obj->$col, $datahashref->{$col});
56         }
57         # multiple related entries (has_many)
58         elsif (ref ($datahashref->{$col}) eq 'ARRAY') {
59             my @dbic_reltable = $dbic_obj->$col;
60             my @hashref_reltable = @{$datahashref->{$col}};
61
62             is (scalar @dbic_reltable, scalar @hashref_reltable, 'number of related entries');
63
64             # for my $index (0..scalar @hashref_reltable) {
65             for my $index (0..scalar @dbic_reltable) {
66                 my $dbic_reltable_obj       = $dbic_reltable[$index];
67                 my $hashref_reltable_entry  = $hashref_reltable[$index];
68
69                 check_cols_of($dbic_reltable_obj, $hashref_reltable_entry);
70             }
71         }
72     }
73 }
74
75 # create a cd without tracks for testing empty has_many relationship
76 $schema->resultset('CD')->create({ title => 'Silence is golden', artist => 3, year => 2006 });
77
78 # order_by to ensure both resultsets have the rows in the same order
79 # also check result_class-as-an-attribute syntax
80 my $rs_dbic = $schema->resultset('CD')->search(undef,
81     {
82         prefetch    => [ qw/ artist tracks / ],
83         order_by    => [ 'me.cdid', 'tracks.position' ],
84     }
85 );
86 my $rs_hashrefinf = $schema->resultset('CD')->search(undef,
87     {
88         prefetch    => [ qw/ artist tracks / ],
89         order_by    => [ 'me.cdid', 'tracks.position' ],
90         result_class => 'DBIx::Class::ResultClass::HashRefInflator',
91     }
92 );
93
94 my @dbic        = $rs_dbic->all;
95 my @hashrefinf  = $rs_hashrefinf->all;
96
97 for my $index (0 .. $#hashrefinf) {
98     my $dbic_obj    = $dbic[$index];
99     my $datahashref = $hashrefinf[$index];
100
101     check_cols_of($dbic_obj, $datahashref);
102 }
103
104 # sometimes for ultra-mega-speed you want to fetch columns in esoteric ways
105 # check the inflator over a non-fetching join 
106 $rs_dbic = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, {
107     prefetch => { cds => 'tracks' },
108     order_by => [qw/cds.cdid tracks.trackid/],
109 });
110
111 $rs_hashrefinf = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, {
112     join     => { cds => 'tracks' },
113     select   => [qw/name   tracks.title      tracks.cd       /],
114     as       => [qw/name   cds.tracks.title  cds.tracks.cd   /],
115     order_by => [qw/cds.cdid tracks.trackid/],
116     result_class => 'DBIx::Class::ResultClass::HashRefInflator',
117 });
118
119 @dbic = map { $_->tracks->all } ($rs_dbic->first->cds->all);
120 @hashrefinf  = $rs_hashrefinf->all;
121
122 is (scalar @dbic, scalar @hashrefinf, 'Equal number of tracks fetched');
123
124 for my $index (0 .. $#hashrefinf) {
125     my $track       = $dbic[$index];
126     my $datahashref = $hashrefinf[$index];
127
128     is ($track->cd->artist->name, $datahashref->{name}, 'Brought back correct artist');
129     for my $col (keys %{$datahashref->{cds}{tracks}}) {
130         is ($track->get_column ($col), $datahashref->{cds}{tracks}{$col}, "Correct track '$col'");
131     }
132 }
133
134 # check for same query as above but using extended columns syntax
135 $rs_hashrefinf = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, {
136     join     => { cds => 'tracks' },
137     columns  => {name => 'name', 'cds.tracks.title' => 'tracks.title', 'cds.tracks.cd' => 'tracks.cd'},
138     order_by => [qw/cds.cdid tracks.trackid/],
139 });
140 $rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator');
141 is_deeply [$rs_hashrefinf->all], \@hashrefinf, 'Check query using extended columns syntax';
142
143 # check nested prefetching of has_many relationships which return nothing
144 my $artist = $schema->resultset ('Artist')->create ({ name => 'unsuccessful artist without CDs'});
145 $artist->discard_changes;
146 my $rs_artists = $schema->resultset ('Artist')->search ({ 'me.artistid' => $artist->id}, {
147     prefetch => { cds => 'tracks' }, result_class => 'DBIx::Class::ResultClass::HashRefInflator',
148 });
149 is_deeply(
150   [$rs_artists->all],
151   [{ $artist->get_columns, cds => [] }],
152   'nested has_many prefetch without entries'
153 );
154
155 done_testing;
156