Skip to content

Commit

Permalink
Add tests for metadata & AstBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
surister committed Jun 11, 2024
1 parent f28b595 commit c5fe54d
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions cratedb_sqlparse_py/tests/test_enricher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from cratedb_sqlparse.parser import Metadata


def test_table_metadata():
from cratedb_sqlparse import sqlparse

query = "SELECT 1; SELECT 2;"
stmts = sqlparse(query)
for stmt in stmts:
assert hasattr(stmt, "metadata")
assert isinstance(stmt.metadata, Metadata)


def test_table_name_statement():
from cratedb_sqlparse import sqlparse

query = "CREATE TABLE doc.tbl2 (a TEXT)"

stmts = sqlparse(query)
stmt = stmts[0]

assert stmt.metadata.schema == "doc"
assert stmt.metadata.table_name == "tbl2"


def test_table_name_statements():
from cratedb_sqlparse import sqlparse

query = """
SELECT A,B,C,D,E FROM doc.tbl1;
SELECT A,B FROM "doc"."tbl1";
SELECT A,B FROM "tbl1";
SELECT A,B FROM tbl1;
"""

stmts = sqlparse(query=query)

assert stmts[0].metadata.schema == "doc"
assert stmts[0].metadata.table_name == "tbl1"

assert stmts[1].metadata.schema == "doc"
assert stmts[1].metadata.table_name == "tbl1"

assert stmts[2].metadata.schema is None
assert stmts[2].metadata.table_name == "tbl1"

assert stmts[3].metadata.schema is None
assert stmts[3].metadata.table_name == "tbl1"

0 comments on commit c5fe54d

Please sign in to comment.