Skip to content

Commit

Permalink
move project access initialization into a separate method and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
osma committed Jan 29, 2019
1 parent abf8ec7 commit c751725
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
15 changes: 9 additions & 6 deletions annif/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,20 @@ def __init__(self, project_id, config, datadir):
self.name = config['name']
self.language = config['language']
self.analyzer_spec = config.get('analyzer', None)
access = config.get('access', self.DEFAULT_ACCESS)
self.vocab_id = config.get('vocab', None)
self._base_datadir = datadir
self._datadir = os.path.join(datadir, 'projects', self.project_id)
self.config = config
self._init_access()

def _init_access(self):
access = self.config.get('access', self.DEFAULT_ACCESS)
try:
self.access = getattr(Access, access)
except AttributeError:
raise ConfigurationException(
"'%s' is not a valid access setting".format(self.access),
"'{}' is not a valid access setting".format(access),
project_id=self.project_id)
self.vocab_id = config.get('vocab', None)
self._base_datadir = datadir
self._datadir = os.path.join(datadir, 'projects', self.project_id)
self.config = config

def _get_datadir(self):
"""return the path of the directory where this project can store its
Expand Down
8 changes: 8 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
from annif.project import Access


def test_create_project_wrong_access(app):
with pytest.raises(ConfigurationException):
project = annif.project.AnnifProject(
'example',
{'name': 'Example', 'language': 'en', 'access': 'invalid'},
'.')


def test_get_project_en(app):
with app.app_context():
project = annif.project.get_project('dummy-en')
Expand Down

0 comments on commit c751725

Please sign in to comment.