Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rcmerlo committed Dec 9, 2024
1 parent 5e6e0ff commit b85f16a
Showing 1 changed file with 163 additions and 0 deletions.
163 changes: 163 additions & 0 deletions src/apps/activities/tests/test_activities.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from apps.shared.test.client import TestClient
from apps.subjects.db.schemas import SubjectSchema
from apps.subjects.domain import Subject
from apps.subjects.services import SubjectsService
from apps.themes.domain import Theme
from apps.users.domain import User
from apps.workspaces.domain.constants import Role
Expand Down Expand Up @@ -1267,6 +1268,26 @@ async def test_assigned_activities_limited_respondent(
assert result_activity["performanceTaskType"] is None
assert len(result_activity["assignments"]) == 0

response = await client.get(
self.activity_metadata_by_subject_url.format(
applet_id=applet_one.id, subject_id=applet_one_shell_account.id
)
)

assert response.status_code == http.HTTPStatus.OK
result = response.json()["result"]

assert result["respondentActivitiesCountExisting"] == 1
assert result["respondentActivitiesCountDeleted"] == 0
assert result["targetActivitiesCountExisting"] == 0
assert result["targetActivitiesCountDeleted"] == 0
assert len(result["activitiesOrFlows"]) == 1
assert result["activitiesOrFlows"][0]["activityOrFlowId"] == str(activity.id)
assert result["activitiesOrFlows"][0]["respondentsCount"] == 0
assert result["activitiesOrFlows"][0]["respondentSubmissionsCount"] == 1
assert result["activitiesOrFlows"][0]["subjectsCount"] == 1
assert result["activitiesOrFlows"][0]["subjectSubmissionsCount"] == 0

@pytest.mark.parametrize("subject_type", ["target", "respondent"])
async def test_assigned_activities_auto_assigned(
self,
Expand Down Expand Up @@ -1595,6 +1616,60 @@ async def test_assigned_activities_manually_assigned(
subject_attr = "targetSubject" if subject_type == "target" else "respondentSubject"
assert flow_assignment[subject_attr]["id"] == str(user_empty_applet_subject.id)

as_respondent = (
{activity_by_number[1].id, activity_by_number[2].id}
if subject_type == "target"
else {activity_by_number[4].id, activity_by_number[3].id}
)
as_target = (
{activity_by_number[1].id, activity_by_number[3].id}
if subject_type == "target"
else {activity_by_number[4].id, activity_by_number[2].id}
)

as_respondent.update(
{flow_by_number[1].id, flow_by_number[2].id}
if subject_type == "target"
else {
flow_by_number[4].id,
flow_by_number[3].id,
}
)
as_target.update(
{flow_by_number[1].id, flow_by_number[3].id}
if subject_type == "target"
else {
flow_by_number[4].id,
flow_by_number[2].id,
}
)

client.login(lucy)

response = await client.get(
self.activity_metadata_by_subject_url.format(
applet_id=empty_applet_lucy_manager.id,
subject_id=user_empty_applet_subject.id if subject_type == "target" else lucy_empty_applet_subject.id,
)
)

assert response.status_code == http.HTTPStatus.OK
result = response.json()["result"]

assert len(result["activitiesOrFlows"]) == 6
assert result["respondentActivitiesCountExisting"] == 4
assert result["respondentActivitiesCountDeleted"] == 0
assert result["targetActivitiesCountExisting"] == 4
assert result["targetActivitiesCountDeleted"] == 0
for activityOrFlow in result["activitiesOrFlows"]:
is_respondent = uuid.UUID(activityOrFlow["activityOrFlowId"]) in as_respondent
is_target = uuid.UUID(activityOrFlow["activityOrFlowId"]) in as_target

assert activityOrFlow["respondentsCount"] == (1 if is_target else 0)
assert activityOrFlow["respondentSubmissionsCount"] == 0
assert activityOrFlow["subjectsCount"] == (1 if is_respondent else 0)
assert activityOrFlow["subjectSubmissionsCount"] == 0

@pytest.mark.parametrize("subject_type", ["target", "respondent"])
async def test_assigned_activities_from_submission(
self,
Expand Down Expand Up @@ -1889,3 +1964,91 @@ async def test_assigned_performance_tasks(
PerformanceTaskType.ABTRAILS.value,
PerformanceTaskType.UNITY.value,
]

async def test_activities_metadata_deleted_subject(
self,
session: AsyncSession,
client: TestClient,
lucy: User,
empty_applet_lucy_manager: AppletFull,
lucy_empty_applet_subject: Subject,
user_empty_applet_subject: Subject,
activity_create_session: ActivityCreate,
):
activity_service = ActivityService(session, lucy.id)
activities = await activity_service.update_create(
empty_applet_lucy_manager.id,
[
ActivityUpdate(
**activity_create_session.dict(exclude={"name", "auto_assign"}),
name="Manual Activity 1",
auto_assign=False,
),
ActivityUpdate(
**activity_create_session.dict(exclude={"name", "auto_assign"}),
name="Manual Activity 2",
auto_assign=False,
),
],
)

await ActivityAssignmentService(session).create_many(
empty_applet_lucy_manager.id,
[
ActivityAssignmentCreate(
activity_id=activities[0].id,
activity_flow_id=None,
respondent_subject_id=lucy_empty_applet_subject.id,
target_subject_id=user_empty_applet_subject.id,
),
ActivityAssignmentCreate(
activity_id=activities[1].id,
activity_flow_id=None,
respondent_subject_id=user_empty_applet_subject.id,
target_subject_id=user_empty_applet_subject.id,
),
],
)

manual_activity_2 = next((activity for activity in activities if activity.name == "Manual Activity 2"))

client.login(lucy)

response = await client.get(
self.activity_metadata_by_subject_url.format(
applet_id=empty_applet_lucy_manager.id, subject_id=user_empty_applet_subject.id
)
)

assert response.status_code == http.HTTPStatus.OK
result = response.json()["result"]

assert result["respondentActivitiesCountExisting"] == 1
assert result["respondentActivitiesCountDeleted"] == 0
assert result["targetActivitiesCountExisting"] == 2
assert result["targetActivitiesCountDeleted"] == 0
assert len(result["activitiesOrFlows"]) == 2
for activityOrFlow in result["activitiesOrFlows"]:
assert activityOrFlow["respondentsCount"] == 1
assert activityOrFlow["respondentSubmissionsCount"] == 0
assert activityOrFlow["subjectsCount"] == (
1 if activityOrFlow["activityOrFlowId"] == str(manual_activity_2.id) else 0
)
assert activityOrFlow["subjectSubmissionsCount"] == 0

await SubjectsService(session, lucy.id).delete(user_empty_applet_subject.id)

response = await client.get(
self.activity_metadata_by_subject_url.format(
applet_id=empty_applet_lucy_manager.id, subject_id=user_empty_applet_subject.id
)
)

assert response.status_code == http.HTTPStatus.OK
result = response.json()["result"]

assert result["respondentActivitiesCountExisting"] == 0
assert result["respondentActivitiesCountDeleted"] == 0
assert result["targetActivitiesCountExisting"] == 0
assert result["targetActivitiesCountDeleted"] == 0
assert len(result["activitiesOrFlows"]) == 0

0 comments on commit b85f16a

Please sign in to comment.