Ejemplo n.º 1
0
    async def test_loading_already_present_imports_async(self):
        """Testing docs that load the same import"""
        test_name = 'TestLoadingAlreadyPresentImportsAsync'
        corpus = TestHelper.get_local_corpus(self.tests_subpath, test_name)
        res_opt = ResolveOptions()
        res_opt.strict_validation = True

        # 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)
Ejemplo n.º 2
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.strict_validation = True
                    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))
Ejemplo n.º 3
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.strict_validation = True

        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)
Ejemplo n.º 4
0
    async def test_strict_validation_on(self):
        error_count = 0
        corpus = TestHelper.get_local_corpus(self.tests_subpath,
                                             'test_strict_validation')

        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.strict_validation = True
        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_strict_validation')

        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.strict_validation = True
        res_opt.shallow_validation = True
        await corpus.fetch_object_async('local:/doc.cdm.json', res_opt=res_opt)
        self.assertEqual(1, error_count)
Ejemplo n.º 5
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.strict_validation = True

        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)
Ejemplo n.º 6
0
    async def test_strict_validation_off(self):
        corpus = TestHelper.get_local_corpus(
            self.tests_subpath,
            'test_strict_validation')  # type: CdmCorpusDefinition

        def callback(status_level: CdmStatusLevel, message: str):
            # when the strict validation is disabled, there should be no reference validation.
            # no error should be logged.
            self.fail(message)

        corpus.set_event_callback(callback, CdmStatusLevel.WARNING)

        # load with strict validation disabled.
        res_opt = ResolveOptions()
        res_opt.strict_validation = False
        await corpus.fetch_object_async('local:/doc.cdm.json', res_opt=res_opt)
Ejemplo n.º 7
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.strict_validation = True

        # 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)
Ejemplo n.º 8
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.strict_validation = True

        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)
Ejemplo n.º 9
0
    async def test_loading_same_imports_async(self):
        """Testing docs that load the same import"""
        test_name = 'TestLoadingSameImportsAsync'
        corpus = TestHelper.get_local_corpus(self.tests_subpath, test_name)
        res_opt = ResolveOptions()
        res_opt.strict_validation = True

        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)