Exemplo n.º 1
0
    def test_valid_root_path(self):
        """Checks Absolute corpus path can be created with valid input."""
        corpus = CdmCorpusDefinition()
        # checks with None object
        absolute_path = corpus.storage.create_absolute_corpus_path('Abc/Def')
        self.assertEqual('/Abc/Def', absolute_path)
        absolute_path = corpus.storage.create_absolute_corpus_path('/Abc/Def')
        self.assertEqual('/Abc/Def', absolute_path)
        absolute_path = corpus.storage.create_absolute_corpus_path('cdm:/Abc/Def')
        self.assertEqual('cdm:/Abc/Def', absolute_path)
        manifest = CdmManifestDefinition(None, None)

        manifest.namespace = ''
        manifest.folder_path = 'Mnp/Qrs/'
        absolute_path = corpus.storage.create_absolute_corpus_path('Abc/Def', manifest)
        self.assertEqual('Mnp/Qrs/Abc/Def', absolute_path)

        manifest.namespace = 'cdm'
        manifest.folder_path = 'Mnp/Qrs/'
        absolute_path = corpus.storage.create_absolute_corpus_path('/Abc/Def', manifest)
        self.assertEqual('cdm:/Abc/Def', absolute_path)

        manifest.namespace = 'cdm'
        manifest.folder_path = 'Mnp/Qrs/'
        absolute_path = corpus.storage.create_absolute_corpus_path('Abc/Def', manifest)
        self.assertEqual('cdm:Mnp/Qrs/Abc/Def', absolute_path)
Exemplo n.º 2
0
    def test_path_that_does_not_end_in_slash(self):
        """FolderPath should always end with a /
        This checks the behavior if FolderPath does not end with a /
        ('/' should be appended and a warning be sent through callback function)"""

        corpus = CdmCorpusDefinition()
        function_was_called = False
        function_parameter1 = CdmStatusLevel.INFO
        function_parameter2 = None

        def callback(status_level: CdmStatusLevel, message1: str):
            nonlocal function_was_called, function_parameter1, function_parameter2
            function_was_called = True
            function_parameter1 = status_level
            function_parameter2 = message1

        corpus.set_event_callback(callback)
        manifest = CdmManifestDefinition(None, None)
        manifest.namespace = 'cdm'
        manifest.folder_path = 'Mnp'
        absolute_path = corpus.storage.create_absolute_corpus_path('Abc', manifest)
        self.assertEqual('cdm:Mnp/Abc', absolute_path)
        self.assertEqual(function_was_called, True)
        self.assertEqual(function_parameter1, CdmStatusLevel.WARNING)
        self.assertTrue(function_parameter2.find('Expected path prefix to end in /, but it didn\'t. Appended the /') != -1)
Exemplo n.º 3
0
    async def test_referenced_entity_declaration_resolution(self):
        corpus = CdmCorpusDefinition()
        corpus.ctx.report_at_level = CdmStatusLevel.WARNING
        corpus.storage.mount('cdm', LocalAdapter(root='../../schemaDocuments'))
        corpus.storage.default_namespace = 'cdm'

        manifest = CdmManifestDefinition(corpus.ctx, 'manifest')

        manifest.entities.append(
            'Account', 'cdm:/core/applicationCommon/foundationCommon/crmCommon/accelerators/healthCare/electronicMedicalRecords/Account.cdm.json/Account')

        referenced_entity_path = \
            'cdm:/core/applicationCommon/foundationCommon/crmCommon/accelerators/healthCare/electronicMedicalRecords/electronicMedicalRecords.manifest.cdm.json/Address'
        referenced_entity = CdmReferencedEntityDeclarationDefinition(corpus.ctx, 'Address')
        referenced_entity.entity_path = referenced_entity_path
        manifest.entities.append(referenced_entity)

        corpus.storage.fetch_root_folder('cdm').documents.append(manifest)

        resolved_manifest = await manifest.create_resolved_manifest_async('resolved_manifest', None)

        self.assertEqual(2, len(resolved_manifest.entities))
        self.assertEqual('core/applicationCommon/foundationCommon/crmCommon/accelerators/healthCare/electronicMedicalRecords/resolved/Account.cdm.json/Account',
                         resolved_manifest.entities[0].entity_path)
        self.assertEqual(referenced_entity_path, resolved_manifest.entities[1].entity_path)
