Spaces:
Runtime error
Runtime error
Download backend/tests/test_api_executor.py from Amitkasar/ai-api-testing: direct link, hf CLI and curl.
- Browser
- Download file 2.38 kB
-
https://huggingface.co/spaces/Amitkasar/ai-api-testing/resolve/main/backend/tests/test_api_executor.py
- Command line
-
hf download hf://spaces/Amitkasar/ai-api-testing/backend/tests/test_api_executor.py
-
curl -L -o test_api_executor.py https://huggingface.co/spaces/Amitkasar/ai-api-testing/resolve/main/backend/tests/test_api_executor.py
2.38 kB
| import json | |
| from unittest.mock import MagicMock | |
| from backend.services.api_executor import prepare_step_request | |
| _VALID_RESPONSE = { | |
| "step": 1, | |
| "request": { | |
| "method": "POST", | |
| "url": "https://api.example.com/auth/login", | |
| "headers": {"Content-Type": "application/json"}, | |
| "query_params": {}, | |
| "form_data": {}, | |
| "body": {"username": "test@example.com", "password": "Pass123!"}, | |
| "files": {}, | |
| }, | |
| "analysis": { | |
| "required_inputs": ["username", "password"], | |
| "values_from_previous_steps": [], | |
| "generated_test_data": [], | |
| }, | |
| } | |
| _TOKEN_USAGE = {"prompt_tokens": 200, "completion_tokens": 80, "total_tokens": 280} | |
| def _mock_llm(return_value=None, side_effect=None): | |
| m = MagicMock() | |
| if side_effect is not None: | |
| m.invoke.side_effect = side_effect | |
| else: | |
| m.invoke.return_value = return_value | |
| return m | |
| def _run(llm, **overrides): | |
| kwargs = dict( | |
| endpoint_spec={"method": "POST", "path": "/auth/login"}, | |
| base_url="https://api.example.com", | |
| scenario="Login user", | |
| step_definition={"step": 1, "endpoint": "POST /auth/login", "purpose": "Login"}, | |
| context_variables={}, | |
| user_credentials={"username": "test@example.com", "password": "Pass123!"}, | |
| llm=llm, | |
| ) | |
| kwargs.update(overrides) | |
| return prepare_step_request(**kwargs) | |
| def test_returns_step_execution_plan(): | |
| llm = _mock_llm((json.dumps(_VALID_RESPONSE), _TOKEN_USAGE)) | |
| result, token_usage = _run(llm) | |
| assert result.request.method == "POST" | |
| assert result.request.url == "https://api.example.com/auth/login" | |
| assert result.request.body == {"username": "test@example.com", "password": "Pass123!"} | |
| def test_token_usage_returned(): | |
| llm = _mock_llm((json.dumps(_VALID_RESPONSE), _TOKEN_USAGE)) | |
| _, token_usage = _run(llm) | |
| assert token_usage["total_tokens"] == 280 | |
| def test_context_variables_included_in_prompt(): | |
| llm = _mock_llm((json.dumps(_VALID_RESPONSE), None)) | |
| _run(llm, context_variables={"access_token": "tok_abc"}) | |
| call_args = llm.invoke.call_args | |
| prompt_text = str(call_args) | |
| assert "access_token" in prompt_text | |
| def test_token_usage_none_when_llm_returns_none(): | |
| llm = _mock_llm((json.dumps(_VALID_RESPONSE), None)) | |
| _, token_usage = _run(llm) | |
| assert token_usage is None | |