Skip to content

Commit bfb8200

Browse files
committed
Remove static variables from mongodb.
1 parent d357376 commit bfb8200

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

mongodb/testcontainers/mongodb/__init__.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from pymongo import MongoClient
1515
from testcontainers.core.generic import DbContainer
1616
from testcontainers.core.waiting_utils import wait_container_is_ready
17+
from typing import Optional
1718

1819

1920
class MongoDbContainer(DbContainer):
@@ -46,26 +47,27 @@ class MongoDbContainer(DbContainer):
4647
... # Find the restaurant document
4748
... cursor = db.restaurants.find({"borough": "Manhattan"})
4849
"""
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:
5453
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")
5557
self.command = "mongo"
5658
self.port_to_expose = port_to_expose
5759
self.with_exposed_ports(self.port_to_expose)
5860

5961
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)
6365

6466
def get_connection_url(self) -> str:
6567
return self._create_connection_url(
6668
dialect='mongodb',
67-
username=self.MONGO_INITDB_ROOT_USERNAME,
68-
password=self.MONGO_INITDB_ROOT_PASSWORD,
69+
username=self.username,
70+
password=self.password,
6971
port=self.port_to_expose,
7072
)
7173

0 commit comments

Comments
 (0)