Example #1
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)
    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)
Example #3
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)
Example #4
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
Example #5
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)
Example #6
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)
Example #7
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)
    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)
Example #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)