From 6dcc9c404e96e2c0735e8a0acf0138217a07dc2f 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 | 133 ++++++++++++++++++ 1 file changed, 133 insertions(+) diff --git a/tests/test_schema_mapper/test_schema_mapper.py b/tests/test_schema_mapper/test_schema_mapper.py index adc8beb..77f0257 100644 --- a/tests/test_schema_mapper/test_schema_mapper.py +++ b/tests/test_schema_mapper/test_schema_mapper.py @@ -5,6 +5,7 @@ from linkml_map.datamodel.transformer_model import ( ClassDerivation, + CopyDirective, TransformationSpecification, ) from linkml_map.inference.schema_mapper import SchemaMapper @@ -74,6 +75,138 @@ 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(), "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(), "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(), "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) + ) + if __name__ == "__main__": unittest.main()