|
| 1 | +import json |
| 2 | +from pathlib import Path |
| 3 | +from openapi_client.cli import sync_to_openapi |
| 4 | + |
| 5 | +def test_sync_to_openapi_dir(tmp_path: Path) -> None: |
| 6 | + project_dir = tmp_path / "project" |
| 7 | + project_dir.mkdir() |
| 8 | + |
| 9 | + client_py = project_dir / "client.py" |
| 10 | + client_py.write_text("class Client:\n pass\n") |
| 11 | + |
| 12 | + mock_py = project_dir / "mock_server.py" |
| 13 | + mock_py.write_text("def test_mock():\n pass\n") |
| 14 | + |
| 15 | + test_py = project_dir / "test_client.py" |
| 16 | + test_py.write_text("def test_client():\n pass\n") |
| 17 | + |
| 18 | + cli_py = project_dir / "cli_main.py" |
| 19 | + cli_py.write_text("import argparse\n") |
| 20 | + |
| 21 | + out_file = tmp_path / "openapi.json" |
| 22 | + sync_to_openapi(str(project_dir), str(out_file)) |
| 23 | + |
| 24 | + assert out_file.exists() |
| 25 | + data = json.loads(out_file.read_text()) |
| 26 | + assert "openapi" in data |
| 27 | + |
| 28 | +def test_sync_to_openapi_empty_output(tmp_path: Path) -> None: |
| 29 | + # Test when output_path is not given (it defaults to "openapi.json") |
| 30 | + import os |
| 31 | + old_cwd = os.getcwd() |
| 32 | + os.chdir(str(tmp_path)) |
| 33 | + try: |
| 34 | + project_dir = tmp_path / "project2" |
| 35 | + project_dir.mkdir() |
| 36 | + client_py = project_dir / "client.py" |
| 37 | + client_py.write_text("class Client:\n pass\n") |
| 38 | + sync_to_openapi(str(project_dir), "") |
| 39 | + assert (tmp_path / "openapi.json").exists() |
| 40 | + finally: |
| 41 | + os.chdir(old_cwd) |
0 commit comments