Skip to content

Commit

Permalink
Merge pull request #1386 from ChildMindInstitute/feature/M2-6637-test…
Browse files Browse the repository at this point in the history
…s-for-loris

tests: BE tests for LORIS (M2-6637)
  • Loading branch information
ZenaY91 authored Jun 13, 2024
2 parents 41d101c + edcfec8 commit 00255d2
Show file tree
Hide file tree
Showing 16 changed files with 540 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/apps/answers/crud/answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ async def get_by_applet_id_and_readiness_to_share_data(
query: Query = select(AnswerSchema)
query = query.where(AnswerSchema.applet_id == applet_id)
query = query.where(AnswerSchema.respondent_id == respondent_id)
query = query.where(AnswerSchema.is_data_share.is_(True))
query = query.where(AnswerSchema.consent_to_share.is_(True))
query = query.order_by(AnswerSchema.created_at.asc())
db_result = await self._execute(query)
return db_result.scalars().all()
Expand All @@ -813,7 +813,7 @@ async def get_respondents_by_applet_id_and_readiness_to_share_data(
) -> list[AnswerSchema] | None:
query: Query = select(AnswerSchema.respondent_id)
query = query.where(AnswerSchema.applet_id == applet_id)
query = query.where(AnswerSchema.is_data_share.is_(True))
query = query.where(AnswerSchema.consent_to_share.is_(True))
query = query.order_by(AnswerSchema.created_at.asc())
db_result = await self._execute(query)
return db_result.scalars().all()
2 changes: 1 addition & 1 deletion src/apps/answers/db/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class AnswerSchema(HistoryAware, Base):
target_subject_id = Column(UUID(as_uuid=True), nullable=True, index=True)
source_subject_id = Column(UUID(as_uuid=True), nullable=True, index=True)
relation = Column(String(length=20), nullable=True)
is_data_share = Column(Boolean(), default=False)
consent_to_share = Column(Boolean(), default=False)

answer_item = relationship(
"AnswerItemSchema",
Expand Down
2 changes: 1 addition & 1 deletion src/apps/answers/domain/answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class AppletAnswerCreate(InternalModel):
client: ClientMeta
target_subject_id: uuid.UUID | None
source_subject_id: uuid.UUID | None
is_data_share: bool
consent_to_share: bool

_dates_from_ms = validator("created_at", pre=True, allow_reuse=True)(datetime_from_ms)

Expand Down
2 changes: 1 addition & 1 deletion src/apps/answers/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ async def _create_answer(self, applet_answer: AppletAnswerCreate) -> AnswerSchem
target_subject_id=target_subject.id,
source_subject_id=source_subject.id if source_subject else None,
relation=relation,
is_data_share=applet_answer.is_data_share,
consent_to_share=applet_answer.consent_to_share,
)
)
item_answer = applet_answer.answer
Expand Down
6 changes: 6 additions & 0 deletions src/apps/answers/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ def answer_create(
answer=answer_item_create,
created_at=datetime.datetime.utcnow().replace(microsecond=0),
client=client_meta,
consent_to_share=False,
)


Expand All @@ -221,6 +222,7 @@ def answer_with_alert_create(
created_at=datetime.datetime.utcnow().replace(microsecond=0),
client=client_meta,
alerts=[answer_alert],
consent_to_share=False,
)


Expand All @@ -245,6 +247,7 @@ def public_answer_create(
answer=item_create,
created_at=datetime.datetime.utcnow().replace(microsecond=0),
client=client_meta,
consent_to_share=False,
)


Expand All @@ -264,6 +267,7 @@ def answer_with_flow_create(
client=client_meta,
flow_id=applet.activity_flows[0].id,
is_flow_completed=True,
consent_to_share=False,
)


Expand Down Expand Up @@ -294,6 +298,7 @@ def answer_reviewable_activity_create(
answer=item_create,
created_at=datetime.datetime.utcnow().replace(microsecond=0),
client=client_meta,
consent_to_share=False,
)


Expand Down Expand Up @@ -455,6 +460,7 @@ def tom_answer_create_data(tom, applet_with_reviewable_activity) -> AppletAnswer
user_public_key=str(tom.id),
),
client=ClientMeta(app_id=f"{uuid.uuid4()}", app_version="1.1", width=984, height=623),
consent_to_share=False,
)


Expand Down
2 changes: 2 additions & 0 deletions src/apps/answers/tests/test_answer_for_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ async def test_answer_activity_items_create_for_respondent(
start_time=10,
end_time=11,
),
consent_to_share=False,
)