Exemplo n.º 4
0
    def test_manifest_copy(self):
        """Tests if the copy function creates copies of the sub objects"""

        corpus = TestHelper.get_local_corpus('',
                                             'test_manifest_copy',
                                             no_input_and_output_folder=True)
        manifest = CdmManifestDefinition(corpus.ctx, 'name')

        entity_name = 'entity'
        sub_manifest_name = 'sub_manifest'
        relationship_name = 'relName'
        trait_name = 'traitName'

        entity_dec = manifest.entities.append(entity_name)
        sub_manifest = manifest.sub_manifests.append(sub_manifest_name)
        relationship = manifest.relationships.append(relationship_name)
        trait = manifest.exhibits_traits.append(trait_name)

        copy = manifest.copy()  # type: CdmManifestDefinition
        copy.entities[0].entity_name = 'newEntity'
        copy.sub_manifests[0].manifest_name = 'newSubManifest'
        copy.relationships[0].name = 'newRelName'
        copy.exhibits_traits[0].named_reference = 'newTraitName'

        self.assertEqual(entity_name, entity_dec.entity_name)
        self.assertEqual(sub_manifest_name, sub_manifest.manifest_name)
        self.assertEqual(relationship_name, relationship.name)
        self.assertEqual(trait_name, trait.named_reference)
Exemplo n.º 5
0
    async def test_imports_relative_path(self):
        # the corpus path in the imports are relative to the document where it was defined.
        # when saving in model.json the documents are flattened to the manifest level
        # so it is necessary to recalculate the path to be relative to the manifest.
        corpus = TestHelper.get_local_corpus('notImportant',
                                             'notImportantLocation')
        folder = corpus.storage.fetch_root_folder('local')

        manifest = CdmManifestDefinition(corpus.ctx, 'manifest')
        manifest.entities.append('EntityName',
                                 'EntityName/EntityName.cdm.json/EntityName')
        folder.documents.append(manifest)

        entity_folder = folder.child_folders.append('EntityName')

        document = CdmDocumentDefinition(corpus.ctx, 'EntityName.cdm.json')
        document.imports.append('subfolder/EntityName.cdm.json')
        document.definitions.append('EntityName')
        entity_folder.documents.append(document)

        sub_folder = entity_folder.child_folders.append('subfolder')
        sub_folder.documents.append('EntityName.cdm.json')

        data = await ManifestPersistence.to_data(manifest, None, None)

        self.assertEqual(1, len(data.entities))
        imports = data.entities[0].get('imports', [])
        self.assertEqual(1, len(imports))
        self.assertEqual('EntityName/subfolder/EntityName.cdm.json',
                         imports[0].corpusPath)
Exemplo n.º 6
0
    async def test_resolved_manifest_import(self):
        """Tests if the imports on the resolved manifest are relative to the resolved manifest location."""

        corpus = TestHelper.get_local_corpus(self.tests_subpath,
                                             'test_resolved_manifest_import')
        # Make sure that we are not picking up the default namespace while testing.
        corpus.storage.default_namespace = 'remote'

        document_name = 'localImport.cdm.json'
        local_folder = corpus.storage.fetch_root_folder('local')

        # Create a manifest that imports a document on the same folder.
        manifest = CdmManifestDefinition(corpus.ctx, 'default')
        manifest.imports.append(document_name)
        local_folder.documents.append(manifest)

        document = CdmDocumentDefinition(corpus.ctx, document_name)
        local_folder.documents.append(document)

        # Resolve the manifest into a different folder.
        resolved_manifest = await manifest.create_resolved_manifest_async(
            'output:/default.manifest.cdm.json', None)

        # Checks if the import path on the resolved manifest points to the original location.
        self.assertEqual(1, len(resolved_manifest.imports))
        self.assertEqual(f'local:/{document_name}',
                         resolved_manifest.imports[0].corpus_path)
