forked from ChildMindInstitute/mindlogger-backend-refactor
-
Notifications
You must be signed in to change notification settings - Fork 0
Add prolific completion code retrieval #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
hamzace
wants to merge
5
commits into
develop
Choose a base branch
from
add-prolific-completion-code-retrieval
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import uuid | ||
|
|
||
| from fastapi import Depends | ||
| from apps.integrations.prolific.domain import PublicProlificIntegration | ||
| from apps.integrations.prolific.service.prolific import ProlificIntegrationService | ||
| from infrastructure.database.deps import get_session | ||
| from infrastructure.http.deps import get_language | ||
|
|
||
|
|
||
| async def get_public_prolific_integration( | ||
| applet_id: uuid.UUID, | ||
| study_id: str, | ||
| language: str = Depends(get_language), | ||
| session=Depends(get_session), | ||
| ) -> PublicProlificIntegration: | ||
| return await ProlificIntegrationService(session=session, applet_id=applet_id).get_public_prolific_integration(study_id, language) | ||
|
|
||
| async def get_study_completion_codes( | ||
| study_id: str, | ||
| applet_id: uuid.UUID, | ||
| session=Depends(get_session)) -> list[str]: | ||
| return await ProlificIntegrationService(session=session, applet_id=applet_id).get_completion_codes(study_id) |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| from fastapi.routing import APIRouter | ||
|
|
||
| from starlette import status | ||
|
|
||
| from apps.integrations.prolific.api import get_public_prolific_integration, get_study_completion_codes | ||
| from apps.integrations.prolific.domain import ProlificCompletionCodeList, PublicProlificIntegration | ||
| from apps.shared.domain.response.errors import AUTHENTICATION_ERROR_RESPONSES, DEFAULT_OPENAPI_RESPONSE | ||
|
|
||
|
|
||
| router = APIRouter(prefix="/integrations/prolific", tags=["Prolific"]) | ||
|
|
||
| router.get( | ||
| "/applet/{applet_id}/study_id/{study_id}", | ||
| description="This endpoint is used to get the prolific configuration for an applet", | ||
| response_model=None, | ||
| status_code=status.HTTP_200_OK, | ||
| responses={ | ||
| status.HTTP_200_OK: {"model": PublicProlificIntegration}, | ||
| **DEFAULT_OPENAPI_RESPONSE, | ||
| **AUTHENTICATION_ERROR_RESPONSES, | ||
| } | ||
| )(get_public_prolific_integration) | ||
|
|
||
| router.get( | ||
| "/applet/{applet_id}/completion_codes/{study_id}", | ||
| description="This endpoint is used to get the list of completion codes for a given study", | ||
| response_model=None, | ||
| status_code=status.HTTP_200_OK, | ||
| responses={ | ||
| status.HTTP_200_OK: {"model": ProlificCompletionCodeList}, | ||
| **DEFAULT_OPENAPI_RESPONSE, | ||
| **AUTHENTICATION_ERROR_RESPONSES, | ||
| }, | ||
| )(get_study_completion_codes) |
This file contains hidden or 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
57 changes: 57 additions & 0 deletions
57
src/apps/integrations/prolific/tests/test_prolific_service.py
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
|
|
||
| from unittest.mock import AsyncMock, patch | ||
| import uuid | ||
|
|
||
| from apps.shared.test.base import BaseTest | ||
| from apps.shared.test.client import TestClient | ||
| from apps.users.domain import User | ||
|
|
||
| from apps.integrations.prolific.router import router as prolific_router | ||
|
|
||
| class TestProlificService(BaseTest): | ||
|
|
||
| @patch('apps.integrations.prolific.service.prolific.requests.get') | ||
| async def test_get_completion_codes(self, mock_get, client: TestClient, user: User, uuid_zero: uuid.UUID): | ||
| mock_response = AsyncMock() | ||
| mock_response.status_code = 200 | ||
| mock_response.json.return_value = { | ||
| "completion_codes": [ | ||
| { | ||
| "code": "code1", | ||
| "code_type": "type1", | ||
| "actions": ["action1"], | ||
| "actor": "actor1" | ||
| }, | ||
| { | ||
| "code": "code2", | ||
| "code_type": "type2", | ||
| "actions": ["action2"], | ||
| "actor": "actor2" | ||
| } | ||
| ] | ||
| } | ||
| mock_get.return_value = mock_response | ||
|
|
||
| study_id = "study123" | ||
| client.login(user) | ||
|
|
||
| completion_codes_url = f"/integrations/prolific/applet/{uuid_zero}/completion_codes/{study_id}" | ||
| result = await client.get(completion_codes_url) | ||
|
|
||
| assert len(result.completion_codes) == 2 | ||
| assert result.completion_codes[0].code == "code1" | ||
| assert result.completion_codes[1].code_type == "type2" | ||
|
|
||
| @patch('apps.integrations.prolific.service.prolific.requests.get') | ||
| async def test_get_completion_codes_failure(self, mock_get, client: TestClient, user: User, uuid_zero: uuid.UUID): | ||
| mock_response = AsyncMock() | ||
| mock_response.status_code = 404 | ||
| mock_response.json.return_value = {"detail": "Not found"} | ||
| mock_get.return_value = mock_response | ||
|
|
||
| study_id = "study123" | ||
| client.login(user) | ||
| completion_codes_url = f"/integrations/prolific/applet/{uuid_zero}/completion_codes/{study_id}" | ||
| result = await client.get(completion_codes_url) | ||
|
|
||
| assert result.status_code == 404 |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.