generated from cfpb/open-source-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 42-add-get-submission-endpoint
- Loading branch information
Showing
5 changed files
with
99 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
|
||
from datetime import datetime | ||
from sqlalchemy import select | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
from sqlalchemy.ext.asyncio import AsyncSession, async_scoped_session | ||
|
||
from entities.models import ( | ||
SubmissionDAO, | ||
|
@@ -17,14 +17,18 @@ | |
SubmissionState, | ||
) | ||
from entities.repos import submission_repo as repo | ||
from pytest_mock import MockerFixture | ||
|
||
from entities.engine import engine as entities_engine | ||
|
||
|
||
class TestSubmissionRepo: | ||
@pytest.fixture(scope="function", autouse=True) | ||
async def setup( | ||
self, | ||
transaction_session: AsyncSession, | ||
self, transaction_session: AsyncSession, mocker: MockerFixture, session_generator: async_scoped_session | ||
): | ||
mocker.patch.object(entities_engine, "SessionLocal", return_value=session_generator) | ||
|
||
filing_period = FilingPeriodDAO( | ||
name="FilingPeriod2024", | ||
start_period=datetime.now(), | ||
|
@@ -85,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 | ||
|
@@ -158,27 +167,40 @@ async def test_add_submission(self, transaction_session: AsyncSession): | |
assert res.filing == 1 | ||
assert res.state == SubmissionState.SUBMISSION_UPLOADED | ||
|
||
async def test_update_submission(self, transaction_session: AsyncSession): | ||
res = await repo.add_submission(transaction_session, SubmissionDTO(submitter="[email protected]", filing=2)) | ||
async def test_update_submission(self, session_generator: async_scoped_session): | ||
async with session_generator() as add_session: | ||
res = await repo.add_submission(add_session, SubmissionDTO(submitter="[email protected]", filing=2)) | ||
|
||
res.state = SubmissionState.VALIDATION_IN_PROGRESS | ||
res = await repo.update_submission(res) | ||
|
||
stmt = select(SubmissionDAO).filter(SubmissionDAO.id == 4) | ||
new_res1 = await transaction_session.scalar(stmt) | ||
assert new_res1.id == 4 | ||
assert new_res1.filing == 2 | ||
assert new_res1.state == SubmissionState.VALIDATION_IN_PROGRESS | ||
async def query_updated_dao(): | ||
async with session_generator() as search_session: | ||
stmt = select(SubmissionDAO).filter(SubmissionDAO.id == 4) | ||
new_res1 = await search_session.scalar(stmt) | ||
assert new_res1.id == 4 | ||
assert new_res1.filing == 2 | ||
assert new_res1.state == SubmissionState.VALIDATION_IN_PROGRESS | ||
|
||
await query_updated_dao() | ||
|
||
validation_json = self.get_error_json() | ||
res.validation_json = validation_json | ||
res.state = SubmissionState.VALIDATION_WITH_ERRORS | ||
|
||
stmt = select(SubmissionDAO).filter(SubmissionDAO.id == 4) | ||
new_res2 = await transaction_session.scalar(stmt) | ||
|
||
assert new_res2.id == 4 | ||
assert new_res2.filing == 2 | ||
assert new_res2.state == SubmissionState.VALIDATION_WITH_ERRORS | ||
assert new_res2.validation_json == validation_json | ||
# to test passing in a session to the update_submission function | ||
async with session_generator() as update_session: | ||
res = await repo.update_submission(res, update_session) | ||
|
||
async def query_updated_dao(): | ||
async with session_generator() as search_session: | ||
stmt = select(SubmissionDAO).filter(SubmissionDAO.id == 4) | ||
new_res2 = await search_session.scalar(stmt) | ||
assert new_res2.id == 4 | ||
assert new_res2.filing == 2 | ||
assert new_res2.state == SubmissionState.VALIDATION_WITH_ERRORS | ||
assert new_res2.validation_json == validation_json | ||
|
||
await query_updated_dao() | ||
|
||
def get_error_json(self): | ||
df_columns = [ | ||
|