|
| 1 | +import ast |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +pytestmark = pytest.mark.architecture |
| 7 | + |
| 8 | + |
| 9 | +MODULE_PATH = Path("hyperbrowser/client/managers/model_request_utils.py") |
| 10 | + |
| 11 | +PARSED_WRAPPER_FUNCTIONS = ( |
| 12 | + "post_model_request", |
| 13 | + "get_model_request", |
| 14 | + "delete_model_request", |
| 15 | + "put_model_request", |
| 16 | + "post_model_request_async", |
| 17 | + "get_model_request_async", |
| 18 | + "delete_model_request_async", |
| 19 | + "put_model_request_async", |
| 20 | + "post_model_request_to_endpoint", |
| 21 | + "post_model_request_to_endpoint_async", |
| 22 | +) |
| 23 | + |
| 24 | +RAW_HELPER_FUNCTIONS = ( |
| 25 | + "post_model_response_data", |
| 26 | + "get_model_response_data", |
| 27 | + "delete_model_response_data", |
| 28 | + "put_model_response_data", |
| 29 | + "post_model_response_data_async", |
| 30 | + "get_model_response_data_async", |
| 31 | + "delete_model_response_data_async", |
| 32 | + "put_model_response_data_async", |
| 33 | + "post_model_response_data_to_endpoint", |
| 34 | + "post_model_response_data_to_endpoint_async", |
| 35 | +) |
| 36 | + |
| 37 | + |
| 38 | +def _collect_function_sources() -> dict[str, str]: |
| 39 | + module_text = MODULE_PATH.read_text(encoding="utf-8") |
| 40 | + module_ast = ast.parse(module_text) |
| 41 | + function_sources: dict[str, str] = {} |
| 42 | + for node in module_ast.body: |
| 43 | + if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)): |
| 44 | + function_source = ast.get_source_segment(module_text, node) |
| 45 | + if function_source is not None: |
| 46 | + function_sources[node.name] = function_source |
| 47 | + return function_sources |
| 48 | + |
| 49 | + |
| 50 | +def test_parsed_model_request_wrappers_do_not_call_transport_directly(): |
| 51 | + function_sources = _collect_function_sources() |
| 52 | + for function_name in PARSED_WRAPPER_FUNCTIONS: |
| 53 | + function_source = function_sources[function_name] |
| 54 | + assert "client.transport." not in function_source |
| 55 | + |
| 56 | + |
| 57 | +def test_raw_model_request_helpers_are_transport_boundary(): |
| 58 | + function_sources = _collect_function_sources() |
| 59 | + for function_name in RAW_HELPER_FUNCTIONS: |
| 60 | + function_source = function_sources[function_name] |
| 61 | + assert "client.transport." in function_source |
0 commit comments