Skip to content

Commit

Permalink
replace "assert False" with "pytest.fail"
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabien Coelho committed Aug 16, 2024
1 parent 49e838d commit e20a2f7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
9 changes: 5 additions & 4 deletions tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dataclasses
import asyncio
import re
import pytest

import aiosql
from utils import log
Expand Down Expand Up @@ -303,7 +304,7 @@ def run_date_time(conn, queries, db):
elif _DB[db] in ("mysql", "mariadb"):
now = queries.misc.my_get_now_date_time(conn)
else:
assert False, f"unexpected driver: {db}"
pytest.fail(f"unexpected driver: {db}")
assert re.match(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$", now)


Expand All @@ -323,17 +324,17 @@ def run_object_attributes(conn, queries, db):
elif isinstance(r, dict):
assert r["name"] == calvin.name and r["age"] == calvin.age
else:
assert False, "unexpected query output"
pytest.fail("unexpected query output")
# failures
try:
queries.misc.person_attributes(conn, q=calvin)
assert False, "should fail on missing parameter p"
pytest.fail("should fail on missing parameter p")
except ValueError as e:
assert "missing named parameter p" in str(e)
del calvin.age
try:
queries.misc.person_attributes(conn, p=calvin)
assert False, "should fail on missing attribute age"
pytest.fail("should fail on missing attribute age")
except ValueError as e:
assert "parameter p is missing attribute age" in str(e)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_asyncpg.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,6 @@ def test_maybe_order_params():
a = aiosql.adapters.asyncpg.AsyncPGAdapter()
try:
a.maybe_order_params("foo", "wrong-type-parameter")
assert False, "exception should be raised" # pragma: no cover
pytest.fail("exception should be raised") # pragma: no cover
except ValueError as e:
assert "dict or tuple" in str(e)
18 changes: 9 additions & 9 deletions tests/test_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def test_loading_query_signature_with_duplicate_parameter():
def test_adapters():
try:
aiosql.aiosql._make_driver_adapter("no-such-driver-adapter")
assert False, "must raise an exception" # pragma: no cover
pytest.fail("must raise an exception") # pragma: no cover
except ValueError as e:
assert "unregistered driver_adapter" in str(e)

Expand All @@ -158,21 +158,21 @@ class NoSuchConnector:

try:
aiosql.aiosql._make_driver_adapter(NoSuchConnector)
assert False, "must raise an exception" # pragma: no cover
pytest.fail("must raise an exception") # pragma: no cover
except ValueError as e:
assert "Unexpected driver_adapter" in str(e)

try:
aiosql.aiosql._make_driver_adapter(True) # type: ignore
assert False, "must raise an exception" # pragma: no cover
pytest.fail("must raise an exception") # pragma: no cover
except ValueError as e:
assert "Unexpected driver_adapter" in str(e)


def test_no_such_path():
try:
aiosql.from_path("/no/such/file", "sqlite3")
assert False, "must raise an exception" # pragma: no cover
pytest.fail("must raise an exception") # pragma: no cover
except SQLLoadException as e:
assert "File does not exist" in str(e)

Expand All @@ -186,18 +186,18 @@ def test_misc(sql_file):
try:
queries = aiosql.queries.Queries("sqlite3")
queries._make_sync_fn(("hello", None, -1, "SELECT NULL;", None, None, None, None))
assert False, "must raise an exception" # pragma: no cover
pytest.fail("must raise an exception") # pragma: no cover
except ValueError as e:
assert "Unknown operation_type" in str(e)
try:
db = aiosql.from_str("-- name: a*b\nSELECT 'ab'\n", "sqlite3")
assert False, "must raise en exception" # pragma: no cover
pytest.fail("must raise en exception") # pragma: no cover
except Exception as e:
assert "invalid query name and operation" in str(e)
ql = aiosql.query_loader.QueryLoader(None, None)
try:
ql.load_query_data_from_dir_path(sql_file)
assert False, "must raise en exception" # pragma: no cover
pytest.fail("must raise en exception") # pragma: no cover
except ValueError as e:
assert "must be a directory" in str(e)

Expand All @@ -211,7 +211,7 @@ def test_kwargs():
assert 42 == queries.plus_one(conn, val=41)
try:
queries.plus_one(conn, 2)
assert False, "must raise an exception" # pragma: no cover
pytest.fail("must raise an exception") # pragma: no cover
except ValueError as e:
assert "kwargs" in str(e)
# kwargs_only == False
Expand All @@ -221,6 +221,6 @@ def test_kwargs():
assert 42 == queries.plus_two(conn, val=40)
try:
queries.plus_two(conn, 2, val=41)
assert False, "must raise an exception" # pragma: no cover
pytest.fail("must raise an exception") # pragma: no cover
except ValueError as e:
assert "mix" in str(e)

0 comments on commit e20a2f7

Please sign in to comment.