Skip to content

Commit

Permalink
TST: first pass status and cl tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tangkong committed Jun 13, 2024
1 parent 6e74fbf commit a1b9470
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ file = "dev-requirements.txt"
file = "docs-requirements.txt"

[tool.pytest.ini_options]
addopts = "--cov=."
addopts = "--cov=. --asyncio-mode=auto"
22 changes: 22 additions & 0 deletions superscore/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from superscore.backends.core import _Backend
from superscore.backends.filestore import FilestoreBackend
from superscore.backends.test import TestBackend
from superscore.control_layers._base_shim import _BaseShim
from superscore.control_layers.core import ControlLayer
from superscore.model import (Collection, Parameter, Readback, Root, Setpoint,
Snapshot)

Expand Down Expand Up @@ -675,3 +677,23 @@ def test_backends(filestore_backend: FilestoreBackend) -> List[_Backend]:
def backends(request, test_backends: List[_Backend]):
i = request.param
return test_backends[i]


class DummyShim(_BaseShim):
"""Shim that does nothing"""
async def get(self, *args, **kwargs):
return

async def put(self, *args, **kwargs):
return

def monitor(self, *args, **kwargs):
return


@pytest.fixture(scope='function')
def dummy_cl() -> ControlLayer:
cl = ControlLayer()
cl.shims['ca'] = DummyShim()
cl.shims['pva'] = DummyShim()
return cl
34 changes: 34 additions & 0 deletions superscore/tests/test_cl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from unittest.mock import AsyncMock

from superscore.control_layers.status import TaskStatus


def test_get(dummy_cl):
mock_ca_get = AsyncMock(return_value='ca_value')
dummy_cl.shims['ca'].get = mock_ca_get
mock_pva_get = AsyncMock(return_value='pva_value')
dummy_cl.shims['pva'].get = mock_pva_get
assert dummy_cl.get("SOME_PREFIX") == "ca_value"
assert dummy_cl.get("ca://SOME_PREFIX") == "ca_value"
assert dummy_cl.get("pva://SOME_PREFIX") == "pva_value"


def test_put(dummy_cl):
result = dummy_cl.put("OTHER:PREFIX", 4)
assert isinstance(result, TaskStatus)
assert result.done

results = dummy_cl.put(["OTHER:PREFIX", "GE", "LT"], [4, 5, 6])
assert all(isinstance(res, TaskStatus) for res in results)
assert result.done


def test_put_callback(dummy_cl):
cbs = []

# callback gets called with the task as a single argument
result = dummy_cl.put("SOME:PREFIX", 2, cbs.append)

assert result.exception() is None
assert result.success is True
assert len(cbs) == 1
54 changes: 54 additions & 0 deletions superscore/tests/test_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import asyncio
from typing import Any, Callable

import pytest

from superscore.control_layers.status import TaskStatus


@pytest.fixture
async def normal_coroutine() -> Callable[[], Any]:
async def inner_coroutine():
await asyncio.sleep(0.01)

return inner_coroutine


@pytest.fixture
async def failing_coroutine() -> Callable[[], Any]:
async def inner_coroutine():
await asyncio.sleep(0.01)
raise ValueError()

return inner_coroutine


async def test_status_success(normal_coroutine):
st = TaskStatus(normal_coroutine())
assert isinstance(st, TaskStatus)
assert not st.done
assert not st.success
await st
assert st.done
assert st.success


async def test_status_fail(failing_coroutine):
status = TaskStatus(failing_coroutine())
assert status.exception() is None

with pytest.raises(ValueError):
await status

assert type(status.exception()) == ValueError


async def test_status_wrap():
@TaskStatus.wrap
async def coro_status():
await asyncio.sleep(0.01)

st = coro_status()
assert isinstance(st, TaskStatus)
await st.task
assert st.done

0 comments on commit a1b9470

Please sign in to comment.