From 2dc720e17af3018f18fbba7d046122deff68d09c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20M=C3=B6ller?= Date: Tue, 29 Aug 2023 15:52:58 +0200 Subject: [PATCH] test.model.test_base: add test for `add` and `del` atomicity of `ConstrainedList` --- test/model/test_base.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/model/test_base.py b/test/model/test_base.py index ace44a33d..2b91ee03e 100644 --- a/test/model/test_base.py +++ b/test/model/test_base.py @@ -1114,3 +1114,28 @@ def del_hook(itm: int, list_: List[int]) -> None: self.assertEqual(existing_items, [1, 2, 3, 4, 10, 11]) check_list.pop() self.assertEqual(c_list, check_list) + + def test_atomicity(self) -> None: + def hook(itm: int, _list: List[int]) -> None: + if itm > 2: + raise ValueError + + c_list: model.ConstrainedList[int] = model.ConstrainedList([], item_add_hook=hook) + with self.assertRaises(ValueError): + c_list = model.ConstrainedList([1, 2, 3], item_add_hook=hook) + self.assertEqual(c_list, []) + with self.assertRaises(ValueError): + c_list.extend([1, 2, 3]) + self.assertEqual(c_list, []) + c_list.extend([1, 2]) + self.assertEqual(c_list, [1, 2]) + + c_list = model.ConstrainedList([1, 2, 3], item_del_hook=hook) + with self.assertRaises(ValueError): + del c_list[0:3] + self.assertEqual(c_list, [1, 2, 3]) + with self.assertRaises(ValueError): + c_list.clear() + self.assertEqual(c_list, [1, 2, 3]) + del c_list[0:2] + self.assertEqual(c_list, [3])