Skip to content

Commit

Permalink
Refactor main/run methods
Browse files Browse the repository at this point in the history
  • Loading branch information
danmichaelo committed Sep 28, 2017
1 parent 2bfe045 commit 519a78e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
23 changes: 13 additions & 10 deletions almar/almar.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,26 +342,23 @@ def get_config_filename():
return filename


def get_config(config=None):
if config is not None:
return config
def get_config():
filename = get_config_filename()
if filename is None:
log.error('Could not find "almar.yml" configuration file. See https://github.com/scriptotek/almar for help.')
sys.exit(1)

try:
return open(filename)
with open(filename) as fp:
config = yaml.load(fp)
except IOError:
log.error('Could not read configuration file "%s"', filename)
sys.exit(1)

return config

def main(config=None, args=None):
global raven_client

with get_config(config) as fp:
config = yaml.load(fp)
def run(config, argv):
global raven_client

username = getpass.getuser()
log.info('Running as %s', username)
Expand All @@ -372,7 +369,7 @@ def main(config=None, args=None):
'username': username
}})

args = parse_args(args or sys.argv[1:], config.get('default_env'))
args = parse_args(argv, config.get('default_env'))

if args.verbose:
# Do this as early as possible
Expand Down Expand Up @@ -404,6 +401,8 @@ def get_env(config, args):
sru = SruClient(env['sru_url'], args.env)
alma = Alma(env['api_region'], env['api_key'], args.env, dry_run=args.dry_run)

log.info('Starting job: %s', ' '.join(argv))

job = Job(sru=sru, ils=alma, **jargs)
job.dry_run = args.dry_run
job.interactive = not args.non_interactive
Expand All @@ -419,5 +418,9 @@ def get_env(config, args):
log.exception('Uncaught exception:')


def main():
run(get_config(), sys.argv[1:])


if __name__ == '__main__':
main()
25 changes: 15 additions & 10 deletions tests/test_almar.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import pytest
import responses
import logging
import yaml
from mock import Mock, MagicMock, patch, ANY, call
from io import BytesIO
from io import open
Expand All @@ -20,7 +21,7 @@
from textwrap import dedent

from almar.bib import Bib
from almar.almar import main, job_args, parse_args, log, get_concept
from almar.almar import run, get_config, job_args, parse_args, log, get_concept
from almar.authorities import Vocabulary
from almar.sru import SruClient, SruErrorResponse, TooManyResults, NSMAP
from almar.alma import Alma
Expand Down Expand Up @@ -823,6 +824,10 @@ def conf():
sru_url: https://sandbox-eu.alma.exlibrisgroup.com/view/sru/DUMMY_SITE
''').encode('utf-8'))

@classmethod
def conf_obj(cls):
return yaml.load(cls.conf())

@staticmethod
def sru_search_mock(*args, **kwargs):
recs = get_sample('sru_sample_response_1.xml', True).findall('srw:records/srw:record/srw:recordData/record', NSMAP)
Expand All @@ -837,7 +842,7 @@ def testMain(self, sru, MockAlma, mock_authorize_term):
new_term = 'Test æøå'
alma = MockAlma.return_value
mock_authorize_term.return_value = {'id': 'REAL030697'}
main(self.conf(), ['-e test_env', '-n', 'replace', term, new_term])
run(self.conf_obj(), ['-e test_env', '-n', 'replace', term, new_term])

sru.search.assert_called_once_with('alma.subjects = "%s" AND alma.authority_vocabulary = "%s"' % (term, 'noubomn'))

Expand All @@ -851,7 +856,7 @@ def testMainNoHits(self, sru, MockAlma, mock_authorize_term):
new_term = 'Test æøå'
mock_authorize_term.return_value = {'id': 'REAL030697'}
alma = MockAlma.return_value
main(self.conf(), ['-e test_env', '-n', 'replace', term, new_term])
run(self.conf_obj(), ['-e test_env', '-n', 'replace', term, new_term])
sru.search.assert_called_once_with('alma.subjects = "%s" AND alma.authority_vocabulary = "%s"' % (term, 'noubomn'))
assert alma.get_record.call_count == 0

Expand All @@ -862,7 +867,7 @@ def testRemoveTerm(self, sru, MockAlma, mock_authorize_term):
term = 'Statistiske modeller'
mock_authorize_term.return_value = {'id': 'REAL030697'}
alma = MockAlma.return_value
main(self.conf(), ['-e test_env', '-n', 'remove', term])
run(self.conf_obj(), ['-e test_env', '-n', 'remove', term])
sru.search.assert_called_once_with('alma.subjects = "%s" AND alma.authority_vocabulary = "%s"' % (term, 'noubomn'))
assert alma.get_record.call_count == 14

Expand All @@ -879,7 +884,7 @@ def testDiffs(self, sru, MockAlma, mock_authorize_term):
bib = Bib(doc)
alma.get_record.return_value = bib

main(self.conf(), ['--diffs', '-e test_env', '-n', 'replace', term, new_term])
run(self.conf_obj(), ['--diffs', '-e test_env', '-n', 'replace', term, new_term])
sru.search.assert_called_once_with('alma.subjects = "%s" AND alma.authority_vocabulary = "%s"' % (term, 'noubomn'))
assert alma.get_record.call_count == 1
assert alma.put_record.call_count == 1
Expand All @@ -896,7 +901,7 @@ def testListCommand(self, sru, MockAlma, mock_authorize_term):
bib = Bib(doc)
alma.get_record.return_value = bib

main(self.conf(), ['-e test_env', '-n', 'list', term])
run(self.conf_obj(), ['-e test_env', '-n', 'list', term])
sru.search.assert_called_once_with(
'alma.subjects = "%s" AND alma.authority_vocabulary = "%s"' % (term, 'noubomn'))
assert alma.get_record.call_count == 1
Expand All @@ -914,7 +919,7 @@ def testDryRun(self, sru, get_record, mock_authorize_term):
doc = get_sample('bib_990705558424702201.xml')
get_record.return_value = Bib(doc)

main(self.conf(), ['--dry_run', '-e test_env', '-n', 'replace', term, new_term])
run(self.conf_obj(), ['--dry_run', '-e test_env', '-n', 'replace', term, new_term])
sru.search.assert_called_once_with('alma.subjects = "%s" AND alma.authority_vocabulary = "%s"' % (term, 'noubomn'))
assert get_record.call_count == 1
assert len(responses.calls) == 0
Expand All @@ -932,7 +937,7 @@ def testCzRecord(self, sru, mock_authorize_term, get_record):
bib = Bib(doc)
get_record.return_value = bib

main(self.conf(), ['-e test_env', '-n', 'replace', term, new_term])
run(self.conf_obj(), ['-e test_env', '-n', 'replace', term, new_term])
sru.search.assert_called_once_with('alma.subjects = "%s" AND alma.authority_vocabulary = "%s"' % (term, 'noubomn'))

assert bib.cz_link is not None
Expand All @@ -946,7 +951,7 @@ def testConfigMissing(self, exists, mock_open):
mock_open.side_effect = IOError('File not found')

with pytest.raises(SystemExit):
main(args=['replace', 'old', 'new'])
get_config()

mock_open.assert_called_once_with('./almar.yml')

Expand Down Expand Up @@ -1111,4 +1116,4 @@ def test_advanced_syntax2(self):
assert jargs['target_concepts'][0].sf == {'a': 'Habsburg', 'c': 'slekten', '0': '90200245', '2': 'bare'}

if __name__ == '__main__':
unittest.main()
unittest.run()

0 comments on commit 519a78e

Please sign in to comment.