Exemple #1
0
    async def save_actual_entity_and_validate_with_expected(
            self,
            expected_path: str,
            actual_resolved_entity_def: CdmEntityDefinition,
            update_expected_output: bool = False) -> None:
        """Runs validation to test actual output vs expected output for attributes collection vs attribute context."""
        co = CopyOptions()
        co._is_top_level_document = False
        await actual_resolved_entity_def.in_document.save_as_async(
            actual_resolved_entity_def.in_document.name, options=co)
        actual_path = actual_resolved_entity_def.ctx.corpus.storage.corpus_path_to_adapter_path(
            actual_resolved_entity_def.in_document.at_corpus_path)

        with open(actual_path, 'r', encoding='utf-8') as actual_file:
            actual_ctx = actual_file.read()

        if update_expected_output:
            with open(expected_path, 'w', encoding='utf-8') as expected_file:
                expected_file.write(actual_ctx)

        with open(expected_path, 'r', encoding='utf-8') as expected_file:
            expected_ctx = expected_file.read()

        self.assertEqual(expected_ctx, actual_ctx)
Exemple #2
0
    async def test_update_relationships(self):
        test_name = 'test_update_relationships'
        expected_rels = TestHelper.get_expected_output_data(
            self.tests_subpath, test_name, 'expectedRels.json')
        temp_from_file_path = 'fromEntTemp.cdm.json'
        temp_from_entity_path = 'local:/fromEntTemp.cdm.json/fromEnt'
        temp_to_entity_path = 'local:/toEnt.cdm.json/toEnt'

        corpus = TestHelper.get_local_corpus(self.tests_subpath, test_name)
        manifest = await corpus.fetch_object_async(
            'local:/main.manifest.cdm.json')  # type: CdmManifestDefinition
        manifest_no_to_ent = await corpus.fetch_object_async(
            'local:/mainNoToEnt.manifest.cdm.json'
        )  # type: CdmManifestDefinition
        from_ent = await corpus.fetch_object_async(
            'local:/fromEnt.cdm.json/fromEnt')  # type: CdmEntityDefinition
        co = CopyOptions()
        co._is_top_level_document = False
        await from_ent.in_document.save_as_async(temp_from_file_path,
                                                 options=co)

        async def reload_from_entity():
            await from_ent.in_document.save_as_async(temp_from_file_path,
                                                     options=co)
            # fetch again to reset the cache
            await corpus.fetch_object_async(temp_from_entity_path,
                                            None,
                                            shallow_validation=False,
                                            force_reload=True)

        try:
            # 1. test when entity attribute is removed
            await corpus.calculate_entity_graph_async(manifest)
            await manifest.populate_manifest_relationships_async()

            # check that the relationship has been created correctly
            self.verify_relationships(manifest, expected_rels)

            # now remove the entity attribute, which removes the relationship
            removed_attribute = from_ent.attributes[
                0]  # type: CdmAttributeItem
            from_ent.attributes.pop(0)
            await reload_from_entity()

            await corpus.calculate_entity_graph_async(manifest)
            await manifest.populate_manifest_relationships_async()

            # check that the relationship has been removed
            self.verify_relationships(manifest, [])

            # 2. test when the to entity is removed
            # restore the entity to the original state
            from_ent.attributes.append(removed_attribute)
            await reload_from_entity()

            await corpus.calculate_entity_graph_async(manifest)
            await manifest.populate_manifest_relationships_async()

            # check that the relationship has been created correctly
            self.verify_relationships(manifest, expected_rels)

            # remove the to entity
            from_ent.attributes.pop(0)
            await reload_from_entity()
            # fetch again to reset the cache
            await corpus.fetch_object_async(temp_to_entity_path, None, False,
                                            True)

            await corpus.calculate_entity_graph_async(manifest_no_to_ent)
            await manifest_no_to_ent.populate_manifest_relationships_async()

            # check that the relationship has been removed
            self.verify_relationships(manifest_no_to_ent, [])
        finally:
            # clean up the file created
            from_path = corpus.storage.corpus_path_to_adapter_path(
                'local:/' + temp_from_file_path)
            if os.path.exists(from_path):
                os.remove(from_path)