From 019cfd9ab211fbdeb8d7f953174517c5b346a2be Mon Sep 17 00:00:00 2001 From: Hugo Mortreux <70602545+hxjo@users.noreply.github.com> Date: Sun, 21 Apr 2024 09:30:46 +0200 Subject: [PATCH 1/2] Move tests inside inertia folder + setup github workflows --- .github/workflows/format.yml | 25 +++++ .github/workflows/test.yml | 23 ++++ backend/{tests => }/__init__.py | 0 backend/inertia/__init__.py | 6 +- backend/inertia/{renderer.py => inertia.py} | 16 +-- backend/inertia/tests/__init__.py | 0 .../tests/dummy_manifest_js.json | 0 .../tests/dummy_manifest_ts.json | 0 .../tests/test_first_request_returns_html.py | 103 ++++++++++++------ .../{ => inertia}/tests/test_flash_message.py | 53 +++++---- .../test_request_with_headers_returns_json.py | 13 ++- ...st_returns_409_if_versions_do_not_match.py | 19 +++- .../{ => inertia}/tests/test_shared_data.py | 23 ++-- backend/{ => inertia}/tests/test_ssr.py | 49 +++++---- backend/inertia/tests/utils.py | 46 ++++++++ backend/main.py | 14 +-- backend/tests/utils.py | 37 ------- 17 files changed, 273 insertions(+), 154 deletions(-) create mode 100644 .github/workflows/format.yml create mode 100644 .github/workflows/test.yml rename backend/{tests => }/__init__.py (100%) rename backend/inertia/{renderer.py => inertia.py} (94%) create mode 100644 backend/inertia/tests/__init__.py rename backend/{ => inertia}/tests/dummy_manifest_js.json (100%) rename backend/{ => inertia}/tests/dummy_manifest_ts.json (100%) rename backend/{ => inertia}/tests/test_first_request_returns_html.py (52%) rename backend/{ => inertia}/tests/test_flash_message.py (71%) rename backend/{ => inertia}/tests/test_request_with_headers_returns_json.py (71%) rename backend/{ => inertia}/tests/test_returns_409_if_versions_do_not_match.py (56%) rename backend/{ => inertia}/tests/test_shared_data.py (68%) rename backend/{ => inertia}/tests/test_ssr.py (75%) create mode 100644 backend/inertia/tests/utils.py delete mode 100644 backend/tests/utils.py diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml new file mode 100644 index 0000000..740b1c6 --- /dev/null +++ b/.github/workflows/format.yml @@ -0,0 +1,25 @@ +name: Run format check + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Set up Python 3.12 + uses: actions/setup-python@v4 + with: + python-version: 3.12 + - run: echo "VIRTUAL_ENV=${Python_ROOT_DIR}" >> $GITHUB_ENV + - run: pip install uv + - name: Install dependencies + run: uv pip install -r requirements.dev.in + - name: Run lint check + run: ruff check . + - name: Run format check + run: ruff format --check . \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..0b95c45 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,23 @@ +name: Run format check + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Set up Python 3.12 + uses: actions/setup-python@v4 + with: + python-version: 3.12 + - run: echo "VIRTUAL_ENV=${Python_ROOT_DIR}" >> $GITHUB_ENV + - run: pip install uv + - name: Install dependencies + run: uv pip install -r requirements.dev.in + - name: Run tests + run: pytest . \ No newline at end of file diff --git a/backend/tests/__init__.py b/backend/__init__.py similarity index 100% rename from backend/tests/__init__.py rename to backend/__init__.py diff --git a/backend/inertia/__init__.py b/backend/inertia/__init__.py index dbff1bb..e767049 100644 --- a/backend/inertia/__init__.py +++ b/backend/inertia/__init__.py @@ -1,12 +1,12 @@ -from .renderer import InertiaResponse, InertiaRenderer, inertia_renderer_factory +from .inertia import InertiaResponse, Inertia, inertia_dependency_factory from .exceptions import inertia_exception_handler, InertiaVersionConflictException from .config import InertiaConfig from .utils import lazy __all__ = [ "InertiaResponse", - "InertiaRenderer", - "inertia_renderer_factory", + "Inertia", + "inertia_dependency_factory", "inertia_exception_handler", "InertiaVersionConflictException", "InertiaConfig", diff --git a/backend/inertia/renderer.py b/backend/inertia/inertia.py similarity index 94% rename from backend/inertia/renderer.py rename to backend/inertia/inertia.py index 36f09c5..13b99c3 100644 --- a/backend/inertia/renderer.py +++ b/backend/inertia/inertia.py @@ -23,7 +23,7 @@ class FlashMessage(TypedDict): category: str -class InertiaRenderer: +class Inertia: @dataclass class InertiaFiles: css_file: Union[str, None] @@ -83,7 +83,7 @@ def _get_flashed_messages(self) -> list[FlashMessage]: ) def _set_inertia_files(self) -> None: - if self._config.ssr_enabled or self._config.environment == "production": + if self._config.environment == "production" or self._config.ssr_enabled: with open(self._config.manifest_json_path, "r") as manifest_file: manifest = json.load(manifest_file) @@ -182,7 +182,9 @@ async def render( self, component: str, props: Optional[Dict[str, Any]] = None ) -> HTMLResponse | JSONResponse: if self._config.use_flash_messages: - self._props.update({self._config.flash_message_key: self._get_flashed_messages()}) + self._props.update( + {self._config.flash_message_key: self._get_flashed_messages()} + ) self._component = component self._props.update(props or {}) @@ -214,10 +216,10 @@ async def render( return HTMLResponse(content=html_content) -def inertia_renderer_factory( +def inertia_dependency_factory( config_: InertiaConfig, -) -> Callable[[Request], InertiaRenderer]: - def inertia_dependency(request: Request) -> InertiaRenderer: - return InertiaRenderer(request, config_) +) -> Callable[[Request], Inertia]: + def inertia_dependency(request: Request) -> Inertia: + return Inertia(request, config_) return inertia_dependency diff --git a/backend/inertia/tests/__init__.py b/backend/inertia/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/tests/dummy_manifest_js.json b/backend/inertia/tests/dummy_manifest_js.json similarity index 100% rename from backend/tests/dummy_manifest_js.json rename to backend/inertia/tests/dummy_manifest_js.json diff --git a/backend/tests/dummy_manifest_ts.json b/backend/inertia/tests/dummy_manifest_ts.json similarity index 100% rename from backend/tests/dummy_manifest_ts.json rename to backend/inertia/tests/dummy_manifest_ts.json diff --git a/backend/tests/test_first_request_returns_html.py b/backend/inertia/tests/test_first_request_returns_html.py similarity index 52% rename from backend/tests/test_first_request_returns_html.py rename to backend/inertia/tests/test_first_request_returns_html.py index 057f109..4fd39e7 100644 --- a/backend/tests/test_first_request_returns_html.py +++ b/backend/inertia/tests/test_first_request_returns_html.py @@ -5,7 +5,12 @@ from starlette.testclient import TestClient -from inertia import InertiaRenderer, inertia_renderer_factory, InertiaConfig, InertiaResponse +from ..inertia import ( + Inertia, + inertia_dependency_factory, + InertiaConfig, + InertiaResponse, +) from .utils import get_stripped_html app = FastAPI() @@ -14,35 +19,36 @@ CUSTOM_URL = "http://some_other_url" -InertiaDep = Annotated[ - InertiaRenderer, Depends(inertia_renderer_factory(InertiaConfig())) -] +InertiaDep = Annotated[Inertia, Depends(inertia_dependency_factory(InertiaConfig()))] CustomUrlInertiaDep = Annotated[ - InertiaRenderer, Depends(inertia_renderer_factory(InertiaConfig( - dev_url=CUSTOM_URL - ))) + Inertia, Depends(inertia_dependency_factory(InertiaConfig(dev_url=CUSTOM_URL))) ] ProductionInertiaDep = Annotated[ - InertiaRenderer, Depends(inertia_renderer_factory(InertiaConfig( - manifest_json_path=manifest_json, - environment="production" - ))) + Inertia, + Depends( + inertia_dependency_factory( + InertiaConfig(manifest_json_path=manifest_json, environment="production") + ) + ), ] TypescriptInertiaDep = Annotated[ - InertiaRenderer, Depends(inertia_renderer_factory(InertiaConfig( - use_typescript=True - ))) + Inertia, Depends(inertia_dependency_factory(InertiaConfig(use_typescript=True))) ] TypescriptProductionInertiaDep = Annotated[ - InertiaRenderer, Depends(inertia_renderer_factory(InertiaConfig( - manifest_json_path=manifest_json_ts, - environment="production", - use_typescript=True - ))) + Inertia, + Depends( + inertia_dependency_factory( + InertiaConfig( + manifest_json_path=manifest_json_ts, + environment="production", + use_typescript=True, + ) + ) + ), ] PROPS = { @@ -68,9 +74,12 @@ async def production(inertia: ProductionInertiaDep) -> InertiaResponse: @app.get("/typescript-production", response_model=None) -async def typescript_production(inertia: TypescriptProductionInertiaDep) -> InertiaResponse: +async def typescript_production( + inertia: TypescriptProductionInertiaDep, +) -> InertiaResponse: return await inertia.render(COMPONENT, PROPS) + @app.get("/custom-url", response_model=None) async def custom_url(inertia: CustomUrlInertiaDep) -> InertiaResponse: return await inertia.render(COMPONENT, PROPS) @@ -80,55 +89,77 @@ def test_first_request_returns_html() -> None: with TestClient(app) as client: response = client.get("/") assert response.status_code == 200 - assert response.headers.get('content-type').split(';')[0] == 'text/html' + assert response.headers.get("content-type").split(";")[0] == "text/html" expected_url = str(client.base_url) + "/" - assert response.text.strip() == get_stripped_html(component_name=COMPONENT, props=PROPS, url=expected_url) - + assert response.text.strip() == get_stripped_html( + component_name=COMPONENT, props=PROPS, url=expected_url + ) def test_first_request_returns_html_custom_url() -> None: with TestClient(app) as client: response = client.get("/custom-url") assert response.status_code == 200 - assert response.headers.get('content-type').split(';')[0] == 'text/html' + assert response.headers.get("content-type").split(";")[0] == "text/html" expected_url = str(client.base_url) + "/custom-url" script_asset_url = CUSTOM_URL + "/src/main.js" - assert response.text.strip() == get_stripped_html(component_name=COMPONENT, props=PROPS, url=expected_url, script_asset_url=script_asset_url) + assert response.text.strip() == get_stripped_html( + component_name=COMPONENT, + props=PROPS, + url=expected_url, + script_asset_url=script_asset_url, + ) def test_first_request_returns_html_typescript() -> None: with TestClient(app) as client: response = client.get("/typescript") assert response.status_code == 200 - assert response.headers.get('content-type').split(';')[0] == 'text/html' + assert response.headers.get("content-type").split(";")[0] == "text/html" expected_url = str(client.base_url) + "/typescript" - assert response.text.strip() == get_stripped_html(component_name=COMPONENT, props=PROPS, url=expected_url, script_asset_url="http://localhost:5173/src/main.ts") + assert response.text.strip() == get_stripped_html( + component_name=COMPONENT, + props=PROPS, + url=expected_url, + script_asset_url="http://localhost:5173/src/main.ts", + ) def test_first_request_returns_html_production() -> None: with open(manifest_json, "r") as manifest_file: manifest = json.load(manifest_file) - css_file = f"/src/{manifest[f"src/main.js"]["css"][0]}" - js_file = f"/{manifest[f"src/main.js"]["file"]}" + css_file = f"/src/{manifest["src/main.js"]["css"][0]}" + js_file = f"/{manifest["src/main.js"]["file"]}" with TestClient(app) as client: response = client.get("/production") assert response.status_code == 200 - assert response.headers.get('content-type').split(';')[0] == 'text/html' + assert response.headers.get("content-type").split(";")[0] == "text/html" expected_url = str(client.base_url) + "/production" - assert response.text.strip() == get_stripped_html(component_name=COMPONENT, props=PROPS, url=expected_url, script_asset_url=js_file, css_asset_url=css_file) + assert response.text.strip() == get_stripped_html( + component_name=COMPONENT, + props=PROPS, + url=expected_url, + script_asset_url=js_file, + css_asset_url=css_file, + ) def test_first_request_returns_html_production_typescript() -> None: with open(manifest_json_ts, "r") as manifest_file: manifest = json.load(manifest_file) - css_file = f"/src/{manifest[f"src/main.ts"]["css"][0]}" - js_file = f"/{manifest[f"src/main.ts"]["file"]}" + css_file = f"/src/{manifest["src/main.ts"]["css"][0]}" + js_file = f"/{manifest["src/main.ts"]["file"]}" with TestClient(app) as client: response = client.get("/typescript-production") assert response.status_code == 200 - assert response.headers.get('content-type').split(';')[0] == 'text/html' + assert response.headers.get("content-type").split(";")[0] == "text/html" expected_url = str(client.base_url) + "/typescript-production" - assert response.text.strip() == get_stripped_html(component_name=COMPONENT, props=PROPS, url=expected_url, script_asset_url=js_file, css_asset_url=css_file) - + assert response.text.strip() == get_stripped_html( + component_name=COMPONENT, + props=PROPS, + url=expected_url, + script_asset_url=js_file, + css_asset_url=css_file, + ) diff --git a/backend/tests/test_flash_message.py b/backend/inertia/tests/test_flash_message.py similarity index 71% rename from backend/tests/test_flash_message.py rename to backend/inertia/tests/test_flash_message.py index 768d821..85ff20f 100644 --- a/backend/tests/test_flash_message.py +++ b/backend/inertia/tests/test_flash_message.py @@ -4,7 +4,12 @@ from starlette.middleware.sessions import SessionMiddleware from starlette.testclient import TestClient -from inertia import InertiaRenderer, inertia_renderer_factory, InertiaConfig, InertiaResponse +from ..inertia import ( + Inertia, + inertia_dependency_factory, + InertiaConfig, + InertiaResponse, +) app = FastAPI() @@ -13,16 +18,18 @@ FLASH_MESSAGE_KEY = "some_special_key" InertiaDep = Annotated[ - InertiaRenderer, Depends(inertia_renderer_factory(InertiaConfig( - use_flash_messages=True, - flash_message_key=FLASH_MESSAGE_KEY - ))), + Inertia, + Depends( + inertia_dependency_factory( + InertiaConfig(use_flash_messages=True, flash_message_key=FLASH_MESSAGE_KEY) + ) + ), ] PROPS = { "message": "hello from index", - FLASH_MESSAGE_KEY: [{"message": "hello from flash message", "category": "info"}] + FLASH_MESSAGE_KEY: [{"message": "hello from flash message", "category": "info"}], } COMPONENT = "IndexPage" @@ -35,30 +42,39 @@ def dependency(inertia: InertiaDep) -> None: @app.get("/", response_model=None, dependencies=[Depends(dependency)]) async def index(inertia: InertiaDep) -> InertiaResponse: - return await inertia.render(COMPONENT, { - "message": PROPS.get("message"), - }) + return await inertia.render( + COMPONENT, + { + "message": PROPS.get("message"), + }, + ) @app.get("/redirect", response_model=None, dependencies=[Depends(dependency)]) async def redirect_page(inertia: InertiaDep) -> InertiaResponse: - return await inertia.render(COMPONENT, { - "message": PROPS.get("message"), - }) + return await inertia.render( + COMPONENT, + { + "message": PROPS.get("message"), + }, + ) @app.get("/other-page", response_model=None) async def other_page(inertia: InertiaDep) -> InertiaResponse: - return await inertia.render(COMPONENT, { - "message": PROPS.get("message"), - }) + return await inertia.render( + COMPONENT, + { + "message": PROPS.get("message"), + }, + ) def test_flash_message_is_included_from_dependency() -> None: with TestClient(app) as client: response = client.get("/", headers={"X-Inertia": "true"}) assert response.status_code == 200 - assert response.headers.get('content-type').split(';')[0] == 'application/json' + assert response.headers.get("content-type").split(";")[0] == "application/json" assert response.json() == { "component": COMPONENT, "props": PROPS, @@ -72,7 +88,7 @@ def test_flash_message_is_not_included_on_second_request() -> None: client.get("/", headers={"X-Inertia": "true"}) response = client.get("/other-page", headers={"X-Inertia": "true"}) assert response.status_code == 200 - assert response.headers.get('content-type').split(';')[0] == 'application/json' + assert response.headers.get("content-type").split(";")[0] == "application/json" assert response.json() == { "component": COMPONENT, "props": { @@ -90,11 +106,10 @@ def test_flash_message_is_persisted_on_redirect() -> None: response = client.get("/redirect", headers=headers) assert response.status_code == 200 - assert response.headers.get('content-type').split(';')[0] == 'application/json' + assert response.headers.get("content-type").split(";")[0] == "application/json" assert response.json() == { "component": COMPONENT, "props": PROPS, "url": f"{client.base_url}/redirect", "version": "1.0", } - diff --git a/backend/tests/test_request_with_headers_returns_json.py b/backend/inertia/tests/test_request_with_headers_returns_json.py similarity index 71% rename from backend/tests/test_request_with_headers_returns_json.py rename to backend/inertia/tests/test_request_with_headers_returns_json.py index 16c9888..8c7f6e6 100644 --- a/backend/tests/test_request_with_headers_returns_json.py +++ b/backend/inertia/tests/test_request_with_headers_returns_json.py @@ -3,13 +3,16 @@ from starlette.testclient import TestClient -from inertia import InertiaRenderer, inertia_renderer_factory, InertiaConfig, InertiaResponse +from ..inertia import ( + Inertia, + inertia_dependency_factory, + InertiaConfig, + InertiaResponse, +) app = FastAPI() -InertiaDep = Annotated[ - InertiaRenderer, Depends(inertia_renderer_factory(InertiaConfig())) -] +InertiaDep = Annotated[Inertia, Depends(inertia_dependency_factory(InertiaConfig()))] PROPS = { "message": "hello from index", @@ -27,7 +30,7 @@ def test_request_with_headers_returns_json() -> None: with TestClient(app) as client: response = client.get("/", headers={"X-Inertia": "true"}) assert response.status_code == 200 - assert response.headers.get('content-type').split(';')[0] == 'application/json' + assert response.headers.get("content-type").split(";")[0] == "application/json" assert response.json() == { "component": COMPONENT, "props": PROPS, diff --git a/backend/tests/test_returns_409_if_versions_do_not_match.py b/backend/inertia/tests/test_returns_409_if_versions_do_not_match.py similarity index 56% rename from backend/tests/test_returns_409_if_versions_do_not_match.py rename to backend/inertia/tests/test_returns_409_if_versions_do_not_match.py index 16f63e7..19af688 100644 --- a/backend/tests/test_returns_409_if_versions_do_not_match.py +++ b/backend/inertia/tests/test_returns_409_if_versions_do_not_match.py @@ -3,14 +3,19 @@ from starlette.testclient import TestClient -from inertia import InertiaRenderer, inertia_renderer_factory, InertiaConfig, InertiaResponse, InertiaVersionConflictException, inertia_exception_handler +from ..inertia import ( + Inertia, + inertia_dependency_factory, + InertiaConfig, + InertiaResponse, +) +from ..exceptions import InertiaVersionConflictException, inertia_exception_handler + app = FastAPI() app.add_exception_handler(InertiaVersionConflictException, inertia_exception_handler) # type: ignore[arg-type] -InertiaDep = Annotated[ - InertiaRenderer, Depends(inertia_renderer_factory(InertiaConfig())) -] +InertiaDep = Annotated[Inertia, Depends(inertia_dependency_factory(InertiaConfig()))] PROPS = { "message": "hello from index", @@ -26,6 +31,8 @@ async def index(inertia: InertiaDep) -> InertiaResponse: def test_returns_409_if_versions_do_not_match() -> None: with TestClient(app) as client: - response = client.get("/", headers={"X-Inertia-Version": str(float(InertiaConfig.version) + 1)}) + response = client.get( + "/", headers={"X-Inertia-Version": str(float(InertiaConfig.version) + 1)} + ) assert response.status_code == 409 - assert response.headers.get('X-Inertia-Location') == f"{client.base_url}/" + assert response.headers.get("X-Inertia-Location") == f"{client.base_url}/" diff --git a/backend/tests/test_shared_data.py b/backend/inertia/tests/test_shared_data.py similarity index 68% rename from backend/tests/test_shared_data.py rename to backend/inertia/tests/test_shared_data.py index da43e4f..3c221e0 100644 --- a/backend/tests/test_shared_data.py +++ b/backend/inertia/tests/test_shared_data.py @@ -3,13 +3,16 @@ from starlette.testclient import TestClient -from inertia import InertiaRenderer, inertia_renderer_factory, InertiaConfig, InertiaResponse +from ..inertia import ( + Inertia, + inertia_dependency_factory, + InertiaConfig, + InertiaResponse, +) app = FastAPI() -InertiaDep = Annotated[ - InertiaRenderer, Depends(inertia_renderer_factory(InertiaConfig())) -] +InertiaDep = Annotated[Inertia, Depends(inertia_dependency_factory(InertiaConfig()))] PROPS = { "message": "hello from index", @@ -25,20 +28,22 @@ def dependency(inertia: InertiaDep) -> None: @app.get("/", response_model=None, dependencies=[Depends(dependency)]) async def index(inertia: InertiaDep) -> InertiaResponse: - return await inertia.render(COMPONENT, { - "message": PROPS.get("message"), - }) + return await inertia.render( + COMPONENT, + { + "message": PROPS.get("message"), + }, + ) def test_shared_data_is_included_from_dependency() -> None: with TestClient(app) as client: response = client.get("/", headers={"X-Inertia": "true"}) assert response.status_code == 200 - assert response.headers.get('content-type').split(';')[0] == 'application/json' + assert response.headers.get("content-type").split(";")[0] == "application/json" assert response.json() == { "component": COMPONENT, "props": PROPS, "url": f"{client.base_url}/", "version": "1.0", } - diff --git a/backend/tests/test_ssr.py b/backend/inertia/tests/test_ssr.py similarity index 75% rename from backend/tests/test_ssr.py rename to backend/inertia/tests/test_ssr.py index 7098287..8b15b3d 100644 --- a/backend/tests/test_ssr.py +++ b/backend/inertia/tests/test_ssr.py @@ -6,7 +6,12 @@ from starlette.testclient import TestClient -from inertia import InertiaRenderer, inertia_renderer_factory, InertiaConfig, InertiaResponse +from ..inertia import ( + Inertia, + inertia_dependency_factory, + InertiaConfig, + InertiaResponse, +) from .utils import get_stripped_html app = FastAPI() @@ -14,11 +19,14 @@ SSR_URL = "http://some_special_url" InertiaDep = Annotated[ - InertiaRenderer, Depends(inertia_renderer_factory(InertiaConfig( - ssr_enabled=True, - manifest_json_path=manifest_json, - ssr_url=SSR_URL - ))) + Inertia, + Depends( + inertia_dependency_factory( + InertiaConfig( + ssr_enabled=True, manifest_json_path=manifest_json, ssr_url=SSR_URL + ) + ) + ), ] PROPS = { "message": "hello from index", @@ -27,13 +35,12 @@ COMPONENT = "IndexPage" - @app.get("/", response_model=None) async def index(inertia: InertiaDep) -> InertiaResponse: return await inertia.render(COMPONENT, PROPS) -@patch('requests.post') +@patch("requests.post") def test_calls_inertia_render(post_function: MagicMock) -> None: with TestClient(app) as client: client.get("/") @@ -49,19 +56,16 @@ def test_calls_inertia_render(post_function: MagicMock) -> None: ) -RETURNED_JSON = { - "head": ["some info in the head"], - "body": "some info in the body" -} +RETURNED_JSON = {"head": ["some info in the head"], "body": "some info in the body"} -@patch('requests.post', return_value=MagicMock(json=lambda: RETURNED_JSON)) +@patch("requests.post", return_value=MagicMock(json=lambda: RETURNED_JSON)) def test_returns_html(post_function: MagicMock) -> None: with open(manifest_json, "r") as manifest_file: manifest = json.load(manifest_file) - css_file = f"/src/{manifest[f"src/main.js"]["css"][0]}" - js_file = f"/{manifest[f"src/main.js"]["file"]}" + css_file = f"/src/{manifest["src/main.js"]["css"][0]}" + js_file = f"/{manifest["src/main.js"]["file"]}" with TestClient(app) as client: response = client.get("/") post_function.assert_called_once_with( @@ -75,7 +79,7 @@ def test_returns_html(post_function: MagicMock) -> None: headers={"Content-Type": "application/json"}, ) assert response.status_code == 200 - assert response.headers.get('content-type').split(';')[0] == 'text/html' + assert response.headers.get("content-type").split(";")[0] == "text/html" assert response.text.strip() == get_stripped_html( component_name=COMPONENT, props=PROPS, @@ -83,17 +87,17 @@ def test_returns_html(post_function: MagicMock) -> None: script_asset_url=js_file, css_asset_url=css_file, body_content=RETURNED_JSON["body"], - additional_head_content="\n".join(RETURNED_JSON["head"]) + additional_head_content="\n".join(RETURNED_JSON["head"]), ) -@patch('requests.post', side_effect=Exception()) +@patch("requests.post", side_effect=Exception()) def test_fallback_to_classic_if_render_errors(post_function: MagicMock) -> None: with open(manifest_json, "r") as manifest_file: manifest = json.load(manifest_file) - css_file = f"/src/{manifest[f"src/main.js"]["css"][0]}" - js_file = f"/{manifest[f"src/main.js"]["file"]}" + css_file = f"/src/{manifest["src/main.js"]["css"][0]}" + js_file = f"/{manifest["src/main.js"]["file"]}" with TestClient(app) as client: response = client.get("/") post_function.assert_called_once_with( @@ -107,7 +111,7 @@ def test_fallback_to_classic_if_render_errors(post_function: MagicMock) -> None: headers={"Content-Type": "application/json"}, ) assert response.status_code == 200 - assert response.headers.get('content-type').split(';')[0] == 'text/html' + assert response.headers.get("content-type").split(";")[0] == "text/html" assert response.text.strip() == get_stripped_html( component_name=COMPONENT, props=PROPS, @@ -115,6 +119,3 @@ def test_fallback_to_classic_if_render_errors(post_function: MagicMock) -> None: script_asset_url=js_file, css_asset_url=css_file, ) - - - diff --git a/backend/inertia/tests/utils.py b/backend/inertia/tests/utils.py new file mode 100644 index 0000000..95380a0 --- /dev/null +++ b/backend/inertia/tests/utils.py @@ -0,0 +1,46 @@ +import json +from string import Template +from typing import Any + + +def get_stripped_html( + *, + component_name: str, + props: dict[str, Any], + url: str, + script_asset_url: str = "http://localhost:5173/src/main.js", + css_asset_url: str = "", + additional_head_content: str = "", + body_content: str = "", +) -> str: + css_link = ( + f'' if css_asset_url else "" + ) + body_content = ( + body_content + or f'
' + ) + return ( + Template(""" + + + + + + $head + $css_link + + + $body_content + + + + """) + .substitute( + body_content=body_content, + head=additional_head_content, + css_link=css_link, + script_asset_url=script_asset_url, + ) + .strip() + ) diff --git a/backend/main.py b/backend/main.py index 9063b4f..b22755c 100644 --- a/backend/main.py +++ b/backend/main.py @@ -5,10 +5,10 @@ from fastapi.responses import RedirectResponse from starlette.staticfiles import StaticFiles from starlette.middleware.sessions import SessionMiddleware -from inertia import ( +from .inertia import ( InertiaResponse, - InertiaRenderer, - inertia_renderer_factory, + Inertia, + inertia_dependency_factory, inertia_exception_handler, InertiaVersionConflictException, InertiaConfig, @@ -25,17 +25,15 @@ ) inertia_config = InertiaConfig( manifest_json_path=manifest_json, - environment="production", + environment="development", ssr_enabled=True, ) -InertiaDep = Annotated[ - InertiaRenderer, Depends(inertia_renderer_factory(inertia_config)) -] +InertiaDep = Annotated[Inertia, Depends(inertia_dependency_factory(inertia_config))] vue_dir = ( os.path.join(os.path.dirname(__file__), "..", "vue", "dist", "client") - if inertia_config.environment != "development" or inertia_config.ssr_enabled is True + if inertia_config.environment != "development" else os.path.join(os.path.dirname(__file__), "..", "vue", "src") ) diff --git a/backend/tests/utils.py b/backend/tests/utils.py deleted file mode 100644 index c553c35..0000000 --- a/backend/tests/utils.py +++ /dev/null @@ -1,37 +0,0 @@ -import json -from string import Template -from typing import Any - - -def get_stripped_html( - *, - component_name: str, - props: dict[str, Any], - url: str, - script_asset_url: str = "http://localhost:5173/src/main.js", - css_asset_url: str = "", - additional_head_content: str = "", - body_content: str = "" -) -> str: - css_link = f"" if css_asset_url else "" - body_content = body_content or f"
" - return Template(""" - - - - - - $head - $css_link - - - $body_content - - - - """).substitute( - body_content=body_content, - head=additional_head_content, - css_link=css_link, - script_asset_url=script_asset_url - ).strip() From 380e6ed57a1ee761486e6371dc4b7a6bd3ed7155 Mon Sep 17 00:00:00 2001 From: Hugo Mortreux <70602545+hxjo@users.noreply.github.com> Date: Sun, 21 Apr 2024 09:33:13 +0200 Subject: [PATCH 2/2] set working directory in gh workflow --- .github/workflows/format.yml | 3 +++ .github/workflows/test.yml | 2 ++ 2 files changed, 5 insertions(+) diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index 740b1c6..5b44edf 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -18,8 +18,11 @@ jobs: - run: echo "VIRTUAL_ENV=${Python_ROOT_DIR}" >> $GITHUB_ENV - run: pip install uv - name: Install dependencies + working-directory: ./backend run: uv pip install -r requirements.dev.in - name: Run lint check + working-directory: ./backend run: ruff check . - name: Run format check + working-directory: ./backend run: ruff format --check . \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0b95c45..35b6a8c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,6 +18,8 @@ jobs: - run: echo "VIRTUAL_ENV=${Python_ROOT_DIR}" >> $GITHUB_ENV - run: pip install uv - name: Install dependencies + working-directory: ./backend run: uv pip install -r requirements.dev.in - name: Run tests + working-directory: ./backend run: pytest . \ No newline at end of file