Skip to content

Commit

Permalink
Merge branch 'fix-30-load' of 'git://github.com/andygrunwald/sortingh…
Browse files Browse the repository at this point in the history
…at.git'

Fix #30
Closes #60
Merges #60
  • Loading branch information
sduenas committed Jan 4, 2016
2 parents 20ed55d + cd898cf commit ee6f4c4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
8 changes: 4 additions & 4 deletions sortinghat/cmd/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import sys

from .. import api
from ..command import Command, CMD_SUCCESS, CMD_FAILURE
from ..command import Command, CMD_SUCCESS
from ..db.model import MIN_PERIOD_DATE, MAX_PERIOD_DATE
from ..exceptions import AlreadyExistsError, NotFoundError,\
InvalidFormatError, LoadError, MatcherNotSupportedError
Expand Down Expand Up @@ -129,7 +129,7 @@ def run(self, *args):
parser = SortingHatParser(stream)
except InvalidFormatError as e:
self.error(str(e))
return CMD_FAILURE
return e.code
except (IOError, TypeError, AttributeError) as e:
raise RuntimeError(str(e))

Expand Down Expand Up @@ -234,7 +234,7 @@ def import_identities(self, parser, matching=None, match_new=False,
matcher = create_identity_matcher(matching, blacklist)
except MatcherNotSupportedError as e:
self.error(str(e))
return CMD_FAILURE
return e.code

uidentities = parser.identities

Expand All @@ -243,7 +243,7 @@ def import_identities(self, parser, matching=None, match_new=False,
verbose)
except LoadError as e:
self.error(str(e))
return CMD_FAILURE
return e.code

return CMD_SUCCESS

Expand Down
19 changes: 10 additions & 9 deletions tests/test_cmd_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@
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.load import Load
from sortinghat.db.database import Database
from sortinghat.db.model import Country
from sortinghat.parsing.sh import SortingHatParser
from sortinghat.exceptions import CODE_MATCHER_NOT_SUPPORTED_ERROR, CODE_INVALID_FORMAT_ERROR

from tests.config import DB_USER, DB_PASSWORD, DB_NAME, DB_HOST, DB_PORT

Expand Down Expand Up @@ -198,7 +199,7 @@ def test_load_identities_invalid_file(self):
"""Test whether it prints error messages while reading invalid files"""

code = self.cmd.run('--identities', 'data/sortinghat_invalid.json')
self.assertEqual(code, CMD_FAILURE)
self.assertEqual(code, CODE_INVALID_FORMAT_ERROR)
output = sys.stderr.getvalue().strip().split('\n')[0]
self.assertEqual(output, LOAD_IDENTITIES_INVALID_JSON_FORMAT_ERROR)

Expand Down Expand Up @@ -233,34 +234,34 @@ def test_invalid_format(self):
"""Check whether it prints an error when parsing invalid files"""

code = self.cmd.run('data/sortinghat_invalid.json')
self.assertEqual(code, CMD_FAILURE)
self.assertEqual(code, CODE_INVALID_FORMAT_ERROR)
output = sys.stderr.getvalue().strip().split('\n')[0]
self.assertEqual(output, LOAD_IDENTITIES_INVALID_JSON_FORMAT_ERROR)

code = self.cmd.run('data/sortinghat_ids_missing_keys.json')
self.assertEqual(code, CMD_FAILURE)
self.assertEqual(code, CODE_INVALID_FORMAT_ERROR)
output = sys.stderr.getvalue().strip().split('\n')[1]
self.assertEqual(output, LOAD_IDENTITIES_MISSING_KEYS_ERROR)

# Context added to catch deprecation warnings raised on Python 3
with warnings.catch_warnings(record=True):
code = self.cmd.run('data/sortinghat_orgs_invalid_json.json')
self.assertEqual(code, CMD_FAILURE)
self.assertEqual(code, CODE_INVALID_FORMAT_ERROR)
output = sys.stderr.getvalue().strip().split('\n')[2]
self.assertRegexpMatches(output, LOAD_ORGS_INVALID_FORMAT_ERROR)

code = self.cmd.run('data/sortinghat_orgs_missing_keys.json')
self.assertEqual(code, CMD_FAILURE)
self.assertEqual(code, CODE_INVALID_FORMAT_ERROR)
output = sys.stderr.getvalue().strip().split('\n')[3]
self.assertEqual(output, LOAD_ORGS_MISSING_KEYS_ERROR)

code = self.cmd.run('data/sortinghat_orgs_invalid_top.json')
self.assertEqual(code, CMD_FAILURE)
self.assertEqual(code, CODE_INVALID_FORMAT_ERROR)
output = sys.stderr.getvalue().strip().split('\n')[4]
self.assertEqual(output, LOAD_ORGS_IS_TOP_ERROR)

code = self.cmd.run('data/sortinghat_blacklist_empty_strings.json')
self.assertEqual(code, CMD_FAILURE)
self.assertEqual(code, CODE_INVALID_FORMAT_ERROR)
output = sys.stderr.getvalue().strip().split('\n')[5]
self.assertEqual(output, LOAD_BLACKLIST_EMPTY_STRINGS_ERROR)

Expand Down Expand Up @@ -711,7 +712,7 @@ def test_invalid_matching_method(self):
parser = self.get_parser('data/sortinghat_valid.json')

code = self.cmd.import_identities(parser, matching='mock')
self.assertEqual(code, CMD_FAILURE)
self.assertEqual(code, CODE_MATCHER_NOT_SUPPORTED_ERROR)

output = sys.stderr.getvalue().strip()
self.assertEqual(output, LOAD_IDENTITIES_MATCHING_ERROR)
Expand Down

0 comments on commit ee6f4c4

Please sign in to comment.