Exemplo n.º 1
0
    async def test_circular_import_with_moniker(self):
        """Test when A -> M/B -> C -> B.
        In this case, although A imports B with a moniker, B should be in the priorityimports because it is imported by C."""
        corpus = TestHelper.get_local_corpus('',
                                             '')  # type: CdmCorpusDefinition
        folder = corpus.storage.fetch_root_folder(
            'local')  # type: CdmFolderDefinition

        doc_a = CdmDocumentDefinition(
            corpus.ctx, 'A.cdm.json')  # type: CdmDocumentDefinition
        folder.documents.append(doc_a)
        doc_a.imports.append('B.cdm.json', "moniker")

        doc_b = CdmDocumentDefinition(
            corpus.ctx, 'B.cdm.json')  # type: CdmDocumentDefinition
        folder.documents.append(doc_b)
        doc_b.imports.append('C.cdm.json')

        doc_c = CdmDocumentDefinition(
            corpus.ctx, 'C.cdm.json')  # type: CdmDocumentDefinition
        folder.documents.append(doc_c)
        doc_c.imports.append('B.cdm.json')

        # forces doc_b to be indexed first.
        await doc_b._index_if_needed(ResolveOptions(), True)
        await doc_a._index_if_needed(ResolveOptions(), True)

        # should contain A, B and C.
        self.assertEqual(3, len(doc_a._import_priorities.import_priority))

        self.assertFalse(doc_a._import_priorities.has_circular_import)

        # doc_b and doc_c should have the hasCircularImport set to true.
        self.assertTrue(doc_b._import_priorities.has_circular_import)
        self.assertTrue(doc_c._import_priorities.has_circular_import)
Exemplo n.º 2
0
    async def test_monikered_import_is_not_added(self):
        """Test if monikered imports are not being added to the priority_list.
        A -> B/M -> C"""
        corpus = TestHelper.get_local_corpus('', '')
        folder = corpus.storage.fetch_root_folder(
            'local')  # type: CdmFolderDefinition

        doc_a = CdmDocumentDefinition(
            corpus.ctx, 'A.cdm.json')  # type: CdmDocumentDefinition
        folder.documents.append(doc_a)
        doc_a.imports.append('B.cdm.json', "moniker")

        doc_b = CdmDocumentDefinition(
            corpus.ctx, 'B.cdm.json')  # type: CdmDocumentDefinition
        folder.documents.append(doc_b)
        doc_b.imports.append('C.cdm.json')

        doc_c = CdmDocumentDefinition(
            corpus.ctx, 'C.cdm.json')  # type: CdmDocumentDefinition
        folder.documents.append(doc_c)

        # forces doc_b to be indexed first, so the priority_list will be read from the cache this time.
        await doc_b._index_if_needed(ResolveOptions(), True)
        await doc_a._index_if_needed(ResolveOptions(), True)

        # should only contain doc_a and doc_c, doc_b should be excluded.
        self.assertEqual(2, len(doc_a._import_priorities.import_priority))

        self.assertFalse(doc_a._import_priorities.has_circular_import)
        self.assertFalse(doc_b._import_priorities.has_circular_import)
        self.assertFalse(doc_c._import_priorities.has_circular_import)
    def test_document_collection_add_range(self):
        manifest = generate_manifest('C:\\Root\\Path')
        folder = CdmFolderDefinition(manifest.ctx, 'Folder')
        folder._corpus = manifest.ctx.corpus
        folder._folder_path = 'FolderPath/'
        folder._namespace = 'Namespace'

        self.assertEqual(0, len(folder.documents))

        document = CdmDocumentDefinition(manifest.ctx, 'DocumentName')
        document2 = CdmDocumentDefinition(manifest.ctx, 'DocumentName2')

        documentList = [document, document2]
        folder.documents.extend(documentList)
        self.assertEqual(2, len(folder.documents))
        self.assertEqual(document, folder.documents[0])
        self.assertEqual(document2, folder.documents[1])

        self.assertEqual('DocumentName', document.name)
        self.assertEqual('FolderPath/', document._folder_path)
        self.assertEqual(folder, document.owner)
        self.assertEqual('Namespace', document._namespace)
        self.assertTrue(document._needs_indexing)

        self.assertEqual('DocumentName2', document2.name)
        self.assertEqual('FolderPath/', document2._folder_path)
        self.assertEqual(folder, document2.owner)
        self.assertEqual('Namespace', document2._namespace)
        self.assertTrue(document2._needs_indexing)
