Allow scalarref in IN and open up non-grouping parenthesis around IN arguments (saves...
[dbsrgits/SQL-Abstract.git] / t / 05in_between.t
CommitLineData
cf02fc47 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use Test::More;
6use Test::Exception;
7use SQL::Abstract::Test import => ['is_same_sql_bind'];
8
9use Data::Dumper;
10use SQL::Abstract;
11
cf02fc47 12my @in_between_tests = (
13 {
14 where => { x => { -between => [1, 2] } },
15 stmt => 'WHERE (x BETWEEN ? AND ?)',
16 bind => [qw/1 2/],
17 test => '-between with two placeholders',
18 },
19 {
20 where => { x => { -between => [\"1", 2] } },
21 stmt => 'WHERE (x BETWEEN 1 AND ?)',
22 bind => [qw/2/],
23 test => '-between with one literal sql arg and one placeholder',
24 },
25 {
26 where => { x => { -between => [1, \"2"] } },
27 stmt => 'WHERE (x BETWEEN ? AND 2)',
28 bind => [qw/1/],
29 test => '-between with one placeholder and one literal sql arg',
30 },
31 {
32 where => { x => { -between => [\'current_date - 1', \'current_date - 0'] } },
33 stmt => 'WHERE (x BETWEEN current_date - 1 AND current_date - 0)',
34 bind => [],
35 test => '-between with two literal sql arguments',
36 },
37 {
4d8b3dc4 38 where => { x => { -between => [ \['current_date - ?', 1], \['current_date - ?', 0] ] } },
39 stmt => 'WHERE (x BETWEEN current_date - ? AND current_date - ?)',
40 bind => [1, 0],
41 test => '-between with two literal sql arguments with bind',
42 },
43 {
cf02fc47 44 where => { x => { -between => \['? AND ?', 1, 2] } },
45 stmt => 'WHERE (x BETWEEN ? AND ?)',
46 bind => [1,2],
47 test => '-between with literal sql with placeholders (\["? AND ?", scalar, scalar])',
48 },
49 {
50 where => { x => { -between => \["'something' AND ?", 2] } },
51 stmt => "WHERE (x BETWEEN 'something' AND ?)",
52 bind => [2],
53 test => '-between with literal sql with one literal arg and one placeholder (\["\'something\' AND ?", scalar])',
54 },
55 {
56 where => { x => { -between => \["? AND 'something'", 1] } },
57 stmt => "WHERE (x BETWEEN ? AND 'something')",
58 bind => [1],
59 test => '-between with literal sql with one placeholder and one literal arg (\["? AND \'something\'", scalar])',
60 },
61 {
4d8b3dc4 62 where => { x => { -between => \"'this' AND 'that'" } },
cf02fc47 63 stmt => "WHERE (x BETWEEN 'this' AND 'that')",
64 bind => [],
4d8b3dc4 65 test => '-between with literal sql with a literal (\"\'this\' AND \'that\'")',
cf02fc47 66 },
4a1f01a3 67
68
69 {
70 parenthesis_significant => 1,
71 where => { x => { -in => [ 1 .. 3] } },
72 stmt => "WHERE ( x IN (?, ?, ?) )",
73 bind => [ 1 .. 3],
74 test => '-in with an array of scalars',
75 },
76 {
77 parenthesis_significant => 1,
78 where => { x => { -in => \'( 1,2,lower(y) )' } },
79 stmt => "WHERE ( x IN (1, 2, lower(y) ) )",
80 bind => [],
81 test => '-in with a literal scalarref',
82 },
83 {
84 parenthesis_significant => 1,
85 where => { x => { -in => \['( ( ?,?,lower(y) ) )', 1, 2] } },
86 stmt => "WHERE ( x IN (?, ?, lower(y) ) )",
87 bind => [1, 2],
88 test => '-in with a literal arrayrefref',
89 },
cf02fc47 90);
91
4d8b3dc4 92plan tests => @in_between_tests*4;
cf02fc47 93
94for my $case (@in_between_tests) {
95 TODO: {
96 local $TODO = $case->{todo} if $case->{todo};
4a1f01a3 97 local $SQL::Abstract::Test::parenthesis_significant = $case->{parenthesis_significant};
cf02fc47 98
99 local $Data::Dumper::Terse = 1;
100
4d8b3dc4 101 lives_ok (sub {
102
103 my @w;
104 local $SIG{__WARN__} = sub { push @w, @_ };
105 my $sql = SQL::Abstract->new ($case->{args} || {});
106 lives_ok (sub {
107 my ($stmt, @bind) = $sql->where($case->{where});
108 is_same_sql_bind(
109 $stmt,
110 \@bind,
111 $case->{stmt},
112 $case->{bind},
113 )
114 || diag "Search term:\n" . Dumper $case->{where};
115 });
116 is (@w, 0, $case->{test} || 'No warnings within in-between tests')
117 || diag join "\n", 'Emitted warnings:', @w;
118 }, "$case->{test} doesn't die");
cf02fc47 119 }
120}