Exemplo n.º 7
0
    def test_manifest_cannot_add_entity_definition_without_creating_document(self):
        cdm_corpus = CdmCorpusDefinition()
        cdm_corpus.storage.default_namespace = 'local'
        function_was_called = False
        function_parameter1 = CdmStatusLevel.INFO
        function_parameter2 = ''

        def callback(status_level: 'CdmStatusLevel', message: str):
            nonlocal function_was_called, function_parameter1, function_parameter2
            function_was_called = True
            function_parameter1 = status_level
            function_parameter2 = message

        cdm_corpus.set_event_callback(callback)

        cdm_corpus.storage.mount('local', LocalAdapter('C:\\Root\\Path'))

        manifest = CdmManifestDefinition(cdm_corpus.ctx, 'manifest')
        manifest._folder_path = '/'
        manifest._namespace = 'local'
        entity = CdmEntityDefinition(manifest.ctx, 'entityName', None)

        manifest.entities.append(entity)
        self.assertTrue(function_was_called)
        self.assertEqual(CdmStatusLevel.ERROR, function_parameter1)
        self.assertTrue('Expected entity to have an \'Owner\' document set. Cannot create entity declaration to add to manifest.' in function_parameter2)
Exemplo n.º 8
0
    def test_path_root_invalid_folder_path(self):
        """"Tests absolute paths cannot be created with wrong parameters.
        Checks behavior if FolderPath is invalid."""
        corpus = CdmCorpusDefinition()
        function_was_called = False
        function_parameter1 = CdmStatusLevel.INFO
        function_parameter2 = None

        def callback(status_level: CdmStatusLevel, message1: str):
            nonlocal function_was_called, function_parameter1, function_parameter2
            function_was_called = True
            function_parameter1 = status_level
            function_parameter2 = message1

        corpus.set_event_callback(callback)
        manifest = CdmManifestDefinition(None, None)

        manifest.namespace = 'cdm'
        manifest.folder_path = './Mnp'
        corpus.storage.create_absolute_corpus_path('Abc', manifest)
        self.assertTrue(function_was_called)
        self.assertEqual(CdmStatusLevel.ERROR, function_parameter1)
        self.assertTrue(function_parameter2.find('The path should not start with ./') != -1)

        function_was_called = False
        manifest.namespace = 'cdm'
        manifest.folder_path = '/./Mnp'
        corpus.storage.create_absolute_corpus_path('Abc', manifest)
        self.assertTrue(function_was_called)
        self.assertEqual(CdmStatusLevel.ERROR, function_parameter1)
        self.assertTrue(function_parameter2.find('The path should not contain /./') != -1)

        function_was_called = False
        manifest.namespace = 'cdm'
        manifest.folder_path = '../Mnp'
        corpus.storage.create_absolute_corpus_path('Abc', manifest)
        function_parameter2 = function_parameter2.split('|')[1].strip()
        self.assertTrue(function_was_called)
        self.assertEqual(CdmStatusLevel.ERROR, function_parameter1)
        self.assertTrue(function_parameter2.find('The path should not contain ../') != -1)

        function_was_called = False
        manifest.namespace = 'cdm'
        manifest.folder_path = 'Mnp/./Qrs'
        corpus.storage.create_absolute_corpus_path('Abc', manifest)
        function_parameter2 = function_parameter2.split('|')[1].strip()
        self.assertTrue(function_was_called)
        self.assertEqual(CdmStatusLevel.ERROR, function_parameter1)
        self.assertTrue(function_parameter2.find('The path should not contain /./') != -1)

        function_was_called = False
        manifest.namespace = 'cdm'
        manifest.folder_path = 'Mnp/../Qrs'
        corpus.storage.create_absolute_corpus_path('Abc', manifest)
        function_parameter2 = function_parameter2.split('|')[1].strip()
        self.assertTrue(function_was_called)
        self.assertEqual(CdmStatusLevel.ERROR, function_parameter1)
        self.assertTrue(function_parameter2.find('The path should not contain ../') != -1)
