Fix some warnings
[scpubgit/Q-Branch.git] / t / 05between.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Test::More;
6 use Test::Exception;
7 use SQL::Abstract::Test import => ['is_same_sql_bind'];
8
9 use Data::Dumper;
10 use SQL::Abstract;
11
12 =begin
13 Test -between and -in 
14   * between
15     * [scalar, scalar]
16     * [scalarref, scalar]
17     * [scalar, scalarref]
18     * [scalarref, scalarref]
19     * \[]
20       * \["? AND ?", scalar, scalar]
21       * \["1 AND ?", scalar]
22       * \["? AND 2", scalar]
23       * \["1 AND 2"]
24 =cut
25
26 my @in_between_tests = (
27   {
28     where => { x => { -between => [1, 2] } },
29     stmt => 'WHERE (x BETWEEN ? AND ?)',
30     bind => [qw/1 2/],
31     test => '-between with two placeholders',
32   },
33   {
34     where => { x => { -between => [\"1", 2] } },
35     stmt => 'WHERE (x BETWEEN 1 AND ?)',
36     bind => [qw/2/],
37     test => '-between with one literal sql arg and one placeholder',
38   },
39   {
40     where => { x => { -between => [1, \"2"] } },
41     stmt => 'WHERE (x BETWEEN ? AND 2)',
42     bind => [qw/1/],
43     test => '-between with one placeholder and one literal sql arg',
44   },
45   {
46     where => { x => { -between => [\'current_date - 1', \'current_date - 0'] } },
47     stmt => 'WHERE (x BETWEEN current_date - 1 AND current_date - 0)',
48     bind => [],
49     test => '-between with two literal sql arguments',
50   },
51   {
52     where => { x => { -between => \['? AND ?', 1, 2] } },
53     stmt => 'WHERE (x BETWEEN ? AND ?)',
54     bind => [1,2],
55     test => '-between with literal sql with placeholders (\["? AND ?", scalar, scalar])',
56   },
57   {
58     where => { x => { -between => \["'something' AND ?", 2] } },
59     stmt => "WHERE (x BETWEEN 'something' AND ?)",
60     bind => [2],
61     test => '-between with literal sql with one literal arg and one placeholder (\["\'something\' AND ?", scalar])',
62   },
63   {
64     where => { x => { -between => \["? AND 'something'", 1] } },
65     stmt => "WHERE (x BETWEEN ? AND 'something')",
66     bind => [1],
67     test => '-between with literal sql with one placeholder and one literal arg (\["? AND \'something\'", scalar])',
68   },
69   {
70     where => { x => { -between => \["'this' AND 'that'"] } },
71     stmt => "WHERE (x BETWEEN 'this' AND 'that')",
72     bind => [],
73     test => '-between with literal sql with two literal args (\["\'this\' AND \'that\'"])',
74   },
75 );
76
77 plan tests => @in_between_tests*3;
78
79 for my $case (@in_between_tests) {
80   TODO: {
81     local $TODO = $case->{todo} if $case->{todo};
82
83     local $Data::Dumper::Terse = 1;
84
85     my @w;
86     local $SIG{__WARN__} = sub { push @w, @_ };
87     my $sql = SQL::Abstract->new ($case->{args} || {});
88     lives_ok (sub { 
89       my ($stmt, @bind) = $sql->where($case->{where});
90       is_same_sql_bind(
91         $stmt,
92         \@bind,
93         $case->{stmt},
94         $case->{bind},
95       )
96         || diag "Search term:\n" . Dumper $case->{where};
97     });
98     is (@w, 0, $case->{test} || 'No warnings within in-between tests')
99       || diag join "\n", 'Emitted warnings:', @w;
100   }
101 }