Tighten up select list processing in ::SQLMaker
[dbsrgits/DBIx-Class.git] / t / sqlmaker / limit_dialects / basic.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Exception;
8
9 use DBICTest;
10
11 my $schema = DBICTest->init_schema();
12
13 # test LIMIT
14 my $it = $schema->resultset("CD")->search( {},
15     { rows => 3,
16       order_by => 'title' }
17 );
18 is( $it->count, 3, "count ok" );
19 is( $it->next->title, "Caterwaulin' Blues", "iterator->next ok" );
20 $it->next;
21 $it->next;
22 is( $it->next, undef, "next past end of resultset ok" );
23
24 # test OFFSET
25 my @cds = $schema->resultset("CD")->search( {},
26     { rows => 2,
27       offset => 2,
28       order_by => 'year' }
29 );
30 is( $cds[0]->title, "Spoonful of bees", "offset ok" );
31
32 # test software-based limiting
33 $it = $schema->resultset("CD")->search( {},
34     { rows => 3,
35       software_limit => 1,
36       order_by => 'title' }
37 );
38 is( $it->count, 3, "software limit count ok" );
39 is( $it->next->title, "Caterwaulin' Blues", "software iterator->next ok" );
40 $it->next;
41 $it->next;
42 is( $it->next, undef, "software next past end of resultset ok" );
43
44 @cds = $schema->resultset("CD")->search( {},
45     { rows => 2,
46       offset => 2,
47       software_limit => 1,
48       order_by => 'year' }
49 );
50 is( $cds[0]->title, "Spoonful of bees", "software offset ok" );
51
52 throws_ok {
53   $schema->resultset("CD")->search({}, {
54     rows => 2,
55     software_limit => 1,
56   })->as_query;
57 } qr/Unable to generate limited query representation with 'software_limit' enabled/;
58
59 @cds = $schema->resultset("CD")->search( {},
60     {
61       offset => 2,
62       order_by => 'year' }
63 );
64 is( $cds[0]->title, "Spoonful of bees", "offset with no limit" );
65
66 $it = $schema->resultset("CD")->search(
67     { title => [
68         -and =>
69             {
70                 -like => '%bees'
71             },
72             {
73                 -not_like => 'Forkful%'
74             }
75         ]
76     },
77     { rows => 5 }
78 );
79 is( $it->count, 1, "complex abstract count ok" );
80
81 done_testing;