|
14 | 14 | from pymongo import MongoClient
|
15 | 15 | from testcontainers.core.generic import DbContainer
|
16 | 16 | from testcontainers.core.waiting_utils import wait_container_is_ready
|
| 17 | +from typing import Optional |
17 | 18 |
|
18 | 19 |
|
19 | 20 | class MongoDbContainer(DbContainer):
|
@@ -46,26 +47,27 @@ class MongoDbContainer(DbContainer):
|
46 | 47 | ... # Find the restaurant document
|
47 | 48 | ... cursor = db.restaurants.find({"borough": "Manhattan"})
|
48 | 49 | """
|
49 |
| - MONGO_INITDB_ROOT_USERNAME = os.environ.get("MONGO_INITDB_ROOT_USERNAME", "test") |
50 |
| - MONGO_INITDB_ROOT_PASSWORD = os.environ.get("MONGO_INITDB_ROOT_PASSWORD", "test") |
51 |
| - MONGO_DB = os.environ.get("MONGO_DB", "test") |
52 |
| - |
53 |
| - def __init__(self, image: str = "mongo:latest", port_to_expose: int = 27017, **kwargs) -> None: |
| 50 | + def __init__(self, image: str = "mongo:latest", port_to_expose: int = 27017, |
| 51 | + username: Optional[str] = None, password: Optional[str] = None, |
| 52 | + dbname: Optional[str] = None, **kwargs) -> None: |
54 | 53 | super(MongoDbContainer, self).__init__(image=image, **kwargs)
|
| 54 | + self.username = username or os.environ.get("MONGO_INITDB_ROOT_USERNAME", "test") |
| 55 | + self.password = password or os.environ.get("MONGO_INITDB_ROOT_PASSWORD", "test") |
| 56 | + self.dbname = dbname or os.environ.get("MONGO_DB", "test") |
55 | 57 | self.command = "mongo"
|
56 | 58 | self.port_to_expose = port_to_expose
|
57 | 59 | self.with_exposed_ports(self.port_to_expose)
|
58 | 60 |
|
59 | 61 | def _configure(self) -> None:
|
60 |
| - self.with_env("MONGO_INITDB_ROOT_USERNAME", self.MONGO_INITDB_ROOT_USERNAME) |
61 |
| - self.with_env("MONGO_INITDB_ROOT_PASSWORD", self.MONGO_INITDB_ROOT_PASSWORD) |
62 |
| - self.with_env("MONGO_DB", self.MONGO_DB) |
| 62 | + self.with_env("MONGO_INITDB_ROOT_USERNAME", self.username) |
| 63 | + self.with_env("MONGO_INITDB_ROOT_PASSWORD", self.password) |
| 64 | + self.with_env("MONGO_DB", self.dbname) |
63 | 65 |
|
64 | 66 | def get_connection_url(self) -> str:
|
65 | 67 | return self._create_connection_url(
|
66 | 68 | dialect='mongodb',
|
67 |
| - username=self.MONGO_INITDB_ROOT_USERNAME, |
68 |
| - password=self.MONGO_INITDB_ROOT_PASSWORD, |
| 69 | + username=self.username, |
| 70 | + password=self.password, |
69 | 71 | port=self.port_to_expose,
|
70 | 72 | )
|
71 | 73 |
|
|
0 commit comments