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