Skip to content

Commit

Permalink
Updated to run checks in all app and test folders
Browse files Browse the repository at this point in the history
  • Loading branch information
k-macmillan committed Sep 5, 2024
1 parent 5154b21 commit e09a07c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 16 deletions.
2 changes: 0 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ repos:
- id: bandit
args: [-c, pyproject.toml, -r, -l]
additional_dependencies: ["bandit[toml]"]
exclude: 'tests/*'
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: "v0.6.4"
hooks:
# Run the linter.
- id: ruff
args: [--fix]
exclude: 'tests/*'
# Run the formatter.
- id: ruff-format
10 changes: 4 additions & 6 deletions app/v3/notifications/route_schema.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
"""Request and Response bodies for v3/notifications."""

from typing import Optional, Union

from pydantic import UUID4, AwareDatetime, BaseModel


class NotificationSingleRequest(BaseModel):
"""Request model for notification endpoint."""

to: str
personalization: Optional[dict[str, str]] = None
personalization: None | dict[str, str] = None
template: UUID4

def serialize(self) -> dict[str, Optional[Union[str, dict[str, str]]]]:
def serialize(self) -> dict[str, None | str | dict[str, str]]:
"""Serialize the pydantic model.
Returns
-------
dict[str, Optional[Union[str, dict[str, str]]]]: Serialized version of the model
dict[str, None | str | dict[str, str]]: Serialized version of the model
"""
serialized = self.model_dump()
Expand All @@ -31,5 +29,5 @@ class NotificationSingleResponse(BaseModel):
id: UUID4
created_at: AwareDatetime
updated_at: AwareDatetime
sent_at: Optional[AwareDatetime] = None
sent_at: None | AwareDatetime = None
to: str
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ preview = true
# Flag errors (`C901`) whenever the complexity level exceeds 5.
max-complexity = 5

[tool.bandit.assert_used]
skips = ['*_test.py', '*/test_*.py']

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
2 changes: 1 addition & 1 deletion scripts/run_checks.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /usr/bin/bash

APP_FOLDER='app'
APP_FOLDER='app tests'

# Wrap each check so it hides the output unless there is failure. Quotes needed in echo to preserve newline
args_check=$(flake8 --select=DCO020 $APP_FOLDER)
Expand Down
57 changes: 50 additions & 7 deletions tests/app/v3/notifications/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,45 @@
from uuid import uuid4

from fastapi import status
from fastapi.testclient import TestClient

from app.v3.notifications.rest import RESPONSE_400
from app.v3.notifications.route_schema import NotificationSingleRequest


def test_get(client):
def test_get(client: TestClient) -> None:
"""Test GET /v3/notifications/.
Args:
----
client(TestClient): FastAPI client fixture
"""
resp = client.get(f'/v3/notifications/{uuid4()}')
assert resp.status_code == status.HTTP_200_OK


def test_get_missing_uuid(client):
def test_get_missing_uuid(client: TestClient) -> None:
"""Test GET /v3/notifications/ with a missing uuid.
Args:
----
client(TestClient): FastAPI client fixture
"""
# will think it's supposed to be a POST so throws 405 instead of 404 (FastAPI)
resp = client.get('/v3/notifications/')
assert resp.status_code == status.HTTP_405_METHOD_NOT_ALLOWED


def test_get_malformed_request(client):
def test_get_malformed_request(client: TestClient) -> None:
"""Test GET /v3/notifications/ with a malformed request.
Args:
----
client(TestClient): FastAPI client fixture
"""
malformed_uuid = '1234'
resp = client.get(f'/v3/notifications/{malformed_uuid}')
resp_text = resp.text
Expand All @@ -32,7 +54,14 @@ def test_get_malformed_request(client):
assert malformed_uuid in resp_text


def test_post(client):
def test_post(client: TestClient) -> None:
"""Test POST /v3/notifications/.
Args:
----
client(TestClient): FastAPI client fixture
"""
srequest = NotificationSingleRequest(
to='[email protected]',
personalization={'hello': 'world'},
Expand All @@ -42,7 +71,14 @@ def test_post(client):
assert resp.status_code == status.HTTP_202_ACCEPTED


def test_post_no_personalization(client):
def test_post_no_personalization(client: TestClient) -> None:
"""Test POST /v3/notifications/ with no personalization.
Args:
----
client(TestClient): FastAPI client fixture
"""
srequest = NotificationSingleRequest(
to='[email protected]',
template=uuid4(),
Expand All @@ -51,8 +87,15 @@ def test_post_no_personalization(client):
assert resp.status_code == status.HTTP_202_ACCEPTED


def test_post_malformed_request(client):
request = {}
def test_post_malformed_request(client: TestClient) -> None:
"""Test POST /v3/notifications/ with a malformed request.
Args:
----
client(TestClient): FastAPI client fixture
"""
request: dict[str, str] = {}
resp = client.post('v3/notifications', data=request)
resp_text = resp.text

Expand Down

0 comments on commit e09a07c

Please sign in to comment.