Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add repr to dbs to help debug with orion db test #958

Merged
merged 4 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
bouthilx marked this conversation as resolved.
Show resolved Hide resolved
return f"{self.__class__.__name__}(host={self.host})"
bouthilx marked this conversation as resolved.
Show resolved Hide resolved

@abstractproperty
def is_connected(self):
"""True, if practical connection has been achieved."""
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):
bouthilx marked this conversation as resolved.
Show resolved Hide resolved
return f"{self.__class__.__name__}()"
bouthilx marked this conversation as resolved.
Show resolved Hide resolved

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

def __repr__(self):
bouthilx marked this conversation as resolved.
Show resolved Hide resolved
return "{name}({args})".format(
name=self.__class__.__name__,
bouthilx marked this conversation as resolved.
Show resolved Hide resolved
args=", ".join(
f"{name}={getattr(self, name)}"
for name in ["host", "name", "port", "username", "password", "options"]
),
)

def __getstate__(self):
state = dict()
for key in ["host", "name", "port", "username", "password", "options"]:
Expand Down Expand Up @@ -151,7 +160,7 @@ def initiate_connection(self):
port=self.port,
username=self.username,
password=self.password,
**self.options
**self.options,
)
self._db = self._conn[self.name]
self._db.command("ismaster") # .. seealso:: :meth:`is_connected`
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):
bouthilx marked this conversation as resolved.
Show resolved Hide resolved
return f"{self.__class__.__name__}(host={self.host}, timeout={self.timeout})"
bouthilx marked this conversation as resolved.
Show resolved Hide resolved

@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):
bouthilx marked this conversation as resolved.
Show resolved Hide resolved
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):
bouthilx marked this conversation as resolved.
Show resolved Hide resolved
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):
bouthilx marked this conversation as resolved.
Show resolved Hide resolved
assert (
str(orion_db) == f"PickledDB(host={orion_db.host}, timeout={orion_db.timeout})"
)