Use ExprDeclare in tests instead of expr.include
[dbsrgits/Data-Query.git] / t / sql.t
1 use strictures 1;
2 use Test::More qw(no_plan);
3
4 use Scalar::Util qw(blessed);
5 use Devel::Dwarn;
6 use Data::Query::Renderer::SQL::Naive;
7 use Data::Query::ExprHelpers qw(perl_scalar_value);
8 use Data::Query::ExprDeclare qw(SELECT AS expr);
9
10 my $rend = Data::Query::Renderer::SQL::Naive->new({ quote_chars => [ "'" ] });
11
12 sub binding { map perl_scalar_value($_), @_ }
13
14 sub expr_sql_is (&;@) {
15   my $sub = shift;
16   my $e = Data::Query::ExprDeclare::_run_expr($sub);
17   $e = blessed($e) ? $e->{expr} : $e;
18   @_
19     ? is_deeply($rend->render($e), @_)
20     : ::Dwarn($rend->render($e));
21 }
22
23 expr_sql_is { $_->foo }
24   [ 'foo' ],
25   "Simple identifier -> SQL";
26
27 expr_sql_is { $_->group }
28   [ q{'group'} ],
29   "Simple identifier needing quoting -> SQL";
30
31 expr_sql_is { $_->foo->group }
32   [ q{foo.'group'} ],
33   "Complex identifier -> SQL";
34
35 expr_sql_is { $_->foo == 1 }
36   [ "foo = ?", binding(1) ],
37   "Simple expression -> SQL";
38
39 expr_sql_is { ($_->foo == 1) & ($_->bar eq "foo") }
40   [ "( foo = ? AND bar = ? )", binding(1, "foo") ],
41   "Compound expression -> SQL";
42
43
44 expr_sql_is { ($_->foo == 1) & ($_->bar eq "foo") & ($_->baz > 3) }
45   [ "( foo = ? AND bar = ? AND baz > ? )", binding(1, "foo", 3) ],
46   "Flatten expression ok";
47
48 expr_sql_is { !$_->foo } # XXX revisit this why are the parens here
49   [ "( NOT foo )" ],
50   "Unary expression ok";
51
52 expr_sql_is { SELECT { $_->foo } }
53   [ "SELECT foo" ],
54   "Simple identifier";
55
56 expr_sql_is { SELECT { $_->foo, 1 } }
57   # the extra space here is a little icky but Naive's _flatten_structure
58   # will need rewriting to fix it - commit bits available if you do it first
59   [ "SELECT foo, ?", binding(1) ],
60   "Identifier and literal";
61
62 expr_sql_is { SELECT { $_->foo => AS("foom"), 1 } }
63   [ "SELECT foo AS foom, ?", binding(1) ],
64   "AS with parens";
65
66 expr_sql_is { SELECT { $_->foo => AS "foom", 1 } }
67   [ "SELECT foo AS foom, ?", binding(1) ],
68   "AS without parens";