Skip to content

Commit

Permalink
Add representation methods to StorageMongoDb (#104)
Browse files Browse the repository at this point in the history
* add representation methods to StorageMongoDb
  • Loading branch information
peendebak authored Jan 20, 2022
1 parent 99ae4d3 commit 34f81b9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/qilib/utils/storage/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ def __init__(self, name: str, host: str = 'localhost', port: int = 27017, databa
self._serialize = serializer.encode_data
self._unserialize = serializer.decode_data

def __repr__(self) -> str:
return f'<{self.__class__.__name__} at 0x{id(self):x}: name {self._db.name}>'

def __str__(self) -> str:
return f'<{type(self).__name__}: name {self._db.name}>'

def _check_server_connection(self, timeout: float) -> None:
""" Check if connection has been established to database server."""
try:
Expand Down
8 changes: 8 additions & 0 deletions src/tests/unittests/utils/storage/test_storage_mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ def tearDown(self) -> None:
self.storage._collection.drop()
self.dummy_storage._collection.drop()

def test_repr(self):
s=str(self.storage)
self.assertTrue(s.startswith('<StorageMongoDb'))
self.assertIn(self.storage.name, s)
s=repr(self.storage)
self.assertIn(f'{id(self.storage):x}', s)
self.assertIn(self.storage.name, s)

def test_server_timeout(self):
error_msg = 'Failed to connect to Mongo database within 0.01 milliseconds$'
self.assertRaisesRegex(ConnectionTimeoutError, error_msg, StorageMongoDb, 'test', port=-1,
Expand Down

0 comments on commit 34f81b9

Please sign in to comment.