-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #770 from ulb-sachsen-anhalt/feat/rfct/pytest3
Feat/rfct/pytest3
- Loading branch information
Showing
4 changed files
with
152 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,111 @@ | ||
from contextlib import contextmanager | ||
from pathlib import Path | ||
from tests.base import TestCase, main # pylint: disable=import-error,no-name-in-module | ||
|
||
from pytest import fixture | ||
|
||
from ocrd_utils import pushd_popd, initLogging | ||
import ocrd_utils.constants | ||
|
||
@contextmanager | ||
def monkey_patch_temp_xdg(): | ||
with pushd_popd(tempdir=True) as tempdir: | ||
old_config = ocrd_utils.constants.XDG_CONFIG_HOME | ||
old_data = ocrd_utils.constants.XDG_DATA_HOME | ||
ocrd_utils.constants.XDG_CONFIG_HOME = tempdir | ||
ocrd_utils.constants.XDG_DATA_HOME = tempdir | ||
from ocrd.resource_manager import OcrdResourceManager | ||
yield tempdir, OcrdResourceManager() | ||
ocrd_utils.constants.XDG_CONFIG_HOME = old_config | ||
ocrd_utils.constants.XDG_DATA_HOME = old_data | ||
|
||
def test_config_created(): | ||
with monkey_patch_temp_xdg() as (tempdir, mgr): | ||
f = Path(tempdir, 'ocrd', 'resources.yml') | ||
assert f.exists() | ||
assert f == mgr.user_list | ||
ret = mgr.add_to_user_database('ocrd-foo', f) | ||
ret = mgr.add_to_user_database('ocrd-foo', f) | ||
assert ret | ||
mgr.list_installed() | ||
proc = 'ocrd-anybaseocr-layout-analysis' | ||
url = 'https://ocr-d-repo.scc.kit.edu/models/dfki/layoutAnalysis/mapping_densenet.pickle' | ||
fpath = mgr.download(proc, url, mgr.location_to_resource_dir('data')) | ||
assert fpath.exists() | ||
ret = mgr.add_to_user_database(proc, fpath) | ||
ret = mgr.add_to_user_database(proc, fpath) | ||
assert ret | ||
# -*- coding: utf-8 -*- | ||
|
||
import os | ||
import pathlib | ||
|
||
from ocrd.resource_manager import OcrdResourceManager | ||
|
||
from pytest import raises | ||
from tests.base import main | ||
|
||
CONST_RESOURCE_YML = 'resources.yml' | ||
CONST_RESOURCE_URL_LAYOUT = 'https://ocr-d-repo.scc.kit.edu/models/dfki/layoutAnalysis/mapping_densenet.pickle' | ||
|
||
|
||
def test_resources_manager_config_default(): | ||
|
||
# act | ||
mgr = OcrdResourceManager() | ||
|
||
# assert | ||
default_config_dir = os.path.join(os.environ['HOME'], '.config', 'ocrd') | ||
f = pathlib.Path(default_config_dir) / CONST_RESOURCE_YML | ||
assert f.exists() | ||
assert f == mgr.user_list | ||
assert mgr.add_to_user_database('ocrd-foo', f) | ||
mgr.list_installed() | ||
proc = 'ocrd-anybaseocr-layout-analysis' | ||
# TODO mock request | ||
fpath = mgr.download(proc, CONST_RESOURCE_URL_LAYOUT, mgr.location_to_resource_dir('data')) | ||
assert fpath.exists() | ||
assert mgr.add_to_user_database(proc, fpath) | ||
|
||
|
||
def test_resources_manager_from_environment(tmp_path, monkeypatch): | ||
|
||
# arrange | ||
monkeypatch.setenv('XDG_CONFIG_HOME', str(tmp_path)) | ||
monkeypatch.setenv('XDG_DATA_HOME', str(tmp_path)) | ||
monkeypatch.setenv('HOME', str(tmp_path)) | ||
|
||
# act | ||
mgr = OcrdResourceManager() | ||
|
||
# assert | ||
f = tmp_path / 'ocrd' / CONST_RESOURCE_YML | ||
assert f.exists() | ||
assert f == mgr.user_list | ||
assert mgr.add_to_user_database('ocrd-foo', f) | ||
mgr.list_installed() | ||
proc = 'ocrd-anybaseocr-layout-analysis' | ||
fpath = mgr.download(proc, CONST_RESOURCE_URL_LAYOUT, mgr.location_to_resource_dir('data')) | ||
assert fpath.exists() | ||
assert mgr.add_to_user_database(proc, fpath) | ||
assert mgr.userdir == str(tmp_path) | ||
|
||
|
||
def test_resources_manager_config_explicite(tmp_path): | ||
|
||
# act | ||
mgr = OcrdResourceManager(xdg_config_home=str(tmp_path)) | ||
|
||
# assert | ||
f = tmp_path / 'ocrd' / CONST_RESOURCE_YML | ||
assert f.exists() | ||
assert f == mgr.user_list | ||
assert mgr.add_to_user_database('ocrd-foo', f) | ||
mgr.list_installed() | ||
proc = 'ocrd-anybaseocr-layout-analysis' | ||
fpath = mgr.download(proc, CONST_RESOURCE_URL_LAYOUT, mgr.location_to_resource_dir('data')) | ||
assert fpath.exists() | ||
assert mgr.add_to_user_database(proc, fpath) | ||
|
||
def test_resources_manager_config_explicit_invalid(tmp_path): | ||
|
||
# act | ||
(tmp_path / 'ocrd').mkdir() | ||
(tmp_path / 'ocrd' / CONST_RESOURCE_YML).write_text('::INVALID::') | ||
|
||
# assert | ||
with raises(ValueError, match='is invalid'): | ||
OcrdResourceManager(xdg_config_home=tmp_path) | ||
|
||
def test_find_resources(tmp_path): | ||
|
||
# act | ||
f = tmp_path / 'ocrd-foo' / 'foo.bar' | ||
f.parent.mkdir() | ||
f.write_text('foobar') | ||
mgr = OcrdResourceManager(xdg_config_home=tmp_path) | ||
|
||
# assert | ||
assert mgr.find_resources(executable='ocrd-foo') == [] | ||
assert mgr.add_to_user_database('ocrd-foo', f, url='http://foo/bar') | ||
assert 'ocrd-foo' in [x for x, _ in mgr.find_resources()] | ||
assert 'ocrd-foo' in [x for x, _ in mgr.find_resources(url='http://foo/bar')] | ||
|
||
def test_parameter_usage(tmp_path): | ||
mgr = OcrdResourceManager(xdg_config_home=tmp_path) | ||
assert mgr.parameter_usage('foo.bar') == 'foo.bar' | ||
assert mgr.parameter_usage('foo.bar', 'without-extension') == 'foo' | ||
with raises(ValueError, match='No such usage'): | ||
mgr.parameter_usage('foo.bar', 'baz') | ||
|
||
def test_default_resource_dir(tmp_path): | ||
mgr = OcrdResourceManager(xdg_data_home=tmp_path) | ||
assert mgr.xdg_config_home != mgr.xdg_data_home | ||
assert mgr.default_resource_dir == str(mgr.xdg_data_home / 'ocrd-resources') | ||
|
||
|
||
if __name__ == "__main__": | ||
main(__file__) |