Skip to content

Commit 560cbba

Browse files
p1-radbanty
andcommitted
Update test_end_to_end.py,openapi_python_client/__init__.py : various imprv
Co-authored-by: Dylan Anthony <[email protected]>
1 parent 20ce01c commit 560cbba

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

Diff for: end_to_end_tests/test_end_to_end.py

+22-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import shutil
22
from filecmp import cmpfiles, dircmp
33
from pathlib import Path
4-
from typing import Dict, Optional
4+
from typing import Dict, List, Optional
55

66
import pytest
77
from typer.testing import CliRunner
@@ -12,11 +12,18 @@
1212
def _compare_directories(
1313
record: Path,
1414
test_subject: Path,
15-
expected_differences: Optional[
16-
Dict[Path, str]
17-
] = None, # key: path relative to generated directory, value: expected generated content
15+
expected_differences: Dict[Path, str],
1816
depth=0,
1917
):
18+
"""
19+
Compare two directories and assert that only expected_differences are different
20+
21+
Args:
22+
record: Path to the expected output
23+
test_subject: Path to the generated code being checked
24+
expected_differences: key: path relative to generated directory, value: expected generated content
25+
depth: Used to track recursion
26+
"""
2027
first_printable = record.relative_to(Path.cwd())
2128
second_printable = test_subject.relative_to(Path.cwd())
2229
dc = dircmp(record, test_subject)
@@ -30,16 +37,14 @@ def _compare_directories(
3037

3138
expected_path_mismatches = []
3239
for file_name in mismatches:
33-
3440
mismatch_file_path = test_subject.joinpath(file_name)
35-
for expected_differences_path in expected_differences.keys():
36-
37-
if mismatch_file_path.match(str(expected_differences_path)):
41+
expected_content = expected_differences.get(mismatch_file_path)
42+
if expected_content is None:
43+
continue
3844

39-
generated_content = (test_subject / file_name).read_text()
40-
expected_content = expected_differences[expected_differences_path]
41-
assert generated_content == expected_content, f"Unexpected output in {mismatch_file_path}"
42-
expected_path_mismatches.append(expected_differences_path)
45+
generated_content = (test_subject / file_name).read_text()
46+
assert generated_content == expected_content, f"Unexpected output in {mismatch_file_path}"
47+
expected_path_mismatches.append(mismatch_file_path)
4348

4449
for path_mismatch in expected_path_mismatches:
4550
matched_file_name = path_mismatch.name
@@ -62,7 +67,7 @@ def _compare_directories(
6267
pytest.fail(failure, pytrace=False)
6368

6469

65-
def run_e2e_test(extra_args=None, expected_differences=None):
70+
def run_e2e_test(extra_args: List[str], expected_differences: Dict[Path, str]):
6671
runner = CliRunner()
6772
openapi_path = Path(__file__).parent / "openapi.json"
6873
config_path = Path(__file__).parent / "config.yml"
@@ -78,6 +83,8 @@ def run_e2e_test(extra_args=None, expected_differences=None):
7883
if result.exit_code != 0:
7984
raise result.exception
8085

86+
# Use absolute paths for expected differences for easier comparisons
87+
expected_differences = {output_path.joinpath(key): value for key, value in expected_differences.items()}
8188
_compare_directories(gr_path, output_path, expected_differences=expected_differences)
8289

8390
import mypy.api
@@ -89,7 +96,7 @@ def run_e2e_test(extra_args=None, expected_differences=None):
8996

9097

9198
def test_end_to_end():
92-
run_e2e_test()
99+
run_e2e_test([], {})
93100

94101

95102
def test_custom_templates():
@@ -104,8 +111,7 @@ def test_custom_templates():
104111

105112
golden_tpls_root_dir = Path(__file__).parent.joinpath("custom-templates-golden-record")
106113
for expected_difference_path in expected_difference_paths:
107-
path = Path("my-test-api-client").joinpath(expected_difference_path)
108-
expected_differences[path] = (golden_tpls_root_dir / expected_difference_path).read_text()
114+
expected_differences[expected_difference_path] = (golden_tpls_root_dir / expected_difference_path).read_text()
109115

110116
run_e2e_test(
111117
extra_args=["--custom-template-path=end_to_end_tests/test_custom_templates/"],

Diff for: openapi_python_client/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ def _build_api(self) -> None:
257257
endpoint_init_template.render(endpoint_collection=collection),
258258
encoding=self.file_encoding,
259259
)
260-
(tag_dir / "__init__.py").touch()
261260

262261
for endpoint in collection.endpoints:
263262
module_path = tag_dir / f"{snake_case(endpoint.name)}.py"

0 commit comments

Comments
 (0)