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