def test_validate_should_raise_an_exception_if_name_is_none_or_empty(self): c = Contact() self.assertRaises(ValidationError, lambda: c.validate()) c.name = '' self.assertRaises(ValidationError, lambda: c.validate())
def test_get_all_should_return_all_contacts(self): self.repo.save(Contact('Pedro', '123', 'abc')) self.repo.save(Contact('Juan', '123', 'abc')) self.repo.save(Contact('Diego', '123', 'abc')) self.flush() contacts = self.repo.get_all() self.assertIsNotNone(contacts) self.assertEqual(3, len(contacts))
def test_get_all_should_return_all_contacts(self): self.repo.get_all.return_value = [ Contact('A'), Contact('B'), Contact('C'), ] contacts = self.manager.get_all() self.assertIsNotNone(contacts) self.assertEqual(3, len(contacts))
def test_get_contact_by_id_should_return_an_instance_when_exists(self): self.repo.get.return_value = Contact('Moe') contact = self.manager.get_contact_by_id(1) self.assertIsNotNone(contact) self.assertEqual('Moe', contact.name)
def create_contact(self, name: str, phone: str, email: str): contact = Contact(name, phone, email) self._validate_contact(contact) self._add_contact(contact) return contact
def test_save_should_persist_and_generate_the_id(self): contact = Contact('Marge Simpson', '+1 23456789', '*****@*****.**') self.repo.save(contact) self.flush() self.assertEqual(1, contact.id)
def test_get_should_return_an_existing_instance(self): new_contact = Contact('Moe', '123', 'abc') self.repo.save(new_contact) self.flush() contact = self.repo.get(new_contact.id) self.assertIsNotNone(contact) self.assertEqual('Moe', contact.name) self.assertEqual('123', contact.phone) self.assertEqual('abc', contact.email)
def test_init_should_initialize_the_instance(self): c = Contact('Homer Simpson', '+1 555-7334', '*****@*****.**') self.assertEqual('Homer Simpson', c.name) self.assertEqual('+1 555-7334', c.phone) self.assertEqual('*****@*****.**', c.email)
def _validate_contact(self, contact: Contact): try: contact.validate() except ValidationError as ve: raise ServiceException from ve