Exemplo n.º 4
0
    async def test_reading_cached_import_priority(self):
        """Test when A -> B -> C/M -> D.
        Index doc_b first then doc_a. Make sure that C does not appear in doc_a priority list."""
        corpus = TestHelper.get_local_corpus('', '')
        folder = corpus.storage.fetch_root_folder('local')  # type: CdmFolderDefinition

        doc_a = CdmDocumentDefinition(corpus.ctx, 'A.cdm.json')  # type: CdmDocumentDefinition
        folder.documents.append(doc_a)
        doc_a.imports.append('B.cdm.json')

        doc_b = CdmDocumentDefinition(corpus.ctx, 'B.cdm.json')  # type: CdmDocumentDefinition
        folder.documents.append(doc_b)
        doc_b.imports.append('C.cdm.json', "moniker")

        doc_c = CdmDocumentDefinition(corpus.ctx, 'C.cdm.json')  # type: CdmDocumentDefinition
        folder.documents.append(doc_c)
        doc_c.imports.append('D.cdm.json')

        doc_d = CdmDocumentDefinition(corpus.ctx, 'D.cdm.json')  # type: CdmDocumentDefinition
        folder.documents.append(doc_d)

        # index doc_b first and check its import priorities.
        await doc_b._index_if_needed(ResolveOptions(), True)

        self.assertEqual(3, len(doc_b._import_priorities.import_priority))
        self._assertImportInfo(doc_b._import_priorities.import_priority[doc_b], 0, False)
        self._assertImportInfo(doc_b._import_priorities.import_priority[doc_d], 1, False)
        self._assertImportInfo(doc_b._import_priorities.import_priority[doc_c], 2, True)

        # now index doc_a, which should read doc_b's priority list from the cache.
        await doc_a._index_if_needed(ResolveOptions(), True)
        self.assertEqual(3, len(doc_a._import_priorities.import_priority))
        self._assertImportInfo(doc_a._import_priorities.import_priority[doc_a], 0, False)
        self._assertImportInfo(doc_a._import_priorities.import_priority[doc_b], 1, False)
        self._assertImportInfo(doc_a._import_priorities.import_priority[doc_d], 2, False)
Exemplo n.º 5
0
    async def test_monikered_import_is_added_to_end(self):
        """Test if monikered imports are added to the end of the priority list.
        A -> B/M -> C"""
        corpus = TestHelper.get_local_corpus('', '')
        folder = corpus.storage.fetch_root_folder('local')  # type: CdmFolderDefinition

        doc_a = CdmDocumentDefinition(corpus.ctx, 'A.cdm.json')  # type: CdmDocumentDefinition
        folder.documents.append(doc_a)
        doc_a.imports.append('B.cdm.json', "moniker")

        doc_b = CdmDocumentDefinition(corpus.ctx, 'B.cdm.json')  # type: CdmDocumentDefinition
        folder.documents.append(doc_b)
        doc_b.imports.append('C.cdm.json')

        doc_c = CdmDocumentDefinition(corpus.ctx, 'C.cdm.json')  # type: CdmDocumentDefinition
        folder.documents.append(doc_c)

        # forces doc_b to be indexed first, so the priority_list will be read from the cache this time.
        await doc_b._index_if_needed(ResolveOptions(), True)
        await doc_a._index_if_needed(ResolveOptions(), True)

        # should contain all three documents.
        self.assertEqual(3, len(doc_a._import_priorities.import_priority))
        self._assertImportInfo(doc_a._import_priorities.import_priority[doc_a], 0, False)
        self._assertImportInfo(doc_a._import_priorities.import_priority[doc_c], 1, False)
        # doc_b is monikered so it should appear at the end of the list.
        self._assertImportInfo(doc_a._import_priorities.import_priority[doc_b], 2, True)

        # make sure that the has circular import is set to False.
        self.assertFalse(doc_a._import_priorities.has_circular_import)
        self.assertFalse(doc_b._import_priorities.has_circular_import)
        self.assertFalse(doc_c._import_priorities.has_circular_import)
