adds test for dict/list garbage collection

This commit is contained in:
Mose Müller 2024-05-21 13:39:41 +02:00
parent f9a5352efe
commit 6a894b6154

View File

@ -311,3 +311,51 @@ def test_list_remove(caplog: pytest.LogCaptureFixture) -> None:
# checks if observer key was updated correctly (was index 1) # checks if observer key was updated correctly (was index 1)
other_observable_instance_2.greeting = "Ciao" other_observable_instance_2.greeting = "Ciao"
assert "'my_list[0].greeting' changed to 'Ciao'" in caplog.text assert "'my_list[0].greeting' changed to 'Ciao'" in caplog.text
def test_list_garbage_collection() -> None:
"""Makes sure that the GC collects lists that are not referenced anymore."""
import gc
import json
list_json = """
[1]
"""
class MyObservable(Observable):
def __init__(self) -> None:
super().__init__()
self.list_attr = json.loads(list_json)
observable = MyObservable()
list_mapping_length = len(observable._list_mapping)
observable.list_attr = json.loads(list_json)
gc.collect()
assert len(observable._list_mapping) <= list_mapping_length
def test_dict_garbage_collection() -> None:
"""Makes sure that the GC collects dicts that are not referenced anymore."""
import gc
import json
dict_json = """
{
"foo": "bar"
}
"""
class MyObservable(Observable):
def __init__(self) -> None:
super().__init__()
self.dict_attr = json.loads(dict_json)
observable = MyObservable()
dict_mapping_length = len(observable._dict_mapping)
observable.dict_attr = json.loads(dict_json)
gc.collect()
assert len(observable._dict_mapping) <= dict_mapping_length