Пример #1
0
    async def test_load_imports(self):
        """Tests the fetch_object_async function with the imports load strategy set to load."""
        error_count = 0
        corpus = TestHelper.get_local_corpus(self.tests_subpath, 'test_imports_load_strategy')
        def callback(status_level: CdmStatusLevel, message: str):
            nonlocal error_count
            if message.index('Unable to resolve the reference') != -1:
                error_count += 1
            else:
                self.fail(message)
        corpus.set_event_callback(callback, CdmStatusLevel.ERROR)

        # load with strict validation.
        res_opt = ResolveOptions()
        res_opt.imports_load_strategy = ImportsLoadStrategy.LOAD
        await corpus.fetch_object_async('local:/doc.cdm.json', res_opt=res_opt)
        self.assertEqual(1, error_count)

        error_count = 0
        corpus = TestHelper.get_local_corpus(self.tests_subpath, 'test_imports_load_strategy')
        def callback1(status_level: CdmStatusLevel, message: str):
            nonlocal error_count
            if status_level == CdmStatusLevel.WARNING and message.index('Unable to resolve the reference') != -1:
                error_count += 1
            else:
                self.fail(message)
        corpus.set_event_callback(callback1, CdmStatusLevel.WARNING)

        # load with strict validation and shallow validation.
        res_opt = ResolveOptions()
        res_opt.imports_load_strategy = ImportsLoadStrategy.LOAD
        res_opt.shallow_validation = True
        await corpus.fetch_object_async('local:/doc.cdm.json', res_opt=res_opt)
        self.assertEqual(1, error_count)
Пример #2
0
    async def test_reading_is_primary_key(self):
        corpus = TestHelper.get_local_corpus(self.tests_subpath, 'test_reading_is_primary_key')

        res_opt = ResolveOptions()
        res_opt.imports_load_strategy = ImportsLoadStrategy.LOAD

        # read from an unresolved entity schema
        entity = await corpus.fetch_object_async('local:/TeamMembership.cdm.json/TeamMembership', res_opt=res_opt)
        attribute_group_ref = entity.attributes[0]  # type: CdmAttributeGroupReference
        attribute_group = attribute_group_ref.explicit_reference # type: CdmAttributeGroupDefinition
        type_attribute = attribute_group.members[0]  # type: CdmTypeAttributeDefinition

        self.assertTrue(type_attribute.is_primary_key)

        # check that the trait "is.identifiedBy" is created with the correct argument.
        is_identified_by1 = type_attribute.applied_traits[1]  # type: CdmTraitReference
        self.assertEqual('is.identifiedBy', is_identified_by1.named_reference)
        self.assertEqual('TeamMembership/(resolvedAttributes)/teamMembershipId', is_identified_by1.arguments[0].value)

        # read from a resolved entity schema
        resolved_entity = await corpus.fetch_object_async('local:/TeamMembership_Resolved.cdm.json/TeamMembership', res_opt=res_opt)
        resolved_type_attribute = resolved_entity.attributes[0]  # type: CdmTypeAttributeDefinition

        self.assertTrue(resolved_type_attribute.is_primary_key)

        # check that the trait "is.identifiedBy" is created with the correct argument.
        is_identified_by2 = resolved_type_attribute.applied_traits[6]  # type: CdmTraitReference
        self.assertEqual('is.identifiedBy', is_identified_by2.named_reference)

        argument_value = is_identified_by2.arguments[0].value  # type: CdmAttributeReference
        self.assertEqual('TeamMembership/(resolvedAttributes)/teamMembershipId', argument_value.named_reference)
