def test_trait_dict_notification_compat(self): events = [] def notifier(*args, **kwargs): event = dict_event_factory(*args, **kwargs) events.append(event) trait_dict = TraitDict( { "3": 3, "4": 4 }, notifiers=[notifier], ) # when del trait_dict["4"] # then event, = events self.assertIs(event.object, trait_dict) self.assertEqual(event.removed, {"4": 4}) # when events.clear() trait_dict.update({"3": None, "1": 1}) # then event, = events self.assertEqual(event.removed, {"3": 3}) self.assertEqual(event.added, {"3": None, "1": 1})
def test_update_iterable(self): td = TraitDict({"a": 1, "b": 2}, key_validator=str_validator, value_validator=int_validator, notifiers=[self.notification_handler]) td.update([("a", 2), ("b", 4), ("c", 5)]) self.assertEqual(self.added, {"c": 5}) self.assertEqual(self.changed, {"a": 1, "b": 2}) self.assertEqual(self.removed, {})
def test_update_with_empty_argument(self): td = TraitDict( {"1": 1, "2": 2}, key_validator=str, notifiers=[self.notification_handler], ) # neither of these should cause a notification to be emitted td.update([]) td.update({}) self.assertEqual(td, {"1": 1, "2": 2}) self.assertIsNone(self.added) self.assertIsNone(self.changed) self.assertIsNone(self.removed)
def test_update_notifies_with_nonempty_argument(self): # Corner case: we don't want to get into the difficulties of # comparing values for equality, so we notify for a non-empty # argument even if the dictionary has not actually changed. td = TraitDict( {"1": 1, "2": 2}, key_validator=str, notifiers=[self.notification_handler], ) td.update({"1": 1}) self.assertEqual(td, {"1": 1, "2": 2}) self.assertEqual(self.added, {}) self.assertEqual(self.changed, {"1": 1}) self.assertEqual(self.removed, {})
def test_update_with_transformation(self): td = TraitDict( {"1": 1, "2": 2}, key_validator=str, notifiers=[self.notification_handler], ) # when td.update({1: 2}) # then self.assertEqual(td, {"1": 2, "2": 2}) self.assertEqual(self.added, {}) self.assertEqual(self.changed, {"1": 1}) self.assertEqual(self.removed, {})