Skip to content

Commit

Permalink
test: add test for expired relation w/ assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
farmerpaul committed Nov 26, 2024
1 parent 4ce21ed commit 678f55b
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions src/apps/answers/tests/test_answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from sqlalchemy.ext.asyncio import AsyncSession

from apps.activities.domain.activity_update import ActivityUpdate
from apps.activity_assignments.domain.assignments import ActivityAssignmentCreate
from apps.activity_assignments.service import ActivityAssignmentService
from apps.answers.crud import AnswerItemsCRUD
from apps.answers.crud.answers import AnswersCRUD
from apps.answers.db.schemas import AnswerItemSchema, AnswerNoteSchema, AnswerSchema
Expand Down Expand Up @@ -1372,6 +1374,96 @@ async def test_answer_activity_items_create_expired_temporary_relation_fail(
response = await client.post(self.answer_url, data=data)
assert response.status_code == http.HTTPStatus.BAD_REQUEST, response.json()

async def test_answer_activity_items_create_expired_temporary_relation_with_assignment_success(
self,
tom: User,
answer_create_applet_one: AppletAnswerCreate,
client: TestClient,
session: AsyncSession,
sam: User,
applet_one: AppletFull,
applet_one_sam_respondent,
applet_one_sam_subject,
) -> None:
client.login(tom)
subject_service = SubjectsService(session, tom.id)

data = answer_create_applet_one.copy(deep=True)

client.login(sam)
subject_service = SubjectsService(session, sam.id)
source_subject = await subject_service.create(
SubjectCreate(
applet_id=applet_one.id,
creator_id=tom.id,
first_name="source",
last_name="subject",
email=EmailStr("[email protected]"),
secret_user_id=f"{uuid.uuid4()}",
)
)
target_subject = await subject_service.create(
SubjectCreate(
applet_id=applet_one.id,
creator_id=tom.id,
first_name="target",
last_name="subject",
secret_user_id=f"{uuid.uuid4()}",
)
)

# Create an assignment by source subject about target subject
await ActivityAssignmentService(session).create_many(
applet_id=applet_one.id,
assignments_create=[
ActivityAssignmentCreate(
activity_id=answer_create_applet_one.activity_id,
activity_flow_id=None,
respondent_subject_id=source_subject.id,
target_subject_id=target_subject.id,
),
],
)

# create a relation between respondent and source
await subject_service.create_relation(
relation="take-now",
source_subject_id=applet_one_sam_subject.id,
subject_id=source_subject.id,
meta={
"expiresAt": (datetime.datetime.now() - datetime.timedelta(days=1)).isoformat(),
},
)
# create a relation between respondent and target
await subject_service.create_relation(
relation="take-now",
source_subject_id=applet_one_sam_subject.id,
subject_id=target_subject.id,
meta={
"expiresAt": (datetime.datetime.now() - datetime.timedelta(days=1)).isoformat(),
},
)

await subject_service.create_relation(
relation="take-now",
source_subject_id=source_subject.id,
subject_id=target_subject.id,
meta={
"expiresAt": (datetime.datetime.now() - datetime.timedelta(days=1)).isoformat(),
},
)

data.source_subject_id = source_subject.id
data.target_subject_id = target_subject.id
data.input_subject_id = applet_one_sam_subject.id

# before posting the request, make sure that there is a temporary relation
existing_relation = await subject_service.get_relation(applet_one_sam_subject.id, target_subject.id)
assert existing_relation

response = await client.post(self.answer_url, data=data)
assert response.status_code == http.HTTPStatus.CREATED, response.json()

async def test_answer_get_export_data__answer_from_respondent(
self,
client: TestClient,
Expand Down

0 comments on commit 678f55b

Please sign in to comment.