Skip to content

Commit 8ca1f91

Browse files
Mark redundant classes and methods as deprecated. (#114)
* Mark redundant classes and methods as deprecated. * Remove inheritance from deprecated container. * Simplify deprecation message. * Resolve merge conflict. * Fix inheritance with deprecation decorator. * Move deprecation decorator to __init__.
1 parent 2b2037e commit 8ca1f91

File tree

10 files changed

+35
-26
lines changed

10 files changed

+35
-26
lines changed

testcontainers/core/container.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from deprecation import deprecated
12
from docker.models.containers import Container
23

34
from testcontainers.core.docker_client import DockerClient
@@ -8,7 +9,7 @@
89

910

1011
class DockerContainer(object):
11-
def __init__(self, image, **kargs):
12+
def __init__(self, image, **kwargs):
1213
self.env = {}
1314
self.ports = {}
1415
self.volumes = {}
@@ -17,7 +18,7 @@ def __init__(self, image, **kargs):
1718
self._container = None
1819
self._command = None
1920
self._name = None
20-
self._kargs = kargs
21+
self._kwargs = kwargs
2122

2223
def with_env(self, key: str, value: str) -> 'DockerContainer':
2324
self.env[key] = value
@@ -33,8 +34,12 @@ def with_exposed_ports(self, *ports) -> 'DockerContainer':
3334
self.ports[port] = None
3435
return self
3536

37+
@deprecated(details='Use `with_kwargs`.')
3638
def with_kargs(self, **kargs) -> 'DockerContainer':
37-
self._kargs = kargs
39+
return self.with_kwargs(**kargs)
40+
41+
def with_kwargs(self, **kwargs) -> 'DockerContainer':
42+
self._kwargs = kwargs
3843
return self
3944

4045
def start(self):
@@ -47,7 +52,7 @@ def start(self):
4752
ports=self.ports,
4853
name=self._name,
4954
volumes=self.volumes,
50-
**self._kargs
55+
**self._kwargs
5156
)
5257
logger.info("Container started: %s", self._container.short_id)
5358
return self

testcontainers/core/generic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ def _configure(self):
5252

5353

5454
class GenericContainer(DockerContainer):
55-
@deprecated(details="use plain DockerContainer instead")
55+
@deprecated(details="Use `DockerContainer`.")
5656
def __init__(self, image):
5757
super(GenericContainer, self).__init__(image)

testcontainers/elasticsearch.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1111
# License for the specific language governing permissions and limitations
1212
# under the License.
13+
from deprecation import deprecated
1314
from testcontainers.core.container import DockerContainer
1415
from testcontainers.core.waiting_utils import wait_container_is_ready
1516
import urllib
@@ -51,4 +52,5 @@ def start(self):
5152
return self
5253

5354

54-
ElasticsearchContainer = ElasticSearchContainer
55+
ElasticsearchContainer = deprecated(details='Use `ElasticSearchContainer` with a capital S instead '
56+
'of `ElasticsearchContainer`.')(ElasticSearchContainer)

testcontainers/general.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
class TestContainer(DockerContainer):
18-
@deprecated(details="use plain DockerContainer instead")
18+
@deprecated(details="Use `DockerContainer`.")
1919
def __init__(self, image, port_to_expose=None):
2020
super(TestContainer, self).__init__(image)
2121
if port_to_expose:

testcontainers/google/pubsub.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
# License for the specific language governing permissions and limitations
1212
# under the License.
1313

14-
from ..core.generic import GenericContainer
14+
from ..core.container import DockerContainer
1515

1616

17-
class PubSubContainer(GenericContainer):
17+
class PubSubContainer(DockerContainer):
1818
"""
1919
PubSub container for testing managed message queues.
2020

testcontainers/mysql.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1111
# License for the specific language governing permissions and limitations
1212
# under the License.
13+
from deprecation import deprecated
1314
from os import environ
1415

1516
from testcontainers.core.generic import DbContainer
@@ -81,5 +82,6 @@ class MariaDbContainer(MySqlContainer):
8182
e = sqlalchemy.create_engine(mariadb.get_connection_url())
8283
result = e.execute("select version()")
8384
"""
85+
@deprecated(details="Use `MySqlContainer` with 'mariadb:latest' image.")
8486
def __init__(self, image="mariadb:latest", **kwargs):
8587
super(MariaDbContainer, self).__init__(image, **kwargs)

testcontainers/nginx.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1111
# License for the specific language governing permissions and limitations
1212
# under the License.
13-
from testcontainers.core.generic import GenericContainer
13+
from deprecation import deprecated
14+
from testcontainers.core.container import DockerContainer
1415

