Skip to content

Commit

Permalink
test: add copy directive tests
Browse files Browse the repository at this point in the history
Signed-off-by: Silvano Cirujano Cuesta <[email protected]>
  • Loading branch information
Silvanoc committed Aug 1, 2024
1 parent f338c7a commit 8f28d95
Showing 1 changed file with 199 additions and 0 deletions.
199 changes: 199 additions & 0 deletions tests/test_schema_mapper/test_schema_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from linkml_map.datamodel.transformer_model import (
ClassDerivation,
TransformationSpecification,
CopyDirective,
)
from linkml_map.inference.schema_mapper import SchemaMapper
from linkml_map.transformer.object_transformer import ObjectTransformer
Expand Down Expand Up @@ -74,6 +75,204 @@ def test_derive_partial(self):
target_schema = tr.derive_schema(specification)
print(yaml_dumper.dumps(target_schema))

def test_full_copy_specification(self):
"""tests copy isomorphism"""
tr = self.mapper
copy_all_directive = {"*": CopyDirective(element_name="*", copy_all=True)}
specification = TransformationSpecification(id="test", copy_directives=copy_all_directive)
source_schema = tr.source_schemaview.schema

target_schema = tr.derive_schema(specification)
# classes, slots and enums must be exactly the same
self.assertEqual(
yaml_dumper.dumps(source_schema.classes), yaml_dumper.dumps(target_schema.classes)
)
self.assertEqual(
yaml_dumper.dumps(source_schema.slots), yaml_dumper.dumps(target_schema.slots)
)
self.assertEqual(
yaml_dumper.dumps(source_schema.enums), yaml_dumper.dumps(target_schema.enums)
)

def test_partial_copy_specification(self):
"""tests copy isomorphism excluding derivations"""
tr = self.mapper
copy_all_directive = {"*": CopyDirective(element_name="*", copy_all=True)}
specification = TransformationSpecification(id="test", copy_directives=copy_all_directive)
source_schema = tr.source_schemaview.schema

derivations = [
ClassDerivation(name="Agent", populated_from="Person"),
]
for derivation in derivations:
specification.class_derivations[derivation.name] = derivation
target_schema = tr.derive_schema(specification)
# classes must be the same with addition
for schema_class in source_schema.classes.keys():
self.assertIn(
schema_class,
target_schema.classes.keys(),
f"Class '{schema_class}' is missing in target",
)
self.assertIn(
"Agent", target_schema.classes.keys(), f"Derived class 'Agent' is missing in target"
)
# slots and enums must be exactly the same
self.assertEqual(
yaml_dumper.dumps(source_schema.slots), yaml_dumper.dumps(target_schema.slots)
)
self.assertEqual(
yaml_dumper.dumps(source_schema.enums), yaml_dumper.dumps(target_schema.enums)
)

def test_full_copy_class(self):
"""tests copy isomorphism with class derivation"""
tr = self.mapper
copy_all_directive = {"*": CopyDirective(element_name="*", copy_all=True)}
specification = TransformationSpecification(id="test", copy_directives=copy_all_directive)
source_schema = tr.source_schemaview.schema

derivations = [
ClassDerivation(
name="Agent", populated_from="Person", copy_directives=copy_all_directive
),
]
for derivation in derivations:
specification.class_derivations[derivation.name] = derivation
target_schema = tr.derive_schema(specification)
# classes must be the same with addition
for schema_class in source_schema.classes.keys():
self.assertIn(
schema_class,
target_schema.classes.keys(),
f"Class '{schema_class}' is missing in target",
)
self.assertIn(
"Agent", target_schema.classes.keys(), f"Derived class 'Agent' is missing in target"
)
self.assertEqual(
yaml_dumper.dumps(source_schema.classes["Person"].slots),
yaml_dumper.dumps(target_schema.classes["Agent"].slots),
)
self.assertEqual(
yaml_dumper.dumps(source_schema.classes["Person"].attributes),
yaml_dumper.dumps(target_schema.classes["Agent"].attributes),
)
# slots and enums must be exactly the same
self.assertEqual(
yaml_dumper.dumps(source_schema.slots), yaml_dumper.dumps(target_schema.slots)
)
self.assertEqual(
yaml_dumper.dumps(source_schema.enums), yaml_dumper.dumps(target_schema.enums)
)

