You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Some MongoDB features such as change streams do not work with standalone instances. In that case a replica set is required. It would be nice if there was an option to start a database as a single-member replica set for being able to use change streams, transactions etc. also in tests.
Here is workaround that I am currently using in most affected projects. As this is required more often, it would be nice if this was part of the pytest-plugin itself:
@pytest.fixture(scope="session")defmongo_repl_proc(mongo_proc):
client=pymongo.MongoClient(f"mongodb://{mongo_proc.host}:{mongo_proc.port}")
client.admin.command("replSetInitiate")
time.sleep(1)
returnmongo_proc@pytest.fixturedefmongo(mongo_repl_proc): # noqa: CCR001""" Provide an empty mongodb client. This is a modification of the fixture from pytest-mongo to support replica sets. These require returning a client that specifically knows about them. """client=pymongo.MongoClient(
f"mongodb://{mongo_repl_proc.host}:{mongo_repl_proc.port}",
replicaset="rep0",
tz_aware=True,
# Ensures that write operations in tests are always visible to subsequent reads# without using explicit sessions.journal=True,
)
# wait for nodeswhilenotclient.nodes:
time.sleep(0.1)
yieldclient# clean the databasefordb_nameinclient.list_database_names():
ifdb_name=="config":
continuedatabase=client[db_name]
forcollection_nameindatabase.list_collection_names():
collection=database[collection_name]
# Do not delete any of Mongo system collectionsif (
collection.name.startswith("system.")
orcollection.name.startswith("oplog.")
orcollection.name.startswith("replset.")
):
continuecollection.drop()
An in pytest.ini:
mongo_params = --replSet rep0
The text was updated successfully, but these errors were encountered:
Some MongoDB features such as change streams do not work with standalone instances. In that case a replica set is required. It would be nice if there was an option to start a database as a single-member replica set for being able to use change streams, transactions etc. also in tests.
Here is workaround that I am currently using in most affected projects. As this is required more often, it would be nice if this was part of the pytest-plugin itself:
An in
pytest.ini
:The text was updated successfully, but these errors were encountered: