Added handling for implicit inflate/deflate of CDBI has_a relationships
[dbsrgits/DBIx-Class.git] / t / 104view.t
CommitLineData
bccd177f 1use strict;
f549392f 2use warnings;
bccd177f 3
4use Test::More;
bccd177f 5use lib qw(t/lib);
6use DBICTest;
7
8my $schema = DBICTest->init_schema();
9
1ee9aa72 10## Real view
3a8d32fa 11my $cds_rs_2000 = $schema->resultset('CD')->search( { year => 2000 });
bccd177f 12my $year2kcds_rs = $schema->resultset('Year2000CDs');
13
3a8d32fa 14is($cds_rs_2000->count, $year2kcds_rs->count, 'View Year2000CDs sees all CDs in year 2000');
bccd177f 15
16
1ee9aa72 17## Virtual view
3a8d32fa 18my $cds_rs_1999 = $schema->resultset('CD')->search( { year => 1999 });
1ee9aa72 19my $year1999cds_rs = $schema->resultset('Year1999CDs');
20
3a8d32fa 21is($cds_rs_1999->count, $year1999cds_rs->count, 'View Year1999CDs sees all CDs in year 1999');
1ee9aa72 22
23
f549392f 24# Test if relationships work correctly
25is_deeply (
26 [
27 $schema->resultset('Year1999CDs')->search (
28 {},
29 {
30 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
31 prefetch => ['artist', { tracks => [qw/cd year1999cd year2000cd/] } ],
1b658919 32 order_by => 'tracks.trackid',
f549392f 33 },
34 )->all
35 ],
36 [
37 $schema->resultset('CD')->search (
38 { 'me.year' => '1999'},
39 {
40 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
41 prefetch => ['artist', { tracks => [qw/cd year1999cd year2000cd/] } ],
42 columns => [qw/cdid single_track title/], # to match the columns retrieved by the virtview
1b658919 43 order_by => 'tracks.trackid',
f549392f 44 },
45 )->all
46 ],
47 'Prefetch over virtual view gives expected result',
48);
1ee9aa72 49
f549392f 50is_deeply (
51 [
52 $schema->resultset('Year2000CDs')->search (
53 {},
54 {
55 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
56 prefetch => ['artist', { tracks => [qw/cd year1999cd year2000cd/] } ],
57 },
58 )->all
59 ],
60 [
61 $schema->resultset('CD')->search (
62 { 'me.year' => '2000'},
63 {
64 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
65 prefetch => ['artist', { tracks => [qw/cd year1999cd year2000cd/] } ],
66 },
67 )->all
68 ],
69 'Prefetch over regular view gives expected result',
70);
bccd177f 71
f549392f 72done_testing;