1516

16-
class NginxContainer(GenericContainer):
17+
class NginxContainer(DockerContainer):
18+
@deprecated(details="Use `DockerContainer` with 'nginx:latest' image and expose port 80.")
1719
def __init__(self, image="nginx:latest", port_to_expose=80):
1820
super(NginxContainer, self).__init__(image)
1921
self.port_to_expose = port_to_expose

tests/test_db_containers.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
from pymongo import MongoClient
44
from pymongo.errors import OperationFailure
55

6-
from testcontainers.core.generic import GenericContainer
6+
from testcontainers.core.container import DockerContainer
77
from testcontainers.core.waiting_utils import wait_for
88
from testcontainers.mongodb import MongoDbContainer
99
from testcontainers.mssql import SqlServerContainer
10-
from testcontainers.mysql import MySqlContainer, MariaDbContainer
10+
from testcontainers.mysql import MySqlContainer
1111
from testcontainers.neo4j import Neo4jContainer
1212
from testcontainers.oracle import OracleDbContainer
1313
from testcontainers.postgres import PostgresContainer
@@ -32,7 +32,7 @@ def test_docker_run_postgress():
3232

3333

3434
def test_docker_run_mariadb():
35-
mariadb_container = MariaDbContainer("mariadb:10.2.9")
35+
mariadb_container = MySqlContainer("mariadb:10.2.9")
3636
with mariadb_container as mariadb:
3737
e = sqlalchemy.create_engine(mariadb.get_connection_url())
3838
result = e.execute("select version()")
@@ -76,8 +76,7 @@ def test_docker_run_mongodb():
7676

7777

7878
def test_docker_run_mongodb_connect_without_credentials():
79-
mongo_container = MongoDbContainer()
80-
with mongo_container as mongo:
79+
with MongoDbContainer() as mongo:
8180
connection_url = "mongodb://{}:{}".format(mongo.get_container_host_ip(),
8281
mongo.get_exposed_port(mongo.port_to_expose))
8382
db = MongoClient(connection_url).test
@@ -120,7 +119,7 @@ def test_docker_run_neo4j_latest():
120119

121120

122121
def test_docker_generic_db():
123-
mongo_container = GenericContainer("mongo:latest")
122+
mongo_container = DockerContainer("mongo:latest")
124123
mongo_container.with_bind_ports(27017, 27017)
125124

126125
with mongo_container:

tests/test_elasticsearch.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import json
22
import urllib
33

4-
from testcontainers.elasticsearch import ElasticsearchContainer
4+
from testcontainers.elasticsearch import ElasticSearchContainer
55

66

77
def test_docker_run_elasticsearch():
8-
config = ElasticsearchContainer()
9-
with config as es:
8+
with ElasticSearchContainer() as es:
109
resp = urllib.request.urlopen(es.get_url())
1110
assert json.loads(resp.read().decode())['version']['number'] == '7.5.0'

tests/test_new_docker_api.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from testcontainers import mysql
66

7-
from testcontainers.core.generic import GenericContainer
7+
from testcontainers.core.container import DockerContainer
88
from importlib import reload
99

1010

@@ -14,7 +14,7 @@ def setup_module(m):
1414

1515

1616
def test_docker_custom_image():
17-
container = GenericContainer("mysql:5.7.17")
17+
container = DockerContainer("mysql:5.7.17")
1818
container.with_exposed_ports(3306)
1919
container.with_env("MYSQL_ROOT_PASSWORD", "root")
2020

@@ -34,15 +34,15 @@ def test_docker_env_variables():
3434
assert re.match(pattern, url)
3535

3636

37-
def test_docker_kargs():
37+
def test_docker_kwargs():
3838
code_dir = Path(__file__).parent
39-
container_first = GenericContainer("nginx:latest")
39+
container_first = DockerContainer("nginx:latest")
4040
container_first.with_volume_mapping(code_dir, '/code')
4141

42-
container_second = GenericContainer("nginx:latest")
42+
container_second = DockerContainer("nginx:latest")
4343

4444
with container_first:
45-
container_second.with_kargs(volumes_from=[container_first._container.short_id])
45+
container_second.with_kwargs(volumes_from=[container_first._container.short_id])
4646
with container_second:
4747
files_first = container_first.exec('ls /code').output.decode('utf-8').strip()
4848
files_second = container_second.exec('ls /code').output.decode('utf-8').strip()

0 commit comments

Comments
 (0)