Some cleanups to the m2m warnings test
[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;
b0930c1e 7my $schema = DBICTest->init_schema();
8
9
10# Under some versions of SQLite if the $rs is left hanging around it will lock
11# So we create a scope here cos I'm lazy
12{
13 my $rs = $schema->resultset('CD');
14
15 # get the defined columns
16 my @dbic_cols = sort $rs->result_source->columns;
17
18 # use the hashref inflator class as result class
19 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
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
29
30sub check_cols_of {
31 my ($dbic_obj, $datahashref) = @_;
32
33 foreach my $col (keys %$datahashref) {
34 # plain column
35 if (not ref ($datahashref->{$col}) ) {
36 is ($datahashref->{$col}, $dbic_obj->get_column($col), 'same value');
37 }
38 # related table entry (belongs_to)
39 elsif (ref ($datahashref->{$col}) eq 'HASH') {
40 check_cols_of($dbic_obj->$col, $datahashref->{$col});
41 }
42 # multiple related entries (has_many)
43 elsif (ref ($datahashref->{$col}) eq 'ARRAY') {
44 my @dbic_reltable = $dbic_obj->$col;
45 my @hashref_reltable = @{$datahashref->{$col}};
46
47 is (scalar @hashref_reltable, scalar @dbic_reltable, 'number of related entries');
48
49 # for my $index (0..scalar @hashref_reltable) {
50 for my $index (0..scalar @dbic_reltable) {
51 my $dbic_reltable_obj = $dbic_reltable[$index];
52 my $hashref_reltable_entry = $hashref_reltable[$index];
53
54 check_cols_of($dbic_reltable_obj, $hashref_reltable_entry);
55 }
56 }
57 }
58}
59
60# create a cd without tracks for testing empty has_many relationship
61$schema->resultset('CD')->create({ title => 'Silence is golden', artist => 3, year => 2006 });
62
63# order_by to ensure both resultsets have the rows in the same order
c5bc9ba6 64# also check result_class-as-an-attribute syntax
b0930c1e 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' ],
c5bc9ba6 75 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
b0930c1e 76 }
77);
b0930c1e 78
79my @dbic = $rs_dbic->all;
80my @hashrefinf = $rs_hashrefinf->all;
81
2328814a 82for my $index (0 .. $#hashrefinf) {
b0930c1e 83 my $dbic_obj = $dbic[$index];
84 my $datahashref = $hashrefinf[$index];
85
86 check_cols_of($dbic_obj, $datahashref);
87}
2328814a 88
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/],
94});
95
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/],
c5bc9ba6 101 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
2328814a 102});
2328814a 103
104@dbic = map { $_->tracks->all } ($rs_dbic->first->cds->all);
105@hashrefinf = $rs_hashrefinf->all;
106
107is (scalar @dbic, scalar @hashrefinf, 'Equal number of tracks fetched');
108
109for my $index (0 .. $#hashrefinf) {
110 my $track = $dbic[$index];
111 my $datahashref = $hashrefinf[$index];
112
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'");
116 }
117}