From 226e0332ac71a3149773122dcbd96d2495a031d9 Mon Sep 17 00:00:00 2001 From: Owen Date: Mon, 23 Oct 2023 16:48:27 +0100 Subject: [PATCH 1/8] Implementation --- darwin/future/core/items/set_priority.py | 43 +++++++++ .../tests/core/items/test_set_priority.py | 93 +++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 darwin/future/core/items/set_priority.py create mode 100644 darwin/future/tests/core/items/test_set_priority.py diff --git a/darwin/future/core/items/set_priority.py b/darwin/future/core/items/set_priority.py new file mode 100644 index 000000000..c2060174c --- /dev/null +++ b/darwin/future/core/items/set_priority.py @@ -0,0 +1,43 @@ +from typing import Dict, Optional + +from darwin.future.core.client import ClientCore +from darwin.future.core.types.common import JSONType +from darwin.future.data_objects.typing import UnknownType + + +def set_item_priority( + api_client: ClientCore, + team_slug: str, + priority: int, + filters: Optional[Dict[str, UnknownType]] = None, +) -> JSONType: + """ + Sets the priority of an item + + Parameters + ---------- + client: Client + The client to use for the request + team_slug: str + The slug of the team to set the priority for + priority: int + The priority to set + + Returns + ------- + JSONType + """ + payload: Dict["str", UnknownType] = { + "priority": priority, + } + + if filters: + payload = { + **payload, + "filters": filters, + } + + return api_client.post( + endpoint=f"/v2/teams/{team_slug}/items/priority", + data=payload, + ) diff --git a/darwin/future/tests/core/items/test_set_priority.py b/darwin/future/tests/core/items/test_set_priority.py new file mode 100644 index 000000000..c605112f4 --- /dev/null +++ b/darwin/future/tests/core/items/test_set_priority.py @@ -0,0 +1,93 @@ +from unittest.mock import Mock + +import pytest + +from darwin.exceptions import DarwinException +from darwin.future.core.client import ClientCore +from darwin.future.core.items.set_priority import set_item_priority + + +def test_set_item_priority(): + # Create a mock API client + api_client = Mock(spec=ClientCore) + + # Define the expected payload + expected_payload = {"priority": 10} + + # Define the expected endpoint + expected_endpoint = "/v2/teams/test-team/items/priority" + + # Define the expected response + expected_response = {"status": "success"} + + # Configure the mock API client to return the expected response + api_client.post.return_value = expected_response + + # Call the function being tested + response = set_item_priority( + api_client=api_client, + team_slug="test-team", + priority=10, + ) + + # Verify that the API client was called with the expected arguments + api_client.post.assert_called_once_with( + endpoint=expected_endpoint, + data=expected_payload, + ) + + # Verify that the response matches the expected response + assert response == expected_response + + +def test_set_item_priority_with_filters(): + # Create a mock API client + api_client = Mock(spec=ClientCore) + + # Define the expected payload + expected_payload = { + "priority": 10, + "filters": {"status": "open"}, + } + + # Define the expected endpoint + expected_endpoint = "/v2/teams/test-team/items/priority" + + # Define the expected response + expected_response = {"status": "success"} + + # Configure the mock API client to return the expected response + api_client.post.return_value = expected_response + + # Call the function being tested + response = set_item_priority( + api_client=api_client, + team_slug="test-team", + priority=10, + filters={"status": "open"}, + ) + + # Verify that the API client was called with the expected arguments + api_client.post.assert_called_once_with( + endpoint=expected_endpoint, + data=expected_payload, + ) + + # Verify that the response matches the expected response + assert response == expected_response + + +def test_set_item_priority_with_error_response(): + # Create a mock API client + api_client = Mock(spec=ClientCore) + + # Configure the mock API client to return the error response + api_client.post.side_effect = DarwinException("Something went wrong") + + # Call the function being tested + with pytest.raises(DarwinException): + set_item_priority( + api_client=api_client, + team_slug="test-team", + priority=10, + ) From 6fd44a9d59aa5e11754a47bcccda9eb7de1babec Mon Sep 17 00:00:00 2001 From: Owen Date: Mon, 23 Oct 2023 16:51:35 +0100 Subject: [PATCH 2/8] Linting --- darwin/future/tests/core/items/test_set_priority.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/darwin/future/tests/core/items/test_set_priority.py b/darwin/future/tests/core/items/test_set_priority.py index c605112f4..a59bbbf6b 100644 --- a/darwin/future/tests/core/items/test_set_priority.py +++ b/darwin/future/tests/core/items/test_set_priority.py @@ -7,7 +7,7 @@ from darwin.future.core.items.set_priority import set_item_priority -def test_set_item_priority(): +def test_set_item_priority() -> None: # Create a mock API client api_client = Mock(spec=ClientCore) @@ -40,7 +40,7 @@ def test_set_item_priority(): assert response == expected_response -def test_set_item_priority_with_filters(): +def test_set_item_priority_with_filters() -> None: # Create a mock API client api_client = Mock(spec=ClientCore) @@ -77,7 +77,7 @@ def test_set_item_priority_with_filters(): assert response == expected_response -def test_set_item_priority_with_error_response(): +def test_set_item_priority_with_error_response() -> None: # Create a mock API client api_client = Mock(spec=ClientCore) From cfa9b924a9a33e8445a49c5331d6e83b142dcdda Mon Sep 17 00:00:00 2001 From: Owen Date: Tue, 24 Oct 2023 10:44:16 +0100 Subject: [PATCH 3/8] Refactor to simplify --- .../{set_priority.py => set_item_priority.py} | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) rename darwin/future/core/items/{set_priority.py => set_item_priority.py} (74%) diff --git a/darwin/future/core/items/set_priority.py b/darwin/future/core/items/set_item_priority.py similarity index 74% rename from darwin/future/core/items/set_priority.py rename to darwin/future/core/items/set_item_priority.py index c2060174c..62bfd537e 100644 --- a/darwin/future/core/items/set_priority.py +++ b/darwin/future/core/items/set_item_priority.py @@ -1,4 +1,5 @@ from typing import Dict, Optional +from uuid import UUID from darwin.future.core.client import ClientCore from darwin.future.core.types.common import JSONType @@ -8,8 +9,10 @@ def set_item_priority( api_client: ClientCore, team_slug: str, + dataset_id: int, + item_id: UUID, priority: int, - filters: Optional[Dict[str, UnknownType]] = None, + filters: Dict[str, UnknownType] = {}, ) -> JSONType: """ Sets the priority of an item @@ -27,16 +30,15 @@ def set_item_priority( ------- JSONType """ - payload: Dict["str", UnknownType] = { + payload = { "priority": priority, + "filters": { + "item_ids": [str(item_id)], + "dataset_ids": [dataset_id], + **filters, + }, } - if filters: - payload = { - **payload, - "filters": filters, - } - return api_client.post( endpoint=f"/v2/teams/{team_slug}/items/priority", data=payload, From a5d34a1adc4a165db50acc42d9ff867763e28751 Mon Sep 17 00:00:00 2001 From: Owen Date: Tue, 24 Oct 2023 11:03:44 +0100 Subject: [PATCH 4/8] Improvements to tests --- .../tests/core/items/test_set_priority.py | 64 ++++++++----------- 1 file changed, 28 insertions(+), 36 deletions(-) diff --git a/darwin/future/tests/core/items/test_set_priority.py b/darwin/future/tests/core/items/test_set_priority.py index a59bbbf6b..fb3885509 100644 --- a/darwin/future/tests/core/items/test_set_priority.py +++ b/darwin/future/tests/core/items/test_set_priority.py @@ -1,53 +1,41 @@ from unittest.mock import Mock +from uuid import UUID import pytest +import responses +from sklearn import base from darwin.exceptions import DarwinException from darwin.future.core.client import ClientCore -from darwin.future.core.items.set_priority import set_item_priority +from darwin.future.core.items.set_item_priority import set_item_priority +from darwin.future.tests.core.fixtures import * -def test_set_item_priority() -> None: - # Create a mock API client - api_client = Mock(spec=ClientCore) - - # Define the expected payload - expected_payload = {"priority": 10} - - # Define the expected endpoint - expected_endpoint = "/v2/teams/test-team/items/priority" - - # Define the expected response - expected_response = {"status": "success"} - - # Configure the mock API client to return the expected response - api_client.post.return_value = expected_response - - # Call the function being tested - response = set_item_priority( - api_client=api_client, - team_slug="test-team", - priority=10, +@responses.activate +def test_set_item_priority(base_client) -> None: + mock_response = responses.add( + responses.POST, + base_client.config.api_endpoint + "v2/teams/test-team/items/priority", + json={"affected_item_count": 0}, ) - # Verify that the API client was called with the expected arguments - api_client.post.assert_called_once_with( - endpoint=expected_endpoint, - data=expected_payload, + response = set_item_priority( + base_client, + "test-team", + 123, + UUID("00000000-0000-0000-0000-000000000000"), + 999, ) - # Verify that the response matches the expected response - assert response == expected_response + assert response == {"affected_item_count": 0} def test_set_item_priority_with_filters() -> None: - # Create a mock API client - api_client = Mock(spec=ClientCore) + base_client = Mock(spec=ClientCore) - # Define the expected payload expected_payload = { "priority": 10, - "filters": {"status": "open"}, + "filters": {"dataset_ids": [123], "item_ids": ["00000000-0000-0000-0000-000000000000"], "status": "open"}, } # Define the expected endpoint @@ -57,18 +45,20 @@ def test_set_item_priority_with_filters() -> None: expected_response = {"status": "success"} # Configure the mock API client to return the expected response - api_client.post.return_value = expected_response + base_client.post.return_value = expected_response # Call the function being tested response = set_item_priority( - api_client=api_client, - team_slug="test-team", + base_client, + "test-team", + 123, + UUID("00000000-0000-0000-0000-000000000000"), priority=10, filters={"status": "open"}, ) # Verify that the API client was called with the expected arguments - api_client.post.assert_called_once_with( + base_client.post.assert_called_once_with( endpoint=expected_endpoint, data=expected_payload, ) @@ -89,5 +79,7 @@ def test_set_item_priority_with_error_response() -> None: set_item_priority( api_client=api_client, team_slug="test-team", + dataset_id=123, + item_id=UUID("00000000-0000-0000-0000-000000000000"), priority=10, ) From f0cf1bc609cf2b9a75a94bb24ca13438b3b7a0b2 Mon Sep 17 00:00:00 2001 From: Owen Date: Tue, 24 Oct 2023 11:15:35 +0100 Subject: [PATCH 5/8] Test file linting --- darwin/future/tests/core/items/test_set_priority.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/darwin/future/tests/core/items/test_set_priority.py b/darwin/future/tests/core/items/test_set_priority.py index fb3885509..aeab5f863 100644 --- a/darwin/future/tests/core/items/test_set_priority.py +++ b/darwin/future/tests/core/items/test_set_priority.py @@ -35,7 +35,11 @@ def test_set_item_priority_with_filters() -> None: expected_payload = { "priority": 10, - "filters": {"dataset_ids": [123], "item_ids": ["00000000-0000-0000-0000-000000000000"], "status": "open"}, + "filters": { + "dataset_ids": [123], + "item_ids": ["00000000-0000-0000-0000-000000000000"], + "status": "open", + }, } # Define the expected endpoint From b30b5653e67e9a4a77a8d286be190b0e5326b415 Mon Sep 17 00:00:00 2001 From: Owen Date: Tue, 24 Oct 2023 11:18:04 +0100 Subject: [PATCH 6/8] Linting --- darwin/future/core/items/set_item_priority.py | 2 +- darwin/future/tests/core/items/test_set_priority.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/darwin/future/core/items/set_item_priority.py b/darwin/future/core/items/set_item_priority.py index 62bfd537e..2fd4c6cf8 100644 --- a/darwin/future/core/items/set_item_priority.py +++ b/darwin/future/core/items/set_item_priority.py @@ -1,4 +1,4 @@ -from typing import Dict, Optional +from typing import Dict from uuid import UUID from darwin.future.core.client import ClientCore diff --git a/darwin/future/tests/core/items/test_set_priority.py b/darwin/future/tests/core/items/test_set_priority.py index aeab5f863..2137d863a 100644 --- a/darwin/future/tests/core/items/test_set_priority.py +++ b/darwin/future/tests/core/items/test_set_priority.py @@ -3,7 +3,6 @@ import pytest import responses -from sklearn import base from darwin.exceptions import DarwinException from darwin.future.core.client import ClientCore From 2277b2a30dad970db6e0964c96c566f75305783d Mon Sep 17 00:00:00 2001 From: Owen Date: Tue, 24 Oct 2023 13:16:51 +0100 Subject: [PATCH 7/8] Another lint fix --- darwin/future/tests/core/items/test_set_priority.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/darwin/future/tests/core/items/test_set_priority.py b/darwin/future/tests/core/items/test_set_priority.py index 2137d863a..3b3d06659 100644 --- a/darwin/future/tests/core/items/test_set_priority.py +++ b/darwin/future/tests/core/items/test_set_priority.py @@ -12,7 +12,7 @@ @responses.activate def test_set_item_priority(base_client) -> None: - mock_response = responses.add( + responses.add( responses.POST, base_client.config.api_endpoint + "v2/teams/test-team/items/priority", json={"affected_item_count": 0}, From 7328bfb53d9ab3298ffe15720d8ce22da12e2cdf Mon Sep 17 00:00:00 2001 From: Owen Date: Fri, 27 Oct 2023 13:31:49 +0100 Subject: [PATCH 8/8] PR Changes suggested --- darwin/future/core/items/set_item_priority.py | 12 ++++++++---- darwin/future/tests/core/items/test_set_priority.py | 10 +++++----- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/darwin/future/core/items/set_item_priority.py b/darwin/future/core/items/set_item_priority.py index 2fd4c6cf8..7ec7124b0 100644 --- a/darwin/future/core/items/set_item_priority.py +++ b/darwin/future/core/items/set_item_priority.py @@ -1,4 +1,4 @@ -from typing import Dict +from typing import Dict, List from uuid import UUID from darwin.future.core.client import ClientCore @@ -10,12 +10,12 @@ def set_item_priority( api_client: ClientCore, team_slug: str, dataset_id: int, - item_id: UUID, + item_ids: List[UUID], priority: int, filters: Dict[str, UnknownType] = {}, ) -> JSONType: """ - Sets the priority of an item + Sets the priority of a list of items Parameters ---------- @@ -23,6 +23,10 @@ def set_item_priority( The client to use for the request team_slug: str The slug of the team to set the priority for + dataset_id: int + The dataset to set the priority for + item_ids: List[UUID] + The item ids to set the priority for priority: int The priority to set @@ -33,7 +37,7 @@ def set_item_priority( payload = { "priority": priority, "filters": { - "item_ids": [str(item_id)], + "item_ids": [str(item_id) for item_id in item_ids], "dataset_ids": [dataset_id], **filters, }, diff --git a/darwin/future/tests/core/items/test_set_priority.py b/darwin/future/tests/core/items/test_set_priority.py index 3b3d06659..9b056bcf0 100644 --- a/darwin/future/tests/core/items/test_set_priority.py +++ b/darwin/future/tests/core/items/test_set_priority.py @@ -15,18 +15,18 @@ def test_set_item_priority(base_client) -> None: responses.add( responses.POST, base_client.config.api_endpoint + "v2/teams/test-team/items/priority", - json={"affected_item_count": 0}, + json={"affected_item_count": 1}, ) response = set_item_priority( base_client, "test-team", 123, - UUID("00000000-0000-0000-0000-000000000000"), + [UUID("00000000-0000-0000-0000-000000000000")], 999, ) - assert response == {"affected_item_count": 0} + assert response == {"affected_item_count": 1} def test_set_item_priority_with_filters() -> None: @@ -55,7 +55,7 @@ def test_set_item_priority_with_filters() -> None: base_client, "test-team", 123, - UUID("00000000-0000-0000-0000-000000000000"), + [UUID("00000000-0000-0000-0000-000000000000")], priority=10, filters={"status": "open"}, ) @@ -83,6 +83,6 @@ def test_set_item_priority_with_error_response() -> None: api_client=api_client, team_slug="test-team", dataset_id=123, - item_id=UUID("00000000-0000-0000-0000-000000000000"), + item_ids=[UUID("00000000-0000-0000-0000-000000000000")], priority=10, )