Skip to content

Commit c5bec0a

Browse files
committed
refactor(test): use pytest.mark.parametrize for path validation tests
Consolidate the three similar test loops (extension paths, slash paths, and combined paths) into a single parametrized test. This reduces code duplication while making test cases more maintainable and test output more descriptive. - Replace repetitive test loops with @pytest.mark.parametrize - Use descriptive test IDs for better test output - Group test cases by type with clear comments - Make parameter names more explicit (path_generator, test_case_description)
1 parent 0a2dd2e commit c5bec0a

File tree

1 file changed

+60
-45
lines changed

1 file changed

+60
-45
lines changed

tests/custom/integration/test_local_file_operations.py

Lines changed: 60 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,62 +7,77 @@
77
from tests.custom.types import GetHumanloopClientFn, SyncableFile
88

99

10+
@pytest.mark.parametrize(
11+
"path_generator,expected_error,test_case_description",
12+
[
13+
# Extension path test cases
14+
# Using lambdas to defer path generation until we have access to the test_file fixture
15+
(
16+
lambda test_file: f"{test_file.path}.{test_file.type}",
17+
"includes a file extension which is not supported",
18+
"Standard extension",
19+
),
20+
(
21+
lambda test_file: f"{test_file.path}.{test_file.type.upper()}",
22+
"includes a file extension which is not supported",
23+
"Uppercase extension",
24+
),
25+
(
26+
lambda test_file: f"{test_file.path}.{test_file.type.capitalize()}",
27+
"includes a file extension which is not supported",
28+
"Mixed case extension",
29+
),
30+
(
31+
lambda test_file: f" {test_file.path}.{test_file.type} ",
32+
"includes a file extension which is not supported",
33+
"With whitespace",
34+
),
35+
# Slash path test cases
36+
(lambda test_file: f"{test_file.path}/", "Path .* format is invalid", "Trailing slash"),
37+
(lambda test_file: f"/{test_file.path}", "Path .* format is invalid", "Leading slash"),
38+
(lambda test_file: f"/{test_file.path}/", "Path .* format is invalid", "Both leading and trailing slashes"),
39+
(
40+
lambda test_file: f"//{test_file.path}//",
41+
"Path .* format is invalid",
42+
"Multiple leading and trailing slashes",
43+
),
44+
# Combined path test cases
45+
(
46+
lambda test_file: f"{test_file.path}.{test_file.type}/",
47+
"Path .* format is invalid",
48+
"Extension and trailing slash",
49+
),
50+
(
51+
lambda test_file: f"/{test_file.path}.{test_file.type}",
52+
"Path .* format is invalid",
53+
"Extension and leading slash",
54+
),
55+
],
56+
ids=lambda x: x[2] if isinstance(x, tuple) else x, # Use test_case_description as the test ID in pytest output
57+
)
1058
def test_path_validation(
1159
get_humanloop_client: GetHumanloopClientFn,
1260
syncable_files_fixture: list[SyncableFile],
1361
tmp_path: Path,
62+
path_generator: callable,
63+
expected_error: str,
64+
test_case_description: str,
1465
):
1566
"""Test validation of path formats for local file operations."""
1667
# GIVEN a client with local files enabled and remote files pulled
1768
humanloop_client = get_humanloop_client(use_local_files=True, local_files_directory=str(tmp_path))
1869
humanloop_client.pull()
1970
test_file = syncable_files_fixture[0]
2071

21-
# WHEN using paths with file extensions (of any case/format)
22-
extension_paths = [
23-
f"{test_file.path}.{test_file.type}", # Standard extension
24-
f"{test_file.path}.{test_file.type.upper()}", # Uppercase extension
25-
f"{test_file.path}.{test_file.type.capitalize()}", # Mixed case extension
26-
f" {test_file.path}.{test_file.type} ", # With whitespace
27-
]
28-
29-
# THEN appropriate error should be raised about file extensions
30-
for path in extension_paths:
31-
with pytest.raises(HumanloopRuntimeError, match="includes a file extension which is not supported"):
32-
if test_file.type == "prompt":
33-
humanloop_client.prompts.call(path=path, messages=[{"role": "user", "content": "Testing"}])
34-
elif test_file.type == "agent":
35-
humanloop_client.agents.call(path=path, messages=[{"role": "user", "content": "Testing"}])
36-
37-
# WHEN using paths with leading/trailing slashes
38-
slash_paths = [
39-
f"{test_file.path}/", # Trailing slash
40-
f"/{test_file.path}", # Leading slash
41-
f"/{test_file.path}/", # Both leading and trailing slashes
42-
f"//{test_file.path}//", # Multiple leading and trailing slashes
43-
]
44-
45-
# THEN appropriate error should be raised about slashes
46-
for path in slash_paths:
47-
with pytest.raises(HumanloopRuntimeError, match="Path .* format is invalid"):
48-
if test_file.type == "prompt":
49-
humanloop_client.prompts.call(path=path, messages=[{"role": "user", "content": "Testing"}])
50-
elif test_file.type == "agent":
51-
humanloop_client.agents.call(path=path, messages=[{"role": "user", "content": "Testing"}])
52-
53-
# WHEN using paths with both extensions and slashes
54-
combined_paths = [
55-
f"{test_file.path}.{test_file.type}/", # Extension and trailing slash
56-
f"/{test_file.path}.{test_file.type}", # Extension and leading slash
57-
]
58-
59-
# THEN the format validation error should be raised first (before extension validation)
60-
for path in combined_paths:
61-
with pytest.raises(HumanloopRuntimeError, match="Path .* format is invalid"):
62-
if test_file.type == "prompt":
63-
humanloop_client.prompts.call(path=path, messages=[{"role": "user", "content": "Testing"}])
64-
elif test_file.type == "agent":
65-
humanloop_client.agents.call(path=path, messages=[{"role": "user", "content": "Testing"}])
72+
# WHEN using the test path
73+
test_path = path_generator(test_file)
74+
75+
# THEN appropriate error should be raised
76+
with pytest.raises(HumanloopRuntimeError, match=expected_error):
77+
if test_file.type == "prompt":
78+
humanloop_client.prompts.call(path=test_path, messages=[{"role": "user", "content": "Testing"}])
79+
elif test_file.type == "agent":
80+
humanloop_client.agents.call(path=test_path, messages=[{"role": "user", "content": "Testing"}])
6681

6782

6883
def test_local_file_call(

0 commit comments

Comments
 (0)