lava-oushudb-dt-sql-parser/test/parser/postgresql/errorStrategy.test.ts
Hayden bb0fad1dbe
refactor: standard naming (#278)
* refactor: rename flinksql to flink

* refactor: rename pgsql to postgresql

* refactor: rename trinosql to trino

* refactor: replace all default exports with named export

* refactor: rename basicParser to basicSQL

* refactor: rename basic-parser-types to types

* refactor: replace arrow func with plain func
2024-03-27 10:33:25 +08:00

64 lines
2.5 KiB
TypeScript

import { PostgreSQL, PostgreSqlSplitListener } from 'src/parser/postgresql';
import { PostgreSqlParserListener } from 'src/lib/postgresql/PostgreSqlParserListener';
const validSQL1 = `INSERT INTO country_page_view
VALUES ('Chinese', 'mumiao', 18),
('Amercian', 'georage', 22);`;
const validSQL2 = 'SELECT * FROM tb;';
const inValidSQL = 'CREATE TABLE';
describe('PgSQL ErrorStrategy test', () => {
const pgSQL = new PostgreSQL();
// TODO: handle unexpected case
// test('begin inValid', () => {
// const sql = [inValidSQL, validSQL1, validSQL2].join('\n');
// // parse with empty errorListener
// const parseTree = pgSQL.parse(sql, () => {});
// const splitListener = new PostgreSQLSplitListener();
// pgSQL.listen(splitListener as PostgreSqlParserListener, parseTree);
// const statementCount = splitListener.statementsContext.length;
// splitListener.statementsContext.map((item, index) => {
// if(index !== statementCount-1 && index !== statementCount - 2) {
// expect(item.exception).not.toBe(null);
// } else {
// expect(item.exception).toBe(null);
// }
// })
// });
test('middle inValid', () => {
const sql = [validSQL1, inValidSQL, validSQL2].join('\n');
// parse with empty errorListener
const parseTree = pgSQL.parse(sql, () => {});
const splitListener = new PostgreSqlSplitListener();
pgSQL.listen(splitListener as PostgreSqlParserListener, parseTree);
const statementCount = splitListener.statementsContext.length;
splitListener.statementsContext.map((item, index) => {
if (index !== statementCount - 1 && index !== 0) {
expect(item.exception).not.toBe(null);
} else {
expect(item.exception).toBe(null);
}
});
});
test('end inValid', () => {
const sql = [validSQL1, validSQL2, inValidSQL].join('\n');
// parse with empty errorListener
const parseTree = pgSQL.parse(sql, () => {});
const splitListener = new PostgreSqlSplitListener();
pgSQL.listen(splitListener as PostgreSqlParserListener, parseTree);
splitListener.statementsContext.map((item, index) => {
if (index !== 0 && index !== 1) {
expect(item.exception).not.toBe(null);
} else {
expect(item.exception).toBe(null);
}
});
});
});