Пример #3
0
    async def _fetch_document_definition(
            self, relative_path: str) -> CdmDocumentDefinition:
        """Helper that fixes a path from local to absolute.Gets the object from that path.
        Created from SaveDirtyLink in order to be able to save docs in parallel.
        Represents the part of SaveDirtyLink that could not be parallelized."""

        # get the document object from the import
        doc_path = self.ctx.corpus.storage.create_absolute_corpus_path(
            relative_path, self)
        if doc_path is None:
            logger.error(self.ctx, self._TAG,
                         self._fetch_document_definition.__name__,
                         self.at_corpus_path,
                         CdmLogCode.ERR_VALDN_INVALID_CORPUS_PATH,
                         relative_path)
            return None

        res_opt = ResolveOptions()
        res_opt.imports_load_strategy = ImportsLoadStrategy.LOAD
        obj_at = await self.ctx.corpus.fetch_object_async(doc_path,
                                                          None,
                                                          res_opt=res_opt)
        if obj_at is None:
            logger.error(self.ctx, self._TAG,
                         self._fetch_document_definition.__name__,
                         self.at_corpus_path,
                         CdmLogCode.ERR_PERSIST_OBJECT_NOT_FOUND, doc_path)
            return None
        document = cast('CdmDocumentDefinition', obj_at.in_document)
        return document
Пример #4
0
        async def seek_entities(f: 'CdmManifestDefinition'):
            if f.entities is not None:
                spew.spew_line(f.folder_path)

                for entity in f.entities:
                    ent = entity
                    current_file = f
                    while isinstance(ent, CdmReferencedEntityDeclarationDefinition):
                        corpus_path = corpus.storage.create_absolute_corpus_path(ent.entity_path, current_file)
                        ent = await corpus.fetch_object_async(corpus_path)
                        current_file = ent

                    corpus_path = corpus.storage.create_absolute_corpus_path(ent.entity_path, current_file)
                    res_opt = ResolveOptions()
                    res_opt.imports_load_strategy = ImportsLoadStrategy.LOAD
                    new_ent = await corpus.fetch_object_async(corpus_path, res_opt=res_opt)
                    res_opt.wrt_doc = new_ent.in_document
                    res_opt.directives = directives
                    res_ent = ResolvedEntity(res_opt, new_ent)

                    res_ent.spew(res_opt, spew, ' ', True)

            if f.sub_manifests:
                for sub_manifest in f.sub_manifests:
                    corpus_path = corpus.storage.create_absolute_corpus_path(sub_manifest.definition, f)
                    await seek_entities(await corpus.fetch_object_async(corpus_path))
Пример #5
0
    async def test_loading_already_present_imports_async(self):
        """Testing docs that load the same import"""
        test_name = 'test_loading_already_present_imports_async'
        corpus = TestHelper.get_local_corpus(self.tests_subpath, test_name)
        res_opt = ResolveOptions()
        res_opt.imports_load_strategy = ImportsLoadStrategy.LOAD

        # load the first doc
        main_doc = await corpus.fetch_object_async('mainEntity.cdm.json',
                                                   res_opt=res_opt)
        self.assertIsNotNone(main_doc)
        self.assertEqual(1, len(main_doc.imports))

        import_doc = main_doc.imports[0]._document
        self.assertIsNotNone(import_doc)

        # now load the second doc, which uses the same import
        # the import should not be loaded again, it should be the same object
        second_doc = await corpus.fetch_object_async('secondEntity.cdm.json',
                                                     res_opt=res_opt)
        self.assertIsNotNone(second_doc)
        self.assertEqual(1, len(second_doc.imports))

        second_import_doc = main_doc.imports[0]._document
        self.assertIsNotNone(second_import_doc)

        self.assertIs(import_doc, second_import_doc)
Пример #6
0
    async def test_loading_same_missing_imports_async(self):
        """Testing docs that load the same import"""
        test_name = 'test_loading_same_missing_imports_async'
        expected_log_codes = {
            CdmLogCode.ERR_PERSIST_FILE_READ_FAILURE,
            CdmLogCode.WARN_RESOLVE_IMPORT_FAILED,
            CdmLogCode.WARN_DOC_IMPORT_NOT_LOADED
        }
        corpus = TestHelper.get_local_corpus(self.tests_subpath,
                                             test_name,
                                             expected_codes=expected_log_codes)

        res_opt = ResolveOptions()
        res_opt.imports_load_strategy = ImportsLoadStrategy.LOAD

        main_doc = await corpus.fetch_object_async('mainEntity.cdm.json',
                                                   res_opt=res_opt)
        self.assertIsNotNone(main_doc)
        self.assertEqual(2, len(main_doc.imports))

        # make sure imports loaded correctly, despite them missing imports
        first_import = main_doc.imports[0]._document
        second_import = main_doc.imports[1]._document

        self.assertEqual(1, len(first_import.imports))
        self.assertIsNone(first_import.imports[0]._document)

        self.assertEqual(1, len(second_import.imports))
        self.assertIsNone(first_import.imports[0]._document)
