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