Selaa lähdekoodia

adding more tests for hplsql parser

ayush.goyal 4 vuotta sitten
vanhempi
commit
835fa5a1b0

+ 1 - 0
.eslintignore

@@ -5,6 +5,7 @@
 /desktop/core/src/desktop/js/parse/solrQueryParser.js
 /desktop/core/src/desktop/js/parse/sqlAutocompleteParser.js
 /desktop/core/src/desktop/js/parse/sqlStatementsParser.js
+/desktop/core/src/desktop/js/parse/hplsqlStatementsParser.js
 /desktop/core/src/desktop/js/parse/sqlSyntaxParser.js
 /desktop/core/src/desktop/js/utils/json.bigDataParse.js
 /desktop/core/src/desktop/js/parse/sql/generic/genericAutocompleteParser.js

+ 15 - 56
desktop/core/src/desktop/js/parse/hplsqlStatementsParser.test.js

@@ -15,70 +15,29 @@
 // limitations under the License.
 
 import hplsqlStatementsParser from './hplsqlStatementsParser';
+import { testParser, runSharedStatementsParserTests } from './sharedStatementsParserTests.ts';
 
 describe('hplsqlStatementsParser.js', () => {
-  const stringifySplitResult = function (result) {
-    let s = '[';
-    let first = true;
-    result.forEach(entry => {
-      s += first ? '{\n' : ', {\n';
-      s += "  statement: '" + entry.statement.replace(/\n/g, '\\n') + "',\n";
-      s +=
-        '  location: { first_line: ' +
-        entry.location.first_line +
-        ', first_column: ' +
-        entry.location.first_column +
-        ', last_line: ' +
-        entry.location.last_line +
-        ', last_column: ' +
-        entry.location.last_column +
-        ' }';
-      if (entry.firstToken) {
-        s += ",\n  firstToken: '" + entry.firstToken + "'";
-      }
-      if (entry.database) {
-        s += ",\n  database: '" + entry.database + "'";
-      }
-      s += '\n}';
-      first = false;
-    });
-    s += ']';
-    return s;
-  };
-
-  const testParser = function (input, expectedOutput) {
-    try {
-      expectedOutput.forEach(entry => {
-        entry.type = 'statement';
-      });
-      const result = hplsqlStatementsParser.parse(input);
-      const because =
-        '\n\nParser output: ' +
-        stringifySplitResult(result) +
-        '\nExpected output: ' +
-        stringifySplitResult(expectedOutput);
-
-      expect(result).toEqual(expectedOutput, because);
-    } catch (error) {
-      console.error(error);
-      console.warn(error.message);
-
-      fail('Got error');
-    }
-  };
+  runSharedStatementsParserTests(hplsqlStatementsParser);
 
   it('should split "CREATE PROCEDURE greet(name STRING)\\nBEGIN\\n  PRINT \'Hello \' || name;END;" correctly', () => {
-    testParser("CREATE PROCEDURE greet(name STRING)\nBEGIN\n  PRINT 'Hello ' || name;END;", [
-      {
-        statement: "CREATE PROCEDURE greet(name STRING)\nBEGIN\n  PRINT 'Hello ' || name;END;",
-        location: { first_line: 1, first_column: 0, last_line: 3, last_column: 29 },
-        firstToken: 'CREATE'
-      }
-    ]);
+    testParser(
+      hplsqlStatementsParser,
+      "CREATE PROCEDURE greet(name STRING)\nBEGIN\n  PRINT 'Hello ' || name;END;",
+      [
+        {
+          type: 'statement',
+          statement: "CREATE PROCEDURE greet(name STRING)\nBEGIN\n  PRINT 'Hello ' || name;END;",
+          location: { first_line: 1, first_column: 0, last_line: 3, last_column: 29 },
+          firstToken: 'CREATE'
+        }
+      ]
+    );
   });
 
   it('should split "CREATE PROCEDURE greet(name STRING)\\nBEGIN\\n  PRINT \'Hello \' || name;\\nEND;/\\nprint "world";" correctly', () => {
     testParser(
+      hplsqlStatementsParser,
       "CREATE PROCEDURE greet(name STRING)\nBEGIN\n  PRINT 'Hello ' || name;\nEND;/\nprint 'world';",
       [
         {

+ 231 - 0
desktop/core/src/desktop/js/parse/sharedStatementsParserTests.ts

@@ -0,0 +1,231 @@
+// Licensed to Cloudera, Inc. under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  Cloudera, Inc. licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+import { SqlStatementsParser } from './types';
+import { ParsedSqlStatement } from './sqlStatementsParser';
+
+const stringifySplitResult = function (result) {
+  let s = '[';
+  let first = true;
+  result.forEach(entry => {
+    s += first ? '{\n' : ', {\n';
+    s += "  statement: '" + entry.statement.replace(/\n/g, '\\n') + "',\n";
+    s +=
+      '  location: { first_line: ' +
+      entry.location.first_line +
+      ', first_column: ' +
+      entry.location.first_column +
+      ', last_line: ' +
+      entry.location.last_line +
+      ', last_column: ' +
+      entry.location.last_column +
+      ' }';
+    if (entry.firstToken) {
+      s += ",\n  firstToken: '" + entry.firstToken + "'";
+    }
+    if (entry.database) {
+      s += ",\n  database: '" + entry.database + "'";
+    }
+    s += '\n}';
+    first = false;
+  });
+  s += ']';
+  return s;
+};
+
+export const testParser = function (
+  parser: SqlStatementsParser,
+  input: string,
+  expectedOutput: Array<Partial<ParsedSqlStatement>>
+): void {
+  try {
+    expectedOutput.forEach(entry => {
+      entry.type = 'statement';
+    });
+    const result = parser.parse(input);
+    const because =
+      '\n\nParser output: ' +
+      stringifySplitResult(result) +
+      '\nExpected output: ' +
+      stringifySplitResult(expectedOutput);
+
+    expect(result).toEqual(expectedOutput, because);
+  } catch (error) {
+    console.error(error);
+    console.warn(error.message);
+
+    fail('Got error');
+  }
+};
+
+export const runSharedStatementsParserTests = (parser: SqlStatementsParser): void => {
+  it('should split "" correctly', () => {
+    testParser(parser, '', [
+      {
+        statement: '',
+        location: { first_line: 1, first_column: 0, last_line: 1, last_column: 0 }
+      }
+    ]);
+  });
+
+  it('should split ";" correctly', () => {
+    testParser(parser, ';', [
+      {
+        statement: ';',
+        location: { first_line: 1, first_column: 0, last_line: 1, last_column: 1 }
+      }
+    ]);
+  });
+
+  it('should handle "-" correctly', () => {
+    testParser(parser, '-', [
+      {
+        statement: '-',
+        location: { first_line: 1, first_column: 0, last_line: 1, last_column: 1 }
+      }
+    ]);
+  });
+
+  it('should split "select * from bla" correctly', () => {
+    testParser(parser, 'select * from bla', [
+      {
+        statement: 'select * from bla',
+        location: { first_line: 1, first_column: 0, last_line: 1, last_column: 17 },
+        firstToken: 'select'
+      }
+    ]);
+  });
+
+  it('should split "select * from bla;" correctly', () => {
+    testParser(parser, 'select * from bla;', [
+      {
+        statement: 'select * from bla;',
+        location: { first_line: 1, first_column: 0, last_line: 1, last_column: 18 },
+        firstToken: 'select'
+      }
+    ]);
+  });
+
+  it('should split "select * from bla where x = ";";" correctly', () => {
+    testParser(parser, 'select * from bla where x = ";";', [
+      {
+        statement: 'select * from bla where x = ";";',
+        location: { first_line: 1, first_column: 0, last_line: 1, last_column: 32 },
+        firstToken: 'select'
+      }
+    ]);
+  });
+
+  it('should split "select * from bla where x = "; AND boo = 1;\\n\\nUSE db" correctly', () => {
+    testParser(parser, 'select * from bla where x = "; AND boo = 1;\n\nUSE db', [
+      {
+        statement: 'select * from bla where x = "; AND boo = 1;\n\nUSE db',
+        location: { first_line: 1, first_column: 0, last_line: 3, last_column: 6 },
+        firstToken: 'select'
+      }
+    ]);
+  });
+
+  it('should split "--- Some comment with ; ; \\nselect * from bla where x = ";";" correctly', () => {
+    testParser(parser, '--- Some comment with ; ; \nselect * from bla where x = ";";', [
+      {
+        statement: '--- Some comment with ; ; \nselect * from bla where x = ";";',
+        location: { first_line: 1, first_column: 0, last_line: 2, last_column: 32 },
+        firstToken: 'select'
+      }
+    ]);
+  });
+
+  it('should split "select *\n-- bla\n from bla;" correctly', () => {
+    testParser(parser, 'select *\n-- bla\n from bla;', [
+      {
+        statement: 'select *\n-- bla\n from bla;',
+        location: { first_line: 1, first_column: 0, last_line: 3, last_column: 10 },
+        firstToken: 'select'
+      }
+    ]);
+  });
+
+  it('should split "select *\\n/* bla \\n\\n*/\\n from bla;" correctly', () => {
+    testParser(parser, 'select *\n/* bla \n\n*/\n from bla;', [
+      {
+        statement: 'select *\n/* bla \n\n*/\n from bla;',
+        location: { first_line: 1, first_column: 0, last_line: 5, last_column: 10 },
+        firstToken: 'select'
+      }
+    ]);
+  });
+
+  it('should split "SELECT\\n id -- some ID\\n FROM customers;" correctly', () => {
+    testParser(parser, 'SELECT\n id -- some ID\n FROM customers;', [
+      {
+        statement: 'SELECT\n id -- some ID\n FROM customers;',
+        location: { first_line: 1, first_column: 0, last_line: 3, last_column: 16 },
+        firstToken: 'SELECT'
+      }
+    ]);
+  });
+
+  it('should split "SELECT\n id -- some ID;\n FROM customers;" correctly', () => {
+    testParser(parser, 'SELECT\n id -- some ID;\n FROM customers;', [
+      {
+        statement: 'SELECT\n id -- some ID;\n FROM customers;',
+        location: { first_line: 1, first_column: 0, last_line: 3, last_column: 16 },
+        firstToken: 'SELECT'
+      }
+    ]);
+  });
+
+  it('should split "SELECT id\\n\\n /* from customers; */ FROM other;" correctly', () => {
+    testParser(parser, 'SELECT id\n\n /* from customers; */ FROM other;', [
+      {
+        statement: 'SELECT id\n\n /* from customers; */ FROM other;',
+        location: { first_line: 1, first_column: 0, last_line: 3, last_column: 34 },
+        firstToken: 'SELECT'
+      }
+    ]);
+  });
+
+  it('should split "SELECT `   " correctly', () => {
+    testParser(parser, 'SELECT `   ', [
+      {
+        statement: 'SELECT `   ',
+        location: { first_line: 1, first_column: 0, last_line: 1, last_column: 11 },
+        firstToken: 'SELECT'
+      }
+    ]);
+  });
+
+  it('should split "SELECT "   " correctly', () => {
+    testParser(parser, 'SELECT "   ', [
+      {
+        statement: 'SELECT "   ',
+        location: { first_line: 1, first_column: 0, last_line: 1, last_column: 11 },
+        firstToken: 'SELECT'
+      }
+    ]);
+  });
+
+  it('should split "SELECT \'   " correctly', () => {
+    testParser(parser, "SELECT '   ", [
+      {
+        statement: "SELECT '   ",
+        location: { first_line: 1, first_column: 0, last_line: 1, last_column: 11 },
+        firstToken: 'SELECT'
+      }
+    ]);
+  });
+};

+ 15 - 216
desktop/core/src/desktop/js/parse/sqlStatementsParser.test.js

@@ -15,87 +15,13 @@
 // limitations under the License.
 
 import sqlStatementsParser from './sqlStatementsParser';
+import { testParser, runSharedStatementsParserTests } from './sharedStatementsParserTests.ts';
 
 describe('sqlStatementsParser.js', () => {
-  const stringifySplitResult = function (result) {
-    let s = '[';
-    let first = true;
-    result.forEach(entry => {
-      s += first ? '{\n' : ', {\n';
-      s += "  statement: '" + entry.statement.replace(/\n/g, '\\n') + "',\n";
-      s +=
-        '  location: { first_line: ' +
-        entry.location.first_line +
-        ', first_column: ' +
-        entry.location.first_column +
-        ', last_line: ' +
-        entry.location.last_line +
-        ', last_column: ' +
-        entry.location.last_column +
-        ' }';
-      if (entry.firstToken) {
-        s += ",\n  firstToken: '" + entry.firstToken + "'";
-      }
-      if (entry.database) {
-        s += ",\n  database: '" + entry.database + "'";
-      }
-      s += '\n}';
-      first = false;
-    });
-    s += ']';
-    return s;
-  };
-
-  const testParser = function (input, expectedOutput) {
-    try {
-      expectedOutput.forEach(entry => {
-        entry.type = 'statement';
-      });
-      const result = sqlStatementsParser.parse(input);
-      const because =
-        '\n\nParser output: ' +
-        stringifySplitResult(result) +
-        '\nExpected output: ' +
-        stringifySplitResult(expectedOutput);
-
-      expect(result).toEqual(expectedOutput, because);
-    } catch (error) {
-      console.error(error);
-      console.warn(error.message);
-
-      fail('Got error');
-    }
-  };
-
-  it('should split "" correctly', () => {
-    testParser('', [
-      {
-        statement: '',
-        location: { first_line: 1, first_column: 0, last_line: 1, last_column: 0 }
-      }
-    ]);
-  });
-
-  it('should split ";" correctly', () => {
-    testParser(';', [
-      {
-        statement: ';',
-        location: { first_line: 1, first_column: 0, last_line: 1, last_column: 1 }
-      }
-    ]);
-  });
-
-  it('should handle "-" correctly', () => {
-    testParser('-', [
-      {
-        statement: '-',
-        location: { first_line: 1, first_column: 0, last_line: 1, last_column: 1 }
-      }
-    ]);
-  });
+  runSharedStatementsParserTests(sqlStatementsParser);
 
   it('should handle "/" correctly', () => {
-    testParser('/', [
+    testParser(sqlStatementsParser, '/', [
       {
         statement: '/',
         location: { first_line: 1, first_column: 0, last_line: 1, last_column: 1 }
@@ -104,7 +30,7 @@ describe('sqlStatementsParser.js', () => {
   });
 
   it('should split escaped \\ correctly in single quotes', () => {
-    testParser("SELECT '\\\\';\n SELECT 1;", [
+    testParser(sqlStatementsParser, "SELECT '\\\\';\n SELECT 1;", [
       {
         type: 'statement',
         statement: "SELECT '\\\\';",
@@ -121,7 +47,7 @@ describe('sqlStatementsParser.js', () => {
   });
 
   it('should split escaped \\ correctly in double quotes', () => {
-    testParser('SELECT "\\\\";\n SELECT 1;', [
+    testParser(sqlStatementsParser, 'SELECT "\\\\";\n SELECT 1;', [
       {
         type: 'statement',
         statement: 'SELECT "\\\\";',
@@ -138,7 +64,7 @@ describe('sqlStatementsParser.js', () => {
   });
 
   it('should split ";   \\n;   \\r\\n;" correctly', () => {
-    testParser(';   \n;   \r\n;', [
+    testParser(sqlStatementsParser, ';   \n;   \r\n;', [
       {
         statement: ';',
         location: { first_line: 1, first_column: 0, last_line: 1, last_column: 1 }
@@ -154,18 +80,8 @@ describe('sqlStatementsParser.js', () => {
     ]);
   });
 
-  it('should split "select * from bla" correctly', () => {
-    testParser('select * from bla', [
-      {
-        statement: 'select * from bla',
-        location: { first_line: 1, first_column: 0, last_line: 1, last_column: 17 },
-        firstToken: 'select'
-      }
-    ]);
-  });
-
   it('should split ";;select * from bla" correctly', () => {
-    testParser(';;select * from bla', [
+    testParser(sqlStatementsParser, ';;select * from bla', [
       {
         statement: ';',
         location: { first_line: 1, first_column: 0, last_line: 1, last_column: 1 }
@@ -182,18 +98,8 @@ describe('sqlStatementsParser.js', () => {
     ]);
   });
 
-  it('should split "select * from bla;" correctly', () => {
-    testParser('select * from bla;', [
-      {
-        statement: 'select * from bla;',
-        location: { first_line: 1, first_column: 0, last_line: 1, last_column: 18 },
-        firstToken: 'select'
-      }
-    ]);
-  });
-
   it('should split "select * from bla;select * from ble" correctly', () => {
-    testParser('select * from bla;select * from ble', [
+    testParser(sqlStatementsParser, 'select * from bla;select * from ble', [
       {
         statement: 'select * from bla;',
         location: { first_line: 1, first_column: 0, last_line: 1, last_column: 18 },
@@ -208,7 +114,7 @@ describe('sqlStatementsParser.js', () => {
   });
 
   it('should split "select * from bla;;select * from ble" correctly', () => {
-    testParser('select * from bla;;select * from ble', [
+    testParser(sqlStatementsParser, 'select * from bla;;select * from ble', [
       {
         statement: 'select * from bla;',
         location: { first_line: 1, first_column: 0, last_line: 1, last_column: 18 },
@@ -227,7 +133,7 @@ describe('sqlStatementsParser.js', () => {
   });
 
   it('should split "select * from bla;\\n;select * from ble" correctly', () => {
-    testParser('select * from bla;\n;select * from ble', [
+    testParser(sqlStatementsParser, 'select * from bla;\n;select * from ble', [
       {
         statement: 'select * from bla;',
         location: { first_line: 1, first_column: 0, last_line: 1, last_column: 18 },
@@ -246,7 +152,7 @@ describe('sqlStatementsParser.js', () => {
   });
 
   it('should split "select * \\nfrom bla;\\r\\nselect * from ble;\\n" correctly', () => {
-    testParser('select * \nfrom bla;\r\nselect * from ble;\n', [
+    testParser(sqlStatementsParser, 'select * \nfrom bla;\r\nselect * from ble;\n', [
       {
         statement: 'select * \nfrom bla;',
         location: { first_line: 1, first_column: 0, last_line: 2, last_column: 9 },
@@ -260,18 +166,9 @@ describe('sqlStatementsParser.js', () => {
     ]);
   });
 
-  it('should split "select * from bla where x = ";";" correctly', () => {
-    testParser('select * from bla where x = ";";', [
-      {
-        statement: 'select * from bla where x = ";";',
-        location: { first_line: 1, first_column: 0, last_line: 1, last_column: 32 },
-        firstToken: 'select'
-      }
-    ]);
-  });
-
   it('should split "select * from bla where x = \';\';\\n\\nSELECT bla FROM foo WHERE y = `;` AND true = false;" correctly', () => {
     testParser(
+      sqlStatementsParser,
       "select * from bla where x = ';';\n\nSELECT bla FROM foo WHERE y = `;` AND true = false;",
       [
         {
@@ -288,108 +185,9 @@ describe('sqlStatementsParser.js', () => {
     );
   });
 
-  it('should split "select * from bla where x = "; AND boo = 1;\\n\\nUSE db" correctly', () => {
-    testParser('select * from bla where x = "; AND boo = 1;\n\nUSE db', [
-      {
-        statement: 'select * from bla where x = "; AND boo = 1;\n\nUSE db',
-        location: { first_line: 1, first_column: 0, last_line: 3, last_column: 6 },
-        firstToken: 'select'
-      }
-    ]);
-  });
-
-  it('should split "--- Some comment with ; ; \\nselect * from bla where x = ";";" correctly', () => {
-    testParser('--- Some comment with ; ; \nselect * from bla where x = ";";', [
-      {
-        statement: '--- Some comment with ; ; \nselect * from bla where x = ";";',
-        location: { first_line: 1, first_column: 0, last_line: 2, last_column: 32 },
-        firstToken: 'select'
-      }
-    ]);
-  });
-
-  it('should split "select *\n-- bla\n from bla;" correctly', () => {
-    testParser('select *\n-- bla\n from bla;', [
-      {
-        statement: 'select *\n-- bla\n from bla;',
-        location: { first_line: 1, first_column: 0, last_line: 3, last_column: 10 },
-        firstToken: 'select'
-      }
-    ]);
-  });
-
-  it('should split "select *\\n/* bla \\n\\n*/\\n from bla;" correctly', () => {
-    testParser('select *\n/* bla \n\n*/\n from bla;', [
-      {
-        statement: 'select *\n/* bla \n\n*/\n from bla;',
-        location: { first_line: 1, first_column: 0, last_line: 5, last_column: 10 },
-        firstToken: 'select'
-      }
-    ]);
-  });
-
-  it('should split "SELECT\\n id -- some ID\\n FROM customers;" correctly', () => {
-    testParser('SELECT\n id -- some ID\n FROM customers;', [
-      {
-        statement: 'SELECT\n id -- some ID\n FROM customers;',
-        location: { first_line: 1, first_column: 0, last_line: 3, last_column: 16 },
-        firstToken: 'SELECT'
-      }
-    ]);
-  });
-
-  it('should split "SELECT\n id -- some ID;\n FROM customers;" correctly', () => {
-    testParser('SELECT\n id -- some ID;\n FROM customers;', [
-      {
-        statement: 'SELECT\n id -- some ID;\n FROM customers;',
-        location: { first_line: 1, first_column: 0, last_line: 3, last_column: 16 },
-        firstToken: 'SELECT'
-      }
-    ]);
-  });
-
-  it('should split "SELECT id\\n\\n /* from customers; */ FROM other;" correctly', () => {
-    testParser('SELECT id\n\n /* from customers; */ FROM other;', [
-      {
-        statement: 'SELECT id\n\n /* from customers; */ FROM other;',
-        location: { first_line: 1, first_column: 0, last_line: 3, last_column: 34 },
-        firstToken: 'SELECT'
-      }
-    ]);
-  });
-
-  it('should split "SELECT `   " correctly', () => {
-    testParser('SELECT `   ', [
-      {
-        statement: 'SELECT `   ',
-        location: { first_line: 1, first_column: 0, last_line: 1, last_column: 11 },
-        firstToken: 'SELECT'
-      }
-    ]);
-  });
-
-  it('should split "SELECT "   " correctly', () => {
-    testParser('SELECT "   ', [
-      {
-        statement: 'SELECT "   ',
-        location: { first_line: 1, first_column: 0, last_line: 1, last_column: 11 },
-        firstToken: 'SELECT'
-      }
-    ]);
-  });
-
-  it('should split "SELECT \'   " correctly', () => {
-    testParser("SELECT '   ", [
-      {
-        statement: "SELECT '   ",
-        location: { first_line: 1, first_column: 0, last_line: 1, last_column: 11 },
-        firstToken: 'SELECT'
-      }
-    ]);
-  });
-
   it('should split "-- Some comment\\n\\n    CREATE TABLE bla (id int); /* some \\nother comment*/ ALTER TABLE boo" correctly', () => {
     testParser(
+      sqlStatementsParser,
       '-- Some comment\n\n    CREATE TABLE bla (id int); /* some \nother comment*/ ALTER TABLE boo',
       [
         {
@@ -408,6 +206,7 @@ describe('sqlStatementsParser.js', () => {
 
   it('should split "SELECT " \\" ;; ", \'"\', \' ;\' from bla; /* \\n\\n"" ; \\n; */ FROM other;" correctly', () => {
     testParser(
+      sqlStatementsParser,
       'USE `db;`;\r\nSELECT " \\" ;; ", \'"\', \' ;\' from bla; /* \n\n"" ; \n;  FROM other;*/',
       [
         {
@@ -430,7 +229,7 @@ describe('sqlStatementsParser.js', () => {
   });
 
   it('should find databases in use statements "-- commented USE statement\\nUSE boo;" correctly', () => {
-    testParser('-- commented USE statement\nUSE boo;/* USE baa; */use `foo`', [
+    testParser(sqlStatementsParser, '-- commented USE statement\nUSE boo;/* USE baa; */use `foo`', [
       {
         statement: '-- commented USE statement\nUSE boo;',
         location: { first_line: 1, first_column: 0, last_line: 2, last_column: 8 },