Skip to content

Commit 71f2a87

Browse files
Harmonise container and improve error messages (#106)
* Harmonise mongodb container with other db containers. * Improve error message for unstarted container (fixes #28).
1 parent 4d8520f commit 71f2a87

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

testcontainers/core/generic.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,17 @@ def _connect(self):
2929
def get_connection_url(self):
3030
raise NotImplementedError
3131

32-
def _create_connection_url(self, dialect, username, password, port, db_name):
32+
def _create_connection_url(self, dialect, username, password, port, db_name=None):
33+
if self._container is None:
34+
raise RuntimeError("container has not been started")
3335
host = self.get_container_host_ip()
3436
port = self.get_exposed_port(port)
35-
return "{dialect}://{username}:{password}@{host}:{port}/{db}".format(
36-
dialect=dialect, username=username, password=password, host=host, port=port, db=db_name
37+
url = "{dialect}://{username}:{password}@{host}:{port}".format(
38+
dialect=dialect, username=username, password=password, host=host, port=port
3739
)
40+
if db_name:
41+
url += '/' + db_name
42+
return url
3843

3944
def start(self):
4045
self._configure()

testcontainers/mongodb.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
# under the License.
1313
import os
1414

15-
from testcontainers.core.generic import DockerContainer
15+
from testcontainers.core.generic import DbContainer
16+
from testcontainers.core.waiting_utils import wait_container_is_ready
1617

1718

18-
class MongoDbContainer(DockerContainer):
19+
class MongoDbContainer(DbContainer):
1920
"""
2021
Mongo document-based database container.
2122
@@ -55,22 +56,23 @@ def __init__(self, image="mongo:latest"):
5556
self.port_to_expose = 27017
5657
self.with_exposed_ports(self.port_to_expose)
5758

58-
def start(self):
59-
self._configure()
60-
super().start()
61-
return self
62-
6359
def _configure(self):
6460
self.with_env("MONGO_INITDB_ROOT_USERNAME", self.MONGO_INITDB_ROOT_USERNAME)
6561
self.with_env("MONGO_INITDB_ROOT_PASSWORD", self.MONGO_INITDB_ROOT_PASSWORD)
6662
self.with_env("MONGO_DB", self.MONGO_DB)
6763

6864
def get_connection_url(self):
69-
port = self.get_exposed_port(self.port_to_expose)
70-
return "mongodb://{}:{}@{}:{}".format(self.MONGO_INITDB_ROOT_USERNAME,
71-
self.MONGO_INITDB_ROOT_PASSWORD,
72-
self.get_container_host_ip(), port)
65+
return self._create_connection_url(
66+
dialect='mongodb',
67+
username=self.MONGO_INITDB_ROOT_USERNAME,
68+
password=self.MONGO_INITDB_ROOT_PASSWORD,
69+
port=self.port_to_expose,
70+
)
7371

74-
def get_connection_client(self):
72+
@wait_container_is_ready()
73+
def _connect(self):
7574
from pymongo import MongoClient
7675
return MongoClient(self.get_connection_url())
76+
77+
def get_connection_client(self):
78+
return self._connect()

0 commit comments

Comments
 (0)