def delete_productline(productline_id: str): user_id = flask.request.user.user_id try: _productlines.delete_productline(user_id, productline_id) except DatabaseError: return 'A database error prevents deletion of this product line', 404 except _productlines.NotFound: return 'Product line not found', 404 except PermissionError: return 'User `{}` does not have permission to delete this product line'.format(user_id), 403 return 'Deleted product line {}'.format(productline_id), 200
def test_throws_on_database_error_during_delete(self): self.mock_select.return_value.fetchone.return_value = {'owned_by': 'test-user-id'} self.mock_delete.side_effect = helpers.create_database_error() with self.assertRaises(DatabaseError): productlines.delete_productline('test-user-id', 'test-productline-id')
def test_throws_on_database_error_during_select(self): self.mock_select.side_effect = helpers.create_database_error() with self.assertRaises(DatabaseError): productlines.delete_productline('test-user-id', 'test-productline-id')
def test_throws_when_not_owned_by_requesting_user(self): self.mock_select.return_value.fetchone.return_value = {'owned_by': 'Rumplestiltzkin'} with self.assertRaises(PermissionError): productlines.delete_productline('person who is not Rumplestiltzkin', 'test-productline-id')
def test_throws_when_productline_not_found(self): self.mock_select.return_value.fetchone.return_value = None with self.assertRaises(productlines.NotFound): productlines.delete_productline('test-user-id', 'test-productline-id')
def test_closes_connection_after_operation(self): self.mock_select.return_value.fetchone.return_value = {'owned_by': 'test-user-id'} productlines.delete_productline('test-user-id', 'test-productline-id') self.assertEqual(1, self._mockdb.close.call_count)
def test_deletes_productline_from_database(self): self.mock_select.return_value.fetchone.return_value = {'owned_by': 'test-user-id'} productlines.delete_productline('test-user-id', 'test-productline-id') self.assertEqual(1, self.mock_delete.call_count)