kaizendorks / pymongo_inmemory

A mongo mocking library with an ephemeral MongoDB running in memory.
MIT License
39 stars 13 forks source link

The client loses database name #89

Closed warvariuc closed 11 months ago

warvariuc commented 1 year ago

Describe the bug To Reproduce

>>> import pymongo_inmemory
>>> client = pymongo_inmemory.MongoClient("mongodb://127.0.0.1/test")
{"t":{"$date":"2023-09-28T10:08:22.707Z"},"s":"I",  "c":"CONTROL",  "id":5324701, "ctx":"-","msg":"Test storage engine does not support enableMajorityReadConcern=true, forcibly setting to false","attr":{"storageEngine":"ephemeralForTest"}}
{"t":{"$date":"2023-09-28T10:08:22.709Z"},"s":"W",  "c":"CONTROL",  "id":4788401, "ctx":"-","msg":"Lock-free reads is not compatible with enableMajorityReadConcern=false: disabling lock-free reads."}
>>> client._MongoClient__default_database_name
>>> client.get_default_database()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../site-packages/pymongo/mongo_client.py", line 2247, in get_default_database
    raise ConfigurationError("No default database name defined or provided.")
pymongo.errors.ConfigurationError: No default database name defined or provided.
>>>

Expected behavior The database name from the connection strings should be kept.

Logs

class MongoClient(pymongo.MongoClient):
    def __init__(self, host=None, port=None, **kwargs):
        self._pim_context: Context = Context()
        self._mongod = Mongod(self._pim_context)
        self._mongod.start()
        super().__init__(self._mongod.connection_string, **kwargs)

Probably this happens because the host argument is not sent to pymongo.MongoClient's constructor.

Screenshots

Context:

ekarademir commented 1 year ago

This is a very interesting case. Thank you for reporting.