fix test
[dbsrgits/DBIx-Class-Historic.git] / t / 104view.t
CommitLineData
bccd177f 1use strict;
f549392f 2use warnings;
bccd177f 3
4use Test::More;
5use Test::Exception;
6use lib qw(t/lib);
7use DBICTest;
8
9my $schema = DBICTest->init_schema();
10
1ee9aa72 11## Real view
3a8d32fa 12my $cds_rs_2000 = $schema->resultset('CD')->search( { year => 2000 });
bccd177f 13my $year2kcds_rs = $schema->resultset('Year2000CDs');
14
3a8d32fa 15is($cds_rs_2000->count, $year2kcds_rs->count, 'View Year2000CDs sees all CDs in year 2000');
bccd177f 16
17
1ee9aa72 18## Virtual view
3a8d32fa 19my $cds_rs_1999 = $schema->resultset('CD')->search( { year => 1999 });
1ee9aa72 20my $year1999cds_rs = $schema->resultset('Year1999CDs');
21
3a8d32fa 22is($cds_rs_1999->count, $year1999cds_rs->count, 'View Year1999CDs sees all CDs in year 1999');
1ee9aa72 23
24
f549392f 25# Test if relationships work correctly
26is_deeply (
27 [
28 $schema->resultset('Year1999CDs')->search (
29 {},
30 {
31 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
32 prefetch => ['artist', { tracks => [qw/cd year1999cd year2000cd/] } ],
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
43 },
44 )->all
45 ],
46 'Prefetch over virtual view gives expected result',
47);
1ee9aa72 48
f549392f 49is_deeply (
50 [
51 $schema->resultset('Year2000CDs')->search (
52 {},
53 {
54 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
55 prefetch => ['artist', { tracks => [qw/cd year1999cd year2000cd/] } ],
56 },
57 )->all
58 ],
59 [
60 $schema->resultset('CD')->search (
61 { 'me.year' => '2000'},
62 {
63 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
64 prefetch => ['artist', { tracks => [qw/cd year1999cd year2000cd/] } ],
65 },
66 )->all
67 ],
68 'Prefetch over regular view gives expected result',
69);
bccd177f 70
f549392f 71done_testing;