|
17 | 17 |
|
18 | 18 | from testcontainers.core.generic import DbContainer
|
19 | 19 | from testcontainers.core.utils import raise_for_deprecated_parameter
|
| 20 | +from testcontainers.core.wait_strategies import HealthcheckWaitStrategy |
20 | 21 | from testcontainers.core.waiting_utils import wait_for_logs
|
21 | 22 |
|
22 | 23 |
|
@@ -81,3 +82,94 @@ def _connect(self) -> None:
|
81 | 82 |
|
82 | 83 | def get_connection_client(self) -> MongoClient:
|
83 | 84 | return MongoClient(self.get_connection_url())
|
| 85 | + |
| 86 | + |
| 87 | +class MongoDBAtlasLocalContainer(DbContainer): |
| 88 | + """ |
| 89 | + MongoDB Atlas Local document-based database container. |
| 90 | +
|
| 91 | + This is the local version of the Mongo Atlas service. |
| 92 | + It includes Mongo DB and Mongo Atlas Search services |
| 93 | + Example: |
| 94 | +
|
| 95 | + .. doctest:: |
| 96 | +
|
| 97 | + >>> from testcontainers.mongodb import MongoDBAtlasLocalContainer |
| 98 | + >>> import time |
| 99 | + >>> with MongoDBAtlasLocalContainer("mongodb/mongodb-atlas-local:8.0.13") as mongo: |
| 100 | + ... db = mongo.get_connection_client().test |
| 101 | + ... # Insert a database entry |
| 102 | + ... result = db.restaurants.insert_one( |
| 103 | + ... { |
| 104 | + ... "name": "Vella", |
| 105 | + ... "cuisine": "Italian", |
| 106 | + ... "restaurant_id": "123456" |
| 107 | + ... } |
| 108 | + ... ) |
| 109 | + ... # add an index |
| 110 | + ... db.restaurants.create_search_index( |
| 111 | + ... { |
| 112 | + ... "definition": { |
| 113 | + ... "mappings": { |
| 114 | + ... "dynamic": True |
| 115 | + ... } |
| 116 | + ... }, |
| 117 | + ... "name": "default" |
| 118 | + ... } |
| 119 | + ... ) |
| 120 | + ... # wait for the index to be created |
| 121 | + ... time.sleep(1) |
| 122 | + ... |
| 123 | + ... # Find the restaurant document |
| 124 | + ... result = db.restaurants.aggregate([{ |
| 125 | + ... "$search": { |
| 126 | + ... "index": "default", |
| 127 | + ... "text": { |
| 128 | + ... "query": "Vella", |
| 129 | + ... "path": "name" |
| 130 | + ... } |
| 131 | + ... } |
| 132 | + ... }]).next() |
| 133 | + ... result["restaurant_id"] |
| 134 | + '123456' |
| 135 | + """ |
| 136 | + |
| 137 | + def __init__( |
| 138 | + self, |
| 139 | + image: str = "mongodb/mongodb-atlas-local:latest", |
| 140 | + port: int = 27017, |
| 141 | + username: Optional[str] = None, |
| 142 | + password: Optional[str] = None, |
| 143 | + dbname: Optional[str] = None, |
| 144 | + **kwargs, |
| 145 | + ) -> None: |
| 146 | + raise_for_deprecated_parameter(kwargs, "port_to_expose", "port") |
| 147 | + super().__init__(image=image, **kwargs) |
| 148 | + self.username = username if username else os.environ.get("MONGODB_INITDB_ROOT_USERNAME", "test") |
| 149 | + self.password = password if password else os.environ.get("MONGODB_INITDB_ROOT_PASSWORD", "test") |
| 150 | + self.dbname = dbname if dbname else os.environ.get("MONGODB_INITDB_DATABASE", "test") |
| 151 | + self.port = port |
| 152 | + self.with_exposed_ports(self.port) |
| 153 | + |
| 154 | + def _configure(self) -> None: |
| 155 | + self.with_env("MONGODB_INITDB_ROOT_USERNAME", self.username) |
| 156 | + self.with_env("MONGODB_INITDB_ROOT_PASSWORD", self.password) |
| 157 | + self.with_env("MONGODB_INITDB_DATABASE", self.dbname) |
| 158 | + |
| 159 | + def get_connection_url(self) -> str: |
| 160 | + return ( |
| 161 | + self._create_connection_url( |
| 162 | + dialect="mongodb", |
| 163 | + username=self.username, |
| 164 | + password=self.password, |
| 165 | + port=self.port, |
| 166 | + ) |
| 167 | + + "?directConnection=true" |
| 168 | + ) |
| 169 | + |
| 170 | + def _connect(self) -> None: |
| 171 | + strategy = HealthcheckWaitStrategy() |
| 172 | + strategy.wait_until_ready(self) |
| 173 | + |
| 174 | + def get_connection_client(self) -> MongoClient: |
| 175 | + return MongoClient(self.get_connection_url()) |
0 commit comments