Skip to content

Commit 7e51d3e

Browse files
committed
1. Flakification. 2. Updated builder.
1 parent 03aaca4 commit 7e51d3e

20 files changed

+188
-177
lines changed

pyessv/parsing/identifiers/builder.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
from pyessv import IDENTIFIER_TYPE_SET, IDENTIFIER_TYPE_FILENAME
44
from pyessv.parsing.identifiers.config import get_config
5+
from pyessv.parsing.identifiers.config import CollectionParsingSpecification
6+
from pyessv.parsing.identifiers.config import ConstantParsingSpecification
7+
from pyessv.parsing.identifiers.config import RegExParsingSpecification
58

69

710
def build_identifier(scope, identifier_type, terms, regex_terms={}):
@@ -46,34 +49,39 @@ def build_identifier(scope, identifier_type, terms, regex_terms={}):
4649
known_terms.extend(set([(name,) for name in regex_terms.keys()])) # hack to fake multiple name in regex_term
4750
known_terms = set.union(*known_terms)
4851
# print(set.union(*known_terms))
52+
4953
for idx, spec in enumerate(cfg.specs):
50-
if not spec.startswith("const") and template_part[idx] not in optional_template_part:
51-
if spec.startswith("regex"):
54+
if not isinstance(spec, ConstantParsingSpecification) and \
55+
template_part[idx] not in optional_template_part:
56+
if isinstance(spec, RegExParsingSpecification):
5257
if template_part[idx] not in known_terms:
5358
msg = f'Invalid known terms : missing {template_part[idx]} to build {identifier_type}'
5459
raise ValueError(msg)
55-
elif spec.split(":")[-1] not in known_terms:
56-
msg = f'Invalid known terms : missing {spec.split(":")[-1]} to build {identifier_type}'
60+
elif spec.namespace.split(":")[-1] not in known_terms:
61+
msg = f'Invalid known terms : missing {spec.namespace.split(":")[-1]} to build {identifier_type}'
5762
raise ValueError(msg)
5863

5964
# Building the identifier
6065
identifier_part = list()
6166
for idx, spec in enumerate(cfg.specs):
67+
# ... collection members.
68+
if isinstance(spec, CollectionParsingSpecification):
69+
for term in terms:
70+
if spec.namespace.replace(term.scope.namespace + ":", "") in term.collection.all_names:
71+
identifier_part.append(term.raw_name)
72+
break
73+
6274
# ... constants.
63-
if spec.startswith("const"):
64-
identifier_part.append(spec.split(":")[1])
75+
elif isinstance(spec, ConstantParsingSpecification):
76+
identifier_part.append(spec.value)
6577

6678
# ... regular expressions
67-
elif spec.startswith("regex"):
79+
elif isinstance(spec, RegExParsingSpecification):
6880
if template_part[idx] in regex_terms.keys():
6981
identifier_part.append(regex_terms[template_part[idx]])
7082

71-
# ... collection members.
7283
else:
73-
for term in terms:
74-
if spec.replace(term.scope.namespace + ":", "") in term.collection.all_names:
75-
identifier_part.append(term.raw_name)
76-
break
84+
raise ValueError("Unsupported specification type")
7785

7886
result = cfg.seperator.join(identifier_part)
7987

