def test_key_validation_raises_exception_if_the_key_is_not_in_every_elem(self): iterable = [{'key': 5}, {'key2': 1}, {'key': 2}] key = 'key' exc = None try: validate_key(iterable, key) except Exception as err: exc = err self.assertIsNotNone(exc) self.assertEqual(str(exc), 'Element without expected key. Cannot compare.')
def test_key_validation_raises_exception_if_key_is_not_string(self): iterable = [{'key': 5}, {'key': 7}, {'key': 2}] key = 5 exc = None try: validate_key(iterable, key) except Exception as err: exc = err self.assertIsNotNone(exc) self.assertEqual(str(exc), 'Key must be of type "string".')
def test_key_validation_raises_exception_if_elems_are_dictionaries_but_key_is_none(self): iterable = [{'key': 5}, {'key': 1}, {'key': 2}] key = None exc = None try: validate_key(iterable, key) except Exception as err: exc = err self.assertIsNotNone(exc) self.assertEqual(str(exc), 'Cannot compare dictionaries without key.')
def test_key_validation_passes_with_correct_input(self): iterable = [{'key': 5}, {'key': 7}, {'key': 2}] key = 'key' validate_key(iterable, key)
def test_key_validation_passes_with_elements_that_are_not_dicts_and_none_key(self): iterable = [4, 5, 3, 1, 2] key = None validate_key(iterable, key)