From 652f80f7849feaa1164bec834cd20669673133d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Otto?= Date: Thu, 2 May 2024 15:04:06 +0200 Subject: [PATCH] Sample generation for submodel templates --- aas_test_engines/file.py | 19 +++++++++++++------ test/test_file.py | 23 ++++++++++++++++++----- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/aas_test_engines/file.py b/aas_test_engines/file.py index 581024d..e15ac5c 100755 --- a/aas_test_engines/file.py +++ b/aas_test_engines/file.py @@ -19,10 +19,11 @@ class AasSchema: - def __init__(self, validator: SchemaValidator, graph: FlowGraph, submodel_templates: Dict[str, SchemaValidator]): + def __init__(self, validator: SchemaValidator, schema: JSON, submodel_templates: Dict[str, SchemaValidator], submodel_schemas: Dict[str, any]): self.validator = validator - self.graph = graph + self.schema = schema self.submodel_templates = submodel_templates + self.submodel_schemas = submodel_schemas def _find_schemas() -> Dict[str, AasSchema]: @@ -38,13 +39,14 @@ def _find_schemas() -> Dict[str, AasSchema]: format_validators=validators ) validator = parse_schema(schema, config) - graph = generate_graph(schema) submodel_templates = {} + submodel_schemas = {} for key, submodel_schema in schema['$defs']['SubmodelTemplates'].items(): submodel_schema['$defs'] = schema['$defs'] submodel_schema['$schema'] = 'https://json-schema.org/draft/2020-12/schema' submodel_templates[key] = parse_schema(submodel_schema, config) - result[i[:-4]] = AasSchema(validator, graph, submodel_templates) + submodel_schemas[key] = submodel_schema + result[i[:-4]] = AasSchema(validator, schema, submodel_templates, submodel_schemas) return result @@ -361,8 +363,13 @@ def check_aasx_file(file: TextIO, version: str = _DEFAULT_VERSION) -> AasTestRes return check_aasx_data(zip, version) -def generate(version: str = _DEFAULT_VERSION) -> Generator[str, None, None]: - graph = _get_schema(version, set()).graph +def generate(version: str = _DEFAULT_VERSION, submodel_template: Optional[str] = None) -> Generator[str, None, None]: + if submodel_template is None: + aas = _get_schema(version, set()) + graph = generate_graph(aas.schema) + else: + aas = _get_schema(version, set([submodel_template])) + graph = generate_graph(aas.submodel_schemas[submodel_template]) for i in graph.generate_paths(): sample = graph.execute(i.path) yield json.dumps(sample) diff --git a/test/test_file.py b/test/test_file.py index b622831..e2866b4 100644 --- a/test/test_file.py +++ b/test/test_file.py @@ -5,7 +5,7 @@ import json from xml.etree import ElementTree -from aas_test_engines import file +from aas_test_engines import file, exception script_dir = os.path.dirname(os.path.realpath(__file__)) @@ -132,13 +132,26 @@ def test_invoke(self): class GenerateTest(TestCase): - def test_a_few(self): + def check(self, generator): i = 0 - for sample in file.generate(): + for _ in generator: i += 1 - if i > 100: + if i > 10: break + def test_meta_model(self): + generator = file.generate() + self.check(generator) + + def test_generate_contact_info(self): + generator = file.generate(submodel_template='ContactInformation') + self.check(generator) + + def test_invalid_name(self): + with self.assertRaises(exception.AasTestToolsException): + generator = file.generate(submodel_template="xyz") + self.check(generator) + class CheckSubmodelTemplate(TestCase): @@ -153,7 +166,7 @@ def test_contact_info(self): result = file.check_json_data(data, submodel_templates=templates) result.dump() self.assertFalse(result.ok()) - + def test_no_submodels(self): data = {} # is compliant to meta-model...