Skip to content

Commit

Permalink
feat(config): add configuration for host and dbname (#94)
Browse files Browse the repository at this point in the history
* feat(config): add configuration for dbname

Signed-off-by: Ertugrul Karademir <[email protected]>

* feat(config): add host configuration

Signed-off-by: Ertugrul Karademir <[email protected]>

* chore: update readme

Signed-off-by: Ertugrul Karademir <[email protected]>

* chore: update readme

Signed-off-by: Ertugrul Karademir <[email protected]>

* fix: lint

Signed-off-by: Ertugrul Karademir <[email protected]>

---------

Signed-off-by: Ertugrul Karademir <[email protected]>
  • Loading branch information
ekarademir authored Oct 28, 2023
1 parent a840985 commit 9e8db8c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 12 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ A mongo mocking library with an ephemeral MongoDB running in memory.

- Tooling enhancements. [[PR #90](https://github.com/kaizendorks/pymongo_inmemory/pull/90)]
- Configuration for data directory. [[PR #90](https://github.com/kaizendorks/pymongo_inmemory/pull/91)]
- Configuration for data directory. [[PR #90](https://github.com/kaizendorks/pymongo_inmemory/pull/94)]

### v0.3.1

Expand Down Expand Up @@ -74,6 +75,8 @@ 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** | `mongo_client_host` | Hostname or connection string | |
| **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`.
Expand Down
4 changes: 4 additions & 0 deletions pymongo_inmemory/_pim.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
class MongoClient(pymongo.MongoClient):
def __init__(self, host=None, port=None, **kwargs):
self._pim_context: Context = Context()
if port is not None:
self._pim_context.mongod_port = port
if host is not None:
self._pim_context.mongo_client_host = host
self._mongod = Mongod(self._pim_context)
self._mongod.start()
super().__init__(self._mongod.connection_string, **kwargs)
Expand Down
5 changes: 4 additions & 1 deletion pymongo_inmemory/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ 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("dbname", "pimtest")
self.mongo_client_host = conf("mongo_client_host", None)

self.operating_system = self._build_operating_system_info(os_name)
self.os_version = conf("os_version", os_ver)
Expand Down Expand Up @@ -131,10 +133,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"
Expand Down
31 changes: 25 additions & 6 deletions pymongo_inmemory/mongod.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ def port(self):
else:
return set_port

@property
def connection_string(self):
if self._pim_context.mongo_client_host is not None:
if self._pim_context.mongo_client_host.startswith("mongodb://"):
return self._pim_context.mongo_client_host
else:
self.local_address = self._pim_context.mongo_client_host

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.
Expand Down Expand Up @@ -146,12 +166,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

Expand Down
29 changes: 24 additions & 5 deletions tests/unit/test_mongod.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from pymongo_inmemory.mongod import Mongod
import pymongo_inmemory.downloader as downloader
from pymongo_inmemory.context import Context


class Popen:
Expand All @@ -25,13 +24,33 @@ 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)

context = Context()
context.mongod_data_folder = "TEST"
monkeypatch.setenv("PYMONGOIM__MONGOD_DATA_FOLDER", "TEST")

with Mongod(context) as md:
with Mongod(None) 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)

monkeypatch.setenv("PYMONGOIM__DBNAME", "TEST")

with Mongod(None) as md:
assert md.config.connection_string.endswith("TEST")


def test_mongo_client_host_config(monkeypatch):
monkeypatch.setattr(subprocess, "Popen", Popen)
monkeypatch.setattr(Mongod, "is_healthy", returns_true)
monkeypatch.setattr(downloader, "download", download)
monkeypatch.setenv("PYMONGOIM__MONGO_CLIENT_HOST", "mongodb://test")

with Mongod(None) as md:
assert md.config.connection_string == "mongodb://test"

0 comments on commit 9e8db8c

Please sign in to comment.