|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +import pytest |
| 4 | +from fastapi.testclient import TestClient |
| 5 | + |
| 6 | +import test_app.calculator.page as calculator_page |
| 7 | +import test_app.page as test_app_page |
| 8 | +import test_app.user._id_.page as user_by_id_page |
| 9 | +import test_app.user.page as user_page |
| 10 | +from test_app.main import app |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.parametrize( |
| 14 | + ("path_name", "url", "path_params"), |
| 15 | + ( |
| 16 | + ("test_app.page", "/", {}), |
| 17 | + (test_app_page.__name__, "/", {}), |
| 18 | + ("test_app.calculator.page", "/calculator/", {}), |
| 19 | + (calculator_page.__name__, "/calculator/", {}), |
| 20 | + ("test_app.user.page", "/user/", {}), |
| 21 | + (user_page.__name__, "/user/", {}), |
| 22 | + ("test_app.user._id_.page", "/user/1/", {"id": 1}), |
| 23 | + (user_by_id_page.__name__, "/user/1/", {"id": 1}), |
| 24 | + ("test_app.user._id_.page", "/user/C0FF33/", {"id": "C0FF33"}), |
| 25 | + (user_by_id_page.__name__, "/user/C0FF33/", {"id": "C0FF33"}), |
| 26 | + ), |
| 27 | +) |
| 28 | +def test_app_url_path_for_page(path_name: str, url: str, path_params: dict[str, Any]) -> None: |
| 29 | + """ |
| 30 | + Tests that `app.url_path_for()` can construct URLs for pages |
| 31 | + based on page module name (import path). |
| 32 | + """ |
| 33 | + assert app.url_path_for(path_name, **path_params) == url |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.parametrize( |
| 37 | + ("path_name", "url"), |
| 38 | + (("test_app.calculator.page.handle_submit", "/calculator/"),), |
| 39 | +) |
| 40 | +def test_app_url_path_for_handle_submit(path_name: str, url: str) -> None: |
| 41 | + """ |
| 42 | + Tests that `app.url_path_for()` can construct URLs for submit handlers |
| 43 | + based on page module name (import path) with the `.handle_submit` suffix. |
| 44 | + """ |
| 45 | + assert app.url_path_for(path_name) == url |
| 46 | + |
| 47 | + |
| 48 | +def test_request_url_for(client: TestClient) -> None: |
| 49 | + response = client.get("/urls") |
| 50 | + assert response.status_code == 200 |
| 51 | + assert response.json() == [ |
| 52 | + "http://testserver/", |
| 53 | + "http://testserver/calculator/", |
| 54 | + "http://testserver/user/", |
| 55 | + "http://testserver/user/1/", |
| 56 | + ] |
0 commit comments