def delete(author_email, contact_id): """Delete a listing contact through the JSON-REST API. @param author_email: The email address that the listing belongs to. @type author_email: str @param contact_id: The integer ID of the contact record to delete. @type contact_id: str or int @return: Confirmation message that the listing contact was deleted with status code 200. @rtype: tuple (str, int) """ # TODO: Put db operations in util.after_this_request listing = services.listing_service.read_by_email(author_email) contacts = listing.get('contact_infos', None) if not contacts: return 'Contact not found', 404 success = util.remove_element_by_id(contacts, contact_id) if not success: return 'Contact not found', 404 listing['contact_infos'] = contacts services.listing_service.update(listing) return 'Contact deleted.', 200
def test_remove_element_by_id_exists(self): tags = [TEST_TAG0, TEST_TAG1] result = util.remove_element_by_id(tags, 0) self.assertTrue(result) self.assertEqual(1, len(tags)) self.assertEqual(TEST_TAG1, tags[0])
def test_remove_element_by_id_not_exist(self): tags = [TEST_TAG0, TEST_TAG1] result = util.remove_element_by_id(tags, 3) self.assertFalse(result) self.assertEqual(2, len(tags)) self.assertEqual(TEST_TAG0, tags[0]) self.assertEqual(TEST_TAG1, tags[1])
def test_delete(self): test_listing = copy.deepcopy(TEST_LISTING) test_listing['contact_infos'] = [TEST_CONTACT] self.mox.StubOutWithMock(services.listing_service, 'read_by_email') services.listing_service.read_by_email(TEST_EMAIL).AndReturn( test_listing ) self.mox.StubOutWithMock(util, 'remove_element_by_id') util.remove_element_by_id(test_listing['contact_infos'], 0).AndReturn( True ) self.mox.StubOutWithMock(services.listing_service, 'update') services.listing_service.update(mox.IsA(dict)) self.mox.ReplayAll() response = self.app.delete( '/author/content/' + TEST_EMAIL + '/contact/0' ) self.assertTrue(200, response.status_code)