diff --git a/src/prefect/client/orchestration/_automations/client.py b/src/prefect/client/orchestration/_automations/client.py index ddd61d422586..b57801d21034 100644 --- a/src/prefect/client/orchestration/_automations/client.py +++ b/src/prefect/client/orchestration/_automations/client.py @@ -45,6 +45,8 @@ def read_automations(self) -> list["Automation"]: return Automation.model_validate_list(response.json()) def find_automation(self, id_or_name: "str | UUID") -> "Automation | None": + from uuid import UUID + if isinstance(id_or_name, str): name = id_or_name try: @@ -202,6 +204,8 @@ async def read_automations(self) -> list["Automation"]: return Automation.model_validate_list(response.json()) async def find_automation(self, id_or_name: "str | UUID") -> "Automation | None": + from uuid import UUID + if isinstance(id_or_name, str): name = id_or_name try: diff --git a/tests/test_automations.py b/tests/test_automations.py index 79d225d676d0..3770f5b619d4 100644 --- a/tests/test_automations.py +++ b/tests/test_automations.py @@ -6,6 +6,7 @@ from prefect.automations import Automation, DoNothing from prefect.events import ResourceSpecification from prefect.events.schemas.automations import EventTrigger, Posture +from prefect.exceptions import ObjectNotFound from prefect.settings import PREFECT_API_SERVICES_TRIGGERS_ENABLED, temporary_settings @@ -206,3 +207,33 @@ def test_disabled_automation_can_be_enabled_sync(automation: Automation): updated_automation = Automation.read(id=automation.id) assert updated_automation.enabled is True + + +async def test_find_automation(automation: Automation): + from prefect.client.orchestration import get_client + + client = get_client() + + # Test finding by UUID + found = await client.find_automation(automation.id) + assert found == automation + + # Test finding by UUID string + found = await client.find_automation(str(automation.id)) + assert found == automation + + # Test finding by exact name + found = await client.find_automation(automation.name) + assert found == automation + + # Test finding by case-insensitive name + found = await client.find_automation(automation.name.upper()) + assert found == automation + + # Test finding nonexistent UUID raises ObjectNotFound + with pytest.raises(ObjectNotFound): + await client.find_automation(UUID("6d222a09-3c68-42d4-b019-bd331a3abb88")) + + # Test finding nonexistent name returns None + found = await client.find_automation("nonexistent_name") + assert found is None