Skip to content

Commit

Permalink
Improved coverage of base device testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
burkeds committed Aug 27, 2024
1 parent 48a18f0 commit e8dfa71
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
9 changes: 4 additions & 5 deletions src/ophyd_async/tango/base_devices/_base_device.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import asyncio
from typing import Optional, Union

from ophyd_async.core import (
Expand Down Expand Up @@ -50,7 +49,7 @@ def __init__(
raise ValueError("Either 'trl' or 'device_proxy' must be provided.")

self.trl = trl if trl else ""
self.proxy = device_proxy if device_proxy else AsyncDeviceProxy(trl)
self.proxy = device_proxy

self.create_children_from_annotations()
super().__init__(name=name)
Expand All @@ -65,12 +64,13 @@ async def closure():
try:
if self.proxy is None:
self.proxy = await AsyncDeviceProxy(self.trl)
elif isinstance(self.proxy, asyncio.Future):
self.proxy = await self.proxy
except Exception as e:
raise RuntimeError("Could not connect to device proxy") from e
return self

if self.trl in ["", None]:
self.trl = self.proxy.name()

await closure()
self.register_signals()
# set_name should be called again to propagate the new signal names
Expand Down Expand Up @@ -120,7 +120,6 @@ def create_children_from_annotations(self):
infer_signal_frontend(trl=f"{self.trl}/" f"{tango_name}"),
)
else:
print(obj_type, type(obj_type))
raise ValueError(f"Invalid signal type {obj_type}")


Expand Down
13 changes: 10 additions & 3 deletions src/ophyd_async/tango/base_devices/_tango_readable.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from __future__ import annotations

from typing import Tuple
from typing import Optional, Tuple, Union

from ophyd_async.core import (
StandardReadable,
)
from ophyd_async.tango.base_devices._base_device import TangoDevice
from tango import DeviceProxy as SyncDeviceProxy
from tango.asyncio import DeviceProxy as AsyncDeviceProxy


class TangoReadable(TangoDevice, StandardReadable):
Expand All @@ -25,5 +27,10 @@ class TangoReadable(TangoDevice, StandardReadable):
# --------------------------------------------------------------------
_polling: Tuple = (False, 0.1, None, 0.1)

def __init__(self, trl: str, name="") -> None:
TangoDevice.__init__(self, trl, name=name)
def __init__(
self,
trl: Optional[str] = None,
device_proxy: Optional[Union[AsyncDeviceProxy, SyncDeviceProxy]] = None,
name: str = "",
) -> None:
TangoDevice.__init__(self, trl, device_proxy=device_proxy, name=name)
43 changes: 38 additions & 5 deletions tests/tango/test_base_device.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import asyncio
import time
from enum import Enum, IntEnum
from typing import Type
from typing import Optional, Type, Union

import bluesky.plan_stubs as bps
import bluesky.plans as bp
import numpy as np
import pytest
from bluesky import RunEngine

import tango
from ophyd_async.core import DeviceCollector, T
from ophyd_async.tango import TangoReadable, get_python_type, tango_signal_auto
from ophyd_async.tango.demo import (
Expand All @@ -24,7 +25,8 @@
CmdArgType,
DevState,
)
from tango.asyncio import DeviceProxy
from tango import DeviceProxy as SyncDeviceProxy
from tango.asyncio import DeviceProxy as AsyncDeviceProxy
from tango.asyncio_executor import set_global_executor
from tango.server import Device, attribute, command
from tango.test_context import MultiDeviceTestContext
Expand Down Expand Up @@ -175,9 +177,14 @@ def raise_exception_cmd(self):
class TestTangoReadable(TangoReadable):
__test__ = False

def __init__(self, trl: str, name="") -> None:
def __init__(
self,
trl: Optional[str] = None,
device_proxy: Optional[Union[AsyncDeviceProxy, SyncDeviceProxy]] = None,
name: str = "",
) -> None:
self.trl = trl
TangoReadable.__init__(self, trl, name)
TangoReadable.__init__(self, trl, device_proxy, name)

def register_signals(self):
for feature in TESTED_FEATURES:
Expand All @@ -196,7 +203,7 @@ def register_signals(self):
async def describe_class(fqtrl):
description = {}
values = {}
dev = await DeviceProxy(fqtrl)
dev = await AsyncDeviceProxy(fqtrl)

for name in TESTED_FEATURES:
if name in dev.get_attribute_list():
Expand Down Expand Up @@ -314,6 +321,11 @@ def compare_values(expected, received):
async def test_connect(tango_test_device):
values, description = await describe_class(tango_test_device)

with pytest.raises(ValueError) as excinfo:
async with DeviceCollector():
TestTangoReadable()
assert "Either 'trl' or 'device_proxy' must be provided." in str(excinfo.value)

async with DeviceCollector():
test_device = TestTangoReadable(tango_test_device)

Expand All @@ -322,6 +334,27 @@ async def test_connect(tango_test_device):
compare_values(values, await test_device.read())


# --------------------------------------------------------------------
@pytest.mark.asyncio
@pytest.mark.parametrize("proxy", [True, False, None])
async def test_connect_proxy(tango_test_device, proxy: Optional[bool]):
if proxy is None:
test_device = TestTangoReadable(trl=tango_test_device)
test_device.proxy = None
await test_device.connect()
assert isinstance(test_device.proxy, tango._tango.DeviceProxy)
elif proxy:
proxy = await AsyncDeviceProxy(tango_test_device)
test_device = TestTangoReadable(device_proxy=proxy)
await test_device.connect()
assert isinstance(test_device.proxy, tango._tango.DeviceProxy)
else:
proxy = SyncDeviceProxy(tango_test_device)
test_device = TestTangoReadable(device_proxy=proxy)
await test_device.connect()
assert isinstance(test_device.proxy, tango._tango.DeviceProxy)


# --------------------------------------------------------------------
@pytest.mark.asyncio
async def test_with_bluesky(tango_test_device):
Expand Down

0 comments on commit e8dfa71

Please sign in to comment.