Merge 'trunk' into 'replication_dedux'
[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);
6use DBICTest;
7use DBIx::Class::ResultClass::HashRefInflator;
8my $schema = DBICTest->init_schema();
9
10
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
13{
14 my $rs = $schema->resultset('CD');
15
16 # get the defined columns
17 my @dbic_cols = sort $rs->result_source->columns;
18
19 # use the hashref inflator class as result class
20 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
21
22 # fetch first record
23 my $datahashref1 = $rs->first;
24
25 my @hashref_cols = sort keys %$datahashref1;
26
27 is_deeply( \@dbic_cols, \@hashref_cols, 'returned columns' );
28}
29
30
31sub check_cols_of {
32 my ($dbic_obj, $datahashref) = @_;
33
34 foreach my $col (keys %$datahashref) {
35 # plain column
36 if (not ref ($datahashref->{$col}) ) {
37 is ($datahashref->{$col}, $dbic_obj->get_column($col), 'same value');
38 }
39 # related table entry (belongs_to)
40 elsif (ref ($datahashref->{$col}) eq 'HASH') {
41 check_cols_of($dbic_obj->$col, $datahashref->{$col});
42 }
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}};
47
48 is (scalar @hashref_reltable, scalar @dbic_reltable, 'number of related entries');
49
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];
54
55 check_cols_of($dbic_reltable_obj, $hashref_reltable_entry);
56 }
57 }
58 }
59}
60
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 });
63
64# order_by to ensure both resultsets have the rows in the same order
65my $rs_dbic = $schema->resultset('CD')->search(undef,
66 {
67 prefetch => [ qw/ artist tracks / ],
68 order_by => [ 'me.cdid', 'tracks.position' ],
69 }
70);
71my $rs_hashrefinf = $schema->resultset('CD')->search(undef,
72 {
73 prefetch => [ qw/ artist tracks / ],
74 order_by => [ 'me.cdid', 'tracks.position' ],
75 }
76);
77$rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator');
78
79my @dbic = $rs_dbic->all;
80my @hashrefinf = $rs_hashrefinf->all;
81
82for my $index (0..scalar @hashrefinf) {
83 my $dbic_obj = $dbic[$index];
84 my $datahashref = $hashrefinf[$index];
85
86 check_cols_of($dbic_obj, $datahashref);
87}