Exemplo n.º 6
0
    async def test_deeper_circular_import_with_moniker(self):
        """Test when A -> B -> C/M -> D -> C.
        In this case, although B imports C with a moniker, C should be in the A's priorityimports because it is imported by D."""
        corpus = TestHelper.get_local_corpus(
            self.tests_subpath, 'test_deeper_circular_import_with_moniker')
        folder = corpus.storage.fetch_root_folder(
            'local')  # type: CdmFolderDefinition

        doc_a = CdmDocumentDefinition(
            corpus.ctx, 'A.cdm.json')  # type: CdmDocumentDefinition
        folder.documents.append(doc_a)
        doc_a.imports.append('B.cdm.json')

        doc_b = CdmDocumentDefinition(
            corpus.ctx, 'B.cdm.json')  # type: CdmDocumentDefinition
        folder.documents.append(doc_b)
        doc_b.imports.append('C.cdm.json', 'moniker')

        doc_c = CdmDocumentDefinition(
            corpus.ctx, 'C.cdm.json')  # type: CdmDocumentDefinition
        folder.documents.append(doc_c)
        doc_c.imports.append('D.cdm.json')

        doc_d = CdmDocumentDefinition(
            corpus.ctx, 'D.cdm.json')  # type: CdmDocumentDefinition
        folder.documents.append(doc_d)
        doc_d.imports.append('C.cdm.json')

        # _index_if_needed will internally call prioritizeimports on every document.
        await doc_b._index_if_needed(ResolveOptions(), True)
        await doc_a._index_if_needed(ResolveOptions(), True)

        self.assertEqual(4, len(doc_a._import_priorities.import_priority))
        self._assertImportInfo(doc_a._import_priorities.import_priority[doc_a],
                               0, False)
        self._assertImportInfo(doc_a._import_priorities.import_priority[doc_b],
                               1, False)
        self._assertImportInfo(doc_a._import_priorities.import_priority[doc_d],
                               2, False)
        self._assertImportInfo(doc_a._import_priorities.import_priority[doc_c],
                               3, False)

        #  reset the _imports_priorities.
        self._mark_documents_to_index(folder.documents)

        # forces doc_c to be indexed first, so the priorityList will be read from the cache this time.
        await doc_c._index_if_needed(ResolveOptions(), True)
        await doc_a._index_if_needed(ResolveOptions(), True)

        self.assertEqual(4, len(doc_a._import_priorities.import_priority))

        # indexes the rest of the documents.
        await doc_b._index_if_needed(ResolveOptions(), True)
        await doc_d._index_if_needed(ResolveOptions(), True)

        self.assertFalse(doc_a._import_priorities.has_circular_import)
        self.assertFalse(doc_b._import_priorities.has_circular_import)
        self.assertTrue(doc_c._import_priorities.has_circular_import)
        self.assertTrue(doc_d._import_priorities.has_circular_import)
    def test_manifest_add_list_of_entity_declarations(self):
        cdm_corpus = CdmCorpusDefinition()
        cdm_corpus.storage.default_namespace = "local"
        cdm_corpus.storage.mount("local", LocalAdapter("CdmCorpus/LocalPath"))

        ctx = cdm_corpus.ctx

        cdmDocument = CdmDocumentDefinition(ctx, "NameOfDocument")
        collection = CdmEntityCollection(ctx, cdmDocument)

        entity_list = []

        for i in range(0, 2):
            entity = CdmEntityDefinition(cdm_corpus.ctx,
                                         'entityName_{}'.format(i), None)
            create_document_for_entity(cdm_corpus, entity)
            entity_list.append(entity)

        self.assertEqual(0, len(collection))

        collection.extend(entity_list)

        self.assertEqual(2, len(collection))

        for i in range(0, 2):
            self.assertEqual('entityName_{}'.format(i),
                             collection[i].entity_name)
    def test_document_collection_insert(self):
        manifest = generate_manifest('C:\\Root\\Path')
        folder = CdmFolderDefinition(manifest.ctx, 'Folder')
        folder.in_document = manifest
        folder._corpus = manifest.ctx.corpus
        folder._folder_path = 'FolderPath/'
        folder._namespace = 'Namespace'
        document = CdmDocumentDefinition(manifest.ctx, 'DocumentName')

        doc1 = folder.documents.append('doc1')
        doc2 = folder.documents.append('doc2')

        manifest._is_dirty = False

        folder.documents.insert(2, document)
        self.assertTrue(manifest._is_dirty)
        self.assertEqual(3, len(folder.documents))
        self.assertEqual(doc1, folder.documents[0])
        self.assertEqual(doc2, folder.documents[1])
        self.assertEqual(document, folder.documents[2])

        self.assertEqual('FolderPath/', document._folder_path)
        self.assertEqual(folder, document.owner)
        self.assertEqual('Namespace', document._namespace)
        self.assertTrue(document._needs_indexing)
        self.assertEqual(folder, document.owner)
        self.assertTrue(document.name in folder._document_lookup)
        self.assertTrue(manifest.ctx.corpus._document_library._contains((folder, document)))

        folder.documents.insert(2, document)
        self.assertEqual(3, len(folder.documents))
