|
12 | 12 | # under the License.
|
13 | 13 | import os
|
14 | 14 |
|
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 |
16 | 17 |
|
17 | 18 |
|
18 |
| -class MongoDbContainer(DockerContainer): |
| 19 | +class MongoDbContainer(DbContainer): |
19 | 20 | """
|
20 | 21 | Mongo document-based database container.
|
21 | 22 |
|
@@ -55,22 +56,23 @@ def __init__(self, image="mongo:latest"):
|
55 | 56 | self.port_to_expose = 27017
|
56 | 57 | self.with_exposed_ports(self.port_to_expose)
|
57 | 58 |
|
58 |
| - def start(self): |
59 |
| - self._configure() |
60 |
| - super().start() |
61 |
| - return self |
62 |
| - |
63 | 59 | def _configure(self):
|
64 | 60 | self.with_env("MONGO_INITDB_ROOT_USERNAME", self.MONGO_INITDB_ROOT_USERNAME)
|
65 | 61 | self.with_env("MONGO_INITDB_ROOT_PASSWORD", self.MONGO_INITDB_ROOT_PASSWORD)
|
66 | 62 | self.with_env("MONGO_DB", self.MONGO_DB)
|
67 | 63 |
|
68 | 64 | 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 | + ) |
73 | 71 |
|
74 |
| - def get_connection_client(self): |
| 72 | + @wait_container_is_ready() |
| 73 | + def _connect(self): |
75 | 74 | from pymongo import MongoClient
|
76 | 75 | return MongoClient(self.get_connection_url())
|
| 76 | + |
| 77 | + def get_connection_client(self): |
| 78 | + return self._connect() |
0 commit comments