updates client tests

This commit is contained in:
Mose Müller 2024-04-09 09:25:15 +02:00
parent 61c7dc8333
commit c148eba5dd

View File

@ -9,12 +9,16 @@ from pydase.client.proxy_loader import ProxyAttributeError
@pytest.fixture(scope="session") @pytest.fixture(scope="session")
def pydase_server() -> Generator[pydase.Server, None, Any]: def pydase_client() -> Generator[pydase.Client, None, Any]:
class SubService(pydase.DataService):
name = "SubService"
class MyService(pydase.DataService): class MyService(pydase.DataService):
def __init__(self) -> None: def __init__(self) -> None:
super().__init__() super().__init__()
self._name = "MyService" self._name = "MyService"
self._my_property = 12.1 self._my_property = 12.1
self.sub_service = SubService()
@property @property
def my_property(self) -> float: def my_property(self) -> float:
@ -32,40 +36,46 @@ def pydase_server() -> Generator[pydase.Server, None, Any]:
return input_str return input_str
server = pydase.Server(MyService(), web_port=9999) server = pydase.Server(MyService(), web_port=9999)
thread = threading.Thread(target=server.run) thread = threading.Thread(target=server.run, daemon=True)
thread.start() thread.start()
time.sleep(0.1) # Wait for the server to start
yield server client = pydase.Client(port=9999)
while not client.proxy.connected:
time.sleep(0.001) # Wait for the client to connect
yield client
server.handle_exit() server.handle_exit()
thread.join() thread.join()
def test_property(pydase_server: pydase.Server) -> None: def test_property(pydase_client: pydase.Client) -> None:
client = pydase.Client(port=9999) assert pydase_client.proxy.my_property == 12.1
pydase_client.proxy.my_property = 2.1
assert client.proxy.my_property == 12.1 assert pydase_client.proxy.my_property == 2.1
client.proxy.my_property = 2.1
assert client.proxy.my_property == 2.1
def test_readonly_property(pydase_server: pydase.Server) -> None: def test_readonly_property(pydase_client: pydase.Client) -> None:
client = pydase.Client(port=9999) assert pydase_client.proxy.name == "MyService"
assert client.proxy.name == "MyService"
with pytest.raises(ProxyAttributeError): with pytest.raises(ProxyAttributeError):
client.proxy.name = "Hello" pydase_client.proxy.name = "Hello"
def test_method_execution(pydase_server: pydase.Server) -> None: def test_method_execution(pydase_client: pydase.Client) -> None:
client = pydase.Client(port=9999) assert pydase_client.proxy.my_method("My return string") == "My return string"
assert (
assert client.proxy.my_method("My return string") == "My return string" pydase_client.proxy.my_method(input_str="My return string")
assert client.proxy.my_method(input_str="My return string") == "My return string" == "My return string"
)
with pytest.raises(TypeError): with pytest.raises(TypeError):
client.proxy.my_method("Something", 2) pydase_client.proxy.my_method("Something", 2)
with pytest.raises(TypeError): with pytest.raises(TypeError):
client.proxy.my_method(kwarg="hello") pydase_client.proxy.my_method(kwarg="hello")
def test_nested_service(pydase_client: pydase.Client) -> None:
assert pydase_client.proxy.sub_service.name == "SubService"
pydase_client.proxy.sub_service.name = "New name"
assert pydase_client.proxy.sub_service.name == "New name"