Document software_limit and properly throw on related as_query calls
[dbsrgits/DBIx-Class.git] / t / sqlmaker / limit_dialects / basic.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 # test LIMIT
12 my $it = $schema->resultset("CD")->search( {},
13     { rows => 3,
14       order_by => 'title' }
15 );
16 is( $it->count, 3, "count ok" );
17 is( $it->next->title, "Caterwaulin' Blues", "iterator->next ok" );
18 $it->next;
19 $it->next;
20 is( $it->next, undef, "next past end of resultset ok" );
21
22 # test OFFSET
23 my @cds = $schema->resultset("CD")->search( {},
24     { rows => 2,
25       offset => 2,
26       order_by => 'year' }
27 );
28 is( $cds[0]->title, "Spoonful of bees", "offset ok" );
29
30 # test software-based limiting
31 $it = $schema->resultset("CD")->search( {},
32     { rows => 3,
33       software_limit => 1,
34       order_by => 'title' }
35 );
36 is( $it->count, 3, "software limit count ok" );
37 is( $it->next->title, "Caterwaulin' Blues", "software iterator->next ok" );
38 $it->next;
39 $it->next;
40 is( $it->next, undef, "software next past end of resultset ok" );
41
42 @cds = $schema->resultset("CD")->search( {},
43     { rows => 2,
44       offset => 2,
45       software_limit => 1,
46       order_by => 'year' }
47 );
48 is( $cds[0]->title, "Spoonful of bees", "software offset ok" );
49
50 throws_ok {
51   $schema->resultset("CD")->search({}, {
52     rows => 2,
53     software_limit => 1,
54   })->as_query;
55 } qr/Unable to generate limited query representation with 'software_limit' enabled/;
56
57 @cds = $schema->resultset("CD")->search( {},
58     {
59       offset => 2,
60       order_by => 'year' }
61 );
62 is( $cds[0]->title, "Spoonful of bees", "offset with no limit" );
63
64 $it = $schema->resultset("CD")->search(
65     { title => [
66         -and =>
67             {
68                 -like => '%bees'
69             },
70             {
71                 -not_like => 'Forkful%'
72             }
73         ]
74     },
75     { rows => 5 }
76 );
77 is( $it->count, 1, "complex abstract count ok" );
78
79 done_testing;