Skip to content

Commit

Permalink
Exceptions classes should have Error suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
Cito committed Apr 29, 2023
1 parent df38633 commit 6414e00
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 49 deletions.
15 changes: 10 additions & 5 deletions dbutils/pooled_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,23 @@ class PooledDBError(Exception):
"""General PooledDB error."""


class InvalidConnection(PooledDBError):
class InvalidConnectionError(PooledDBError):
"""Database connection is invalid."""


class NotSupportedError(PooledDBError):
"""DB-API module not supported by PooledDB."""


class TooManyConnections(PooledDBError):
class TooManyConnectionsError(PooledDBError):
"""Too many database connections were opened."""


# deprecated alias names for error classes
InvalidConnection = InvalidConnectionError
TooManyConnections = TooManyConnectionsError


class PooledDB:
"""Pool for DB-API 2 connections.
Expand Down Expand Up @@ -392,7 +397,7 @@ def __del__(self):
def _wait_lock(self):
"""Wait until notified or report an error."""
if not self._blocking:
raise TooManyConnections
raise TooManyConnectionsError
self._lock.wait()


Expand Down Expand Up @@ -427,7 +432,7 @@ def __getattr__(self, name):
"""Proxy all members of the class."""
if self._con:
return getattr(self._con, name)
raise InvalidConnection
raise InvalidConnectionError

def __del__(self):
"""Delete the pooled connection."""
Expand Down Expand Up @@ -519,7 +524,7 @@ def __getattr__(self, name):
"""Proxy all members of the class."""
if self._con:
return getattr(self._con, name)
raise InvalidConnection
raise InvalidConnectionError

def __del__(self):
"""Delete the pooled connection."""
Expand Down
13 changes: 9 additions & 4 deletions dbutils/pooled_pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,19 @@ class PooledPgError(Exception):
"""General PooledPg error."""


class InvalidConnection(PooledPgError):
class InvalidConnectionError(PooledPgError):
"""Database connection is invalid."""


class TooManyConnections(PooledPgError):
class TooManyConnectionsError(PooledPgError):
"""Too many database connections were opened."""


# deprecated alias names for error classes
InvalidConnection = InvalidConnectionError
TooManyConnections = TooManyConnectionsError


class PooledPg:
"""Pool for classic PyGreSQL connections.
Expand Down Expand Up @@ -208,7 +213,7 @@ def connection(self):
"""Get a steady, cached PostgreSQL connection from the pool."""
if self._connections:
if not self._connections.acquire(self._blocking):
raise TooManyConnections
raise TooManyConnectionsError
try:
con = self._cache.get_nowait()
except Empty:
Expand Down Expand Up @@ -290,7 +295,7 @@ def __getattr__(self, name):
"""Proxy all members of the class."""
if self._con:
return getattr(self._con, name)
raise InvalidConnection
raise InvalidConnectionError

def __del__(self):
"""Delete the pooled connection."""
Expand Down
2 changes: 1 addition & 1 deletion dbutils/simple_pooled_pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
Licensed under the MIT license.
"""

from pg import DB as PgConnection
from pg import DB as PgConnection # noqa: N811

from . import __version__

Expand Down
8 changes: 6 additions & 2 deletions dbutils/steady_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,14 @@ class SteadyDBError(Exception):
"""General SteadyDB error."""


class InvalidCursor(SteadyDBError):
class InvalidCursorError(SteadyDBError):
"""Database cursor is invalid."""


# deprecated alias names for error classes
InvalidCursor = InvalidCursorError


def connect(
creator, maxusage=None, setsession=None,
failures=None, ping=1, closeable=True, *args, **kwargs):
Expand Down Expand Up @@ -703,7 +707,7 @@ def __getattr__(self, name):
# make execution methods "tough"
return self._get_tough_method(name)
return getattr(self._cursor, name)
raise InvalidCursor
raise InvalidCursorError

def __del__(self):
"""Delete the steady cursor."""
Expand Down
10 changes: 7 additions & 3 deletions dbutils/steady_pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
Licensed under the MIT license.
"""

