Skip to content

Commit

Permalink
Simplify tests for splits in negative test data (#30)
Browse files Browse the repository at this point in the history
We propagate the split of negative samples in
[aas-core3.0-testgen 08d3ec37c] into unserializable and invalid negative
examples. This makes the generation and future maintenance of the test
code easier in this SDK as well, since we now do not have to track the
individual negative cases.

[aas-core3.0-testgen 08d3ec37c]: aas-core-works/aas-core3.0-testgen@08d3ec37c
  • Loading branch information
mristin authored May 30, 2024
1 parent da19ec0 commit dc093c1
Show file tree
Hide file tree
Showing 7,683 changed files with 4,539 additions and 5,135 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def _generate_for_self_contained(
) -> Stripped:
"""Generate the test class for a self-contained class."""
# noinspection PyListCreation
cls_name_json_literal = aas_core_codegen.python.common.string_literal(
aas_core_codegen.naming.json_model_type(cls.name)
)
cls_name_json = aas_core_codegen.naming.json_model_type(cls.name)

cls_name_json_literal = aas_core_codegen.python.common.string_literal(cls_name_json)

deserialization_function = aas_core_codegen.python.naming.function_name(
aas_core_codegen.common.Identifier(f"{cls.name}_from_jsonable")
Expand Down Expand Up @@ -106,89 +106,83 @@ def test_that_deserialization_from_non_object_fails(self) -> None:
Stripped(
f"""\
def test_deserialization_failures(self) -> None:
{I}for cause in _CAUSES_FOR_DESERIALIZATION_FAILURE:
{II}base_dir = (
{III}tests.common.TEST_DATA_DIR
{III}/ "Json"
{III}/ "SelfContained"
{III}/ "Unexpected"
{III}/ cause
{III}/ {cls_name_json_literal}
{II})
{I}unserializable_dir = (
{II}tests.common.TEST_DATA_DIR
{II}/ "Json"
{II}/ "SelfContained"
{II}/ "Unexpected"
{II}/ "Unserializable"
{I})
{II}if not base_dir.exists():
{III}# There are no failure cases
{III}# for :py:class:`{aas_module}.types.{cls_name}`
{III}# and this ``cause``.
{III}continue
{I}# The first ``*`` corresponds to the cause.
{I}glob_pattern = "*/{cls_name_json}/**/*.json"
{II}for path in sorted(base_dir.glob("**/*.json")):
{III}with path.open("rt") as fid:
{IIII}jsonable = json.load(fid)
{I}paths = sorted(unserializable_dir.glob(glob_pattern))
{I}for path in paths:
{II}with path.open("rt") as fid:
{III}jsonable = json.load(fid)
{III}observed_exception: Optional[
{IIII}aas_jsonization.DeserializationException
{III}] = None
{II}observed_exception: Optional[
{III}aas_jsonization.DeserializationException
{II}] = None
{III}try:
{IIII}_ = aas_jsonization.{deserialization_function}(jsonable)
{III}except aas_jsonization.DeserializationException as exception:
{IIII}observed_exception = exception
{II}try:
{III}_ = aas_jsonization.{deserialization_function}(jsonable)
{II}except aas_jsonization.DeserializationException as exception:
{III}observed_exception = exception
{III}assert observed_exception is not None, (
{IIII}f"Expected an exception, but got none for: {{path}}"
{III})
{II}assert observed_exception is not None, (
{III}f"Expected an exception, but got none for: {{path}}"
{II})
{III}tests.common.record_or_check(
{IIII}path=path.parent / (path.name + ".exception"),
{IIII}got=f"{{observed_exception.path}}: {{observed_exception.cause}}"
{III})"""
{II}tests.common.record_or_check(
{III}path=path.parent / (path.name + ".exception"),
{III}got=f"{{observed_exception.path}}: {{observed_exception.cause}}"
{II})"""
),
Stripped(
f"""\
def test_verification_failures(self) -> None:
{I}for cause in tests.common.CAUSES_FOR_VERIFICATION_FAILURE:
{II}base_dir = (
{III}tests.common.TEST_DATA_DIR
{III}/ "Json"
{III}/ "SelfContained"
{III}/ "Unexpected"
{III}/ cause
{III}/ {cls_name_json_literal}
{I}invalid_dir = (
{II}tests.common.TEST_DATA_DIR
{II}/ "Json"
{II}/ "SelfContained"
{II}/ "Unexpected"
{II}/ "Invalid"
{I})
{I}# The first ``*`` corresponds to the cause.
{I}glob_pattern = "*/{cls_name_json}/**/*.json"
{I}paths = sorted(invalid_dir.glob(glob_pattern))
{I}for path in paths:
{II}with path.open("rt") as fid:
{III}jsonable = json.load(fid)
{II}try:
{III}instance = aas_jsonization.{deserialization_function}(jsonable)
{II}except aas_jsonization.DeserializationException as exception:
{III}raise AssertionError(
{IIII}f"Expected no deserialization exception from {{path}}"
{III}) from exception
{II}errors = list(aas_verification.verify(instance))
{II}self.assertGreater(
{III}len(errors),
{III}0,
{III}f"Expected verification errors from {{path}}, but got none"
{II})
{II}if not base_dir.exists():
{III}# There are no failure cases
{III}# for :py:class:`{aas_module}.types.{cls_name}`
{III}# and this ``cause``.
{III}continue
{II}for path in sorted(base_dir.glob("**/*.json")):
{III}with path.open("rt") as fid:
{IIII}jsonable = json.load(fid)
{III}try:
{IIII}instance = aas_jsonization.{deserialization_function}(jsonable)
{III}except aas_jsonization.DeserializationException as exception:
{IIII}raise AssertionError(
{IIIII}f"Expected no deserialization exception from {{path}}"
{IIII}) from exception
{III}errors = list(aas_verification.verify(instance))
{III}self.assertGreater(
{IIII}len(errors),
{IIII}0,
{IIII}f"Expected verification errors from {{path}}, but got none"
{II}tests.common.record_or_check(
{III}path=path.parent / (path.name + ".errors"),
{III}got="\\n".join(
{IIII}f"{{error.path}}: {{error.cause}}"
{IIII}for error in errors
{III})
{III}tests.common.record_or_check(
{IIII}path=path.parent / (path.name + ".errors"),
{IIII}got="\\n".join(
{IIIII}f"{{error.path}}: {{error.cause}}"
{IIIII}for error in errors
{IIII})
{III})"""
{II})"""
),
]

Expand Down Expand Up @@ -223,9 +217,9 @@ def _generate_for_contained_in_environment(
aas_module: aas_core_codegen.python.common.QualifiedModuleName,
) -> Stripped:
"""Generate the tests for a class contained in an ``Environment`` instance."""
cls_name_json_literal = aas_core_codegen.python.common.string_literal(
aas_core_codegen.naming.json_model_type(cls.name)
)
cls_name_json = aas_core_codegen.naming.json_model_type(cls.name)

cls_name_json_literal = aas_core_codegen.python.common.string_literal(cls_name_json)

deserialization_function = aas_core_codegen.python.naming.function_name(
aas_core_codegen.common.Identifier(f"{container_cls.name}_from_jsonable")
Expand Down Expand Up @@ -294,89 +288,83 @@ def test_that_deserialization_from_non_object_fails(self) -> None:
Stripped(
f"""\
def test_deserialization_failures(self) -> None:
{I}for cause in _CAUSES_FOR_DESERIALIZATION_FAILURE:
{II}base_dir = (
{III}tests.common.TEST_DATA_DIR
{III}/ "Json"
{III}/ "ContainedInEnvironment"
{III}/ "Unexpected"
{III}/ cause
{III}/ {cls_name_json_literal}
{II})
{I}unserializable_dir = (
{II}tests.common.TEST_DATA_DIR
{II}/ "Json"
{II}/ "ContainedInEnvironment"
{II}/ "Unexpected"
{II}/ "Unserializable"
{I})
{II}if not base_dir.exists():
{III}# There are no failure cases
{III}# for :py:class:`{aas_module}.types.{cls_name}`
{III}# and this ``cause``.
{III}continue
{I}# The first ``*`` corresponds to the cause.
{I}glob_pattern = "*/{cls_name_json}/**/*.json"
{II}for path in sorted(base_dir.glob("**/*.json")):
{III}with path.open("rt") as fid:
{IIII}jsonable = json.load(fid)
{I}paths = sorted(unserializable_dir.glob(glob_pattern))
{III}observed_exception: Optional[
{IIII}aas_jsonization.DeserializationException
{III}] = None
{I}for path in paths:
{II}with path.open("rt") as fid:
{III}jsonable = json.load(fid)
{III}try:
{IIII}_ = aas_jsonization.{deserialization_function}(jsonable)
{III}except aas_jsonization.DeserializationException as exception:
{IIII}observed_exception = exception
{II}observed_exception: Optional[
{III}aas_jsonization.DeserializationException
{II}] = None
{III}assert observed_exception is not None, (
{IIII}f"Expected an exception, but got none for: {{path}}"
{III})
{II}try:
{III}_ = aas_jsonization.{deserialization_function}(jsonable)
{II}except aas_jsonization.DeserializationException as exception:
{III}observed_exception = exception
{III}tests.common.record_or_check(
{IIII}path=path.parent / (path.name + ".exception"),
{IIII}got=f"{{observed_exception.path}}: {{observed_exception.cause}}"
{III})"""
{II}assert observed_exception is not None, (
{III}f"Expected an exception, but got none for: {{path}}"
{II})
{II}tests.common.record_or_check(
{III}path=path.parent / (path.name + ".exception"),
{III}got=f"{{observed_exception.path}}: {{observed_exception.cause}}"
{II})"""
),
Stripped(
f"""\
def test_verification_failures(self) -> None:
{I}for cause in tests.common.CAUSES_FOR_VERIFICATION_FAILURE:
{II}base_dir = (
{III}tests.common.TEST_DATA_DIR
{III}/ "Json"
{III}/ "ContainedInEnvironment"
{III}/ "Unexpected"
{III}/ cause
{III}/ {cls_name_json_literal}
{I}invalid_dir = (
{II}tests.common.TEST_DATA_DIR
{II}/ "Json"
{II}/ "ContainedInEnvironment"
{II}/ "Unexpected"
{II}/ "Invalid"
{I})
{I}# The first ``*`` corresponds to the cause.
{I}glob_pattern = "*/{cls_name_json}/**/*.json"
{I}paths = sorted(invalid_dir.glob(glob_pattern))
{I}for path in paths:
{II}with path.open("rt") as fid:
{III}jsonable = json.load(fid)
{II}try:
{III}container = aas_jsonization.{deserialization_function}(jsonable)
{II}except aas_jsonization.DeserializationException as exception:
{III}raise AssertionError(
{IIII}f"Expected no deserialization exception from {{path}}"
{III}) from exception
{II}errors = list(aas_verification.verify(container))
{II}self.assertGreater(
{III}len(errors),
{III}0,
{III}f"Expected verification errors from {{path}}, but got none"
{II})
{II}if not base_dir.exists():
{III}# There are no failure cases
{III}# for :py:class:`{aas_module}.types.{cls_name}`
{III}# and this ``cause``.
{III}continue
{II}for path in sorted(base_dir.glob("**/*.json")):
{III}with path.open("rt") as fid:
{IIII}jsonable = json.load(fid)
{III}try:
{IIII}container = aas_jsonization.{deserialization_function}(jsonable)
{III}except aas_jsonization.DeserializationException as exception:
{IIII}raise AssertionError(
{IIIII}f"Expected no deserialization exception from {{path}}"
{IIII}) from exception
{III}errors = list(aas_verification.verify(container))
{III}self.assertGreater(
{IIII}len(errors),
{IIII}0,
{IIII}f"Expected verification errors from {{path}}, but got none"
{II}tests.common.record_or_check(
{III}path=path.parent / (path.name + ".errors"),
{III}got="\\n".join(
{IIII}f"{{error.path}}: {{error.cause}}"
{IIII}for error in errors
{III})
{III}tests.common.record_or_check(
{IIII}path=path.parent / (path.name + ".errors"),
{IIII}got="\\n".join(
{IIIII}f"{{error.path}}: {{error.cause}}"
{IIIII}for error in errors
{IIII})
{III})"""
{II})"""
),
]

Expand Down Expand Up @@ -427,18 +415,6 @@ def main() -> int:
import tests.common
import tests.common_jsonization"""
),
Stripped(
f"""\
#: List of identifiers for serialization failures (corresponding to subdirectory names
#: in the directory with test data)
_CAUSES_FOR_DESERIALIZATION_FAILURE = [
{I}"TypeViolation",
{I}"RequiredViolation",
{I}"EnumViolation",
{I}"NullViolation",
{I}"UnexpectedAdditionalProperty",
]"""
),
] # type: List[Stripped]

environment_cls = symbol_table.must_find_concrete_class(
Expand Down
Loading

0 comments on commit dc093c1

Please sign in to comment.