response = await client.post(self.answer_url, data=create_data)
Expand Down Expand Up @@ -121,6 +122,7 @@ async def test_answer_activity_items_create_for_respondent(
start_time=10,
end_time=11,
),
consent_to_share=False,
)

response = await client.post(self.answer_url, data=create_data)
Expand Down
9 changes: 9 additions & 0 deletions src/apps/answers/tests/test_answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ async def tom_answer_on_reviewable_applet(
user_public_key=str(tom.id),
),
client=ClientMeta(app_id=f"{uuid.uuid4()}", app_version="1.1", width=984, height=623),
consent_to_share=False,
)
)

Expand All @@ -167,6 +168,7 @@ async def tom_answer_activity_flow(session: AsyncSession, tom: User, applet_with
identifier="encrypted_identifier",
),
client=ClientMeta(app_id=f"{uuid.uuid4()}", app_version="1.1", width=984, height=623),
consent_to_share=False,
)
)

Expand Down Expand Up @@ -197,6 +199,7 @@ def applet_with_flow_answer_create(applet_with_flow: AppletFull) -> list[AppletA
**answer_item_data,
),
**answer_data,
consent_to_share=False,
),
# flow#2 submission#1
AppletAnswerCreate(
Expand All @@ -210,6 +213,7 @@ def applet_with_flow_answer_create(applet_with_flow: AppletFull) -> list[AppletA
**answer_item_data,
),
**answer_data,
consent_to_share=False,
),
AppletAnswerCreate(
submit_id=submit_id,
Expand All @@ -218,6 +222,7 @@ def applet_with_flow_answer_create(applet_with_flow: AppletFull) -> list[AppletA
activity_id=applet_with_flow.activities[1].id,
answer=ItemAnswerCreate(item_ids=[applet_with_flow.activities[1].items[0].id], **answer_item_data),
**answer_data,
consent_to_share=False,
),
# flow#1 submission#2
AppletAnswerCreate(
Expand All @@ -231,6 +236,7 @@ def applet_with_flow_answer_create(applet_with_flow: AppletFull) -> list[AppletA
**answer_item_data,
),
**answer_data,
consent_to_share=False,
),
]
return answers
Expand Down Expand Up @@ -266,6 +272,7 @@ async def tom_answer_activity_no_flow(session: AsyncSession, tom: User, applet_w
user_public_key=str(tom.id),
),
client=ClientMeta(app_id=f"{uuid.uuid4()}", app_version="1.1", width=984, height=623),
consent_to_share=False,
)
)

Expand Down Expand Up @@ -368,6 +375,7 @@ async def tom_answer_activity_flow_not_completed(
identifier="encrypted_identifier",
),
client=ClientMeta(app_id=f"{uuid.uuid4()}", app_version="1.1", width=984, height=623),
consent_to_share=False,
)
)

Expand Down Expand Up @@ -1400,6 +1408,7 @@ async def test_summary_activities_submitted_date_with_answers(
user_public_key=str(tom.id),
),
client=ClientMeta(app_id=f"{uuid.uuid4()}", app_version="1.1", width=984, height=623),
consent_to_share=False,
)
)
submit_dates.append(answer.created_at)
Expand Down
25 changes: 25 additions & 0 deletions src/apps/integrations/loris/tests/test_api_applet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import uuid
from unittest.mock import AsyncMock, patch

from fastapi import BackgroundTasks

from apps.integrations.loris.api.applets import start_transmit_process
from apps.shared.test.client import TestClient
from apps.users.domain import User


async def test_start_transmit_process(client: TestClient, user: User, uuid_zero: uuid.UUID, session):
with patch("apps.authentication.deps.get_current_user", return_value=user):
with patch("infrastructure.database.deps.get_session", return_value=session):
with patch("apps.integrations.loris.api.applets.integration", new_callable=AsyncMock) as mock_integration:
background_tasks = BackgroundTasks()

response = await start_transmit_process(
applet_id=uuid_zero, background_tasks=background_tasks, user=user, session=session
)

for task in background_tasks.tasks:
await task()

assert response.status_code == 202
mock_integration.assert_called_once_with(uuid_zero, session, user)
127 changes: 127 additions & 0 deletions src/apps/integrations/loris/tests/test_crud_relationship.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import uuid
from unittest.mock import AsyncMock, MagicMock

import pytest
from sqlalchemy.exc import IntegrityError

