add test for somewhat complex sql and add extra config for missing keywords
Arthur Axel "fREW" Schmidt [Sat, 4 Sep 2010 03:21:05 +0000 (03:21 +0000)]
lib/SQL/Abstract/Tree.pm
t/11unparse.t

index ce36f30..24dbf6a 100644 (file)
@@ -78,9 +78,12 @@ my $tokenizer_re = qr/ \s* ( $tokenizer_re_str | \( | \) | \? ) \s* /xi;
 sub _binary_op_keywords { @binary_op_keywords }
 
 my %indents = (
-   select => 0,
-   where  => 1,
-   from   => 1,
+   select     => 0,
+   where      => 1,
+   from       => 1,
+   join       => 1,
+   on         => 2,
+   'group by' => 1,
 );
 
 my %profiles = (
@@ -103,9 +106,12 @@ my %profiles = (
       indent_amount => 2,
       newline       => "<br />\n",
       colormap      => {
-         select => ['<span class="select">', '</span>'],
-         where  => ['<span class="where">', '</span>'],
-         from   => ['<span class="from">', '</span>'],
+         select     => ['<span class="select">'  , '</span>'],
+         where      => ['<span class="where">'   , '</span>'],
+         from       => ['<span class="from">'    , '</span>'],
+         join       => ['<span class="join">'    , '</span>'],
+         on         => ['<span class="on">'      , '</span>'],
+         'group by' => ['<span class="group-by">', '</span>'],
       },
       indentmap     => { %indents },
    },
index a91b88d..2b92623 100644 (file)
@@ -34,6 +34,16 @@ subtest no_formatting => sub {
          'simple statement with parens in where formatted correctly'
       );
    }
+
+   {
+      my $sql = "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]";
+      my $expected_sql =
+         "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] ";
+
+      is($sqlat->format($sql), $expected_sql,
+         'real life statement 1 formatted correctly'
+      );
+   }
    done_testing;
 };
 
@@ -79,6 +89,28 @@ subtest console_monochrome => sub {
          'simple statement with parens in where formatted correctly'
       );
    }
+
+   {
+      my $sql = "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]";
+      my $expected_sql =
+         "SELECT [screen].[id], [screen].[name], [screen].[section_id], [screen].[xtype] \n".
+         "  FROM [users_roles] [me] \n" .
+         "  JOIN [roles] [role] \n" .
+         "    ON [role].[id] = [me].[role_id] \n" .
+         "  JOIN [roles_permissions] [role_permissions] \n" .
+         "    ON [role_permissions].[role_id] = [role].[id] \n" .
+         "  JOIN [permissions] [permission] \n" .
+         "    ON [permission].[id] = [role_permissions].[permission_id] \n" .
+         "  JOIN [permissionscreens] [permission_screens] \n" .
+         "    ON [permission_screens].[permission_id] = [permission].[id] \n" .
+         "  JOIN [screens] [screen] \n" .
+         "    ON [screen].[id] = [permission_screens].[screen_id] \n" .
+         "  WHERE ([me].[user_id] = ?) \n" .
+         "  GROUP BY [screen].[id], [screen].[name], [screen].[section_id], [screen].[xtype] ";
+
+      my $gotten = $sqlat->format($sql);
+      is($gotten, $expected_sql, 'real life statement 1 formatted correctly');
+   }
    done_testing;
 };
 
@@ -124,6 +156,28 @@ subtest html => sub {
          'simple statement with parens in where formatted correctly'
       );
    }
+
+   {
+      my $sql = "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]";
+      my $expected_sql =
+         qq{<span class="select">SELECT</span> [screen].[id], [screen].[name], [screen].[section_id], [screen].[xtype] <br />\n}.
+         qq{&nbsp;&nbsp;<span class="from">FROM</span> [users_roles] [me] <br />\n}.
+         qq{&nbsp;&nbsp;<span class="join">JOIN</span> [roles] [role] <br />\n}.
+         qq{&nbsp;&nbsp;&nbsp;&nbsp;<span class="on">ON</span> [role].[id] = [me].[role_id] <br />\n}.
+         qq{&nbsp;&nbsp;<span class="join">JOIN</span> [roles_permissions] [role_permissions] <br />\n}.
+         qq{&nbsp;&nbsp;&nbsp;&nbsp;<span class="on">ON</span> [role_permissions].[role_id] = [role].[id] <br />\n}.
+         qq{&nbsp;&nbsp;<span class="join">JOIN</span> [permissions] [permission] <br />\n}.
+         qq{&nbsp;&nbsp;&nbsp;&nbsp;<span class="on">ON</span> [permission].[id] = [role_permissions].[permission_id] <br />\n}.
+         qq{&nbsp;&nbsp;<span class="join">JOIN</span> [permissionscreens] [permission_screens] <br />\n}.
+         qq{&nbsp;&nbsp;&nbsp;&nbsp;<span class="on">ON</span> [permission_screens].[permission_id] = [permission].[id] <br />\n}.
+         qq{&nbsp;&nbsp;<span class="join">JOIN</span> [screens] [screen] <br />\n}.
+         qq{&nbsp;&nbsp;&nbsp;&nbsp;<span class="on">ON</span> [screen].[id] = [permission_screens].[screen_id] <br />\n}.
+         qq{&nbsp;&nbsp;<span class="where">WHERE</span> ([me].[user_id] = ?) <br />\n}.
+         qq{&nbsp;&nbsp;<span class="group-by">GROUP BY</span> [screen].[id], [screen].[name], [screen].[section_id], [screen].[xtype] };
+
+      my $gotten = $sqlat->format($sql);
+      is($gotten, $expected_sql, 'real life statement 1 formatted correctly');
+   }
    done_testing;
 };
 
@@ -172,10 +226,31 @@ subtest configuration => sub {
          'simple statement with parens in where formatted correctly'
       );
    }
+
+   {
+      my $sql = "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]";
+      my $expected_sql =
+         "SELECT [screen].[id], [screen].[name], [screen].[section_id], [screen].[xtype] \r\n".
+         "\tFROM [users_roles] [me] \r\n" .
+         "\tJOIN [roles] [role] \r\n" .
+         "\t\tON [role].[id] = [me].[role_id] \r\n" .
+         "\tJOIN [roles_permissions] [role_permissions] \r\n" .
+         "\t\tON [role_permissions].[role_id] = [role].[id] \r\n" .
+         "\tJOIN [permissions] [permission] \r\n" .
+         "\t\tON [permission].[id] = [role_permissions].[permission_id] \r\n" .
+         "\tJOIN [permissionscreens] [permission_screens] \r\n" .
+         "\t\tON [permission_screens].[permission_id] = [permission].[id] \r\n" .
+         "\tJOIN [screens] [screen] \r\n" .
+         "\t\tON [screen].[id] = [permission_screens].[screen_id] \r\n" .
+         "\tWHERE ([me].[user_id] = ?) \r\n" .
+         "\tGROUP BY [screen].[id], [screen].[name], [screen].[section_id], [screen].[xtype] ";
+
+      my $gotten = $sqlat->format($sql);
+      is($gotten, $expected_sql, 'real life statement 1 formatted correctly');
+   }
    done_testing;
 };
 
 done_testing;
 # stuff we want:
 #    Max Width
-#    Color coding (html)