Пример #7
0
    async def test_lazy_load_create_resolved_entity(self):
        corpus = TestHelper.get_local_corpus(self.tests_subpath, 'TestLazyLoadCreateResolvedEntity')
        def callback(status_level: CdmStatusLevel, message: str):
            # no error should be logged.
            self.fail(message)
        corpus.set_event_callback(callback, CdmStatusLevel.WARNING)

        # load with deferred imports.
        res_opt = ResolveOptions()
        res_opt.imports_load_strategy = ImportsLoadStrategy.LAZY_LOAD

        # load entB which is imported by entA document.
        doc_b = await corpus.fetch_object_async('local:/entB.cdm.json', None, res_opt=res_opt)  # type: CdmDocumentDefinition
        ent_a = await corpus.fetch_object_async('local:/entA.cdm.json/entA', None, res_opt=res_opt)  # type: CdmEntityDefinition

        self.assertIsNone(ent_a.in_document._import_priorities)
        self.assertIsNone(doc_b._import_priorities)

        # create_resolved_entity_async will force the entA document to be indexed.
        res_ent_a = await ent_a.create_resolved_entity_async('resolved-EntA')

        # in create_resolved_entity_async the documents should be indexed.
        self.assertIsNotNone(ent_a.in_document._import_priorities)
        self.assertIsNotNone(doc_b._import_priorities)
        self.assertIsNotNone(res_ent_a.in_document._import_priorities)
Пример #8
0
    async def test_lazy_load_imports(self):
        """Tests the fetch_object_async function with the lazy imports load."""
        corpus = TestHelper.get_local_corpus(self.tests_subpath, 'test_imports_load_strategy')  # type: CdmCorpusDefinition
        def callback(status_level: CdmStatusLevel, message: str):
            # when the imports are not loaded, there should be no reference validation.
            # no error should be logged.
            self.fail(message)
        corpus.set_event_callback(callback, CdmStatusLevel.WARNING)

        # load with deferred imports.
        res_opt = ResolveOptions()
        res_opt.imports_load_strategy = ImportsLoadStrategy.LAZY_LOAD
        await corpus.fetch_object_async('local:/doc.cdm.json', res_opt=res_opt)
Пример #9
0
    async def test_entity_with_missing_import(self):
        """The path between TestDataPath and TestName."""
        test_name = 'TestEntityWithMissingImport'
        corpus = TestHelper.get_local_corpus(self.tests_subpath, test_name)
        res_opt = ResolveOptions()
        res_opt.imports_load_strategy = ImportsLoadStrategy.LOAD

        doc = await corpus.fetch_object_async('local:/missingImport.cdm.json',
                                              res_opt=res_opt)
        self.assertIsNotNone(doc)
        self.assertEqual(1, len(doc.imports))
        self.assertEqual('missing.cdm.json', doc.imports[0].corpus_path)
        self.assertIsNone(doc.imports[0]._document)
Пример #10
0
    async def test_entity_with_same_imports_async(self):
        test_name = 'TestEntityWithSameImportsAsync'
        corpus = TestHelper.get_local_corpus(self.tests_subpath, test_name)
        res_opt = ResolveOptions()
        res_opt.imports_load_strategy = ImportsLoadStrategy.LOAD

        doc = await corpus.fetch_object_async(
            'local:/multipleImports.cdm.json', res_opt=res_opt)
        self.assertIsNotNone(doc)
        self.assertEqual(2, len(doc.imports))
        first_import = doc.imports[0]._document
        self.assertEqual('missingImport.cdm.json', first_import.name)
        self.assertEqual(1, len(first_import.imports))
        second_import = doc.imports[1]._document
        self.assertEqual('notMissing.cdm.json', second_import.name)
