Skip to content

Commit 0721566

Browse files
committed
Fix bugs
1 parent fb4baf5 commit 0721566

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

pydynamodb/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
if TYPE_CHECKING:
77
from .connection import Connection
88

9-
__version__: str = "0.8.1"
9+
__version__: str = "0.8.2"
1010

1111
# Globals https://www.python.org/dev/peps/pep-0249/#globals
1212
apilevel: str = "2.0"

pydynamodb/sqlalchemy_dynamodb/pydynamodb.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,22 @@ def _create_connect_args(self, url):
712712
return opts
713713

714714
def do_ping(self, dbapi_connection):
715-
return dbapi_connection.test_connection()
715+
connection = dbapi_connection
716+
717+
for attr_name in ("driver_connection", "dbapi_connection", "connection"):
718+
nested_connection = getattr(connection, attr_name, None)
719+
if nested_connection is not None:
720+
connection = nested_connection
721+
break
722+
723+
test_connection = getattr(connection, "test_connection", None)
724+
if not callable(test_connection):
725+
return False
726+
727+
try:
728+
return bool(test_connection())
729+
except Exception:
730+
return False
716731

717732
def get_schema_names(self, connection, **kw):
718733
# DynamoDB does not have the concept of a schema

tests/test_sqlalchemy_dynamodb.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,35 @@ def test_ping(self, engine):
4040
conn = engine.raw_connection().dbapi_connection
4141
assert engine.dialect.do_ping(conn)
4242

43+
def test_ping_returns_false_without_test_connection(self, engine):
44+
engine, _ = engine
45+
46+
class ConnectionWithoutPing:
47+
pass
48+
49+
assert not engine.dialect.do_ping(ConnectionWithoutPing())
50+
51+
def test_ping_uses_wrapped_driver_connection(self, engine):
52+
engine, _ = engine
53+
54+
class WrappedConnection:
55+
def __init__(self):
56+
self.driver_connection = self
57+
58+
def test_connection(self):
59+
return True
60+
61+
assert engine.dialect.do_ping(WrappedConnection())
62+
63+
def test_ping_returns_false_when_test_connection_raises(self, engine):
64+
engine, _ = engine
65+
66+
class FailingConnection:
67+
def test_connection(self):
68+
raise RuntimeError("ping failed")
69+
70+
assert not engine.dialect.do_ping(FailingConnection())
71+
4372
def test_basic_insert(self, engine):
4473
engine, conn = engine
4574

0 commit comments

Comments
 (0)