From 8f28d9513a59522f21d17d2dbb6d771ffcb47e7e Mon Sep 17 00:00:00 2001 From: Silvano Cirujano Cuesta Date: Thu, 1 Aug 2024 13:16:28 +0200 Subject: [PATCH] test: add copy directive tests Signed-off-by: Silvano Cirujano Cuesta --- .../test_schema_mapper/test_schema_mapper.py | 199 ++++++++++++++++++ 1 file changed, 199 insertions(+) diff --git a/tests/test_schema_mapper/test_schema_mapper.py b/tests/test_schema_mapper/test_schema_mapper.py index adc8beb..ee2eac5 100644 --- a/tests/test_schema_mapper/test_schema_mapper.py +++ b/tests/test_schema_mapper/test_schema_mapper.py @@ -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 @@ -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()