Пример #11
0
    async def test_entity_with_missing_import(self):
        """The path between TestDataPath and TestName."""
        test_name = 'test_entity_with_missing_import'
        expected_log_codes = {
            CdmLogCode.ERR_PERSIST_FILE_READ_FAILURE,
            CdmLogCode.WARN_RESOLVE_IMPORT_FAILED,
            CdmLogCode.WARN_DOC_IMPORT_NOT_LOADED
        }
        corpus = TestHelper.get_local_corpus(self.tests_subpath,
                                             test_name,
                                             expected_codes=expected_log_codes)

        res_opt = ResolveOptions()
        res_opt.imports_load_strategy = ImportsLoadStrategy.LOAD

        doc = await corpus.fetch_object_async('local:/missingImport.cdm.json',
                                              res_opt=res_opt)
        self.assertIsNotNone(doc)
        self.assertEqual(1, len(doc.imports))
        self.assertEqual('missing.cdm.json', doc.imports[0].corpus_path)
        self.assertIsNone(doc.imports[0]._document)
Пример #12
0
    async def test_loading_same_missing_imports_async(self):
        """Testing docs that load the same import"""
        test_name = 'TestLoadingSameMissingImportsAsync'
        corpus = TestHelper.get_local_corpus(self.tests_subpath, test_name)
        res_opt = ResolveOptions()
        res_opt.imports_load_strategy = ImportsLoadStrategy.LOAD

        main_doc = await corpus.fetch_object_async('mainEntity.cdm.json',
                                                   res_opt=res_opt)
        self.assertIsNotNone(main_doc)
        self.assertEqual(2, len(main_doc.imports))

        # make sure imports loaded correctly, despite them missing imports
        first_import = main_doc.imports[0]._document
        second_import = main_doc.imports[1]._document

        self.assertEqual(1, len(first_import.imports))
        self.assertIsNone(first_import.imports[0]._document)

        self.assertEqual(1, len(second_import.imports))
        self.assertIsNone(first_import.imports[0]._document)
Пример #13
0
    async def test_loading_same_imports_async(self):
        """Testing docs that load the same import"""
        test_name = 'test_loading_same_imports_async'
        corpus = TestHelper.get_local_corpus(self.tests_subpath, test_name)
        res_opt = ResolveOptions()
        res_opt.imports_load_strategy = ImportsLoadStrategy.LOAD

        main_doc = await corpus.fetch_object_async('mainEntity.cdm.json',
                                                   res_opt=res_opt)
        self.assertIsNotNone(main_doc)
        self.assertEqual(2, len(main_doc.imports))

        first_import = main_doc.imports[0]._document
        second_import = main_doc.imports[1]._document

        # since these two imports are loaded asynchronously, we need to make sure that
        # the import that they share (targetImport) was loaded, and that the
        # targetImport doc is attached to both of these import objects
        self.assertEqual(1, len(first_import.imports))
        self.assertIsNotNone(first_import.imports[0]._document)
        self.assertEqual(1, len(second_import.imports))
        self.assertIsNotNone(second_import.imports[0]._document)
Пример #14
0
    async def test_entity_with_same_imports_async(self):
        test_name = 'test_entity_with_same_imports_async'
        expected_log_codes = {
            CdmLogCode.ERR_PERSIST_FILE_READ_FAILURE,
            CdmLogCode.WARN_RESOLVE_IMPORT_FAILED,
            CdmLogCode.WARN_DOC_IMPORT_NOT_LOADED
        }
        corpus = TestHelper.get_local_corpus(self.tests_subpath,
                                             test_name,
                                             expected_codes=expected_log_codes)

        res_opt = ResolveOptions()
        res_opt.imports_load_strategy = ImportsLoadStrategy.LOAD

        doc = await corpus.fetch_object_async(
            'local:/multipleImports.cdm.json', res_opt=res_opt)
        self.assertIsNotNone(doc)
        self.assertEqual(2, len(doc.imports))
        first_import = doc.imports[0]._document
        self.assertEqual('missingImport.cdm.json', first_import.name)
        self.assertEqual(1, len(first_import.imports))
        second_import = doc.imports[1]._document
        self.assertEqual('notMissing.cdm.json', second_import.name)