Exemplo n.º 9
0
    def test_cdm_collection_appending_list(self):
        cdm_corpus = CdmCorpusDefinition()
        cdm_corpus.storage.default_namespace = 'local'
        cdm_corpus.storage.mount('local', LocalAdapter('CdmCorpus/LocalPath'))

        ctx = cdm_corpus.ctx

        cdm_document = CdmDocumentDefinition(ctx, 'NameOfDocument')
        collection = CdmCollection(ctx, cdm_document,
                                   CdmObjectType.LOCAL_ENTITY_DECLARATION_DEF)

        entity_list = []

        for i in range(0, 2):
            entity = CdmEntityDefinition(cdm_corpus.ctx,
                                         'entityName_{}'.format(i), None)
            create_document_for_entity(cdm_corpus, entity)

            entity_declaration = cdm_corpus.make_object(
                CdmObjectType.LOCAL_ENTITY_DECLARATION_DEF, entity.entity_name,
                False)  # type: CdmLocalEntityDeclarationDefinition
            entity_declaration.entity_path = '{}/{}'.format(
                entity.owner.at_corpus_path, entity.entity_name)
            entity_declaration.owner = entity.owner
            entity_list.append(entity_declaration)

        self.assertEqual(0, len(collection))

        collection.extend(entity_list)

        self.assertEqual(2, len(collection))

        for i in range(0, 2):
            self.assertEqual('entityName_{}'.format(i),
                             collection[i].entity_name)
Exemplo n.º 10
0
    def test_cdm_collection_append_method(self):
        cdm_corpus = CdmCorpusDefinition()
        cdm_corpus.storage.default_namespace = 'local'
        cdm_corpus.storage.mount('local', LocalAdapter('CdmCorpus/LocalPath'))

        ctx = cdm_corpus.ctx

        cdm_document = CdmDocumentDefinition(ctx, 'NameOfDocument')
        collection = CdmCollection(ctx, cdm_document,
                                   CdmObjectType.ATTRIBUTE_CONTEXT_DEF)

        appended_attribute_context = collection.append('nameOfNewAttribute')
        self.assertEqual(1, len(collection))
        self.assertEqual('nameOfNewAttribute', collection[0].name)
        self.assertEqual(cdm_document, collection[0].owner)
        self.assertEqual(ctx, collection[0].ctx)

        self.assertEqual(collection[0], appended_attribute_context)

        attribute_context = CdmAttributeContext(ctx, 'NameOfAttributeContext')
        appended_attribute = collection.append(attribute_context)
        self.assertEqual(2, len(collection))
        self.assertEqual(attribute_context, appended_attribute)
        self.assertEqual(attribute_context, collection[1])
        self.assertEqual(cdm_document, attribute_context.owner)
Exemplo n.º 11
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.º 12
0
    def create_and_initialize_all_imports_file(self) -> 'CdmDocumentDefinition':
        all_importsDoc = CdmDocumentDefinition(self._corpus.ctx, self._all_imports_name)  # type: CdmDocumentDefinition

        all_imports_doc_def = self._local_storage_root.documents.append(all_importsDoc, self._all_imports_doc_name)  # type: CdmDocumentDefinition
        all_imports_doc_def.imports.append(self._foundation_json_path)

        return all_imports_doc_def
