Skip to content

Commit

Permalink
Add json output (fixes #20)
Browse files Browse the repository at this point in the history
  • Loading branch information
otto-ifak committed Jun 12, 2024
1 parent 4154059 commit 592d3ee
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 26 additions & 13 deletions aas_test_engines/__main__.py
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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):
Expand Down Expand Up @@ -97,6 +109,7 @@ def generate_files(argv):
if i > 100:
break


commands = {
'check_file': run_file_test,
'check_server': run_api_test,
Expand Down
4 changes: 2 additions & 2 deletions aas_test_engines/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ def to_html(self) -> str:
content = f.read()
return content.replace("<!-- CONTENT -->", 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
Expand Down
2 changes: 1 addition & 1 deletion test/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 592d3ee

Please sign in to comment.