From ccc4283f7b89c6e8ab43518bd67f451b748c2cee Mon Sep 17 00:00:00 2001 From: Ertugrul Karademir Date: Sat, 28 Oct 2023 07:15:47 +0100 Subject: [PATCH] feat(config): add configuration for dbname Signed-off-by: Ertugrul Karademir --- README.md | 1 + pymongo_inmemory/context.py | 4 +++- pymongo_inmemory/mongod.py | 25 +++++++++++++++++++------ tests/unit/test_mongod.py | 14 +++++++++++++- 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 0411738..b323873 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,7 @@ with MongoClient() as client: | | `download_folder` | Override the default download location. | pymongo_inmemory/.cache/download | | | `extract_folder` | Override the default extraction location. | pymongo_inmemory/.cache/extract | | **NEW** | `mongod_data_folder` | Provide a data folder to be used by MongoD. | A `TemporaryDirectory` will be used | +| **NEW** | `dbname` | Provide a database name to connect | 'pimtest' | | | | | - \***_Note 1:_** Generic Linux version offering for MongoDB ends with version **4.0.23**. If the operating system is just `linux` and if selected MongoDB version is higher, it will default to `4.0.23`. diff --git a/pymongo_inmemory/context.py b/pymongo_inmemory/context.py index e52083c..72b3e02 100644 --- a/pymongo_inmemory/context.py +++ b/pymongo_inmemory/context.py @@ -104,6 +104,7 @@ def __init__( self.mongo_version = conf("mongo_version", version) self.mongod_port = conf("mongod_port", None, coerce_with=int) self.mongod_data_folder = conf("mongod_data_folder", None) + self.dbname = conf("mongod_data_folder", "pimtest") self.operating_system = self._build_operating_system_info(os_name) self.os_version = conf("os_version", os_ver) @@ -131,10 +132,11 @@ def __str__(self): f"Mongo Version {self.mongo_version}\n" f"MongoD Port {self.mongod_port}\n" f"MongoD Data Folder {self.mongod_data_folder}\n" + f"Database Name {self.dbname}\n" f"OS Name {self.operating_system}\n" f"OS Version {self.os_version}\n" f"Download URL {self.download_url}\n" - f"URL hash {self.url_hash}\n" + f"URL Hash {self.url_hash}\n" f"Download Version {self.downloaded_version}\n" f"Ignore Cache {self.ignore_cache}\n" f"Use Local MongoD {self.use_local_mongod}\n" diff --git a/pymongo_inmemory/mongod.py b/pymongo_inmemory/mongod.py index 6b0fc58..36c59e9 100644 --- a/pymongo_inmemory/mongod.py +++ b/pymongo_inmemory/mongod.py @@ -61,6 +61,20 @@ def port(self): else: return set_port + @property + def connection_string(self): + if self.local_address is not None and self.port is not None: + if self._pim_context.dbname is None: + return "mongodb://{host}:{port}".format( + host=self.local_address, port=self.port + ) + else: + return "mongodb://{host}:{port}/{dbname}".format( + host=self.local_address, + port=self.port, + dbname=self._pim_context.dbname, + ) + class Mongod: """Wrapper for MongoDB daemon instance. Can be used with context managers. @@ -146,12 +160,11 @@ def connection_string(self): if self._connection_string is not None: return self._connection_string - if self.config.local_address is not None and self.config.port is not None: - self._connection_string = "mongodb://{host}:{port}".format( - host=self.config.local_address, port=self.config.port - ) - else: - self._connection_string = None + self._connection_string = ( + self.config.connection_string + if self.config.connection_string is not None + else None + ) return self._connection_string diff --git a/tests/unit/test_mongod.py b/tests/unit/test_mongod.py index 986c7be..c269f40 100644 --- a/tests/unit/test_mongod.py +++ b/tests/unit/test_mongod.py @@ -25,7 +25,7 @@ def download(): return "" -def test_mongod(monkeypatch): +def test_mongod_data_folder_config(monkeypatch): monkeypatch.setattr(subprocess, "Popen", Popen) monkeypatch.setattr(Mongod, "is_healthy", returns_true) monkeypatch.setattr(downloader, "download", download) @@ -35,3 +35,15 @@ def test_mongod(monkeypatch): with Mongod(context) as md: assert md.data_folder == "TEST" + + +def test_dbname_config(monkeypatch): + monkeypatch.setattr(subprocess, "Popen", Popen) + monkeypatch.setattr(Mongod, "is_healthy", returns_true) + monkeypatch.setattr(downloader, "download", download) + + context = Context() + context.dbname = "TEST" + + with Mongod(context) as md: + assert md.config.connection_string.endswith("TEST")