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, + )