from apps.integrations.loris.crud.user_relationship import MlLorisUserRelationshipCRUD
from apps.integrations.loris.db.schemas import MlLorisUserRelationshipSchema
from apps.integrations.loris.domain import MlLorisUserRelationship
from apps.integrations.loris.errors import MlLorisUserRelationshipError, MlLorisUserRelationshipNotFoundError


@pytest.fixture
def relationship_data(uuid_zero: uuid.UUID):
return {"ml_user_uuid": uuid_zero, "loris_user_id": "loris_user_123"}


@pytest.fixture
def relationship_schema(relationship_data):
return MlLorisUserRelationshipSchema(**relationship_data)


@pytest.fixture
def relationship_instance(relationship_data):
return MlLorisUserRelationship(**relationship_data)


@pytest.fixture
async def crud(session):
return MlLorisUserRelationshipCRUD(session)


@pytest.mark.asyncio
async def test_save(crud, relationship_schema, relationship_instance, mocker):
mock_create = mocker.patch.object(crud, "_create", return_value=AsyncMock())
mock_create.return_value = relationship_schema

result = await crud.save(relationship_schema)

assert result == relationship_instance
mock_create.assert_called_once_with(relationship_schema)


@pytest.mark.asyncio
async def test_save_integrity_error(crud, relationship_schema, mocker):
mock_create = mocker.patch.object(crud, "_create", side_effect=IntegrityError("mock", "mock", "mock"))

with pytest.raises(MlLorisUserRelationshipError):
await crud.save(relationship_schema)

mock_create.assert_called_once_with(relationship_schema)


@pytest.mark.asyncio
async def test_get_by_ml_user_id(crud, relationship_schema, relationship_instance, mocker):
ml_user_uuid = relationship_schema.ml_user_uuid

mock_execute = mocker.patch.object(crud, "_execute", return_value=MagicMock())
mock_scalars = mock_execute.return_value.scalars.return_value
mock_scalars.one_or_none = MagicMock(return_value=relationship_schema)

result = await crud.get_by_ml_user_id(ml_user_uuid)

assert result == relationship_instance
mock_execute.assert_called_once()
mock_scalars.one_or_none.assert_called_once()


@pytest.mark.asyncio
async def test_get_by_ml_user_id_not_found(crud, relationship_schema, mocker):
ml_user_uuid = relationship_schema.ml_user_uuid

mock_execute = mocker.patch.object(crud, "_execute", return_value=MagicMock())
mock_scalars = mock_execute.return_value.scalars.return_value
mock_scalars.one_or_none = MagicMock(return_value=None)

with pytest.raises(MlLorisUserRelationshipNotFoundError):
await crud.get_by_ml_user_id(ml_user_uuid)

mock_execute.assert_called_once()
mock_scalars.one_or_none.assert_called_once()


@pytest.mark.asyncio
async def test_get_by_loris_user_id(crud, relationship_schema, relationship_instance, mocker):
loris_user_id = relationship_schema.loris_user_id

mock_execute = mocker.patch.object(crud, "_execute", return_value=MagicMock())
mock_scalars = mock_execute.return_value.scalars.return_value
mock_scalars.one_or_none = MagicMock(return_value=relationship_schema)

result = await crud.get_by_loris_user_id(loris_user_id)

assert result == relationship_instance
mock_execute.assert_called_once()
mock_scalars.one_or_none.assert_called_once()


@pytest.mark.asyncio
async def test_get_by_loris_user_id_not_found(crud, relationship_schema, mocker):
loris_user_id = relationship_schema.loris_user_id

mock_execute = mocker.patch.object(crud, "_execute", return_value=MagicMock())
mock_scalars = mock_execute.return_value.scalars.return_value
mock_scalars.one_or_none = MagicMock(return_value=None)

with pytest.raises(MlLorisUserRelationshipNotFoundError):
await crud.get_by_loris_user_id(loris_user_id)

mock_execute.assert_called_once()
mock_scalars.one_or_none.assert_called_once()


# @pytest.mark.asyncio
# async def test_update(crud, relationship_schema, relationship_instance, mocker):
# ml_user_uuid = relationship_schema.ml_user_uuid

# mock_update_one = mocker.patch.object(crud, '_update_one', return_value=relationship_schema)

# result = await crud.update(ml_user_uuid, relationship_instance)

# assert result == relationship_instance
# mock_update_one.assert_called_once_with(
# lookup="ml_user_uuid",
# value=ml_user_uuid,
# schema=relationship_schema
# )
Loading

0 comments on commit 00255d2

Please sign in to comment.