|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import importlib.util |
3 | 4 | import json |
4 | 5 | from contextlib import contextmanager |
5 | 6 | from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer |
@@ -45,6 +46,17 @@ def assert_specific_tag_routers_generated(output_dir: Path) -> None: |
45 | 46 | validate_generated_code(output_dir) |
46 | 47 |
|
47 | 48 |
|
| 49 | +def assert_generated_module_has_attribute( |
| 50 | + module_path: Path, module_name: str, attribute_name: str |
| 51 | +) -> None: |
| 52 | + spec = importlib.util.spec_from_file_location(module_name, module_path) |
| 53 | + assert spec is not None |
| 54 | + assert spec.loader is not None |
| 55 | + module = importlib.util.module_from_spec(spec) |
| 56 | + spec.loader.exec_module(module) |
| 57 | + assert hasattr(module, attribute_name) |
| 58 | + |
| 59 | + |
48 | 60 | def assert_generated_draft_request_is_hashable(models_path: Path) -> None: |
49 | 61 | namespace: dict[str, Any] = {} |
50 | 62 | exec( # noqa: S102 - execute generated fixture code in a test namespace. |
@@ -251,6 +263,24 @@ def test_generate_from_json_input(tmp_path: Path, output_dir: Path) -> None: |
251 | 263 | validate_generated_code(output_dir) |
252 | 264 |
|
253 | 265 |
|
| 266 | +@freeze_time("2020-06-19") |
| 267 | +def test_generate_discriminated_union_with_simple_type(output_dir: Path) -> None: |
| 268 | + run_cli_and_assert( |
| 269 | + input_path=DATA_PATH |
| 270 | + / OPEN_API_DEFAULT_TEMPLATE_DIR_NAME |
| 271 | + / "discriminated_union_simple_type.yaml", |
| 272 | + output_path=output_dir, |
| 273 | + expected_path=EXPECTED_OPENAPI_PATH |
| 274 | + / "default_template" |
| 275 | + / "discriminated_union_simple_type", |
| 276 | + ) |
| 277 | + assert_generated_module_has_attribute( |
| 278 | + output_dir / "models.py", |
| 279 | + "generated_discriminated_union_simple_type", |
| 280 | + "Content", |
| 281 | + ) |
| 282 | + |
| 283 | + |
254 | 284 | @freeze_time("2020-06-19") |
255 | 285 | def test_generate_escapes_aliases_in_parameter_defaults(output_dir: Path) -> None: |
256 | 286 | spec = """openapi: 3.0.0 |
|
0 commit comments