def test_comparison(self): """Frozen nodes with the same values are considered equal""" args = {'text': 'text', 'children': [struct.FrozenNode(text='child')], 'label': ['b', 'c'], 'title': 'title', 'tagged_text': 'tagged_text'} left = struct.FrozenNode(**args) right = struct.FrozenNode(**args) self.assertNotEqual(id(left), id(right)) self.assertEqual(left, right)
def test_hash(self): """Different fields lead to different hashes. The same fields lead to the same hash""" args = {'text': 'text', 'children': [struct.FrozenNode(text='child')], 'label': ['b', 'c'], 'title': 'title', 'tagged_text': 'tagged_text'} same1 = struct.FrozenNode(**args) same2 = struct.FrozenNode(**args) args['text'] = 'new text' diff = struct.FrozenNode(**args) self.assertNotEqual(id(same1), id(same2)) self.assertNotEqual(id(same2), id(diff)) self.assertEqual(same1.hash, same2.hash) self.assertNotEqual(same1.hash, diff.hash)
def test_in_set(self): """Frozen nodes with the same values are not replaced in a set""" args = {'text': 'text', 'children': [struct.FrozenNode(text='child')], 'label': ['b', 'c'], 'title': 'title', 'tagged_text': 'tagged_text'} left = struct.FrozenNode(**args) right = struct.FrozenNode(**args) self.assertNotEqual(id(left), id(right)) dict_to_set = {left: {left}} self.assertTrue(right in dict_to_set) self.assertTrue(right in dict_to_set[right]) set_node = dict_to_set[right].pop() self.assertEqual(id(set_node), id(left)) self.assertNotEqual(id(set_node), id(right))