Exemplo n.º 9
0
    def test_path_root_invalid_folder_path(self):
        """"Tests absolute paths cannot be created with wrong parameters.
        Checks behavior if FolderPath is invalid."""
        expected_log_codes = {CdmLogCode.ERR_STORAGE_INVALID_PATH_FORMAT}
        corpus = TestHelper.get_local_corpus(
            self.tests_subpath,
            'test_path_root_invalid_folder_path',
            expected_codes=expected_log_codes,
            no_input_and_output_folder=True)

        manifest = CdmManifestDefinition(None, None)
        manifest._namespace = 'cdm'
        manifest._folder_path = './Mnp'
        corpus.storage.create_absolute_corpus_path('Abc', manifest)
        TestHelper.assert_cdm_log_code_equality(
            corpus, CdmLogCode.ERR_STORAGE_INVALID_PATH_FORMAT, True, self)

        manifest = CdmManifestDefinition(None, None)
        manifest._namespace = 'cdm'
        manifest._folder_path = '/./Mnp'
        corpus.storage.create_absolute_corpus_path('Abc', manifest)
        TestHelper.assert_cdm_log_code_equality(
            corpus, CdmLogCode.ERR_STORAGE_INVALID_PATH_FORMAT, True, self)

        manifest = CdmManifestDefinition(None, None)
        manifest._namespace = 'cdm'
        manifest._folder_path = '../Mnp'
        corpus.storage.create_absolute_corpus_path('Abc', manifest)
        TestHelper.assert_cdm_log_code_equality(
            corpus, CdmLogCode.ERR_STORAGE_INVALID_PATH_FORMAT, True, self)

        manifest = CdmManifestDefinition(None, None)
        manifest._namespace = 'cdm'
        manifest._folder_path = 'Mnp/./Qrs'
        corpus.storage.create_absolute_corpus_path('Abc', manifest)
        TestHelper.assert_cdm_log_code_equality(
            corpus, CdmLogCode.ERR_STORAGE_INVALID_PATH_FORMAT, True, self)

        manifest = CdmManifestDefinition(None, None)
        manifest._namespace = 'cdm'
        manifest._folder_path = 'Mnp/../Qrs'
        corpus.storage.create_absolute_corpus_path('Abc', manifest)
        TestHelper.assert_cdm_log_code_equality(
            corpus, CdmLogCode.ERR_STORAGE_INVALID_PATH_FORMAT, True, self)
Exemplo n.º 10
0
    async def test_to_incremental_partition_without_trait(self):
        """
        Testing saving manifest with local entity declaration having an incremental partition without incremental trait.
        """
        test_name = 'test_to_incremental_partition_without_trait'
        corpus = TestHelper.get_local_corpus(self.test_subpath, test_name)
        error_message_verified = False

        # not checking the CdmLogCode here as we want to check if this error message constructed correctly for the partition (it shares the same CdmLogCode with partition pattern)
        def callback(level, message):
            if 'Failed to persist object \'DeletePartition\'. This object does not contain the trait \'is.partition.incremental\', so it should not be in the collection \'incremental_partitions\'. | to_data' in message:
                nonlocal error_message_verified
                error_message_verified = True
            else:
                self.fail('Some unexpected failure - {}!'.format(message))

        corpus.set_event_callback(callback, CdmStatusLevel.WARNING)

        manifest = CdmManifestDefinition(corpus.ctx, 'manifest')
        corpus.storage.fetch_root_folder('local').documents.append(manifest)
        entity = CdmEntityDefinition(corpus.ctx, 'entityName', None)
        create_document_for_entity(corpus, entity)
        localized_entity_declaration = manifest.entities.append(entity)

        upsert_incremental_partition = corpus.make_object(
            CdmObjectType.DATA_PARTITION_DEF, 'UpsertPartition', False)
        upsert_incremental_partition.location = '/IncrementalData'
        upsert_incremental_partition.specialized_schema = 'csv'
        upsert_incremental_partition.exhibits_traits.append(
            Constants._INCREMENTAL_TRAIT_NAME,
            [['type', CdmIncrementalPartitionType.UPSERT.value]])

        delete_partition = corpus.make_object(CdmObjectType.DATA_PARTITION_DEF,
                                              'DeletePartition', False)
        delete_partition.location = '/IncrementalData'
        delete_partition.specialized_schema = 'csv'
        localized_entity_declaration.incremental_partitions.append(
            upsert_incremental_partition)
        localized_entity_declaration.incremental_partitions.append(
            delete_partition)

        with logger._enter_scope(DataPartitionTest.__name__, corpus.ctx,
                                 test_name):
            manifest_data = ManifestPersistence.to_data(manifest, None, None)

            self.assertEqual(1, len(manifest_data.entities))
            entity_data = manifest_data.entities[0]
            self.assertEqual(1, len(entity_data.incrementalPartitions))
            partition_data = entity_data.incrementalPartitions[0]
            self.assertEqual('UpsertPartition', partition_data.name)
            self.assertEqual(1, len(partition_data.exhibitsTraits))
            self.assertEqual(Constants._INCREMENTAL_TRAIT_NAME,
                             partition_data.exhibitsTraits[0].traitReference)

        self.assertTrue(error_message_verified)
Exemplo n.º 11
0
def generate_manifest() -> 'CdmManifestDefinition':
    """
        Creates a manifest used for the tests.
    """
    cdmCorpus = TestHelper.get_local_corpus(None,
                                            generate_manifest.__name__,
                                            no_input_and_output_folder=True)

    manifest = CdmManifestDefinition(cdmCorpus.ctx, 'manifest')
    manifest._folder_path = '/'
    manifest._namespace = 'local'

    return manifest
Exemplo n.º 12
0
def generate_manifest(local_root_path: str) -> 'CdmManifestDefinition':
    """
        Creates a manifest used for the tests.
    """
    cdmCorpus = CdmCorpusDefinition()
    cdmCorpus.storage.default_namespace = 'local'
    adapter = LocalAdapter(root=local_root_path)
    cdmCorpus.storage.mount('local', adapter)
    # add cdm namespace
    cdmCorpus.storage.mount('cdm', adapter)

    manifest = CdmManifestDefinition(cdmCorpus.ctx, 'manifest')
    manifest._folder_path = '/'
    manifest._namespace = 'local'

    return manifest
Exemplo n.º 13
0
    async def test_saving_invalid_model_json_name(self):
        corpus = CdmCorpusDefinition()
        corpus.ctx.report_at_level = CdmStatusLevel.WARNING
        corpus.storage.unmount('cdm')
        corpus.storage.default_namespace = 'local'
        manifest = CdmManifestDefinition(corpus.ctx, 'manifest')
        corpus.storage.fetch_root_folder('local').documents.append(manifest)

        all_docs = {}  # type: Dict[str, str]
        test_adapter = MockStorageAdapter(all_docs)
        corpus.storage._set_adapter('local', test_adapter)

        new_manifest_from_model_json_name = 'my.model.json'
        await manifest.save_as_async(new_manifest_from_model_json_name, True)
        # TODO: because we can load documents properly now, save_as_async returns false. Will check the value returned from save_as_async() when the problem is solved
        self.assertFalse('/' + new_manifest_from_model_json_name in all_docs)
Exemplo n.º 14
0
    def test_path_that_does_not_end_in_slash(self):
        """FolderPath should always end with a /
        This checks the behavior if FolderPath does not end with a /
        ('/' should be appended and a warning be sent through callback function)"""

        expected_log_codes = {CdmLogCode.WARN_STORAGE_EXPECTED_PATH_PREFIX}
        corpus = TestHelper.get_local_corpus(
            self.tests_subpath,
            'test_path_that_does_not_end_in_slash',
            expected_codes=expected_log_codes,
            no_input_and_output_folder=True)

        manifest = CdmManifestDefinition(None, None)
        manifest._namespace = 'cdm'
        manifest._folder_path = 'Mnp'
        absolute_path = corpus.storage.create_absolute_corpus_path(
            'Abc', manifest)
        self.assertEqual('cdm:Mnp/Abc', absolute_path)

        TestHelper.assert_cdm_log_code_equality(
            corpus, CdmLogCode.WARN_STORAGE_EXPECTED_PATH_PREFIX, True, self)