from pg import DB as PgConnection
from pg import DB as PgConnection # noqa: N811

from . import __version__

Expand All @@ -78,10 +78,14 @@ class SteadyPgError(Exception):
"""General SteadyPg error."""


class InvalidConnection(SteadyPgError):
class InvalidConnectionError(SteadyPgError):
"""Database connection is invalid."""


# deprecated alias names for error classes
InvalidConnection = InvalidConnectionError


class SteadyPgConnection:
"""Class representing steady connections to a PostgreSQL database.
Expand Down Expand Up @@ -299,7 +303,7 @@ def __getattr__(self, name):
or name.startswith('get_')):
attr = self._get_tough_method(attr)
return attr
raise InvalidConnection
raise InvalidConnectionError

def __del__(self):
"""Delete the steady connection."""
Expand Down
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ select = [
]
# You can use `ruff rule ...` to see what these ignored rules check
ignore = [
"N811",
"N818",
"PIE790",
"PLR5501",
"PTH122",
Expand Down
48 changes: 24 additions & 24 deletions tests/test_pooled_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
import pytest

from dbutils.pooled_db import (
InvalidConnection,
InvalidConnectionError,
NotSupportedError,
PooledDB,
SharedDBConnection,
TooManyConnections,
TooManyConnectionsError,
)
from dbutils.steady_db import SteadyDBConnection

Expand Down Expand Up @@ -207,7 +207,7 @@ def test_close_connection(dbapi, threadsafety): # noqa: F811
if shareable:
assert db._shared_con is None
assert shared_con.shared == 0
with pytest.raises(InvalidConnection):
with pytest.raises(InvalidConnectionError):
assert db._usage
assert not hasattr(db_con, '_num_queries')
assert len(pool._idle_cache) == 1
Expand Down Expand Up @@ -692,9 +692,9 @@ def test_maxconnections(dbapi, threadsafety): # noqa: F811, PLR0915
assert len(pool._idle_cache) == 0
if shareable:
assert len(pool._shared_cache) == 0
with pytest.raises(TooManyConnections):
with pytest.raises(TooManyConnectionsError):
pool.connection(False)
with pytest.raises(TooManyConnections):
with pytest.raises(TooManyConnectionsError):
pool.connection()
cache = []
assert pool._connections == 0
Expand All @@ -712,13 +712,13 @@ def test_maxconnections(dbapi, threadsafety): # noqa: F811, PLR0915
assert len(pool._shared_cache) == 2
else:
assert pool._connections == 3
with pytest.raises(TooManyConnections):
with pytest.raises(TooManyConnectionsError):
pool.connection(False)
if shareable:
cache.append(pool.connection(True))
assert pool._connections == 3
else:
with pytest.raises(TooManyConnections):
with pytest.raises(TooManyConnectionsError):
pool.connection()
del cache
assert pool._connections == 0
Expand All @@ -732,9 +732,9 @@ def test_maxconnections(dbapi, threadsafety): # noqa: F811, PLR0915
assert len(pool._idle_cache) == 0
if shareable:
assert len(pool._shared_cache) == 0
with pytest.raises(TooManyConnections):
with pytest.raises(TooManyConnectionsError):
pool.connection(False)
with pytest.raises(TooManyConnections):
with pytest.raises(TooManyConnectionsError):
pool.connection()
assert db
del db
Expand All @@ -750,17 +750,17 @@ def test_maxconnections(dbapi, threadsafety): # noqa: F811, PLR0915
assert len(pool._shared_cache) == 1
assert pool._shared_cache[0].shared == 2
else:
with pytest.raises(TooManyConnections):
with pytest.raises(TooManyConnectionsError):
pool.connection()
with pytest.raises(TooManyConnections):
with pytest.raises(TooManyConnectionsError):
pool.connection(False)
if shareable:
cache.append(pool.connection(True))
assert pool._connections == 1
assert len(pool._shared_cache) == 1
assert pool._shared_cache[0].shared == 3
else:
with pytest.raises(TooManyConnections):
with pytest.raises(TooManyConnectionsError):
pool.connection(True)
del cache
assert pool._connections == 0
Expand All @@ -786,9 +786,9 @@ def test_maxconnections(dbapi, threadsafety): # noqa: F811, PLR0915
assert len(pool._idle_cache) == 0
if shareable:
assert len(pool._shared_cache) == 0
with pytest.raises(TooManyConnections):
with pytest.raises(TooManyConnectionsError):
pool.connection(False)
with pytest.raises(TooManyConnections):
with pytest.raises(TooManyConnectionsError):
pool.connection()
pool = PooledDB(dbapi, 4, 3, 2, 1, False)
assert pool._maxconnections == 4
Expand All @@ -799,9 +799,9 @@ def test_maxconnections(dbapi, threadsafety): # noqa: F811, PLR0915
cache.append(pool.connection(False))
assert pool._connections == 4
assert len(pool._idle_cache) == 0
with pytest.raises(TooManyConnections):
with pytest.raises(TooManyConnectionsError):
pool.connection(False)
with pytest.raises(TooManyConnections):
with pytest.raises(TooManyConnectionsError):
pool.connection()
pool = PooledDB(dbapi, 1, 2, 3, 4, False)
assert pool._maxconnections == 4
Expand All @@ -819,9 +819,9 @@ def test_maxconnections(dbapi, threadsafety): # noqa: F811, PLR0915
assert pool._connections == 4
else:
assert pool._connections == 4
with pytest.raises(TooManyConnections):
with pytest.raises(TooManyConnectionsError):
pool.connection()
with pytest.raises(TooManyConnections):
with pytest.raises(TooManyConnectionsError):
pool.connection(False)
pool = PooledDB(dbapi, 0, 0, 3, 3, False)
assert pool._maxconnections == 3
Expand All @@ -830,9 +830,9 @@ def test_maxconnections(dbapi, threadsafety): # noqa: F811, PLR0915
for _i in range(3):
cache.append(pool.connection(False))
assert pool._connections == 3
with pytest.raises(TooManyConnections):
with pytest.raises(TooManyConnectionsError):
pool.connection(False)
with pytest.raises(TooManyConnections):
with pytest.raises(TooManyConnectionsError):
pool.connection(True)
cache = []
assert pool._connections == 0
Expand All @@ -844,9 +844,9 @@ def test_maxconnections(dbapi, threadsafety): # noqa: F811, PLR0915
cache.append(pool.connection())
assert pool._connections == 3
else:
with pytest.raises(TooManyConnections):
with pytest.raises(TooManyConnectionsError):
pool.connection()
with pytest.raises(TooManyConnections):
with pytest.raises(TooManyConnectionsError):
pool.connection(False)
pool = PooledDB(dbapi, 0, 0, 3)
assert pool._maxconnections == 0
Expand Down Expand Up @@ -1165,7 +1165,7 @@ def test_shared_in_transaction(dbapi): # noqa: F811
db = pool.connection()
db.begin()
pool.connection(False)
with pytest.raises(TooManyConnections):
with pytest.raises(TooManyConnectionsError):
pool.connection()
pool = PooledDB(dbapi, 0, 2, 2)
db1 = pool.connection()
Expand All @@ -1183,7 +1183,7 @@ def test_shared_in_transaction(dbapi): # noqa: F811
db.close()
db2.begin()
pool.connection(False)
with pytest.raises(TooManyConnections):
with pytest.raises(TooManyConnectionsError):
pool.connection()
db1.rollback()
db = pool.connection()
Expand Down

0 comments on commit 6414e00

Please sign in to comment.