Handle field convertor
[dbsrgits/SQL-Abstract-2.0-ish.git] / t / compat / 00new.t
1 use strict;
2 use warnings;
3 use Test::More;
4
5 use SQL::Abstract::Test import => ['is_same_sql_bind'];
6
7 #LDNOTE: renamed all "bind" into "where" because that's what they are
8
9 my @handle_tests = (
10       #1
11       {
12               args => {logic => 'OR'},
13 #              stmt => 'SELECT * FROM test WHERE ( a = ? OR b = ? )'
14 # LDNOTE: modified the line above (changing the test suite!!!) because
15 # the test was not consistent with the doc: hashrefs should not be
16 # influenced by the current logic, they always mean 'AND'. So 
17 # { a => 4, b => 0} should ALWAYS mean ( a = ? AND b = ? ).
18 #
19 # acked by RIBASUSHI
20               stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? )'
21       },
22
23       #2
24       {
25               args => {},
26               stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? )'
27       },
28       #3
29       {
30               args => {case => "upper"},
31               stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? )'
32       },
33       #4
34       {
35               args => {case => "upper", cmp => "="},
36               stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? )'
37       },
38       #5
39       {
40               args => {cmp => "=", logic => 'or'},
41 # LDNOTE idem
42 #              stmt => 'SELECT * FROM test WHERE ( a = ? OR b = ? )'
43 # acked by RIBASUSHI
44               stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? )'
45       },
46       #6
47       {
48               args => {cmp => "like"},
49               stmt => 'SELECT * FROM test WHERE ( a LIKE ? AND b LIKE ? )'
50       },
51       #7
52       {
53               args => {logic => "or", cmp => "like"},
54 # LDNOTE idem
55 #              stmt => 'SELECT * FROM test WHERE ( a LIKE ? OR b LIKE ? )'
56 # acked by RIBASUSHI
57               stmt => 'SELECT * FROM test WHERE ( a LIKE ? AND b LIKE ? )'
58       },
59       #8
60       {
61               todo => 'lower',
62               args => {case => "lower"},
63               stmt => 'select * from test where ( a = ? and b = ? )'
64       },
65       #9
66       {
67               todo => 'lower',
68               args => {case => "lower", cmp => "="},
69               stmt => 'select * from test where ( a = ? and b = ? )'
70       },
71       #10
72       {
73               todo => 'lower',
74               args => {case => "lower", cmp => "like"},
75               stmt => 'select * from test where ( a like ? and b like ? )'
76       },
77       #11
78       {
79               todo => 'lower',
80               args => {case => "lower", convert => "lower", cmp => "like"},
81               stmt => 'select * from test where ( lower(a) like lower(?) and lower(b) like lower(?) )'
82       },
83       #12
84       {
85               args => {convert => "Round"},
86               stmt => 'SELECT * FROM test WHERE ( ROUND(a) = ROUND(?) AND ROUND(b) = ROUND(?) )',
87       },
88       #13
89       {
90               todo => 'lower',
91               args => {convert => "lower"},
92               stmt => 'SELECT * FROM test WHERE ( ( LOWER(ticket) = LOWER(?) ) OR ( LOWER(hostname) = LOWER(?) ) OR ( LOWER(taco) = LOWER(?) ) OR ( LOWER(salami) = LOWER(?) ) )',
93               where => [ { ticket => 11 }, { hostname => 11 }, { taco => 'salad' }, { salami => 'punch' } ],
94       },
95       #14
96       {
97               args => {convert => "upper"},
98               stmt => 'SELECT * FROM test WHERE ( ( UPPER(hostname) IN ( UPPER(?), UPPER(?), UPPER(?), UPPER(?) ) AND ( ( UPPER(ticket) = UPPER(?) ) OR ( UPPER(ticket) = UPPER(?) ) OR ( UPPER(ticket) = UPPER(?) ) ) ) OR ( UPPER(tack) BETWEEN UPPER(?) AND UPPER(?) ) OR ( ( ( UPPER(a) = UPPER(?) ) OR ( UPPER(a) = UPPER(?) ) OR ( UPPER(a) = UPPER(?) ) ) AND ( ( UPPER(e) != UPPER(?) ) OR ( UPPER(e) != UPPER(?) ) ) AND UPPER(q) NOT IN ( UPPER(?), UPPER(?), UPPER(?), UPPER(?), UPPER(?), UPPER(?), UPPER(?) ) ) )',
99               where => [ { ticket => [11, 12, 13], 
100                            hostname => { in => ['ntf', 'avd', 'bvd', '123'] } },
101                         { tack => { between => [qw/tick tock/] } },
102                         { a => [qw/b c d/], 
103                           e => { '!=', [qw(f g)] }, 
104                           q => { 'not in', [14..20] } } ],
105       },
106
107 );
108
109 plan tests => (1 + scalar(@handle_tests));
110
111 use_ok('SQL::Abstract::Compat');
112
113 for (@handle_tests) {
114   my $sql  = SQL::Abstract::Compat->new($_->{args});
115   my $where = $_->{where} || { a => 4, b => 0};
116   my($stmt, @bind) = $sql->select('test', '*', $where);
117
118   local $TODO = $_->{todo};
119
120   # LDNOTE: this original test suite from NWIGER did no comparisons
121   # on @bind values, just checking if @bind is nonempty.
122   # So here we just fake a [1] bind value for the comparison.
123   is_same_sql_bind($stmt, [@bind ? 1 : 0], $_->{stmt}, [1]);
124 }