Skip to content

Commit fe9b728

Browse files
committed
Generate Python-conform JSON schema for tests
We generate a separate, Python-conform JSON schema to perform verification in the tests since the original JSON schema uses surrogate pairs to represent the characters above the Basic Multilingual Plain. This is related to [the following issue in jsonschema library]. [the following issue in jsonschema library]: python-jsonschema/jsonschema#1142
1 parent 9bc48f9 commit fe9b728

File tree

4 files changed

+137
-68
lines changed

4 files changed

+137
-68
lines changed

dev_scripts/update_to_aas_core_meta_and_codegen.py

+76-35
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
# exactly that meta-model version.
2626
import aas_core_meta.v3
2727

28+
import aas_core_codegen.jsonschema.main
29+
import aas_core_codegen.run
30+
import aas_core_codegen.specific_implementations
31+
import aas_core_codegen.common
32+
2833
AAS_CORE_META_DEPENDENCY_RE = re.compile(
2934
r"aas-core-meta@git\+https://github.com/aas-core-works/aas-core-meta@([a-fA-F0-9]+)#egg=aas-core-meta"
3035
)
@@ -35,7 +40,7 @@
3540

3641

3742
def _make_sure_no_changed_files(
38-
repo_dir: pathlib.Path, expected_branch: str
43+
repo_dir: pathlib.Path, expected_branch: str
3944
) -> Optional[int]:
4045
"""
4146
Make sure that no files are modified in the given repository.
@@ -63,8 +68,7 @@ def _make_sure_no_changed_files(
6368

6469

6570
def _update_setup_py(
66-
our_repo: pathlib.Path, aas_core_meta_revision: str,
67-
aas_core_codegen_revision: str
71+
our_repo: pathlib.Path, aas_core_meta_revision: str, aas_core_codegen_revision: str
6872
) -> None:
6973
"""Update the aas-core-meta in setup.py."""
7074
setup_py = our_repo / "setup.py"
@@ -88,7 +92,7 @@ def _update_setup_py(
8892

8993

9094
def _uninstall_and_install_aas_core_meta(
91-
our_repo: pathlib.Path, aas_core_meta_revision: str
95+
our_repo: pathlib.Path, aas_core_meta_revision: str
9296
) -> None:
9397
"""Uninstall and install the latest aas-core-meta in the virtual environment."""
9498
subprocess.check_call(
@@ -108,7 +112,7 @@ def _uninstall_and_install_aas_core_meta(
108112

109113

110114
def _uninstall_and_install_aas_core_codegen(
111-
our_repo: pathlib.Path, aas_core_codegen_revision: str
115+
our_repo: pathlib.Path, aas_core_codegen_revision: str
112116
) -> None:
113117
"""Uninstall and install the latest aas-core-codegen in the virtual environment."""
114118
subprocess.check_call(
@@ -127,15 +131,15 @@ def _uninstall_and_install_aas_core_codegen(
127131
)
128132

129133

130-
def _copy_python_sdk_and_schemas_from_aas_core_codegen(
131-
aas_core_codegen_repo: pathlib.Path,
132-
our_repo: pathlib.Path,
133-
aas_core_codegen_revision: str,
134+
def _copy_python_sdk_and_xml_schema_from_aas_core_codegen(
135+
aas_core_codegen_repo: pathlib.Path,
136+
our_repo: pathlib.Path,
137+
aas_core_codegen_revision: str,
134138
) -> None:
135139
"""Copy the generated Python SDK from aas-core-codegen's test data."""
136140
source_dir = (
137-
aas_core_codegen_repo
138-
/ "test_data/python/test_main/aas_core_meta.v3/expected_output"
141+
aas_core_codegen_repo
142+
/ "test_data/python/test_main/aas_core_meta.v3/expected_output"
139143
)
140144

