feat(mongodb): Add Atlas Local for MongoDb by jeff-goddard · Pull Request #873 · testcontainers/testcontainers-python · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/mongodb/README.rst
92 changes: 92 additions & 0 deletions modules/mongodb/testcontainers/mongodb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from testcontainers.core.generic import DbContainer
from testcontainers.core.utils import raise_for_deprecated_parameter
from testcontainers.core.wait_strategies import HealthcheckWaitStrategy
from testcontainers.core.waiting_utils import wait_for_logs


Expand Down Expand Up @@ -87,3 +88,94 @@ def predicate(text: str) -> bool:

def get_connection_client(self) -> MongoClient:
return MongoClient(self.get_connection_url())


class MongoDBAtlasLocalContainer(DbContainer):
"""
MongoDB Atlas Local document-based database container.

This is the local version of the Mongo Atlas service.
It includes Mongo DB and Mongo Atlas Search services
Example:

.. doctest::

>>> from testcontainers.mongodb import MongoDBAtlasLocalContainer
>>> import time
>>> with MongoDBAtlasLocalContainer("mongodb/mongodb-atlas-local:8.0.13") as mongo:
... db = mongo.get_connection_client().test
... # Insert a database entry
... result = db.restaurants.insert_one(
... {
... "name": "Vella",
... "cuisine": "Italian",
... "restaurant_id": "123456"
... }
... )
... # add an index
... _ = db.restaurants.create_search_index(
... {
... "definition": {
... "mappings": {
... "dynamic": True
... }
... },
... "name": "default"
... }
... )
... # wait for the index to be created
... time.sleep(1)
...
... # Find the restaurant document
... result = db.restaurants.aggregate([{
... "$search": {
... "index": "default",
... "text": {
... "query": "Vella",
... "path": "name"
... }
... }
... }]).next()
... result["restaurant_id"]
'123456'
"""

def __init__(
self,
image: str = "mongodb/mongodb-atlas-local:latest",
port: int = 27017,
username: Optional[str] = None,
password: Optional[str] = None,
dbname: Optional[str] = None,
**kwargs,
) -> None:
raise_for_deprecated_parameter(kwargs, "port_to_expose", "port")
super().__init__(image=image, **kwargs)
self.username = username if username else os.environ.get("MONGODB_INITDB_ROOT_USERNAME", "test")
self.password = password if password else os.environ.get("MONGODB_INITDB_ROOT_PASSWORD", "test")
self.dbname = dbname if dbname else os.environ.get("MONGODB_INITDB_DATABASE", "test")
self.port = port
self.with_exposed_ports(self.port)

def _configure(self) -> None:
self.with_env("MONGODB_INITDB_ROOT_USERNAME", self.username)
self.with_env("MONGODB_INITDB_ROOT_PASSWORD", self.password)
self.with_env("MONGODB_INITDB_DATABASE", self.dbname)

def get_connection_url(self) -> str:
return (
self._create_connection_url(
dialect="mongodb",
username=self.username,
password=self.password,
port=self.port,
)
+ "?directConnection=true"
)

def _connect(self) -> None:
strategy = HealthcheckWaitStrategy()
strategy.wait_until_ready(self)

def get_connection_client(self) -> MongoClient:
return MongoClient(self.get_connection_url())
50 changes: 49 additions & 1 deletion modules/mongodb/tests/test_mongodb.py
Loading