Exemplo n.º 13
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.º 14
0
    def test_document_collection_remove(self):
        manifest = generate_manifest('C:\\Root\\Path')
        folder = CdmFolderDefinition(manifest.ctx, 'Folder')
        folder._corpus = manifest.ctx.corpus
        folder._folder_path = 'FolderPath/'
        folder._namespace = 'Namespace'

        self.assertEqual(0, len(folder.documents))

        document = CdmDocumentDefinition(manifest.ctx, 'DocumentName')
        document2 = CdmDocumentDefinition(manifest.ctx, 'DocumentName2')

        documentList = [document, document2]
        folder.documents.extend(documentList)
        self.assertEqual(2, len(folder.documents))
        self.assertEqual(document, folder.documents[0])
        self.assertEqual(document2, folder.documents[1])
        self.assertEqual(folder, document.owner)

        folder.documents.remove(document)
        self.assertFalse(document in folder.documents)
        self.assertEqual(1, len(folder.documents))
        self.assertEqual(document2, folder.documents[0])
        self.assertEqual(None, document.owner)

        folder.documents.remove(document)
        self.assertEqual(1, len(folder.documents))
        self.assertEqual(document2, folder.documents[0])

        folder.documents.append(document)
        self.assertEqual(2, len(folder.documents))
        self.assertEqual(folder, document.owner)
        folder.documents.remove(document.name)
        self.assertEqual(1, len(folder.documents))
        self.assertEqual(document2, folder.documents[0])
        self.assertEqual(None, document.owner)
Exemplo n.º 15
0
    def test_document_collection_add(self):
        manifest = generate_manifest('C:\\Root\\Path')
        folder = CdmFolderDefinition(manifest.ctx, 'Folder')
        folder._corpus = manifest.ctx.corpus
        folder.folder_path = 'FolderPath/'
        folder.namespace = 'Namespace'
        document = CdmDocumentDefinition(manifest.ctx, 'DocumentName')

        self.assertEqual(0, len(folder.documents))
        added_document = folder.documents.append(document)
        self.assertEqual(1, len(folder.documents))
        self.assertEqual(document, folder.documents[0])
        self.assertEqual(document, added_document)
        self.assertEqual('FolderPath/', document.folder_path)
        self.assertEqual(folder, document.owner)
        self.assertEqual('Namespace', document.namespace)
        self.assertTrue(document._needs_indexing)
Exemplo n.º 16
0
    def test_cdm_collection_remove_method(self):
        cdm_corpus = CdmCorpusDefinition()
        cdm_corpus.storage.default_namespace = 'local'
        cdm_corpus.storage.mount('local', LocalAdapter('CdmCorpus/LocalPath'))

        ctx = cdm_corpus.ctx

        cdm_document = CdmDocumentDefinition(ctx, 'NameOfDocument')
        collection = CdmCollection(ctx, cdm_document, CdmObjectType.ATTRIBUTE_CONTEXT_DEF)

        appended_document = collection.append('nameOfNewDocument')
        collection.append('otherDocument')

        self.assertEqual(2, len(collection))

        collection.remove(appended_document)
        self.assertTrue(appended_document not in collection)

        # try to remove a second time.
        collection.remove(appended_document)
        self.assertTrue(appended_document not in collection)
        self.assertEqual(1, len(collection))
Exemplo n.º 17
0
    def test_cdm_collection_remove_at(self):
        cdm_corpus = CdmCorpusDefinition()
        cdm_corpus.storage.default_namespace = 'local'
        cdm_corpus.storage.mount('local', LocalAdapter('CdmCorpus/LocalPath'))

        ctx = cdm_corpus.ctx

        cdm_document = CdmDocumentDefinition(ctx, 'NameOfDocument')
        collection = CdmCollection(ctx, cdm_document, CdmObjectType.ATTRIBUTE_CONTEXT_DEF)

        collection.append('nameOfNewDocument')
        appended_document2 = collection.append('otherDocument')

        self.assertEqual(2, len(collection))

        collection.pop(0)
        self.assertEqual(1, len(collection))
        self.assertEqual(appended_document2, collection[0])
        collection.pop(1)
        self.assertEqual(1, len(collection))
        self.assertEqual(appended_document2, collection[0])
        collection.pop(0)
        self.assertEqual(0, len(collection))