Skip to content

Commit

Permalink
Removed upsert function in favor of just add (since we don't need spe…
Browse files Browse the repository at this point in the history
…cific update functions

as validation processing can directly update the attached DAO), and simplified pytests
to remove the upsert functions
  • Loading branch information
jcadam14 committed Jan 4, 2024
1 parent 0da38e3 commit 11e0b1c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 49 deletions.
25 changes: 9 additions & 16 deletions src/entities/repos/submission_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,14 @@ async def get_submission(session: AsyncSession, submission_id: int) -> Submissio
return await session.scalar(stmt)


async def upsert_submission(session: AsyncSession, submission: SubmissionDTO) -> SubmissionDAO:
async def add_submission(session: AsyncSession, submission: SubmissionDTO) -> SubmissionDAO:
async with session.begin():
# Not sure if we really need this, since once we add the Submission to the session
# and return that DAO, all future updates to that object during validation processing
# should automatically update the db since it's still attached. All those updates
# *should* be happening within a single submission session
if submission.submission_id:
await session.merge(submission)
else:
submission = SubmissionDAO(
submitter=submission.submitter,
state=SubmissionState.SUBMISSION_UPLOADED,
validation_ruleset_version="v1", # think we need a new function in the data-validator to return the version?
filing=submission.filing,
)
session.add(submission)
new_sub = SubmissionDAO(
submitter=submission.submitter,
state=SubmissionState.SUBMISSION_UPLOADED,
validation_ruleset_version="v1", # think we need a new function in the data-validator to return the version?
filing=submission.filing,
)
session.add(new_sub)
await session.commit()
return submission
return new_sub
45 changes: 12 additions & 33 deletions tests/entities/repos/test_submission_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ async def test_get_submissions(self, query_session: AsyncSession):
assert {SubmissionState.SUBMISSION_UPLOADED} == set([s.state for s in res])

async def test_add_submission(self, transaction_session: AsyncSession):
res = await repo.upsert_submission(transaction_session, SubmissionDTO(submitter="[email protected]", filing=1))
res = await repo.add_submission(transaction_session, SubmissionDTO(submitter="[email protected]", filing=1))
assert res.submission_id == 4
assert res.submitter == "[email protected]"
assert res.filing == 1
Expand All @@ -99,50 +99,29 @@ async def test_add_submission(self, transaction_session: AsyncSession):
# This tests directly updating the attached DAO in a session. Since
# once we add a Submission we'll have reference to that attached object,
# validation should be able to directly update the DAO state and validation
# json without having to call repo.upsert
async def test_update_submission(self, query_session: AsyncSession):
res = await repo.get_submission(query_session, submission_id=1)
# json without having to call updates on the object
async def test_update_submission(self, transaction_session: AsyncSession):
res = await repo.add_submission(transaction_session, SubmissionDTO(submitter="[email protected]", filing=2))
res.state = SubmissionState.VALIDATION_IN_PROGRESS

stmt = select(SubmissionDAO).filter(SubmissionDAO.submission_id == 1)
new_res1 = await query_session.scalar(stmt)
assert new_res1.submission_id == 1
assert new_res1.filing == 1
stmt = select(SubmissionDAO).filter(SubmissionDAO.submission_id == 4)
new_res1 = await transaction_session.scalar(stmt)
assert new_res1.submission_id == 4
assert new_res1.filing == 2
assert new_res1.state == SubmissionState.VALIDATION_IN_PROGRESS

validation_json = self.get_error_json()
res.validation_json = validation_json
res.state = SubmissionState.VALIDATION_WITH_ERRORS

stmt = select(SubmissionDAO).filter(SubmissionDAO.submission_id == 1)
new_res2 = await query_session.scalar(stmt)
assert new_res2.submission_id == 1
stmt = select(SubmissionDAO).filter(SubmissionDAO.submission_id == 4)
new_res2 = await transaction_session.scalar(stmt)

assert new_res2.submission_id == 4
assert new_res2.filing == 1
assert new_res2.state == SubmissionState.VALIDATION_WITH_ERRORS
assert new_res2.validation_json == validation_json

# This tests the upsert merge function instead of directly updating
# the attached DAO in the session. This is in case it's preferred to
# call upsert on each DAO update. Depends on if the plan is to pass around
# the session or get a new one on each update.
async def test_upsert_submission(self, transaction_session: AsyncSession):
updated_sub = SubmissionDAO(
submission_id=1,
validation_json={},
submitter="[email protected]",
filing=1,
state=SubmissionState.SUBMISSION_SIGNED,
validation_ruleset_version="v1",
)
await repo.upsert_submission(transaction_session, updated_sub)

stmt = select(SubmissionDAO).filter(SubmissionDAO.submission_id == 1)
new_res2 = await transaction_session.scalar(stmt)
assert new_res2.submission_id == 1
assert new_res2.filing == 1
assert new_res2.state == SubmissionState.SUBMISSION_SIGNED
assert new_res2.validation_json == {}

def get_error_json(self):
df_columns = [
"record_no",
Expand Down

0 comments on commit 11e0b1c

Please sign in to comment.