-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We introduce additional functions which can de-serialize all model instances by determining the type based on the first start element and dispatching to the concrete de-serialization function accordingly. Based on [aas-core-codegen 3cacd0b3]. [aas-core-codegen 3cacd0b3]: aas-core-works/aas-core-codegen@3cacd0b3
- Loading branch information
Showing
3 changed files
with
354 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
"""Test general XML de-serialization of a type given by the start element.""" | ||
|
||
# pylint: disable=missing-docstring | ||
|
||
import io | ||
import unittest | ||
import xml.etree.ElementTree as ET | ||
|
||
|
||
import aas_core3.xmlization as aas_xmlization | ||
import aas_core3.verification as aas_verification | ||
|
||
import tests.common | ||
import tests.common_xmlization | ||
|
||
|
||
class TestGeneral(unittest.TestCase): | ||
def test_ok(self) -> None: | ||
paths = sorted((tests.common.TEST_DATA_DIR / "Xml").glob("*/Expected/**/*.xml")) | ||
|
||
for path in paths: | ||
text = path.read_text(encoding="utf-8") | ||
|
||
try: | ||
instance = aas_xmlization.from_file(path) | ||
except Exception as exception: # pylint: disable=broad-except | ||
raise AssertionError( | ||
f"Unexpected exception when de-serializing: {path}" | ||
) from exception | ||
|
||
errors = list(aas_verification.verify(instance)) | ||
|
||
if len(errors) > 0: | ||
errors_joined = "\n\n".join( | ||
f"{error.path}: {error.cause}" for error in errors | ||
) | ||
raise AssertionError( | ||
f"One or more unexpected errors from {path}:\n{errors_joined}" | ||
) | ||
|
||
writer = io.StringIO() | ||
aas_xmlization.write(instance, writer) | ||
|
||
# Check the round-trip | ||
original = ET.fromstring(text) | ||
tests.common_xmlization.remove_redundant_whitespace(original) | ||
|
||
serialized = ET.fromstring(aas_xmlization.to_str(instance)) | ||
tests.common_xmlization.remove_redundant_whitespace(serialized) | ||
|
||
tests.common_xmlization.assert_elements_equal( | ||
original, serialized, f"={path}" | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |