From 592d3eec4175613b1fc3a2b2017d2bef2f106754 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Otto?= Date: Wed, 12 Jun 2024 08:51:50 +0200 Subject: [PATCH] Add json output (fixes #20) --- README.md | 4 ++++ aas_test_engines/__main__.py | 39 ++++++++++++++++++++++++------------ aas_test_engines/result.py | 4 ++-- test/test_result.py | 2 +- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 39f4d50..2785c83 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,10 @@ python -m aas_test_engines check_server https://localhost --suite 'Asset Adminis # Generate test data python -m aas_test_engines generate_files output_dir + +# Alternative output formats +python -m aas_test_engines check_file test.aasx --output html > output.html +python -m aas_test_engines check_file test.aasx --output json > output.json ``` ## Check AAS Type 1 (File) diff --git a/aas_test_engines/__main__.py b/aas_test_engines/__main__.py index 2c11080..dbdb4e1 100644 --- a/aas_test_engines/__main__.py +++ b/aas_test_engines/__main__.py @@ -1,11 +1,12 @@ import argparse import sys import os +import json from aas_test_engines import api, file from enum import Enum -class Formats(Enum): +class InputFormats(Enum): xml = 'xml' json = 'json' aasx = 'aasx' @@ -14,40 +15,51 @@ def __str__(self): return self.value +class OutputFormats(Enum): + TEXT = 'text' + JSON = 'json' + HTML = 'html' + + def run_file_test(argv): parser = argparse.ArgumentParser(description='Checks a file for compliance with the AAS meta-model') parser.add_argument('file', type=argparse.FileType('rb'), help='the file to check') parser.add_argument('--format', - type=Formats, - default=Formats.aasx, - choices=list(Formats)) + type=InputFormats, + default=InputFormats.aasx, + choices=list(InputFormats)) parser.add_argument('--submodel_template', type=str, default=None, help="Additionally check for compliance to a submodel template") - parser.add_argument('--html', - type=argparse.FileType('w'), - default=None) + parser.add_argument('--output', + type=OutputFormats, + default=OutputFormats.TEXT, + choices=list(OutputFormats)) args = parser.parse_args(argv) if args.submodel_template is None: submodel_templates = set() else: submodel_templates = set([args.submodel_template]) - if args.format == Formats.aasx: + if args.format == InputFormats.aasx: result = file.check_aasx_file(args.file) - elif args.format == Formats.json: + elif args.format == InputFormats.json: result = file.check_json_file(args.file, submodel_templates=submodel_templates) - elif args.format == Formats.xml: + elif args.format == InputFormats.xml: result = file.check_xml_file(args.file) else: raise Exception(f"Invalid format {args.format}") - if args.html: - args.html.write(result.to_html()) - else: + if args.output == OutputFormats.TEXT: result.dump() + elif args.output == OutputFormats.HTML: + print(result.to_html()) + elif args.output == OutputFormats.JSON: + print(json.dumps(result.to_dict())) + else: + raise Exception(f"Invalid output {args.output}") def run_api_test(argv): @@ -97,6 +109,7 @@ def generate_files(argv): if i > 100: break + commands = { 'check_file': run_file_test, 'check_server': run_api_test, diff --git a/aas_test_engines/result.py b/aas_test_engines/result.py index ccad7fd..0fd3527 100644 --- a/aas_test_engines/result.py +++ b/aas_test_engines/result.py @@ -75,12 +75,12 @@ def to_html(self) -> str: content = f.read() return content.replace("", self._to_html()) - def to_json(self): + def to_dict(self): return { 'm': self.message, 'f': self.path_fragment, 'l': self.level.value, - 's': [i.to_json() for i in self.sub_results] + 's': [i.to_dict() for i in self.sub_results] } @classmethod diff --git a/test/test_result.py b/test/test_result.py index a255c83..c629658 100644 --- a/test/test_result.py +++ b/test/test_result.py @@ -34,7 +34,7 @@ def test_dump(self): self.result.dump() def test_to_json(self): - j = self.result.to_json() + j = self.result.to_dict() result = AasTestResult.from_json(j) self.assertEqual(len(self.result.sub_results), len(result.sub_results)) self.assertEqual(self.result.level, result.level)