Skip to content

Commit

Permalink
Merge pull request #150 from simonsobs/dev
Browse files Browse the repository at this point in the history
Add query `preview`
  • Loading branch information
TaiSakuma authored Jan 10, 2025
2 parents 265f7cb + 9174eee commit 6b59e40
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/nextline_schedule/graphql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
9 changes: 9 additions & 0 deletions src/nextline_schedule/graphql/queries/scheduler/Preview.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
query ScheduleSchedulerPreview {
schedule {
scheduler {
preview {
script
}
}
}
}
17 changes: 17 additions & 0 deletions src/nextline_schedule/schema/scheduler/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
29 changes: 29 additions & 0 deletions tests/schema/queries/scheduler/test_preview.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6b59e40

Please sign in to comment.