pyessv/parsing/identifiers/parser.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,16 @@ def parse_identifer(scope, identifier_type, identifier, strictness=PARSING_STRIC
4343
# For each identifier element, execute relevant vaidator.
4444
result = set()
4545
for idx, (element, spec) in enumerate(zip(elements, cfg.specs)):
46+
# ... vocabulary collection members.
47+
if isinstance(spec, CollectionParsingSpecification):
48+
match_result = match_term(load_collection(spec.namespace), element, strictness)
49+
if match_result is False:
50+
msg = 'Invalid identifier - failed vocab check. Element=#{}::({}). Identifier={}'
51+
raise ValueError(msg.format(idx + 1, element, identifier))
52+
result.add(match_result)
53+
4654
# ... constants.
47-
if isinstance(spec, ConstantParsingSpecification):
55+
elif isinstance(spec, ConstantParsingSpecification):
4856
if element != spec.value:
4957
msg = 'Invalid identifier - failed const check. Element=#{}::({}). Identifier={}'
5058
raise ValueError(msg.format(idx + 1, element, spec.value, identifier))
@@ -57,14 +65,6 @@ def parse_identifer(scope, identifier_type, identifier, strictness=PARSING_STRIC
5765
msg = 'Invalid identifier - failed regex check. Element=#{}::({}). Identifier={}'
5866
raise ValueError(msg.format(idx + 1, element, identifier))
5967

60-
# ... vocabulary collection members.
61-
elif isinstance(spec, CollectionParsingSpecification):
62-
match_result = match_term(load_collection(spec.namespace), element, strictness)
63-
if match_result is False:
64-
msg = 'Invalid identifier - failed vocab check. Element=#{}::({}). Identifier={}'
65-
raise ValueError(msg.format(idx + 1, element, identifier))
66-
result.add(match_result)
67-
6868
else:
6969
raise ValueError("Unsupported specification type")
7070

tests/_test_parser_template.py

Lines changed: 0 additions & 74 deletions
This file was deleted.

tests/fixtures/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,13 @@
44
from tests.fixtures.identifiers import directory_identifiers_invalid
55
from tests.fixtures.identifiers import filename_identifiers
66
from tests.fixtures.identifiers import filename_identifiers_invalid
7+
8+
9+
__all__ = [
10+
dataset_identifiers,
11+
dataset_identifiers_invalid,
12+
directory_identifiers,
13+
directory_identifiers_invalid,
14+
filename_identifiers,
15+
filename_identifiers_invalid
16+
]

tests/fixtures/identifiers.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,38 @@
1010
# Path to test assets folder.
1111
_ASSETS: pathlib.Path = pathlib.Path(os.path.dirname(__file__)).parent / "assets" / "identifiers"
1212

13+
1314
@pytest.fixture(scope="session")
1415
def dataset_identifiers() -> typing.Tuple[pyessv.Scope, str]:
15-
return list(_yield_valid_identifiers(pyessv.IDENTIFIER_TYPE_DATASET))
16+
return list(_yield_identifiers(pyessv.IDENTIFIER_TYPE_DATASET))
17+
1618

1719
@pytest.fixture(scope="session")
1820
def dataset_identifiers_invalid() -> typing.Tuple[pyessv.Scope, str]:
19-
return list(_yield_invalid_identifiers(pyessv.IDENTIFIER_TYPE_DATASET))
21+
return list(_yield_identifiers_invalid(pyessv.IDENTIFIER_TYPE_DATASET))
22+
2023

2124
@pytest.fixture(scope="session")
2225
def directory_identifiers() -> typing.Tuple[pyessv.Scope, str]:
23-
return list(_yield_valid_identifiers(pyessv.IDENTIFIER_TYPE_DIRECTORY))
26+
return list(_yield_identifiers(pyessv.IDENTIFIER_TYPE_DIRECTORY))
27+
2428

2529
@pytest.fixture(scope="session")
2630
def directory_identifiers_invalid() -> typing.Tuple[pyessv.Scope, str]:
27-
return list(_yield_invalid_identifiers(pyessv.IDENTIFIER_TYPE_DIRECTORY))
31+
return list(_yield_identifiers_invalid(pyessv.IDENTIFIER_TYPE_DIRECTORY))
32+
2833

2934
@pytest.fixture(scope="session")
3035
def filename_identifiers() -> typing.Tuple[pyessv.Scope, str]:
31-
return list(_yield_valid_identifiers(pyessv.IDENTIFIER_TYPE_FILENAME))
36+
return list(_yield_identifiers(pyessv.IDENTIFIER_TYPE_FILENAME))
37+
3238

3339
@pytest.fixture(scope="session")
3440
def filename_identifiers_invalid() -> typing.Tuple[pyessv.Scope, str]:
35-
return list(_yield_invalid_identifiers(pyessv.IDENTIFIER_TYPE_FILENAME))
41+
return list(_yield_identifiers_invalid(pyessv.IDENTIFIER_TYPE_FILENAME))
3642

37-
def _yield_valid_identifiers(identifier_type: str) -> typing.Iterator[typing.Tuple[pyessv.Scope, str]]:
43+
44+
def _yield_identifiers(identifier_type: str) -> typing.Iterator[typing.Tuple[pyessv.Scope, str]]:
3845
fpath: pathlib.Path = _ASSETS
3946
if not fpath.exists():
4047
return []
@@ -47,8 +54,9 @@ def _yield_valid_identifiers(identifier_type: str) -> typing.Iterator[typing.Tup
4754
for identifier in [i for i in fixture["identifiers"] if i]:
4855
yield scope, identifier
4956

50-
def _yield_invalid_identifiers(identifier_type: str):
51-
for scope, identifier in _yield_valid_identifiers(identifier_type):
57+
58+
def _yield_identifiers_invalid(identifier_type: str):
59+
for scope, identifier in _yield_identifiers(identifier_type):
5260
elements = identifier.split(".")
5361

5462
# Invalid: too few elements.

tests/test_cache.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,25 @@
1212

1313

1414
def test_interface():
15-
"""pyessv-tests: caching: interface.
15+
"""pyessv-tests: caching: interface.
1616
17-
"""
18-
assert inspect.isfunction(encache)
19-
assert inspect.isfunction(decache)
20-
assert inspect.isfunction(get_cached)
17+
"""
18+
assert inspect.isfunction(encache)
19+
assert inspect.isfunction(decache)
20+
assert inspect.isfunction(get_cached)
2121

2222

2323
def test_cache():
24-
"""pyessv-tests: caching: cache
24+
"""pyessv-tests: caching: cache
2525
26-
"""
27-
assert isinstance(get_cached(tu.AUTHORITY_NAME), pyessv.Authority)
26+
"""
27+
assert isinstance(get_cached(tu.AUTHORITY_NAME), pyessv.Authority)
2828

2929

3030
def test_decache():
31-
"""pyessv-tests: caching: decache
31+
"""pyessv-tests: caching: decache
3232
33-
"""
34-
decache(tu.AUTHORITY_NAME)
33+
"""
34+
decache(tu.AUTHORITY_NAME)
3535

36-
assert get_cached(tu.AUTHORITY_NAME) is None
36+
assert get_cached(tu.AUTHORITY_NAME) is None

tests/test_codecs.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import inspect
2-
31
import pytest
42

53
from pyessv.codecs import decode
@@ -36,7 +34,7 @@ def yield_parameterizations():
3634
tu.create_term_01,
3735
tu.create_term_02,
3836
tu.create_term_03
39-
):
37+
):
4038
for encoding in ENCODING_SET:
4139
yield node_factory(), encoding
4240

tests/test_factory.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
def _yield_parameterizations():
1616
"""Test parameterizations.
1717
18-
"""
18+
"""
1919
for factory, typeof in (
2020
(tu.create_authority, Authority),
2121
(tu.create_scope, Scope),
@@ -25,7 +25,7 @@ def _yield_parameterizations():
2525
(tu.create_term_01, Term),
2626
(tu.create_term_02, Term),
2727
(tu.create_term_03, Term)
28-
):
28+
):
2929
yield factory, typeof
3030

3131

tests/test_governance.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
def yield_parameterizations():
88
"""Test parameterizations.
99
10-
"""
10+
"""
1111
for func, status in (
1212
(pyessv.accept, pyessv.GOVERNANCE_STATUS_ACCEPTED),
1313
(pyessv.reject, pyessv.GOVERNANCE_STATUS_REJECTED),
1414
(pyessv.reset, pyessv.GOVERNANCE_STATUS_PENDING),
1515
(pyessv.deprecate, pyessv.GOVERNANCE_STATUS_DEPRECATED)
16-
):
16+
):
1717
yield func, status
1818

1919

tests/test_interface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
def yield_parameterizations():
9393
"""Yields test parameterizations.
9494
95-
"""
95+
"""
9696
for members, member_type in (
9797
(_CLASSES, 'class'),
9898
(_CONSTANTS, 'constant'),

0 commit comments

Comments
 (0)