141145
target_dir = our_repo / "aas_core3"
@@ -157,22 +161,63 @@ def _copy_python_sdk_and_schemas_from_aas_core_codegen(
157161
'''
158162
init_py.write_text(text, encoding="utf-8")
159163

160-
shutil.copy(
161-
aas_core_codegen_repo
162-
/ "test_data/jsonschema/test_main/aas_core_meta.v3/expected_output/schema.json",
163-
our_repo / "test_data/schema.json",
164-
)
165-
166164
shutil.copy(
167165
aas_core_codegen_repo
168166
/ "test_data/xsd/test_main/aas_core_meta.v3/expected_output/schema.xsd",
169167
our_repo / "test_data/schema.xsd",
170168
)
171169

172170

171+
def _generate_jsonschema_for_python(
172+
our_repo: pathlib.Path,
173+
) -> None:
174+
model_path = pathlib.Path(aas_core_meta.v3.__file__)
175+
symbol_table_atok, error = aas_core_codegen.run.load_model(model_path=model_path)
176+
assert error is None, f"Unexpected error loading {model_path}: {error}"
177+
assert symbol_table_atok is not None
178+
179+
symbol_table, _ = symbol_table_atok
180+
181+
text, errors = aas_core_codegen.jsonschema.main.generate(
182+
symbol_table=symbol_table,
183+
spec_impls={
184+
aas_core_codegen.specific_implementations.ImplementationKey(
185+
"schema_base.json"
186+
): aas_core_codegen.common.Stripped(
187+
"""\
188+
{
189+
"$schema": "https://json-schema.org/draft/2019-09/schema",
190+
"title": "AssetAdministrationShellEnvironment",
191+
"type": "object",
192+
"allOf": [
193+
{
194+
"$ref": "#/definitions/Environment"
195+
}
196+
]
197+
}"""
198+
)
199+
},
200+
# NOTE (mristin):
201+
# We rely that the aas-core-meta uses Python-conform regular expressions,
202+
# which jsonschema library can readily use as well.
203+
fix_pattern=lambda pattern: pattern,
204+
)
205+
206+
if errors is not None:
207+
errors_joined = "\n\n".join(error.message for error in errors)
208+
raise AssertionError(
209+
f"Unexpected errors when generating the Python-conform JSON schema:\n"
210+
f"{errors_joined}"
211+
)
212+
213+
assert text is not None
214+
215+
(our_repo / "test_data/schema.json").write_text(text, encoding="utf-8")
216+
217+
173218
def _run_in_parallel(
174-
calls: Sequence[Callable[[], subprocess.Popen[AnyStr]]],
175-
on_status_update: Callable[[int], None],
219+
calls: Sequence[Callable[[], subprocess.Popen[AnyStr]]],
220+
on_status_update: Callable[[int], None],
176221
) -> Optional[int]:
177222
"""
178223
Run the given scripts in parallel.
@@ -301,9 +346,9 @@ def _run_tests_in_parallel(our_repo: pathlib.Path) -> Optional[int]:
301346
f"tests.{pth.stem}"
302347
for pth in (our_repo / "tests").glob("test_*.py")
303348
if (
304-
pth.is_file()
305-
and not pth.name.startswith("__")
306-
and not pth.name.startswith(".")
349+
pth.is_file()
350+
and not pth.name.startswith("__")
351+
and not pth.name.startswith(".")
307352
)
308353
]
309354

@@ -367,23 +412,18 @@ def _generate_test_data(our_repo: pathlib.Path) -> Optional[int]:
367412
for name in ("generate_json.py", "generate_rdf.py", "generate_xml.py")
368413
]
369414

370-
# pylint: disable=consider-using-with
371-
commands = [
372-
[
415+
start = time.perf_counter()
416+
417+
for script in scripts:
418+
command = [
373419
sys.executable,
374420
str(script),
375421
"--model_path",
376422
aas_core_meta.v3.__file__,
377423
"--test_data_dir",
378-
test_data_dir,
424+
str(test_data_dir),
379425
]
380-
for script in scripts
381-
]
382-
# pylint: enable=consider-using-with
383426

384-
start = time.perf_counter()
385-
386-
for command in commands:
387427
command_escaped = " ".join(shlex.quote(part) for part in command)
388428
print(f"Running: {command_escaped}")
389429
subprocess.check_call(command, cwd=str(our_repo))
@@ -395,8 +435,7 @@ def _generate_test_data(our_repo: pathlib.Path) -> Optional[int]:
395435

396436

397437
def _create_branch_commit_and_push(
398-
our_repo: pathlib.Path, aas_core_meta_revision: str,
399-
aas_core_codegen_revision: str
438+
our_repo: pathlib.Path, aas_core_meta_revision: str, aas_core_codegen_revision: str
400439
) -> None:
401440
"""Create a feature branch, commit the changes and push it."""
402441
branch = (
@@ -593,7 +632,9 @@ def main() -> int:
593632
our_repo=our_repo, aas_core_codegen_revision=aas_core_codegen_revision
594633
)
595634

596-
_copy_python_sdk_and_schemas_from_aas_core_codegen(
635+
_generate_jsonschema_for_python(our_repo=our_repo)
636+
637+
_copy_python_sdk_and_xml_schema_from_aas_core_codegen(
597638
aas_core_codegen_repo=aas_core_codegen_repo,
598639
our_repo=our_repo,
599640
aas_core_codegen_revision=aas_core_codegen_revision,

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"icontract>=2.5.2,<3",
3838
"networkx==2.8",
3939
"typing-extensions==4.5.0",
40-
"aas-core-codegen@git+https://github.com/aas-core-works/aas-core-codegen@4433d092#egg=aas-core-codegen",
40+
"aas-core-codegen@git+https://github.com/aas-core-works/aas-core-codegen@c77b7dc6#egg=aas-core-codegen",
4141
],
4242
# fmt: off
4343
extras_require={

0 commit comments

Comments
 (0)