better money value comparison in tests
[dbsrgits/DBIx-Class.git] / t / prefetch / via_search_related.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6
7 use lib qw(t/lib);
8 use DBICTest;
9
10 my $schema = DBICTest->init_schema();
11
12 lives_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
40
41 lives_ok (sub {
42     my $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
43               ->search_related('artwork_to_artist')->search_related('artist',
44                 undef,
45                 { prefetch => 'cds' },
46               );
47     is($rs->all, 0, 'prefetch without WHERE (objects)');
48     is($rs->count, 0, 'prefetch without WHERE (count)');
49
50     $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
51               ->search_related('artwork_to_artist')->search_related('artist',
52                 { 'cds.title' => 'foo' },
53                 { prefetch => 'cds' },
54               );
55     is($rs->all, 0, 'prefetch with WHERE (objects)');
56     is($rs->count, 0, 'prefetch with WHERE (count)');
57
58
59 # test where conditions at the root of the related chain
60     my $artist_rs = $schema->resultset("Artist")->search({artistid => 11});
61
62
63     $rs = $artist_rs->search_related('cds')->search_related('genre',
64                     { 'genre.name' => 'foo' },
65                     { prefetch => 'cds' },
66                  );
67     is($rs->all, 0, 'prefetch without distinct (objects)');
68     is($rs->count, 0, 'prefetch without distinct (count)');
69
70
71
72     $rs = $artist_rs->search(undef, {distinct => 1})
73                 ->search_related('cds')->search_related('genre',
74                     { 'genre.name' => 'foo' },
75                  );
76     is($rs->all, 0, 'distinct without prefetch (objects)');
77     is($rs->count, 0, 'distinct without prefetch (count)');
78
79
80
81     $rs = $artist_rs->search({}, {distinct => 1})
82                 ->search_related('cds')->search_related('genre',
83                     { 'genre.name' => 'foo' },
84                     { prefetch => 'cds' },
85                  );
86     is($rs->all, 0, 'distinct with prefetch (objects)');
87     is($rs->count, 0, 'distinct with prefetch (count)');
88
89
90
91 }, 'distinct generally works with prefetch on deep search_related chains');
92
93 done_testing;