From 6d23151d322635f999af4bfdc7f62bb815661459 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mose=20M=C3=BCller?= Date: Mon, 11 Dec 2023 09:15:22 +0100 Subject: [PATCH] updates tests --- tests/components/test_coloured_enum.py | 5 +- tests/data_service/test_data_service.py | 83 +++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/tests/components/test_coloured_enum.py b/tests/components/test_coloured_enum.py index 2e90884..3e652b0 100644 --- a/tests/components/test_coloured_enum.py +++ b/tests/components/test_coloured_enum.py @@ -39,6 +39,9 @@ def test_warning(caplog: LogCaptureFixture) -> None: class ServiceClass(DataService): status = MyStatus.RUNNING + ServiceClass() + assert ( - "Warning: Class MyStatus does not inherit from DataService." not in caplog.text + "Class 'MyStatus' does not inherit from DataService. This may lead to " + "unexpected behaviour!" not in caplog.text ) diff --git a/tests/data_service/test_data_service.py b/tests/data_service/test_data_service.py index bb44942..4c7fa68 100644 --- a/tests/data_service/test_data_service.py +++ b/tests/data_service/test_data_service.py @@ -1,3 +1,5 @@ +from enum import Enum + import pydase.units as u from pydase import DataService from pydase.data_service.data_service_observer import DataServiceObserver @@ -27,3 +29,84 @@ def test_unexpected_type_change_warning(caplog: LogCaptureFixture) -> None: "Type of 'current' changed from 'Quantity' to 'int'. This may have unwanted " "side effects! Consider setting it to 'Quantity' directly." in caplog.text ) + + +def test_basic_inheritance_warning(caplog: LogCaptureFixture) -> None: + class SubService(DataService): + ... + + class SomeEnum(Enum): + HI = 0 + + class ServiceClass(DataService): + sub_service = SubService() + some_int = 1 + some_float = 1.0 + some_bool = True + some_quantity = 1.0 * u.units.A + some_string = "Hello" + some_enum = SomeEnum.HI + _name = "Service" + + @property + def name(self) -> str: + return self._name + + def some_method(self) -> None: + ... + + ServiceClass() + + # neither of the attributes, methods or properties cause a warning log + assert "WARNING" not in caplog.text + + +def test_class_attr_inheritance_warning(caplog: LogCaptureFixture) -> None: + class SubClass: + name = "Hello" + + class ServiceClass(DataService): + attr_1 = SubClass() + + ServiceClass() + + assert ( + "Class 'SubClass' does not inherit from DataService. This may lead to " + "unexpected behaviour!" + ) in caplog.text + + +def test_instance_attr_inheritance_warning(caplog: LogCaptureFixture) -> None: + class SubClass: + name = "Hello" + + class ServiceClass(DataService): + def __init__(self) -> None: + super().__init__() + self.attr_1 = SubClass() + + ServiceClass() + + assert ( + "Class 'SubClass' does not inherit from DataService. This may lead to " + "unexpected behaviour!" + ) in caplog.text + + +def test_protected_and_private_attribute_warning(caplog: LogCaptureFixture) -> None: + class SubClass: + name = "Hello" + + class ServiceClass(DataService): + def __init__(self) -> None: + super().__init__() + self._subclass = SubClass() + self.__other_subclass = SubClass() + + ServiceClass() + + # Protected and private attributes are not checked + assert ( + "Class 'SubClass' does not inherit from DataService. This may lead to " + "unexpected behaviour!" + ) not in caplog.text