diff --git a/sortinghat/cmd/enroll.py b/sortinghat/cmd/enroll.py index 15c32fd1f..6a64f7873 100644 --- a/sortinghat/cmd/enroll.py +++ b/sortinghat/cmd/enroll.py @@ -26,8 +26,8 @@ import argparse from .. import api, utils -from ..command import Command, CMD_SUCCESS, CMD_FAILURE -from ..exceptions import AlreadyExistsError, InvalidDateError, NotFoundError +from ..command import Command, CMD_SUCCESS +from ..exceptions import AlreadyExistsError, InvalidDateError, NotFoundError, WrappedValueError class Enroll(Command): @@ -96,7 +96,7 @@ def run(self, *args): code = self.enroll(uuid, organization, from_date, to_date, merge) except InvalidDateError as e: self.error(str(e)) - return CMD_FAILURE + return e.code return code @@ -128,20 +128,20 @@ def enroll(self, uuid, organization, from_date=None, to_date=None, merge=False): try: api.add_enrollment(self.db, uuid, organization, from_date, to_date) code = CMD_SUCCESS - except (NotFoundError, ValueError) as e: + except (NotFoundError, WrappedValueError) as e: self.error(str(e)) - code = CMD_FAILURE + code = e.code except AlreadyExistsError as e: if not merge: self.error(str(e)) - code = CMD_FAILURE + code = e.code if not merge: return code try: api.merge_enrollments(self.db, uuid, organization) - except (NotFoundError, ValueError) as e: + except (NotFoundError, WrappedValueError) as e: # These exceptions were checked above. If any of these raises # is due to something really wrong has happened raise RuntimeError(str(e)) diff --git a/tests/test_cmd_enroll.py b/tests/test_cmd_enroll.py index f5150753d..69b7980ac 100644 --- a/tests/test_cmd_enroll.py +++ b/tests/test_cmd_enroll.py @@ -32,9 +32,10 @@ sys.path.insert(0, '..') from sortinghat import api -from sortinghat.command import CMD_SUCCESS, CMD_FAILURE +from sortinghat.command import CMD_SUCCESS from sortinghat.cmd.enroll import Enroll from sortinghat.db.database import Database +from sortinghat.exceptions import CODE_INVALID_DATE_ERROR, CODE_VALUE_ERROR, CODE_NOT_FOUND_ERROR, CODE_ALREADY_EXISTS_ERROR from tests.config import DB_USER, DB_PASSWORD, DB_NAME, DB_HOST, DB_PORT @@ -175,25 +176,25 @@ def test_invalid_dates(self): code = self.cmd.run('--from', '1999-13-01', 'John Smith', 'Example') - self.assertEqual(code, CMD_FAILURE) + self.assertEqual(code, CODE_INVALID_DATE_ERROR) output = sys.stderr.getvalue().strip('\n').split('\n')[0] self.assertEqual(output, ENROLL_INVALID_DATE_ERROR) code = self.cmd.run('--from', 'YYZYY', 'John Smith', 'Example') - self.assertEqual(code, CMD_FAILURE) + self.assertEqual(code, CODE_INVALID_DATE_ERROR) output = sys.stderr.getvalue().strip('\n').split('\n')[1] self.assertEqual(output, ENROLL_INVALID_FORMAT_DATE_ERROR) code = self.cmd.run('--to', '1999-13-01', 'John Smith', 'Example') - self.assertEqual(code, CMD_FAILURE) + self.assertEqual(code, CODE_INVALID_DATE_ERROR) output = sys.stderr.getvalue().strip('\n').split('\n')[2] self.assertEqual(output, ENROLL_INVALID_DATE_ERROR) code = self.cmd.run('--to', 'YYZYY', 'John Smith', 'Example') - self.assertEqual(code, CMD_FAILURE) + self.assertEqual(code, CODE_INVALID_DATE_ERROR) output = sys.stderr.getvalue().strip('\n').split('\n')[3] self.assertEqual(output, ENROLL_INVALID_FORMAT_DATE_ERROR) @@ -300,7 +301,7 @@ def test_period_ranges(self): code = self.cmd.enroll('John Smith', 'Example', datetime.datetime(2001, 1, 1), datetime.datetime(1999, 1, 1)) - self.assertEqual(code, CMD_FAILURE) + self.assertEqual(code, CODE_VALUE_ERROR) output = sys.stderr.getvalue().strip() self.assertEqual(output, ENROLL_INVALID_PERIOD_ERROR) @@ -308,7 +309,7 @@ def test_non_existing_uuid(self): """Check if it fails adding enrollments to unique identities that do not exist""" code = self.cmd.enroll('Jane Roe', 'Example') - self.assertEqual(code, CMD_FAILURE) + self.assertEqual(code, CODE_NOT_FOUND_ERROR) output = sys.stderr.getvalue().strip() self.assertEqual(output, ENROLL_UUID_NOT_FOUND_ERROR) @@ -316,7 +317,7 @@ def test_non_existing_organization(self): """Check if it fails adding enrollments to organizations that do not exist""" code = self.cmd.enroll('John Smith', 'LibreSoft') - self.assertEqual(code, CMD_FAILURE) + self.assertEqual(code, CODE_NOT_FOUND_ERROR) output = sys.stderr.getvalue().strip() self.assertEqual(output, ENROLL_ORG_NOT_FOUND_ERROR) @@ -332,14 +333,14 @@ def test_existing_enrollment(self): code2 = self.cmd.enroll('John Smith', 'Example', datetime.datetime(1900, 1, 1), datetime.datetime(2100, 1, 1)) - self.assertEqual(code2, CMD_FAILURE) + self.assertEqual(code2, CODE_ALREADY_EXISTS_ERROR) output = sys.stderr.getvalue().strip().split('\n')[0] self.assertEqual(output, ENROLL_EXISTING_ERROR) # Test it without giving any period code = self.cmd.enroll('John Smith', 'Example') - self.assertEqual(code, CMD_FAILURE) + self.assertEqual(code, CODE_ALREADY_EXISTS_ERROR) output = sys.stderr.getvalue().strip().split('\n')[1] self.assertEqual(output, ENROLL_EXISTING_ERROR)