fix: removes monkey path of emit_notification, adapts affected tests

This commit is contained in:
Mose Müller
2023-11-06 13:46:08 +01:00
parent 53ce51991f
commit f88493d97c
10 changed files with 221 additions and 589 deletions

View File

@ -1,9 +1,9 @@
from pytest import CaptureFixture
from pytest import LogCaptureFixture
from pydase import DataService
def test_properties(capsys: CaptureFixture) -> None:
def test_properties(caplog: LogCaptureFixture) -> None:
class ServiceClass(DataService):
_voltage = 10.0
_current = 1.0
@ -31,30 +31,17 @@ def test_properties(capsys: CaptureFixture) -> None:
test_service = ServiceClass()
test_service.voltage = 1
captured = capsys.readouterr()
expected_output = sorted(
[
"ServiceClass.power = 1.0",
"ServiceClass.voltage = 1.0",
]
)
actual_output = sorted(captured.out.strip().split("\n"))
assert actual_output == expected_output
assert "ServiceClass.power changed to 1.0" in caplog.text
assert "ServiceClass.voltage changed to 1.0" in caplog.text
caplog.clear()
test_service.current = 12.0
captured = capsys.readouterr()
expected_output = sorted(
[
"ServiceClass.power = 12.0",
"ServiceClass.current = 12.0",
]
)
actual_output = sorted(captured.out.strip().split("\n"))
assert actual_output == expected_output
assert "ServiceClass.power changed to 12.0" in caplog.text
assert "ServiceClass.current changed to 12.0" in caplog.text
def test_nested_properties(capsys: CaptureFixture) -> None:
def test_nested_properties(caplog: LogCaptureFixture) -> None:
class SubSubClass(DataService):
name = "Hello"
@ -77,45 +64,31 @@ def test_nested_properties(capsys: CaptureFixture) -> None:
test_service = ServiceClass()
test_service.name = "Peepz"
captured = capsys.readouterr()
expected_output = sorted(
[
"ServiceClass.name = Peepz",
"ServiceClass.sub_name = Hello Peepz",
"ServiceClass.subsub_name = Hello Peepz",
]
)
actual_output = sorted(captured.out.strip().split("\n"))
assert actual_output == expected_output
assert "ServiceClass.name changed to Peepz" in caplog.text
assert "ServiceClass.sub_name changed to Hello Peepz" in caplog.text
assert "ServiceClass.subsub_name changed to Hello Peepz" in caplog.text
caplog.clear()
test_service.class_attr.name = "Hi"
captured = capsys.readouterr()
expected_output = sorted(
[
"ServiceClass.sub_name = Hi Peepz",
"ServiceClass.subsub_name = Hello Peepz", # registers subclass changes
"ServiceClass.class_attr.name = Hi",
]
)
actual_output = sorted(captured.out.strip().split("\n"))
assert actual_output == expected_output
assert "ServiceClass.sub_name changed to Hi Peepz" in caplog.text
assert (
"ServiceClass.subsub_name changed to Hello Peepz" in caplog.text
) # registers subclass changes
assert "ServiceClass.class_attr.name changed to Hi" in caplog.text
caplog.clear()
test_service.class_attr.class_attr.name = "Ciao"
captured = capsys.readouterr()
expected_output = sorted(
[
"ServiceClass.sub_name = Hi Peepz", # registers subclass changes
"ServiceClass.subsub_name = Ciao Peepz",
"ServiceClass.class_attr.class_attr.name = Ciao",
]
)
actual_output = sorted(captured.out.strip().split("\n"))
assert actual_output == expected_output
assert (
"ServiceClass.sub_name changed to Hi Peepz" in caplog.text
) # registers subclass changes
assert "ServiceClass.subsub_name changed to Ciao Peepz" in caplog.text
assert "ServiceClass.class_attr.class_attr.name changed to Ciao" in caplog.text
caplog.clear()
def test_simple_list_properties(capsys: CaptureFixture) -> None:
def test_simple_list_properties(caplog: LogCaptureFixture) -> None:
class ServiceClass(DataService):
list = ["Hello", "Ciao"]
name = "World"
@ -127,30 +100,17 @@ def test_simple_list_properties(capsys: CaptureFixture) -> None:
test_service = ServiceClass()
test_service.name = "Peepz"
captured = capsys.readouterr()
expected_output = sorted(
[
"ServiceClass.name = Peepz",
"ServiceClass.total_name = Hello Peepz",
]
)
actual_output = sorted(captured.out.strip().split("\n"))
assert actual_output == expected_output
assert "ServiceClass.name changed to Peepz" in caplog.text
assert "ServiceClass.total_name changed to Hello Peepz" in caplog.text
caplog.clear()
test_service.list[0] = "Hi"
captured = capsys.readouterr()
expected_output = sorted(
[
"ServiceClass.total_name = Hi Peepz",
"ServiceClass.list[0] = Hi",
]
)
actual_output = sorted(captured.out.strip().split("\n"))
assert actual_output == expected_output
assert "ServiceClass.total_name changed to Hi Peepz" in caplog.text
assert "ServiceClass.list[0] changed to Hi" in caplog.text
def test_class_list_properties(capsys: CaptureFixture) -> None:
def test_class_list_properties(caplog: LogCaptureFixture) -> None:
class SubClass(DataService):
name = "Hello"
@ -165,30 +125,17 @@ def test_class_list_properties(capsys: CaptureFixture) -> None:
test_service = ServiceClass()
test_service.name = "Peepz"
captured = capsys.readouterr()
expected_output = sorted(
[
"ServiceClass.name = Peepz",
"ServiceClass.total_name = Hello Peepz",
]
)
actual_output = sorted(captured.out.strip().split("\n"))
assert actual_output == expected_output
assert "ServiceClass.name changed to Peepz" in caplog.text
assert "ServiceClass.total_name changed to Hello Peepz" in caplog.text
caplog.clear()
test_service.list[0].name = "Hi"
captured = capsys.readouterr()
expected_output = sorted(
[
"ServiceClass.total_name = Hi Peepz",
"ServiceClass.list[0].name = Hi",
]
)
actual_output = sorted(captured.out.strip().split("\n"))
assert actual_output == expected_output
assert "ServiceClass.total_name changed to Hi Peepz" in caplog.text
assert "ServiceClass.list[0].name changed to Hi" in caplog.text
def test_subclass_properties(capsys: CaptureFixture) -> None:
def test_subclass_properties(caplog: LogCaptureFixture) -> None:
class SubClass(DataService):
name = "Hello"
_voltage = 10.0
@ -224,21 +171,15 @@ def test_subclass_properties(capsys: CaptureFixture) -> None:
test_service = ServiceClass()
test_service.class_attr.voltage = 10.0
captured = capsys.readouterr()
expected_output = sorted(
{
"ServiceClass.class_attr.voltage = 10.0",
"ServiceClass.class_attr.power = 10.0",
"ServiceClass.voltage = 10.0",
}
)
# using a set here as "ServiceClass.voltage = 10.0" is emitted twice. Once for
# changing voltage, and once for changing power.
actual_output = sorted(set(captured.out.strip().split("\n")))
assert actual_output == expected_output
assert "ServiceClass.class_attr.voltage changed to 10.0" in caplog.text
assert "ServiceClass.class_attr.power changed to 10.0" in caplog.text
assert "ServiceClass.voltage changed to 10.0" in caplog.text
caplog.clear()
def test_subclass_properties_2(capsys: CaptureFixture) -> None:
def test_subclass_properties_2(caplog: LogCaptureFixture) -> None:
class SubClass(DataService):
name = "Hello"
_voltage = 10.0
@ -274,24 +215,17 @@ def test_subclass_properties_2(capsys: CaptureFixture) -> None:
test_service = ServiceClass()
test_service.class_attr[1].current = 10.0
captured = capsys.readouterr()
expected_output = sorted(
{
"ServiceClass.class_attr[1].current = 10.0",
"ServiceClass.class_attr[1].power = 100.0",
"ServiceClass.voltage = 10.0",
}
)
# using a set here as "ServiceClass.voltage = 10.0" is emitted twice. Once for
# changing current, and once for changing power. Note that the voltage property is
# only dependent on class_attr[0] but still emits an update notification. This is
# because every time any item in the list `test_service.class_attr` is changed,
# a notification will be emitted.
actual_output = sorted(set(captured.out.strip().split("\n")))
assert actual_output == expected_output
assert "ServiceClass.class_attr[1].current changed to 10.0" in caplog.text
assert "ServiceClass.class_attr[1].power changed to 100.0" in caplog.text
assert "ServiceClass.voltage changed to 10.0" in caplog.text
def test_subsubclass_properties(capsys: CaptureFixture) -> None:
def test_subsubclass_properties(caplog: LogCaptureFixture) -> None:
class SubSubClass(DataService):
_voltage = 10.0
@ -321,21 +255,18 @@ def test_subsubclass_properties(capsys: CaptureFixture) -> None:
test_service = ServiceClass()
test_service.class_attr[1].class_attr.voltage = 100.0
captured = capsys.readouterr()
expected_output = sorted(
{
"ServiceClass.class_attr[0].class_attr.voltage = 100.0",
"ServiceClass.class_attr[1].class_attr.voltage = 100.0",
"ServiceClass.class_attr[0].power = 50.0",
"ServiceClass.class_attr[1].power = 50.0",
"ServiceClass.power = 50.0",
}
assert (
"ServiceClass.class_attr[0].class_attr.voltage changed to 100.0" in caplog.text
)
actual_output = sorted(set(captured.out.strip().split("\n")))
assert actual_output == expected_output
assert (
"ServiceClass.class_attr[1].class_attr.voltage changed to 100.0" in caplog.text
)
assert "ServiceClass.class_attr[0].power changed to 50.0" in caplog.text
assert "ServiceClass.class_attr[1].power changed to 50.0" in caplog.text
assert "ServiceClass.power changed to 50.0" in caplog.text
def test_subsubclass_instance_properties(capsys: CaptureFixture) -> None:
def test_subsubclass_instance_properties(caplog: LogCaptureFixture) -> None:
class SubSubClass(DataService):
def __init__(self) -> None:
self._voltage = 10.0
@ -369,16 +300,9 @@ def test_subsubclass_instance_properties(capsys: CaptureFixture) -> None:
test_service = ServiceClass()
test_service.class_attr[1].attr[0].voltage = 100.0
captured = capsys.readouterr()
# again, changing an item in a list will trigger the callbacks. This is why a
# notification for `ServiceClass.power` is emitted although it did not change its
# value
expected_output = sorted(
{
"ServiceClass.class_attr[1].attr[0].voltage = 100.0",
"ServiceClass.class_attr[1].power = 50.0",
"ServiceClass.power = 5.0",
}
)
actual_output = sorted(set(captured.out.strip().split("\n")))
assert actual_output == expected_output
assert "ServiceClass.class_attr[1].attr[0].voltage changed to 100.0" in caplog.text
assert "ServiceClass.class_attr[1].power changed to 50.0" in caplog.text
assert "ServiceClass.power changed to 5.0" in caplog.text