Skip to content

Commit

Permalink
more empty query checks and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabien Coelho committed Jan 28, 2025
1 parent 3d5f5ad commit 1323c70
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
4 changes: 3 additions & 1 deletion aiosql/query_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,11 @@ def _make_query_datum(
if re.search(r"[^A-Za-z0-9_]", qname):
log.warning(f"non ASCII character in query name: {qname}")
if len(lines) <= 1:
raise SQLParseException(f"empty query for: {qname}")
raise SQLParseException(f"empty query for: {qname} at {floc[0]}:{floc[1]}")
record_class = self._get_record_class(lines[1])
sql, doc = self._get_sql_doc(lines[2 if record_class else 1 :])
if re.search("(?s)^[\t\n\r ;]*$", sql):
raise SQLParseException(f"empty sql for: {qname} at {floc[0]}:{floc[1]}")
signature = self._build_signature(sql, qname, qsig)
query_fqn = ".".join(ns_parts + [qname])
if self.attribute: # :u.a -> :u__a, **after** signature generation
Expand Down
11 changes: 8 additions & 3 deletions tests/test_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,14 @@ def test_empty_query():
aiosql.from_str("-- name: foo\n-- just a comment\n--name: bla\n", "sqlite3")
pytest.fail("must raise an exception")
except SQLParseException as e:
assert "empty query" in str(e)
assert "empty sql" in str(e)
try:
aiosql.from_str("-- name: foo\n-- just a comment\n \n-- hop\n--name: bla\n", "sqlite3")
aiosql.from_str("-- name: foo\n-- just a comment\n ; \n-- hop\n--name: bla\n", "sqlite3")
pytest.fail("must raise an exception")
except SQLParseException as e:
assert "empty query" in str(e)
assert "empty sql" in str(e)
try:
aiosql.from_str("-- name: foo\n-- just a comment\n;\n", "sqlite3")
pytest.fail("must raise an exception")
except SQLParseException as e:
assert "empty sql" in str(e)

0 comments on commit 1323c70

Please sign in to comment.