Omnipotent 'between'
[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 => [ \['current_date - ?', 1], \['current_date - ?', 0] ] } },
53     stmt => 'WHERE (x BETWEEN current_date - ? AND current_date - ?)',
54     bind => [1, 0],
55     test => '-between with two literal sql arguments with bind',
56   },
57   {
58     where => { x => { -between => \['? AND ?', 1, 2] } },
59     stmt => 'WHERE (x BETWEEN ? AND ?)',
60     bind => [1,2],
61     test => '-between with literal sql with placeholders (\["? AND ?", scalar, scalar])',
62   },
63   {
64     where => { x => { -between => \["'something' AND ?", 2] } },
65     stmt => "WHERE (x BETWEEN 'something' AND ?)",
66     bind => [2],
67     test => '-between with literal sql with one literal arg and one placeholder (\["\'something\' AND ?", scalar])',
68   },
69   {
70     where => { x => { -between => \["? AND 'something'", 1] } },
71     stmt => "WHERE (x BETWEEN ? AND 'something')",
72     bind => [1],
73     test => '-between with literal sql with one placeholder and one literal arg (\["? AND \'something\'", scalar])',
74   },
75   {
76     where => { x => { -between => \"'this' AND 'that'" } },
77     stmt => "WHERE (x BETWEEN 'this' AND 'that')",
78     bind => [],
79     test => '-between with literal sql with a literal (\"\'this\' AND \'that\'")',
80   },
81 );
82
83 plan tests => @in_between_tests*4;
84
85 for my $case (@in_between_tests) {
86   TODO: {
87     local $TODO = $case->{todo} if $case->{todo};
88
89     local $Data::Dumper::Terse = 1;
90
91     lives_ok (sub {
92
93       my @w;
94       local $SIG{__WARN__} = sub { push @w, @_ };
95       my $sql = SQL::Abstract->new ($case->{args} || {});
96       lives_ok (sub { 
97         my ($stmt, @bind) = $sql->where($case->{where});
98         is_same_sql_bind(
99           $stmt,
100           \@bind,
101           $case->{stmt},
102           $case->{bind},
103         )
104           || diag "Search term:\n" . Dumper $case->{where};
105       });
106       is (@w, 0, $case->{test} || 'No warnings within in-between tests')
107         || diag join "\n", 'Emitted warnings:', @w;
108     }, "$case->{test} doesn't die");
109   }
110 }