diff --git a/src/nextline_schedule/graphql/__init__.py b/src/nextline_schedule/graphql/__init__.py index 936bbe7..da39f5e 100644 --- a/src/nextline_schedule/graphql/__init__.py +++ b/src/nextline_schedule/graphql/__init__.py @@ -33,7 +33,8 @@ def read_gql(path: PathLike | str) -> str: QUERY_AUTO_MODE_STATE = read_gql(sub / 'auto_mode' / 'State.gql') QUERY_AUTO_MODE_MODE = read_gql(sub / 'auto_mode' / 'Mode.gql') QUERY_QUEUE_ITEMS = read_gql(sub / 'queue' / 'Items.gql') -QUERY_SCHEDULER = read_gql(sub / 'Scheduler.gql') +QUERY_SCHEDULER = read_gql(sub / 'scheduler' / 'Scheduler.gql') +QUERY_SCHEDULER_PREVIEW = read_gql(sub / 'scheduler' / 'Preview.gql') sub = pwd / 'subscriptions' SUBSCRIBE_AUTO_MODE_STATE = read_gql(sub / 'ScheduleAutoModeState.gql') diff --git a/src/nextline_schedule/graphql/queries/scheduler/Preview.gql b/src/nextline_schedule/graphql/queries/scheduler/Preview.gql new file mode 100644 index 0000000..f28c91b --- /dev/null +++ b/src/nextline_schedule/graphql/queries/scheduler/Preview.gql @@ -0,0 +1,9 @@ +query ScheduleSchedulerPreview { + schedule { + scheduler { + preview { + script + } + } + } +} diff --git a/src/nextline_schedule/graphql/queries/Scheduler.gql b/src/nextline_schedule/graphql/queries/scheduler/Scheduler.gql similarity index 100% rename from src/nextline_schedule/graphql/queries/Scheduler.gql rename to src/nextline_schedule/graphql/queries/scheduler/Scheduler.gql diff --git a/src/nextline_schedule/schema/scheduler/query.py b/src/nextline_schedule/schema/scheduler/query.py index e15c14b..e44a9c4 100644 --- a/src/nextline_schedule/schema/scheduler/query.py +++ b/src/nextline_schedule/schema/scheduler/query.py @@ -22,8 +22,25 @@ def query_scheduler_policy(info: Info) -> str: return scheduler._policy +@strawberry.type +class ScheduleSchedulerPreviewItem: + script: str + # TODO: Add metadata, e.g., timestamp, etc. + + +async def query_scheduler_preview(info: Info) -> ScheduleSchedulerPreviewItem: + scheduler = info.context['schedule']['scheduler'] + assert callable(scheduler) + statement = await scheduler() + assert isinstance(statement, str) + return ScheduleSchedulerPreviewItem(script=statement) + + @strawberry.type class QueryScheduleScheduler: api_url: str = strawberry.field(resolver=query_scheduler_api_url) length_minutes: int = strawberry.field(resolver=query_scheduler_length_minutes) policy: str = strawberry.field(resolver=query_scheduler_policy) + preview: ScheduleSchedulerPreviewItem = strawberry.field( + resolver=query_scheduler_preview + ) diff --git a/tests/schema/queries/scheduler/test_preview.py b/tests/schema/queries/scheduler/test_preview.py new file mode 100644 index 0000000..d187708 --- /dev/null +++ b/tests/schema/queries/scheduler/test_preview.py @@ -0,0 +1,29 @@ +from unittest.mock import AsyncMock + +from hypothesis import given +from hypothesis import strategies as st + +from nextline_schedule.graphql import QUERY_SCHEDULER_PREVIEW +from nextline_test_utils.strategies import st_python_scripts +from tests.schema.conftest import Schema + + +@given(d=st.data()) +async def test_schema(d: st.DataObject, schema: Schema) -> None: + script = d.draw(st_python_scripts()) + scheduler = AsyncMock(return_value=script) + + context = {'schedule': {'scheduler': scheduler}} + resp = await schema.execute(QUERY_SCHEDULER_PREVIEW, context_value=context) + assert (data := resp.data) + + expected = { + 'schedule': { + 'scheduler': { + 'preview': { + 'script': script, + } + } + } + } + assert data == expected