diff options
author | Matthias Baumgartner <dev@igsor.net> | 2022-12-15 17:21:20 +0100 |
---|---|---|
committer | Matthias Baumgartner <dev@igsor.net> | 2022-12-15 17:21:20 +0100 |
commit | 3b41b2a4b7532c911b63b41066a75b3e1546d214 (patch) | |
tree | a9e7b4ee1b0d8f38a2d9c1a74673b465d71e37c8 | |
parent | 37510d134458bf954ca2da6d40be0d6c76661e8c (diff) | |
download | bsie-3b41b2a4b7532c911b63b41066a75b3e1546d214.tar.gz bsie-3b41b2a4b7532c911b63b41066a75b3e1546d214.tar.bz2 bsie-3b41b2a4b7532c911b63b41066a75b3e1546d214.zip |
minor test improvements and information hiding in builder
-rw-r--r-- | bsie/tools/builder.py | 25 | ||||
-rw-r--r-- | test/apps/test_index.py | 2 | ||||
-rw-r--r-- | test/apps/test_info.py | 13 |
3 files changed, 20 insertions, 20 deletions
diff --git a/bsie/tools/builder.py b/bsie/tools/builder.py index 24aea84..190d9bf 100644 --- a/bsie/tools/builder.py +++ b/bsie/tools/builder.py @@ -12,7 +12,6 @@ import typing # bsie imports from bsie import base from bsie.base import errors -from bsie.utils.bsfs import URI, typename from bsie.utils import bsfs # inner-module imports @@ -75,20 +74,20 @@ class ReaderBuilder(): """ # keyword arguments - kwargs: typing.Dict[str, typing.Dict[str, typing.Any]] + _kwargs: typing.Dict[str, typing.Dict[str, typing.Any]] # cached readers - cache: typing.Dict[str, base.reader.Reader] + _cache: typing.Dict[str, base.Reader] def __init__(self, kwargs: typing.Dict[str, typing.Dict[str, typing.Any]]): - self.kwargs = kwargs - self.cache = {} + self._kwargs = kwargs + self._cache = {} def build(self, name: str) -> base.Reader: """Return an instance for the qualified class name.""" # return cached instance - if name in self.cache: - return self.cache[name] + if name in self._cache: + return self._cache[name] # check name and get module/class components module_name, class_name = _unpack_name(name) @@ -97,14 +96,14 @@ class ReaderBuilder(): cls = _safe_load(module_name, class_name) # get kwargs - kwargs = self.kwargs.get(name, {}) + kwargs = self._kwargs.get(name, {}) if not isinstance(kwargs, dict): raise TypeError(f'expected a kwargs dict, found {bsfs.typename(kwargs)}') try: # build, cache, and return instance obj = cls(**kwargs) # cache instance - self.cache[name] = obj + self._cache[name] = obj # return instance return obj @@ -125,19 +124,19 @@ class ExtractorBuilder(): """ # build specifications - specs: typing.List[typing.Dict[str, typing.Dict[str, typing.Any]]] + _specs: typing.List[typing.Dict[str, typing.Dict[str, typing.Any]]] def __init__(self, specs: typing.List[typing.Dict[str, typing.Dict[str, typing.Any]]]): - self.specs = specs + self._specs = specs def __iter__(self) -> typing.Iterator[int]: """Iterate over extractor specifications.""" - return iter(range(len(self.specs))) + return iter(range(len(self._specs))) def build(self, index: int) -> base.Extractor: """Return an instance of the n'th extractor (n=*index*).""" # get build instructions - specs = self.specs[index] + specs = self._specs[index] # check specs structure. expecting[{name: {kwargs}}] if not isinstance(specs, dict): diff --git a/test/apps/test_index.py b/test/apps/test_index.py index c567dea..2be8470 100644 --- a/test/apps/test_index.py +++ b/test/apps/test_index.py @@ -5,10 +5,10 @@ A copy of the license is provided with the project. Author: Matthias Baumgartner, 2022 """ # imports +import contextlib import io import os import rdflib -import sys import unittest # bsie imports diff --git a/test/apps/test_info.py b/test/apps/test_info.py index 60a540e..ad39c64 100644 --- a/test/apps/test_info.py +++ b/test/apps/test_info.py @@ -6,9 +6,9 @@ Author: Matthias Baumgartner, 2022 """ # imports import argparse +import contextlib import io import os -import sys import unittest # objects to test @@ -19,10 +19,10 @@ from bsie.apps.info import main class TestIndex(unittest.TestCase): def test_predicates(self): - stdout, sys.stdout = sys.stdout, io.StringIO() - # show predicates infos - main(['predicates']) - outbuf, sys.stdout = sys.stdout, stdout + outbuf = io.StringIO() + with contextlib.redirect_stdout(outbuf): + # show predicates infos + main(['predicates']) # verify output self.assertSetEqual({pred for pred in outbuf.getvalue().split('\n') if pred != ''}, { 'http://bsfs.ai/schema/Entity#author', @@ -32,7 +32,8 @@ class TestIndex(unittest.TestCase): }) def test_invalid(self): - self.assertRaises(SystemExit, main, ['foobar']) + with contextlib.redirect_stderr(io.StringIO()): + self.assertRaises(SystemExit, main, ['foobar']) ## main ## |