From: Peter Rabbitson Date: Sun, 2 Jun 2013 15:17:06 +0000 (+0200) Subject: All roundtrip tests now look for the exact string X-Git-Tag: v1.74~8 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=87abf9bc3395870cef72a3f00b7d4cfe18abd980;p=dbsrgits%2FSQL-Abstract.git All roundtrip tests now look for the exact string Merge test files while at it --- diff --git a/lib/SQL/Abstract/Tree.pm b/lib/SQL/Abstract/Tree.pm index 81a360d..a4b01fd 100644 --- a/lib/SQL/Abstract/Tree.pm +++ b/lib/SQL/Abstract/Tree.pm @@ -596,14 +596,17 @@ sub _unparse { } else { my ($l, $r) = @{$self->pad_keyword($op, $depth)}; - return sprintf "$l%s%s%s$r", - $self->format_keyword($op), + + my $rhs = $self->_unparse($args, $bindargs, $depth); + + return sprintf "$l%s$r", join( ( ref $args eq 'ARRAY' and @{$args} == 1 and $args->[0][0] eq '-PAREN' ) ? '' # mysql-- : ' ' , - $self->_unparse($args, $bindargs, $depth), - ; + $self->format_keyword($op), + (length $rhs ? $rhs : () ), + ); } } diff --git a/t/14roundtrippin.t b/t/14roundtrippin.t index 1e2de9b..4155cc5 100644 --- a/t/14roundtrippin.t +++ b/t/14roundtrippin.t @@ -1,4 +1,5 @@ -#!/usr/bin/env perl +use warnings; +use strict; use Test::More; use Test::Exception; @@ -11,25 +12,49 @@ my $sqlat = SQL::Abstract::Tree->new; my @sql = ( "INSERT INTO artist DEFAULT VALUES", "INSERT INTO artist VALUES ()", - "SELECT a, b, c FROM foo WHERE foo.a =1 and foo.b LIKE 'station'", + "SELECT a, b, c FROM foo WHERE foo.a = 1 and foo.b LIKE 'station'", "SELECT COUNT( * ) FROM foo", + "SELECT COUNT( * ), SUM( blah ) FROM foo", "SELECT * FROM (SELECT * FROM foobar) WHERE foo.a = 1 and foo.b LIKE 'station'", - "SELECT * FROM lolz WHERE ( foo.a =1 ) and foo.b LIKE 'station'", + "SELECT * FROM lolz WHERE ( foo.a = 1 ) and foo.b LIKE 'station'", "SELECT [screen].[id], [screen].[name], [screen].[section_id], [screen].[xtype] FROM [users_roles] [me] JOIN [roles] [role] ON [role].[id] = [me].[role_id] JOIN [roles_permissions] [role_permissions] ON [role_permissions].[role_id] = [role].[id] JOIN [permissions] [permission] ON [permission].[id] = [role_permissions].[permission_id] JOIN [permissionscreens] [permission_screens] ON [permission_screens].[permission_id] = [permission].[id] JOIN [screens] [screen] ON [screen].[id] = [permission_screens].[screen_id] WHERE ( [me].[user_id] = ? ) GROUP BY [screen].[id], [screen].[name], [screen].[section_id], [screen].[xtype]", "SELECT * FROM foo WHERE NOT EXISTS (SELECT bar FROM baz)", "SELECT * FROM (SELECT SUM (CASE WHEN me.artist = 'foo' THEN 1 ELSE 0 END AS artist_sum) FROM foobar) WHERE foo.a = 1 and foo.b LIKE 'station'", "SELECT COUNT( * ) FROM foo me JOIN bar rel_bar ON rel_bar.id_bar = me.fk_bar WHERE NOT EXISTS (SELECT inner_baz.id_baz FROM baz inner_baz WHERE ( ( inner_baz.fk_a != ? AND ( fk_bar = me.fk_bar AND name = me.name ) ) ) )", + "SELECT foo AS bar FROM baz ORDER BY x + ? DESC, baz.g", ); -for (@sql) { - # Needs whitespace preservation in the AST to work, pending - #local $SQL::Abstract::Test::mysql_functions = 1; - is_same_sql ($sqlat->format($_), $_, sprintf 'roundtrip works (%s...)', substr $_, 0, 20); -} +# FIXME FIXME FIXME +# The formatter/unparser accumulated a ton of technical debt, +# and I don't have time to fix it all :( Some of the problems: +# - format() does an implicit parenthesis unroll for prettyness +# which makes it hard to do exact comparisons +# - there is no space preservation framework (also makes comparisons +# problematic) +# - there is no operator case preservation framework either +# +# So what we do instead is resort to some monkey patching and +# lowercasing and stuff to get something we can compare to the +# original SQL string +# Ugly but somewhat effective + +for my $orig (@sql) { + my $plain_formatted = $sqlat->format($orig); + is_same_sql( $plain_formatted, $orig, 'Formatted string is_same_sql()-matched' ); + + my $ast = $sqlat->parse($orig); + my $reassembled = do { + no warnings 'redefine'; + local *SQL::Abstract::Tree::_parenthesis_unroll = sub {}; + $sqlat->unparse($ast); + }; -# delete this test when mysql_functions gets implemented -my $sql = 'SELECT COUNT( * ), SUM( blah ) FROM foo'; -is($sqlat->format($sql), $sql, 'Roundtripping to mysql-compatible paren. syntax'); + # deal with parenthesis readjustment + $_ =~ s/\s*([\(\)])\s*/$1 /g + for ($orig, $reassembled); + + is (lc($reassembled), lc($orig), sprintf 'roundtrip works (%s...)', substr $orig, 0, 20); +} lives_ok { $sqlat->unparse( $sqlat->parse( <<'EOS' ) ) } 'Able to parse/unparse grossly malformed sql'; SELECT diff --git a/t/23reassembly-bugs.t b/t/23reassembly-bugs.t deleted file mode 100644 index 0954dd4..0000000 --- a/t/23reassembly-bugs.t +++ /dev/null @@ -1,15 +0,0 @@ -use strict; -use warnings; - -use Test::More; -use SQL::Abstract::Tree; - -my $sqlat = SQL::Abstract::Tree->new({}); - -is( - $sqlat->format('SELECT foo AS bar FROM baz ORDER BY x + ? DESC, baz.g'), - 'SELECT foo AS bar FROM baz ORDER BY x + ? DESC, baz.g', - 'complex order by correctly reassembled' -); - -done_testing;