Skip to content

Commit 612e30f

Browse files
committed
parametrize tests to simplify a little
1 parent e989f8c commit 612e30f

File tree

2 files changed

+23
-59
lines changed

2 files changed

+23
-59
lines changed

core/testcontainers/core/container.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ def _transfer_into_container(
325325
if isinstance(source, bytes):
326326
file_content = source
327327
elif isinstance(source, pathlib.Path):
328+
assert source.is_file() # Temporary, only copying file supported
328329
file_content = source.read_bytes()
329330
else:
330331
raise TypeError("source must be bytes or PathLike")

core/tests/test_core.py

Lines changed: 22 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import tempfile
22
from pathlib import Path
3+
from typing import Union
34

5+
import pytest
46
from testcontainers.core.container import DockerContainer
57
from testcontainers.core.transferable import Transferable
68

@@ -49,79 +51,41 @@ def test_docker_container_with_env_file():
4951
print(output)
5052

5153

52-
def test_copy_file_into_container_at_runtime(tmp_path: Path):
53-
# Given
54-
my_file = tmp_path / "my_file"
55-
my_file.write_text("hello world")
56-
destination_in_container = "/tmp/my_file"
57-
58-
with DockerContainer("bash", command="sleep infinity") as container:
59-
# When
60-
container.copy_into_container(my_file, destination_in_container)
61-
result = container.exec(f"cat {destination_in_container}")
62-
63-
# Then
64-
assert result.exit_code == 0
65-
assert result.output == b"hello world"
66-
67-
68-
def test_copy_file_into_container_at_startup(tmp_path: Path):
69-
# Given
70-
my_file = tmp_path / "my_file"
71-
my_file.write_text("hello world")
72-
destination_in_container = "/tmp/my_file"
73-
74-
container = DockerContainer("bash", command="sleep infinity")
75-
container.with_copy_into_container(my_file, destination_in_container)
76-
77-
with container:
78-
# When
79-
result = container.exec(f"cat {destination_in_container}")
80-
81-
# Then
82-
assert result.exit_code == 0
83-
assert result.output == b"hello world"
54+
@pytest.fixture(name="copy_source", params=(bytes, Path))
55+
def copy_source_fixture(request, tmp_path: Path):
56+
"""
57+
Provide source argument for tests of copy_into_container
58+
"""
59+
raw_data = b"hello world"
60+
if request.param is bytes:
61+
return raw_data
62+
elif request.param is Path:
63+
my_file = tmp_path / "my_file"
64+
my_file.write_bytes(raw_data)
65+
return my_file
66+
pytest.fail("Invalid type")
8467

8568

86-
def test_copy_file_into_container_via_initializer(tmp_path: Path):
69+
def test_copy_into_container_at_runtime(copy_source: Union[bytes, Path]):
8770
# Given
88-
my_file = tmp_path / "my_file"
89-
my_file.write_text("hello world")
90-
destination_in_container = "/tmp/my_file"
91-
transferables = [Transferable(my_file, destination_in_container)]
92-
93-
with DockerContainer("bash", command="sleep infinity", transferables=transferables) as container:
94-
# When
95-
result = container.exec(f"cat {destination_in_container}")
96-
97-
# Then
98-
assert result.exit_code == 0
99-
assert result.output == b"hello world"
100-
101-
102-
def test_copy_bytes_to_container_at_runtime():
103-
# Given
104-
file_content = b"hello world"
10571
destination_in_container = "/tmp/my_file"
10672

10773
with DockerContainer("bash", command="sleep infinity") as container:
10874
# When
109-
container.copy_into_container(file_content, destination_in_container)
110-
111-
# Then
75+
container.copy_into_container(copy_source, destination_in_container)
11276
result = container.exec(f"cat {destination_in_container}")
11377

78+
# Then
11479
assert result.exit_code == 0
11580
assert result.output == b"hello world"
11681

11782

118-
def test_copy_bytes_to_container_at_startup():
83+
def test_copy_into_container_at_startup(copy_source: Union[bytes, Path]):
11984
# Given
120-
file_content = b"hello world"
12185
destination_in_container = "/tmp/my_file"
12286

12387
container = DockerContainer("bash", command="sleep infinity")
124-
container.with_copy_into_container(file_content, destination_in_container)
88+
container.with_copy_into_container(copy_source, destination_in_container)
12589

12690
with container:
12791
# When
@@ -132,11 +96,10 @@ def test_copy_bytes_to_container_at_startup():
13296
assert result.output == b"hello world"
13397

13498

135-
def test_copy_bytes_to_container_via_initializer():
99+
def test_copy_into_container_via_initializer(copy_source: Union[bytes, Path]):
136100
# Given
137-
file_content = b"hello world"
138101
destination_in_container = "/tmp/my_file"
139-
transferables = [Transferable(file_content, destination_in_container)]
102+
transferables = [Transferable(copy_source, destination_in_container)]
140103

141104
with DockerContainer("bash", command="sleep infinity", transferables=transferables) as container:
142105
# When

0 commit comments

Comments
 (0)