/
00-base.t
108 lines (97 loc) · 3.39 KB
/
00-base.t
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
use strict;
use warnings;
use Test::More;
use SQL::Abstract::FromQuery;
use UNIVERSAL::DOES qw/does/;
use Try::Tiny;
diag( "Testing SQL::Abstract::FromQuery "
. "$SQL::Abstract::FromQuery::VERSION, Perl $], $^X" );
my $have_obj = eval { require CGI; 1; } || 0;
my $parser = SQL::Abstract::FromQuery->new(
-fields => {IGNORE => qr/^foo/},
);
my @tests = (
# test_name => [$given, $expected]
# ========= ===================
regular => ['foo',
'foo'],
list => ['foo,bar, buz',
{-in => [qw/foo bar buz/]}],
neg => ['!foo',
{'<>' => 'foo'}],
neg_list => ['!foo,bar,buz',
{-not_in => [qw/foo bar buz/]}],
neg_minus => ['-foo',
{'<>' => 'foo'}],
bad_neg => ['!',
{DIE => qr/value after negation/}],
num => ['-123',
-123],
between => ['BETWEEN a AND z',
{-between => [qw/a z/]}],
between_nums => ['BETWEEN -2 AND 3',
{-between => [qw/-2 3/]}],
bad_between => ['BETWEEN ! - %*"',
{DIE => qr/Expected min and max/}],
between_typo => ['BETWEEN a ANND z',
{DIE => qr/Expected min and max/}],
between_silly => ['BETWEEN a AND b AND c',
{-between => ['a', 'b AND c']}],
pattern => ['foo*',
{-like => 'foo%'}],
greater => ['> foo',
{'>' => 'foo'}],
greater_or_eq => ['>= foo',
{'>=' => 'foo'}],
null => ['NULL',
{'=' => undef}],
not_null => ['!NULL',
{'<>' => undef}],
date_dash => ['03-2-1',
'2003-02-01'],
date_dot => ['1.2.03',
'2003-02-01'],
date_yyyy => ['1.1.1970',
'1970-01-01'],
time => ['1:02',
'01:02:00'],
time_with_s => ['1:02:03',
'01:02:03'],
double_quoted => ['"foo bar"',
'foo bar'],
single_quoted => ["'foo bar'",
'foo bar'],
quoted_list => ["'foo,bar',buz",
{-in => ['foo,bar', 'buz']}],
two_words => ['a z',
'a z'],
double_space => ['a z',
'a z'],
initial_space => [' a z',
'a z'],
trailing_space => ['a z ',
'a z'],
foo_ignore => ['BETWEEN ! - %*"', # will be IGNOREd
undef],
);
plan tests => @tests / (2 - $have_obj);
while (my ($test_name, $test_data) = splice(@tests, 0, 2)) {
my ($given, $expected) = @$test_data;
for my $i (0 .. $have_obj) {
my ($where, $parse_arg);
$parse_arg = {$test_name => $given};
if ($i) {
no warnings 'once';
$CGI::LIST_CONTEXT_WARN = 0;
$parse_arg = CGI->new ($parse_arg);
}
try {$where = $parser->parse($parse_arg)}
catch {$where = {DIED => $_}; };
if (does($expected, 'HASH') && $expected->{DIE}) {
like $where->{DIED} || $where->{$test_name}, $expected->{DIE}, $test_name;
}
else {
is_deeply($where->{$test_name} || $where->{DIED}, $expected, $test_name);
}
}
}