Skip to content

Commit caf428c

Browse files
committed
pytest-simcore safe changes
1 parent be9f062 commit caf428c

File tree

2 files changed

+45
-26
lines changed

2 files changed

+45
-26
lines changed

packages/pytest-simcore/src/pytest_simcore/file_extra.py

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

55
import pytest
66
from faker import Faker
7-
from pydantic import ByteSize
7+
from pydantic import ByteSize, NonNegativeInt
88
from pytest_simcore.helpers.logging_tools import log_context
99

1010

@@ -26,6 +26,37 @@ def _creator(size: ByteSize, name: str | None = None) -> Path:
2626
return _creator
2727

2828

29+
def _create_random_content(
30+
faker: Faker,
31+
*,
32+
base_dir: Path,
33+
file_min_size: ByteSize,
34+
file_max_size: ByteSize,
35+
remaining_size: ByteSize,
36+
depth: NonNegativeInt | None,
37+
) -> ByteSize:
38+
if remaining_size <= 0:
39+
return remaining_size
40+
41+
file_size = ByteSize(
42+
faker.pyint(
43+
min_value=min(file_min_size, remaining_size),
44+
max_value=min(remaining_size, file_max_size),
45+
)
46+
)
47+
if depth is None:
48+
depth = faker.pyint(0, 5)
49+
file_path = base_dir / f"{faker.unique.file_path(depth=depth, absolute=False)}"
50+
file_path.parent.mkdir(parents=True, exist_ok=True)
51+
assert not file_path.exists()
52+
with file_path.open("wb") as fp:
53+
fp.write(f"I am a {file_size.human_readable()} file".encode())
54+
fp.truncate(file_size)
55+
assert file_path.exists()
56+
57+
return ByteSize(remaining_size - file_size)
58+
59+
2960
@pytest.fixture
3061
def create_folder_of_size_with_multiple_files(
3162
tmp_path: Path, faker: Faker
@@ -34,33 +65,12 @@ def _create_folder_of_size_with_multiple_files(
3465
directory_size: ByteSize,
3566
file_min_size: ByteSize,
3667
file_max_size: ByteSize,
68+
depth: NonNegativeInt | None = None,
3769
) -> Path:
3870
# Helper function to create random files and directories
3971
assert file_min_size > 0
4072
assert file_min_size <= file_max_size
4173

42-
def create_random_content(base_dir: Path, remaining_size: ByteSize) -> ByteSize:
43-
if remaining_size <= 0:
44-
return remaining_size
45-
46-
# Decide to create a file or a subdirectory
47-
# Create a file
48-
file_size = ByteSize(
49-
faker.pyint(
50-
min_value=min(file_min_size, remaining_size),
51-
max_value=min(remaining_size, file_max_size),
52-
)
53-
) # max file size 1MB
54-
file_path = base_dir / f"{faker.file_path(depth=4, absolute=False)}"
55-
file_path.parent.mkdir(parents=True, exist_ok=True)
56-
assert not file_path.exists()
57-
with file_path.open("wb") as fp:
58-
fp.write(f"I am a {file_size.human_readable()} file".encode())
59-
fp.truncate(file_size)
60-
assert file_path.exists()
61-
62-
return ByteSize(remaining_size - file_size)
63-
6474
# Recursively create content in the temporary directory
6575
remaining_size = directory_size
6676
with log_context(
@@ -70,7 +80,14 @@ def create_random_content(base_dir: Path, remaining_size: ByteSize) -> ByteSize:
7080
) as ctx:
7181
num_files_created = 0
7282
while remaining_size > 0:
73-
remaining_size = create_random_content(tmp_path, remaining_size)
83+
remaining_size = _create_random_content(
84+
faker,
85+
base_dir=tmp_path,
86+
file_min_size=file_min_size,
87+
file_max_size=file_max_size,
88+
remaining_size=remaining_size,
89+
depth=depth,
90+
)
7491
num_files_created += 1
7592
ctx.logger.info("created %s files", num_files_created)
7693
return tmp_path

packages/pytest-simcore/src/pytest_simcore/helpers/httpx_assert_checks.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def assert_status(
2020
response_model: type[T] | None,
2121
*,
2222
expected_msg: str | None = None,
23-
is_enveloped: bool = True,
23+
expect_envelope: bool = True,
2424
) -> tuple[T | None, Any]:
2525
"""
2626
Asserts for enveloped responses
@@ -36,7 +36,7 @@ def assert_status(
3636
if expected_status_code == status.HTTP_204_NO_CONTENT:
3737
assert response.text == ""
3838
return None, None
39-
if is_enveloped:
39+
if expect_envelope:
4040
validated_response = TypeAdapter(Envelope[response_model]).validate_json(
4141
response.text
4242
)
@@ -49,6 +49,8 @@ def assert_status(
4949
expected_status_code,
5050
expected_msg,
5151
)
52+
else:
53+
assert data is not None
5254
return data, error
5355

5456
if is_error(expected_status_code):

0 commit comments

Comments
 (0)