Skip to content

Commit

Permalink
Merge pull request #958 from bouthilx/feature/db_repr
Browse files Browse the repository at this point in the history
Add repr to dbs to help debug with orion db test
  • Loading branch information
bouthilx committed Jul 6, 2022
2 parents cff4374 + e4b78e1 commit 20994af
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/orion/core/io/database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ def __init__(
self._conn = None
self.initiate_connection()

def __repr__(self) -> str:
return f"{type(self).__qualname__}(host={self.host})"

@property
@abstractmethod
def is_connected(self):
Expand Down
3 changes: 3 additions & 0 deletions src/orion/core/io/database/ephemeraldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class EphemeralDB(Database):
"""

def __repr__(self) -> str:
return f"{type(self).__qualname__}()"

@property
def is_connected(self):
"""Return true, always."""
Expand Down
8 changes: 8 additions & 0 deletions src/orion/core/io/database/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ def __init__(
authSource=name,
)

def __repr__(self) -> str:
name = type(self).__qualname__
args = ", ".join(
f"{name}={getattr(self, name)}"
for name in ["host", "name", "port", "username", "password", "options"]
)
return f"{name}({args})"

def __getstate__(self):
state = {}
for key in ["host", "name", "port", "username", "password", "options"]:
Expand Down
3 changes: 3 additions & 0 deletions src/orion/core/io/database/pickleddb.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ def __init__(self, host="", timeout=60, *args, **kwargs):
if os.path.dirname(host):
os.makedirs(os.path.dirname(host), exist_ok=True)

def __repr__(self) -> str:
return f"{type(self).__qualname__}(host={self.host}, timeout={self.timeout})"

@property
def is_connected(self):
"""Return true, always."""
Expand Down
4 changes: 4 additions & 0 deletions tests/unittests/core/database/test_ephemeraldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def collection(document, db_type):
# TESTS SET


def test_repr(orion_db: EphemeralDB):
assert str(orion_db) == f"EphemeralDB()"


@pytest.mark.usefixtures("clean_db")
class TestIndex(object):
"""Test index for :meth:`orion.core.io.database.ephemeraldb.EphemeralCollection`."""
Expand Down
7 changes: 7 additions & 0 deletions tests/unittests/core/database/test_mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,10 @@ def test_non_converted_errors(self, orion_db, test_collection):

with pytest.raises(pymongo.errors.OperationFailure):
orion_db.read_and_write("test_collection", query, config_to_add)


def test_repr(orion_db: MongoDB):
assert str(orion_db) == (
f"MongoDB(host=localhost, name=orion_test, port=27017, username=user, "
f"password=pass, options={orion_db.options})"
)
6 changes: 6 additions & 0 deletions tests/unittests/core/database/test_pickleddb.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,9 @@ def _get_fs(path):
monkeypatch.setattr("orion.core.io.database.pickleddb._get_fs", _get_fs)

assert isinstance(_create_lock("/whatever/the/path/is"), file_lock_class)


def test_repr(orion_db: PickledDB):
assert (
str(orion_db) == f"PickledDB(host={orion_db.host}, timeout={orion_db.timeout})"
)

0 comments on commit 20994af

Please sign in to comment.