Skip to content

Commit

Permalink
Merge pull request #5 from hxjo/chore/move-tests-and-add-github-workflow
Browse files Browse the repository at this point in the history
Chore: Move tests inside inertia folder + setup Github workflows
  • Loading branch information
hxjo authored Apr 21, 2024
2 parents 7d82da4 + 380e6ed commit f14af5d
Show file tree
Hide file tree
Showing 17 changed files with 278 additions and 154 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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
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 .
25 changes: 25 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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
working-directory: ./backend
run: uv pip install -r requirements.dev.in
- name: Run tests
working-directory: ./backend
run: pytest .
File renamed without changes.
6 changes: 3 additions & 3 deletions backend/inertia/__init__.py
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
16 changes: 9 additions & 7 deletions backend/inertia/renderer.py → backend/inertia/inertia.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class FlashMessage(TypedDict):
category: str


class InertiaRenderer:
class Inertia:
@dataclass
class InertiaFiles:
css_file: Union[str, None]
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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 {})
Expand Down Expand Up @@ -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
Empty file.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 = {
Expand All @@ -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)
Expand All @@ -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,
)
Loading

0 comments on commit f14af5d

Please sign in to comment.