Skip to content

Commit

Permalink
40 create filing period get route (#41)
Browse files Browse the repository at this point in the history
Closes #40

- Added /periods route to filing.py, along with the supporting
get_session function for db connections
- Added get_filing_periods function to submission_repo to return all
FilingPeriod objects in the db
- Added pytests for the route and the new submission_repo function
  • Loading branch information
jcadam14 authored Jan 31, 2024
1 parent a393623 commit 906df07
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 3 deletions.
47 changes: 46 additions & 1 deletion poetry.lock

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

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ ruff = "0.1.6"
black = "~23.11.0"
pytest-asyncio = "^0.23.2"
aiosqlite = "^0.19.0"
httpx = "^0.26.0"

[build-system]
requires = ["poetry-core"]
Expand Down
7 changes: 7 additions & 0 deletions src/entities/repos/submission_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ async def get_submissions(session: AsyncSession, filing_id: int = None) -> List[
return results.all()


async def get_filing_periods(session: AsyncSession) -> List[FilingPeriodDAO]:
async with session.begin():
stmt = select(FilingPeriodDAO)
results = await session.scalars(stmt)
return results.all()


async def get_submission(session: AsyncSession, submission_id: int) -> SubmissionDAO:
return await query_helper(session, submission_id, SubmissionDAO)

Expand Down
21 changes: 19 additions & 2 deletions src/routers/filing.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
from http import HTTPStatus
from fastapi import Request, UploadFile, BackgroundTasks
from fastapi import Depends, Request, UploadFile, BackgroundTasks
from regtech_api_commons.api import Router
from services import submission_processor
from typing import Annotated, List

router = Router()
from entities.engine import get_session
from entities.models import FilingPeriodDTO
from entities.repos import submission_repo as repo

from sqlalchemy.ext.asyncio import AsyncSession


async def set_db(request: Request, session: Annotated[AsyncSession, Depends(get_session)]):
request.state.db_session = session


router = Router(dependencies=[Depends(set_db)])


@router.get("/periods", response_model=List[FilingPeriodDTO])
async def get_filing_periods(request: Request):
return await repo.get_filing_periods(request.state.db_session)


@router.post("/{lei}/submissions/{submission_id}", status_code=HTTPStatus.ACCEPTED)
Expand Down
30 changes: 30 additions & 0 deletions tests/api/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pytest

from datetime import datetime
from fastapi import FastAPI
from pytest_mock import MockerFixture
from unittest.mock import Mock

from entities.models import FilingPeriodDAO, FilingType


@pytest.fixture
def app_fixture(mocker: MockerFixture) -> FastAPI:
from main import app

return app


@pytest.fixture
def get_filing_period_mock(mocker: MockerFixture) -> Mock:
mock = mocker.patch("entities.repos.submission_repo.get_filing_periods")
mock.return_value = [
FilingPeriodDAO(
name="FilingPeriod2024",
start_period=datetime.now(),
end_period=datetime.now(),
due=datetime.now(),
filing_type=FilingType.MANUAL,
)
]
return mock
14 changes: 14 additions & 0 deletions tests/api/routers/test_filing_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from unittest.mock import Mock

from fastapi import FastAPI
from fastapi.testclient import TestClient
from pytest_mock import MockerFixture


class TestFilingApi:
def test_get_periods(self, mocker: MockerFixture, app_fixture: FastAPI, get_filing_period_mock: Mock):
client = TestClient(app_fixture)
res = client.get("/v1/filing/periods")
assert res.status_code == 200
assert len(res.json()) == 1
assert res.json()[0]["name"] == "FilingPeriod2024"
5 changes: 5 additions & 0 deletions tests/entities/repos/test_submission_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ async def test_add_filing_period(self, transaction_session: AsyncSession):
assert res.id == 2
assert res.filing_type == FilingType.MANUAL

async def test_get_filing_periods(self, query_session: AsyncSession):
res = await repo.get_filing_periods(query_session)
assert len(res) == 1
assert res[0].name == "FilingPeriod2024"

async def test_get_filing_period(self, query_session: AsyncSession):
res = await repo.get_filing_period(query_session, filing_period_id=1)
assert res.id == 1
Expand Down

0 comments on commit 906df07

Please sign in to comment.