Move more code to DBIHacks, put back the update/delete rs check, just in case
[dbsrgits/DBIx-Class.git] / t / prefetch / via_search_related.t
CommitLineData
5fc1d686 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
6
7use lib qw(t/lib);
8use DBICTest;
9
10my $schema = DBICTest->init_schema();
11
e711f316 12lives_ok ( sub {
13 my $no_prefetch = $schema->resultset('Track')->search_related(cd =>
14 {
15 'cd.year' => "2000",
16 },
17 {
18 join => 'tags',
19 order_by => 'me.trackid',
20 rows => 1,
21 }
22 );
23
24 my $use_prefetch = $no_prefetch->search(
25 {},
26 {
27 prefetch => 'tags',
28 }
29 );
30
31 is($use_prefetch->count, $no_prefetch->count, 'counts with and without prefetch match');
32 is(
33 scalar ($use_prefetch->all),
34 scalar ($no_prefetch->all),
35 "Amount of returned rows is right"
36 );
37
38}, 'search_related prefetch with order_by works');
39
340926fd 40lives_ok ( sub {
41 my $no_prefetch = $schema->resultset('Track')->search_related(cd =>
42 {
43 'cd.year' => "2000",
44 'tagid' => 1,
45 },
46 {
47 join => 'tags',
48 rows => 1,
49 }
50 );
51
52 my $use_prefetch = $no_prefetch->search(
53 undef,
54 {
55 prefetch => 'tags',
56 }
57 );
58
59 is($use_prefetch->count, $no_prefetch->count, 'counts with and without prefetch match');
60 is(
61 scalar ($use_prefetch->all),
62 scalar ($no_prefetch->all),
63 "Amount of returned rows is right"
64 );
65
66}, 'search_related prefetch with condition referencing unqualified column of a joined table works');
67
68
5fc1d686 69lives_ok (sub {
70 my $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
71 ->search_related('artwork_to_artist')->search_related('artist',
72 undef,
73 { prefetch => 'cds' },
74 );
75 is($rs->all, 0, 'prefetch without WHERE (objects)');
76 is($rs->count, 0, 'prefetch without WHERE (count)');
77
78 $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
79 ->search_related('artwork_to_artist')->search_related('artist',
80 { 'cds.title' => 'foo' },
81 { prefetch => 'cds' },
82 );
83 is($rs->all, 0, 'prefetch with WHERE (objects)');
84 is($rs->count, 0, 'prefetch with WHERE (count)');
85
86
87# test where conditions at the root of the related chain
5e8cb53c 88 my $artist_rs = $schema->resultset("Artist")->search({artistid => 2});
89 my $artist = $artist_rs->next;
90 $artist->create_related ('cds', $_) for (
91 {
92 year => 1999, title => 'vague cd', genre => { name => 'vague genre' }
93 },
94 {
95 year => 1999, title => 'vague cd2', genre => { name => 'vague genre' }
96 },
97 );
5fc1d686 98
99 $rs = $artist_rs->search_related('cds')->search_related('genre',
5e8cb53c 100 { 'genre.name' => 'vague genre' },
5fc1d686 101 { prefetch => 'cds' },
102 );
5e8cb53c 103 is($rs->all, 1, 'base without distinct (objects)');
104 is($rs->count, 1, 'base without distinct (count)');
105 # artist -> 2 cds -> 2 genres -> 2 cds for each genre = 4
106 is($rs->search_related('cds')->all, 4, 'prefetch without distinct (objects)');
107 is($rs->search_related('cds')->count, 4, 'prefetch without distinct (count)');
5fc1d686 108
109
110 $rs = $artist_rs->search(undef, {distinct => 1})
111 ->search_related('cds')->search_related('genre',
5e8cb53c 112 { 'genre.name' => 'vague genre' },
5fc1d686 113 );
5e8cb53c 114 is($rs->all, 1, 'distinct without prefetch (objects)');
115 is($rs->count, 1, 'distinct without prefetch (count)');
5fc1d686 116
117
118 $rs = $artist_rs->search({}, {distinct => 1})
119 ->search_related('cds')->search_related('genre',
5e8cb53c 120 { 'genre.name' => 'vague genre' },
5fc1d686 121 { prefetch => 'cds' },
122 );
5e8cb53c 123 is($rs->all, 1, 'distinct with prefetch (objects)');
124 is($rs->count, 1, 'distinct with prefetch (count)');
125 # artist -> 2 cds -> 2 genres -> 2 cds for each genre + distinct = 2
126 is($rs->search_related('cds')->all, 2, 'prefetched distinct with prefetch (objects)');
127 is($rs->search_related('cds')->count, 2, 'prefetched distinct with prefetch (count)');
5fc1d686 128
129
130
e711f316 131}, 'distinct generally works with prefetch on deep search_related chains');
5fc1d686 132
133done_testing;