def test_copy_blacklisting(self):
"""tests copy on a blacklist approach"""
tr = self.mapper
blacklist = ["Person"]
copy_all_directive = {
"*": CopyDirective(element_name="*", copy_all=True, exclude=blacklist)
}
specification = TransformationSpecification(id="test", copy_directives=copy_all_directive)
source_schema = tr.source_schemaview.schema

derivations = [
ClassDerivation(name="Agent", populated_from="Person"),
]
for derivation in derivations:
specification.class_derivations[derivation.name] = derivation
target_schema = tr.derive_schema(specification)
# classes must be the same with addition
for schema_class in source_schema.classes.keys():
if schema_class in blacklist:
self.assertNotIn(
schema_class,
target_schema.classes.keys(),
f"Class '{schema_class}' is missing in target",
)
else:
self.assertIn(
schema_class,
target_schema.classes.keys(),
f"Class '{schema_class}' is missing in target",
)
self.assertIn(
"Agent", target_schema.classes.keys(), f"Derived class 'Agent' is missing in target"
)
# slots and enums must be exactly the same
self.assertEqual(
yaml_dumper.dumps(source_schema.slots), yaml_dumper.dumps(target_schema.slots)
)
self.assertEqual(
yaml_dumper.dumps(source_schema.enums), yaml_dumper.dumps(target_schema.enums)
)

# def test_copy_whitelisting(self):
# """tests copy on a whitelist approach"""
# tr = self.mapper
# whitelist = ["NamedThing"]
# copy_all_directive = { '*': CopyDirective(element_name='*', exclude_all=True, include=whitelist)}
# specification = TransformationSpecification(id="test", copy_directives=copy_all_directive)
# source_schema = tr.source_schemaview.schema
#
# derivations = [
# ClassDerivation(name="Agent", populated_from="Person"),
# ]
# for derivation in derivations:
# specification.class_derivations[derivation.name] = derivation
# target_schema = tr.derive_schema(specification)
# # classes must be the same with addition
# for schema_class in source_schema.classes.keys():
# if schema_class in whitelist:
# self.assertIn(schema_class, target_schema.classes.keys(), f"Class '{schema_class}' is missing in target")
# else:
# self.assertNotIn(schema_class, target_schema.classes.keys(), f"Class '{schema_class}' is missing in target")
# self.assertIn("Agent", target_schema.classes.keys(), f"Derived class 'Agent' is missing in target")
# # slots and enums must be exactly the same
# self.assertEqual(yaml_dumper.dumps(source_schema.slots), yaml_dumper.dumps(target_schema.slots))
# self.assertEqual(yaml_dumper.dumps(source_schema.enums), yaml_dumper.dumps(target_schema.enums))

def test_something(self):
"""tests copy isomorphism with class derivation"""
tr = self.mapper
copy_all_directive = {"*": CopyDirective(element_name="*", copy_all=True)}
specification = TransformationSpecification(id="test", copy_directives=copy_all_directive)
source_schema = tr.source_schemaview.schema

derivations = [
ClassDerivation(
name="Agent", populated_from="Person", copy_directives=copy_all_directive
),
]
for derivation in derivations:
specification.class_derivations[derivation.name] = derivation
target_schema = tr.derive_schema(specification)
# classes must be the same with addition
for schema_class in source_schema.classes.keys():
self.assertIn(
schema_class,
target_schema.classes.keys(),
f"Class '{schema_class}' is missing in target",
)
self.assertIn(
"Agent", target_schema.classes.keys(), f"Derived class 'Agent' is missing in target"
)
self.assertEqual(
yaml_dumper.dumps(source_schema.classes["Person"].slots),
yaml_dumper.dumps(target_schema.classes["Agent"].slots),
)
self.assertEqual(
yaml_dumper.dumps(source_schema.classes["Person"].attributes),
yaml_dumper.dumps(target_schema.classes["Agent"].attributes),
)
# slots and enums must be exactly the same
self.assertEqual(
yaml_dumper.dumps(source_schema.slots), yaml_dumper.dumps(target_schema.slots)
)
self.assertEqual(
yaml_dumper.dumps(source_schema.enums), yaml_dumper.dumps(target_schema.enums)
)


if __name__ == "__main__":
unittest.main()

0 comments on commit 8f28d95

Please sign in to comment.