1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 31;
use Test::Exception;
# use Data::Dumper::Simple;
# this represents a single page of results
my @dataset = qw( fee fi fo foo fum );
{
package TestApp;
use base 'Class::DBI';
use Class::DBI::Plugin::Pager;
sub count_search_where { 27 }
# the '@_' appends the class name, SQL and bind values passed in from
# search_where_limitable
sub retrieve_from_sql { @dataset, @_ }
sub __driver { 'MySQL' } # LimitOffset syntax
}
my $where = { 'this' => 'that' };
my $order_by = [ 'fig' ];
my ( $pager, @results );
#lives_ok { ( $pager, @results ) = TestApp->search_where_paged( { this => 'that' },
# { order_by => 'fig' },
# scalar( @dataset ),
# 3,
# ) } 'survived search_where_paged';
# it's ugly - @results contains @dataset, 'TestApp', $phrase, @bind_values
# because of TestApp::retrieve_from_sql overriding the real CDBI::retrieve_from_sql,
# instead of being a list of CDBI objects
lives_ok { ( $pager, @results ) = TestApp->pager->search_where( { this => 'that' },
{ order_by => 'fig' },
scalar( @dataset ),
3,
) } 'survived search_where';
ok( @results > 0, 'got some results' );
is($results[-2], '( this = ? ) ORDER BY fig LIMIT 5 OFFSET 10', 'search_where results');
lives_ok { $pager = TestApp->pager } 'get pager - no args';
isa_ok( $pager, 'Data::Page', 'the pager' );
lives_ok { $pager->page( 3 ) } 'set page';
lives_ok { $pager->per_page( scalar( @dataset ) ) } 'set per_page';
lives_ok { $pager->where( $where ) } 'set where';
lives_ok { $pager->order_by( $order_by ) } 'set order_by';
lives_ok { @results = $pager->search_where } 'search_where';
is_deeply( \@results, [ @dataset, 'TestApp', '( this = ? ) ORDER BY fig LIMIT 5 OFFSET 10', 'that' ], 'LimitOffset results' );
is_deeply( [ $pager->current_page,
$pager->total_entries,
$pager->last_page,
],
[ 3, 27, int( 27 / scalar( @dataset ) ) + 1 ],
'pager numbers' );
# -----------------------
my %conf = ( page => 3,
per_page => scalar( @dataset ),
where => $where,
order_by => $order_by,
syntax => 'RowsTo',
);
lives_ok { $pager = TestApp->pager( %conf ) } 'pager - named args';
lives_ok { @results = $pager->search_where } 'search_where';
is_deeply( \@results, [ @dataset, 'TestApp', '( this = ? ) ORDER BY fig ROWS 10 TO 15', 'that' ], 'RowsTo results' );
$pager = TestApp->pager;
$conf{syntax} = 'LimitXY';
lives_ok { @results = $pager->search_where( %conf ) } 'search_where - named args';
is_deeply( \@results, [ @dataset, 'TestApp', '( this = ? ) ORDER BY fig LIMIT 10, 5', 'that' ], 'LimitXY results' );
my @args = ( $where, $order_by, scalar( @dataset ), 3, 'RowsTo' );
lives_ok { $pager = TestApp->pager( @args ) } 'pager - positional args';
lives_ok { @results = $pager->search_where } 'search_where';
is_deeply( \@results, [ @dataset, 'TestApp', '( this = ? ) ORDER BY fig ROWS 10 TO 15', 'that' ], 'RowsTo results' );
# accepts arrayref 'where' clause - first with named args, then with positional
$pager = undef;
@results = ();
$conf{ where } = [
age => {'<=', 80},
age => {'>=', 20},
city => 'Jerusalem',
];
$conf{ abstract_attr } = { logic => 'AND' };
lives_ok { $pager = TestApp->pager( %conf ) } 'new pager - arrayref where (named args)';
lives_ok { @results = $pager->search_where } 'search_where';
is_deeply( \@results, [ @dataset,
'TestApp',
'( ( age <= ? AND age >= ? AND city = ? ) ) ORDER BY fig LIMIT 10, 5',
'80', '20', 'Jerusalem',
],
'arrayref where (named args) results' );
$pager = undef;
@results = ();
# ok( @{ $conf{ where } }, 'where not eaten' );
$conf{ where } = [
age => {'<=', 80},
age => {'>=', 20},
city => 'Jerusalem',
];
$args[0] = $conf{ where };
# ok( @{ $args[0] }, 'where not eaten' );
lives_ok { $pager = TestApp->pager( $args[0], { logic => 'AND' }, @args[1..$#args] ) } 'new pager - arrayref where (positional args)';
lives_ok { @results = $pager->search_where } 'search_where';
is_deeply( \@results, [ @dataset,
'TestApp',
'( ( age <= ? AND age >= ? AND city = ? ) ) ORDER BY fig ROWS 10 TO 15',
'80', '20', 'Jerusalem',
],
'arrayref where (positional args) results' );
# retrieve_all
$pager = undef;
@results = ();
@args = ( $order_by, scalar( @dataset ), 3, 'RowsTo' );
lives_ok { $pager = TestApp->pager } 'no args constructor';
lives_ok { @results = $pager->retrieve_all( @args ) } '@args passed to retrieve_all';
#is_deeply( \@results, [ @dataset, 'TestApp', '( 1 = ? ) ORDER BY fig ROWS 10 TO 15', '1' ], 'retrieve_all results' );
is_deeply( \@results, [ @dataset, 'TestApp', ' 1=1 ORDER BY fig ROWS 10 TO 15' ], 'retrieve_all results' );
$pager = TestApp->pager;
$pager->order_by( $order_by );
$pager->per_page( scalar( @dataset ) );
$pager->page( 3 );
$pager->set_syntax( 'RowsTo' );
lives_ok { @results = $pager->retrieve_all } 'retrieve_all without args';
#is_deeply( \@results, [ @dataset, 'TestApp', '( 1 = ? ) ORDER BY fig ROWS 10 TO 15', '1' ], 'retrieve_all results' );
is_deeply( \@results, [ @dataset, 'TestApp', ' 1=1 ORDER BY fig ROWS 10 TO 15' ], 'retrieve_all results' );
#use YAML;
#warn Dump( $pager );
|