def test_get_valid_items(self): self.assertEqual(get_item(self.test_dict, 'name'), 'Sherlock Holmes') self.assertEqual(get_item(self.test_dict, 'books'), [ 'A Study in Scarlet', 'The Sign of the Four', 'The Adventures of Sherlock Holmes' ]) self.assertEqual(get_item(self.test_dict, 'address'), { 'street': {'type': 'Street', 'name': 'Baker'}, 'number': '221B', 'city': 'London', 'country': 'England' }) self.assertEqual(get_item(self.test_dict, 'address.city'), 'London') self.assertEqual( get_item(self.test_dict, 'address.street.name'), 'Baker' )
def test_get_invalid_items(self): with self.assertRaises(KeyError) as cm: get_item(self.test_dict, 'birth_date') self.assertEqual( cm.exception.args[0] if PYTHON3 else cm.exception.message, 'birth_date' ) with self.assertRaises(KeyError) as cm: get_item(self.test_dict, 'address.postal_code') self.assertEqual( cm.exception.args[0] if PYTHON3 else cm.exception.message, 'postal_code' ) with self.assertRaises(KeyError) as cm: get_item(self.test_dict, 'address.street.old_name') self.assertEqual( cm.exception.args[0] if PYTHON3 else cm.exception.message, 'old_name' )