-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add new method to update project workflow (#1836)
- Loading branch information
1 parent
1e9cc08
commit fb90304
Showing
14 changed files
with
204 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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,3 @@ | ||
# Project Workflow module | ||
|
||
::: kili.presentation.client.project_workflow.ProjectWorkflowClientMethods |
This file contains 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 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
Empty file.
12 changes: 12 additions & 0 deletions
12
src/kili/adapters/kili_api_gateway/project_workflow/mappers.py
This file contains 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,12 @@ | ||
"""GraphQL payload data mappers for project operations.""" | ||
|
||
from typing import Dict | ||
|
||
from .types import ProjectWorkflowDataKiliAPIGatewayInput | ||
|
||
|
||
def project_input_mapper(data: ProjectWorkflowDataKiliAPIGatewayInput) -> Dict: | ||
"""Build the GraphQL ProjectWorfklowData variable to be sent in an operation.""" | ||
return { | ||
"enforceStepSeparation": data.enforce_step_separation, | ||
} |
12 changes: 12 additions & 0 deletions
12
src/kili/adapters/kili_api_gateway/project_workflow/operations.py
This file contains 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,12 @@ | ||
"""GraphQL Project Workflow operations.""" | ||
|
||
|
||
def get_update_project_workflow_mutation(fragment: str) -> str: | ||
"""Return the GraphQL editProjectWorkflowSettings mutation.""" | ||
return f""" | ||
mutation editProjectWorkflowSettings($input: EditProjectWorkflowSettingsInput!) {{ | ||
data: editProjectWorkflowSettings(input: $input) {{ | ||
{fragment} | ||
}} | ||
}} | ||
""" |
37 changes: 37 additions & 0 deletions
37
src/kili/adapters/kili_api_gateway/project_workflow/operations_mixin.py
This file contains 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,37 @@ | ||
"""Mixin extending Kili API Gateway class with Projects related operations.""" | ||
|
||
from typing import Dict | ||
|
||
from kili.adapters.kili_api_gateway.base import BaseOperationMixin | ||
from kili.adapters.kili_api_gateway.helpers.queries import ( | ||
fragment_builder, | ||
) | ||
from kili.domain.project import ProjectId | ||
|
||
from .mappers import project_input_mapper | ||
from .operations import ( | ||
get_update_project_workflow_mutation, | ||
) | ||
from .types import ProjectWorkflowDataKiliAPIGatewayInput | ||
|
||
|
||
class ProjectWorkflowOperationMixin(BaseOperationMixin): | ||
"""Mixin extending Kili API Gateway class with Projects workflow related operations.""" | ||
|
||
def update_project_workflow( | ||
self, | ||
project_id: ProjectId, | ||
project_workflow_data: ProjectWorkflowDataKiliAPIGatewayInput, | ||
) -> Dict: | ||
"""Update properties in a project workflow.""" | ||
project_workflow_input = project_input_mapper(data=project_workflow_data) | ||
|
||
fields = tuple(name for name, val in project_workflow_input.items() if val is not None) | ||
fragment = fragment_builder(fields) | ||
mutation = get_update_project_workflow_mutation(fragment) | ||
|
||
project_workflow_input["projectId"] = project_id | ||
|
||
variables = {"input": project_workflow_input} | ||
result = self.graphql_client.execute(mutation, variables) | ||
return result["data"] |
11 changes: 11 additions & 0 deletions
11
src/kili/adapters/kili_api_gateway/project_workflow/types.py
This file contains 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,11 @@ | ||
"""Types for the ProjectWorkflow-related Kili API gateway functions.""" | ||
|
||
from dataclasses import dataclass | ||
from typing import Optional | ||
|
||
|
||
@dataclass | ||
class ProjectWorkflowDataKiliAPIGatewayInput: | ||
"""ProjectWorkflow input data for Kili API Gateway.""" | ||
|
||
enforce_step_separation: Optional[bool] |
This file contains 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 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 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,37 @@ | ||
"""Client presentation methods for project workflow.""" | ||
|
||
from typing import Any, Dict, Optional | ||
|
||
from typeguard import typechecked | ||
|
||
from kili.domain.project import ProjectId | ||
from kili.use_cases.project_workflow import ProjectWorkflowUseCases | ||
|
||
from .base import BaseClientMethods | ||
|
||
|
||
class ProjectWorkflowClientMethods(BaseClientMethods): | ||
"""Client presentation methods for project workflow.""" | ||
|
||
@typechecked | ||
def update_project_workflow( | ||
self, | ||
project_id: str, | ||
enforce_step_separation: Optional[bool] = None, | ||
) -> Dict[str, Any]: | ||
"""Update properties of a project workflow. | ||
Args: | ||
project_id: Id of the project. | ||
enforce_step_separation: Prevents the same user from being assigned to | ||
multiple steps in the workflow for a same asset, | ||
ensuring independent review and labeling processes | ||
Returns: | ||
A dict with the changed properties which indicates if the mutation was successful, | ||
else an error message. | ||
""" | ||
return ProjectWorkflowUseCases(self.kili_api_gateway).update_project_workflow( | ||
project_id=ProjectId(project_id), | ||
enforce_step_separation=enforce_step_separation, | ||
) |
This file contains 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,25 @@ | ||
"""Project use cases.""" | ||
|
||
from typing import Dict, Optional | ||
|
||
from kili.adapters.kili_api_gateway.project_workflow.types import ( | ||
ProjectWorkflowDataKiliAPIGatewayInput, | ||
) | ||
from kili.domain.project import ProjectId | ||
from kili.use_cases.base import BaseUseCases | ||
|
||
|
||
class ProjectWorkflowUseCases(BaseUseCases): | ||
"""ProjectWorkflow use cases.""" | ||
|
||
def update_project_workflow( | ||
self, | ||
project_id: ProjectId, | ||
enforce_step_separation: Optional[bool] = None, | ||
) -> Dict[str, object]: | ||
"""Update properties in a project workflow.""" | ||
project_workflow_data = ProjectWorkflowDataKiliAPIGatewayInput( | ||
enforce_step_separation=enforce_step_separation, | ||
) | ||
|
||
return self._kili_api_gateway.update_project_workflow(project_id, project_workflow_data) |
This file contains 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,29 @@ | ||
import pytest_mock | ||
|
||
from kili.adapters.kili_api_gateway.kili_api_gateway import KiliAPIGateway | ||
from kili.adapters.kili_api_gateway.project_workflow.operations import ( | ||
get_update_project_workflow_mutation, | ||
) | ||
from kili.presentation.client.project_workflow import ProjectWorkflowClientMethods | ||
|
||
|
||
def test_when_updating_project_workflow_then_it_returns_updated_project_workflow( | ||
mocker: pytest_mock.MockerFixture, | ||
): | ||
kili = ProjectWorkflowClientMethods() | ||
kili.kili_api_gateway = KiliAPIGateway( | ||
graphql_client=mocker.MagicMock(), http_client=mocker.MagicMock() | ||
) | ||
# Given | ||
project_id = "fake_proj_id" | ||
|
||
# When | ||
kili.update_project_workflow(project_id, enforce_step_separation=False) | ||
|
||
# Then | ||
kili.kili_api_gateway.graphql_client.execute.assert_called_once_with( | ||
get_update_project_workflow_mutation(" enforceStepSeparation"), | ||
{ | ||
"input": {"projectId": "fake_proj_id", "enforceStepSeparation": False}, | ||
}, | ||
) |
This file contains 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,31 @@ | ||
from kili.adapters.kili_api_gateway.kili_api_gateway import KiliAPIGateway | ||
from kili.adapters.kili_api_gateway.project_workflow.types import ( | ||
ProjectWorkflowDataKiliAPIGatewayInput, | ||
) | ||
from kili.domain.project import ProjectId | ||
from kili.use_cases.project_workflow import ProjectWorkflowUseCases | ||
|
||
|
||
def test_given_a_project_workflow_when_update_it_then_it_updates_project_workflow_props( | ||
kili_api_gateway: KiliAPIGateway, | ||
): | ||
# Given | ||
def mocked_update_project_workflow( | ||
project_id: ProjectId, | ||
project_workflow_data: ProjectWorkflowDataKiliAPIGatewayInput, | ||
): | ||
return { | ||
"enforce_step_separation": project_workflow_data.enforce_step_separation, | ||
"project_id": project_id, | ||
} | ||
|
||
kili_api_gateway.update_project_workflow.side_effect = mocked_update_project_workflow | ||
|
||
# When | ||
project = ProjectWorkflowUseCases(kili_api_gateway).update_project_workflow( | ||
project_id=ProjectId("fake_proj_id"), | ||
enforce_step_separation=False, | ||
) | ||
|
||
# Then | ||
assert project == {"enforce_step_separation": False, "project_id": "fake_proj_id"} |