-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |