File: 68inflate_resultclass_hashrefinflator.t

package info (click to toggle)
libdbix-class-perl 0.08010-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 2,052 kB
  • ctags: 1,064
  • sloc: perl: 10,536; sql: 225; makefile: 45
file content (87 lines) | stat: -rwxr-xr-x 2,768 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use strict;
use warnings;  

use Test::More qw(no_plan);
use lib qw(t/lib);
use DBICTest;
use DBIx::Class::ResultClass::HashRefInflator;
my $schema = DBICTest->init_schema();


# Under some versions of SQLite if the $rs is left hanging around it will lock
# So we create a scope here cos I'm lazy
{
    my $rs = $schema->resultset('CD');

    # get the defined columns
    my @dbic_cols = sort $rs->result_source->columns;

    # use the hashref inflator class as result class
    $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');

    # fetch first record
    my $datahashref1 = $rs->first;

    my @hashref_cols = sort keys %$datahashref1;

    is_deeply( \@dbic_cols, \@hashref_cols, 'returned columns' );
}


sub check_cols_of {
    my ($dbic_obj, $datahashref) = @_;
    
    foreach my $col (keys %$datahashref) {
        # plain column
        if (not ref ($datahashref->{$col}) ) {
            is ($datahashref->{$col}, $dbic_obj->get_column($col), 'same value');
        }
        # related table entry (belongs_to)
        elsif (ref ($datahashref->{$col}) eq 'HASH') {
            check_cols_of($dbic_obj->$col, $datahashref->{$col});
        }
        # multiple related entries (has_many)
        elsif (ref ($datahashref->{$col}) eq 'ARRAY') {
            my @dbic_reltable = $dbic_obj->$col;
            my @hashref_reltable = @{$datahashref->{$col}};
  
            is (scalar @hashref_reltable, scalar @dbic_reltable, 'number of related entries');

            # for my $index (0..scalar @hashref_reltable) {
            for my $index (0..scalar @dbic_reltable) {
                my $dbic_reltable_obj       = $dbic_reltable[$index];
                my $hashref_reltable_entry  = $hashref_reltable[$index];
                
                check_cols_of($dbic_reltable_obj, $hashref_reltable_entry);
            }
        }
    }
}

# create a cd without tracks for testing empty has_many relationship
$schema->resultset('CD')->create({ title => 'Silence is golden', artist => 3, year => 2006 });

# order_by to ensure both resultsets have the rows in the same order
my $rs_dbic = $schema->resultset('CD')->search(undef,
    {
        prefetch    => [ qw/ artist tracks / ],
        order_by    => [ 'me.cdid', 'tracks.position' ],
    }
);
my $rs_hashrefinf = $schema->resultset('CD')->search(undef,
    {
        prefetch    => [ qw/ artist tracks / ],
        order_by    => [ 'me.cdid', 'tracks.position' ],
    }
);
$rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator');

my @dbic        = $rs_dbic->all;
my @hashrefinf  = $rs_hashrefinf->all;

for my $index (0..scalar @hashrefinf) {
    my $dbic_obj    = $dbic[$index];
    my $datahashref = $hashrefinf[$index];

    check_cols_of($dbic_obj, $datahashref);
}