-
Notifications
You must be signed in to change notification settings - Fork 33
Feat/rfct/pytest3 #770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Feat/rfct/pytest3 #770
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
50c2305
[test][rfct] decoupling: res-manager vs constants
M3ssman 62e9f82
[test][rfct] drop useless imports
M3ssman 1a507b7
[app][rfct] turn xdg_data_home to property
M3ssman 477fc1b
resource_manager: make xdg_data_home and userdir properties
kba d7de707
resource manager: reload after add_to_user_database
kba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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__) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.