Skip to content

Commit

Permalink
Merge branch 'fix-27-enroll' of 'git://github.com/andygrunwald/sortin…
Browse files Browse the repository at this point in the history
…ghat.git'

Fix chaoss#27
Closes chaoss#58
Merges chaoss#58
  • Loading branch information
sduenas committed Jan 4, 2016
2 parents 1bb92cb + 80ecdc2 commit 8479abe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
14 changes: 7 additions & 7 deletions sortinghat/cmd/enroll.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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))
Expand Down
21 changes: 11 additions & 10 deletions tests/test_cmd_enroll.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -300,23 +301,23 @@ 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)

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)

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)

Expand All @@ -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)

Expand Down

0 comments on commit 8479abe

Please sign in to comment.