Better concurrency in test (parent blocks)
[dbsrgits/DBIx-Class.git] / t / 104view.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8
9 my $schema = DBICTest->init_schema();
10
11 ## Real view
12 my $cds_rs_2000 = $schema->resultset('CD')->search( { year => 2000 });
13 my $year2kcds_rs = $schema->resultset('Year2000CDs');
14
15 is($cds_rs_2000->count, $year2kcds_rs->count, 'View Year2000CDs sees all CDs in year 2000');
16
17
18 ## Virtual view
19 my $cds_rs_1999 = $schema->resultset('CD')->search( { year => 1999 });
20 my $year1999cds_rs = $schema->resultset('Year1999CDs');
21
22 is($cds_rs_1999->count, $year1999cds_rs->count, 'View Year1999CDs sees all CDs in year 1999');
23
24
25 # Test if relationships work correctly
26 is_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 );
48
49 is_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 );
70
71 done_testing;