@@ -33,24 +33,19 @@ class CrateDBContainer(DockerContainer):
3333
3434 .. doctest::
3535
36- >>> from cratedb_toolkit.testing. testcontainers. cratedb import CrateDBContainer
36+ >>> from testcontainers import cratedb import CrateDBContainer
3737 >>> import sqlalchemy
3838
39- >>> cratedb_container = CrateDBContainer("crate:5.2.3")
40- >>> cratedb_container.start()
41- >>> with cratedb_container as cratedb:
39+ >>> cratedb_container =
40+ >>> with CrateDBContainer("crate:6.0") as cratedb:
4241 ... engine = sqlalchemy.create_engine(cratedb.get_connection_url())
4342 ... with engine.begin() as connection:
4443 ... result = connection.execute(sqlalchemy.text("select version()"))
4544 ... version, = result.fetchone()
4645 >>> version
47- 'CrateDB 5.2.3. ..'
46+ 'CrateDB 6.0.2 ..'
4847 """
4948
50- CRATEDB_USER = os .environ .get ("CRATEDB_USER" , "crate" )
51- CRATEDB_PASSWORD = os .environ .get ("CRATEDB_PASSWORD" , "crate" )
52- CRATEDB_DB = os .environ .get ("CRATEDB_DB" , "doc" )
53- KEEPALIVE = asbool (os .environ .get ("CRATEDB_KEEPALIVE" , os .environ .get ("TC_KEEPALIVE" , False )))
5449 CMD_OPTS : t .ClassVar [dict [str , str ]] = {
5550 "discovery.type" : "single-node" ,
5651 "node.attr.storage" : "hot" ,
@@ -82,21 +77,30 @@ def __init__(
8277 :param kwargs: misc keyword arguments
8378 """
8479 super ().__init__ (image = image , ** kwargs )
85-
86- self ._name = "testcontainers-cratedb"
87-
8880 cmd_opts = cmd_opts or {}
8981 self ._command = self ._build_cmd ({** self .CMD_OPTS , ** cmd_opts })
9082
91- self .CRATEDB_USER = user or self . CRATEDB_USER
92- self .CRATEDB_PASSWORD = password or self . CRATEDB_PASSWORD
93- self .CRATEDB_DB = dbname or self . CRATEDB_DB
83+ self .CRATEDB_USER = user or os . environ . get ( " CRATEDB_USER" , "crate" )
84+ self .CRATEDB_PASSWORD = password or os . environ . get ( " CRATEDB_PASSWORD" , "crate" )
85+ self .CRATEDB_DB = dbname or os . environ . get ( " CRATEDB_DB" , "doc" )
9486
9587 self .port_mapping = ports if ports else {4200 : None }
96- self .port_to_expose , _ = next (iter (self .port_mapping .items ()))
88+ self .port_to_expose = next (iter (self .port_mapping .items ()))
9789
9890 self .waiting_for (HttpWaitStrategy (4200 ).for_status_code (200 ).with_startup_timeout (5 ))
9991
92+ def exposed_ports (self ) -> dict [int , int ]:
93+ """Returns a dictionary with the ports that are currently exposed in the container.
94+
95+ Contrary to the '--port' parameter used in docker cli, this returns {internal_port: external_port}
96+
97+ Examples:
98+ {4200: 19382}
99+
100+ :returns: The exposed ports.
101+ """
102+ return {port : self .get_exposed_port (port ) for port in self .ports }
103+
100104 @staticmethod
101105 def _build_cmd (opts : dict ) -> str :
102106 """
@@ -127,21 +131,21 @@ def _configure(self) -> None:
127131 self ._configure_credentials ()
128132
129133 def get_connection_url (self , dialect : str = "crate" , host : t .Optional [str ] = None ) -> str :
134+ # We should remove this method once the new DBContainer generic gets added to the library.
130135 """
131136 Return a connection URL to the DB
132137
133138 :param host: optional string
134139 :param dialect: a string with the dialect name to generate a DB URI
135140 :return: string containing a connection URL to te DB
136141 """
137- # TODO: When using `db_name=self.CRATEDB_DB`:
138- # Connection.__init__() got an unexpected keyword argument 'database'
139142 return self ._create_connection_url (
140143 dialect = dialect ,
141144 username = self .CRATEDB_USER ,
142145 password = self .CRATEDB_PASSWORD ,
143146 host = host ,
144- port = self .port_to_expose ,
147+ port = self .port_to_expose [0 ],
148+ dbname = self .CRATEDB_DB ,
145149 )
146150
147151 def _create_connection_url (
@@ -156,12 +160,16 @@ def _create_connection_url(
156160 ) -> str :
157161 if raise_for_deprecated_parameter (kwargs , "db_name" , "dbname" ):
158162 raise ValueError (f"Unexpected arguments: { ',' .join (kwargs )} " )
163+
159164 if self ._container is None :
160165 raise ContainerStartException ("container has not been started" )
166+
161167 host = host or self .get_container_host_ip ()
162168 assert port is not None
169+
163170 port = self .get_exposed_port (port )
164171 quoted_password = quote (password , safe = " +" )
172+
165173 url = f"{ dialect } ://{ username } :{ quoted_password } @{ host } :{ port } "
166174 if dbname :
167175 url = f"{ url } /{ dbname } "
0 commit comments