Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ dependencies = [

[dependency-groups]
dev = [
# `httpx` is a dependency of FastAPI's `TestClient` class.
# Docs: https://fastapi.tiangolo.com/tutorial/testing/#using-testclient
"httpx>=0.28.1",
"pre-commit>=4.1.0",
"pyright>=1.1.386",
"pytest>=8.3.5",
Expand Down
1 change: 1 addition & 0 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
- `lib/`: Library of helper functions, constants, etc.
- `README.md`: This document
- `server.py`: The BERtron API
- `tests/`: Tests targeting things implemented in this directory
9 changes: 8 additions & 1 deletion src/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from pydantic import BaseModel, Field
from pydantic import BaseModel, ConfigDict, Field
from typing import Optional


class HealthResponse(BaseModel):
r"""A response containing system health information."""

# Raise a `ValidationError` if extra parameters are passed in when instantiating this class.
# Note: This facilitates having our tests confirm API responses don't include extra fields.
# Docs: https://docs.pydantic.dev/latest/api/config/#pydantic.config.ConfigDict.extra
model_config = ConfigDict(extra="forbid")

web_server: bool = Field(
...,
title="Web server health",
Expand All @@ -20,6 +25,8 @@ class HealthResponse(BaseModel):
class VersionResponse(BaseModel):
r"""A response containing system version information."""

model_config = ConfigDict(extra="forbid")

api: Optional[str] = Field(
...,
title="API version",
Expand Down
2 changes: 1 addition & 1 deletion src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"[View source](https://github.com/ber-data/bertron/blob/main/src/server.py)\n\n"
f"[BERtron schema](https://ber-data.github.io/bertron-schema/) version: `{get_package_version('bertron-schema')}`"
),
version=get_package_version("bertron"),
version=f"{get_package_version('bertron')}",
)


Expand Down
34 changes: 34 additions & 0 deletions src/tests/test_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
r"""
This file contains tests targeting `src/server.py`.

You can learn about testing FastAPI apps here:
https://fastapi.tiangolo.com/tutorial/testing/
"""

import pytest
from fastapi.testclient import TestClient
from starlette import status

from models import VersionResponse
from server import app


@pytest.fixture
def test_client():
test_client = TestClient(app)
yield test_client


def test_root_endpoint_redirects_to_api_docs(test_client: TestClient):
response = test_client.get("/", follow_redirects=False)
assert response.status_code == status.HTTP_307_TEMPORARY_REDIRECT
assert response.headers["location"] == "/docs"


def test_version_endpoint_returns_version_response(test_client: TestClient):
response = test_client.get("/version")
assert response.status_code == status.HTTP_200_OK
# Note: This will raise a `ValidationError` if the response is not
# a valid `VersionResponse` (e.g. if it has extra fields or
# its fields' values are of an incompatible data type).
_ = VersionResponse(**response.json())
4 changes: 3 additions & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.