'undef =>' isn't what one would want
[dbsrgits/DBIx-Class-Historic.git] / t / 104view.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 my $schema = DBICTest->init_schema();
9
10 ## Real view
11 my $cds_rs_2000 = $schema->resultset('CD')->search( { year => 2000 });
12 my $year2kcds_rs = $schema->resultset('Year2000CDs');
13
14 is($cds_rs_2000->count, $year2kcds_rs->count, 'View Year2000CDs sees all CDs in year 2000');
15
16
17 ## Virtual view
18 my $cds_rs_1999 = $schema->resultset('CD')->search( { year => 1999 });
19 my $year1999cds_rs = $schema->resultset('Year1999CDs');
20
21 is($cds_rs_1999->count, $year1999cds_rs->count, 'View Year1999CDs sees all CDs in year 1999');
22
23
24 # Test if relationships work correctly
25 is_deeply (
26   [
27     $schema->resultset('Year1999CDs')->search (
28       {},
29       {
30         result_class => 'DBIx::Class::ResultClass::HashRefInflator',
31         prefetch => ['artist', { tracks => [qw/cd year1999cd year2000cd/] } ],
32         order_by => 'tracks.trackid',
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         order_by => 'tracks.trackid',
44       },
45     )->all
46   ],
47   'Prefetch over virtual view gives expected result',
48 );
49
50 is_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 );
71
72 done_testing;