def test_manifest_add_entity_with_localized_paths(self): manifest = generate_manifest("C:\\Root\\Path") cdm_corpus = manifest.ctx.corpus entity = CdmEntityDefinition(cdm_corpus.ctx, "entityName", None) entity.explanation = "The explanation of the entity" create_document_for_entity(cdm_corpus, entity) cdm_entity = CdmEntityDefinition(cdm_corpus.ctx, "cdm_entityName", None) create_document_for_entity(cdm_corpus, cdm_entity, "cdm") localized_entity_declaration = manifest.entities.append(entity) cdm_entity_declaration = manifest.entities.append(cdm_entity) self.assertEqual("The explanation of the entity", localized_entity_declaration.explanation) self.assertEqual("entityName.cdm.json/entityName", localized_entity_declaration.entity_path) self.assertEqual("entityName", localized_entity_declaration.entity_name) self.assertEqual("cdm:/cdm_entityName.cdm.json/cdm_entityName", cdm_entity_declaration.entity_path) self.assertEqual("entityName", localized_entity_declaration.entity_name) self.assertEqual(2, len(manifest.entities)) self.assertEqual(localized_entity_declaration, manifest.entities[0]) self.assertEqual(cdm_entity_declaration, manifest.entities[1])
def to_data(instance: CdmEntityDefinition, res_opt: ResolveOptions, options: CopyOptions) -> Entity: exhibits_traits = [ trait for trait in instance.exhibits_traits if not trait.is_from_property ] data = Entity() data.explanation = instance.explanation data.entityName = instance.entity_name data.extendsEntity = EntityReferencePersistence.to_data( instance.extends_entity, res_opt, options) if instance.extends_entity else None data.ExtendsEntityResolutionGuidance = AttributeResolutionGuidancePersistence.to_data( instance.extends_entity_resolution_guidance, res_opt, options) if instance.extends_entity_resolution_guidance else None data.exhibitsTraits = utils.array_copy_data(res_opt, exhibits_traits, options) data.sourceName = instance.fetch_property('sourceName') data.displayName = instance.fetch_property('displayName') data.description = instance.fetch_property('description') data.version = instance.fetch_property('version') data.cdmSchemas = instance.fetch_property('cdmSchemas') data.attributeContext = AttributeContextPersistence.to_data( instance.attribute_context, res_opt, options) if instance.attribute_context else None # After the properties so they show up first in doc data.hasAttributes = utils.array_copy_data(res_opt, instance.attributes, options) return data
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)
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_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)
def test_manifest_can_add_entity_definition(self): manifest = generate_manifest() entity = CdmEntityDefinition(manifest.ctx, 'entityName', None) create_document_for_entity(manifest.ctx.corpus, entity) manifest.entities.append(entity) self.assertEqual(1, len(manifest.entities)) self.assertEqual('entityName', manifest.entities[0].entity_name)
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)
def test_cdm_entity_collection_remove_entity_declaration_definition(self): manifest = generate_manifest() entity = CdmEntityDefinition(manifest.ctx, 'entityName', None) create_document_for_entity(manifest.ctx.corpus, entity) other_entity = CdmEntityDefinition(manifest.ctx, 'otherEntityName', None) create_document_for_entity(manifest.ctx.corpus, other_entity) manifest.entities.append(entity) manifest.entities.append(other_entity) self.assertEqual(2, len(manifest.entities)) manifest.entities.remove(entity) self.assertFalse(entity in manifest.entities) self.assertEqual(1, len(manifest.entities)) self.assertEqual(other_entity.entity_name, manifest.entities[0].entity_name) manifest.entities.remove(entity) self.assertEqual(1, len(manifest.entities))
def test_manifest_can_add_entity_definition(self): manifest = generate_manifest("C:\\Root\\Path") entity = CdmEntityDefinition(manifest.ctx, "entityName", None) entity.explanation = "The explanation of the entity" create_document_for_entity(manifest.ctx.corpus, entity) entity_declaration = manifest.ctx.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) manifest.entities.append(entity_declaration) self.assertEqual("local:/entityName.cdm.json/entityName", entity_declaration.entity_path) self.assertEqual("entityName", entity_declaration.entity_name) self.assertEqual(1, len(manifest.entities)) self.assertEqual(entity_declaration, manifest.entities[0])
def test_manifest_add_entity_with_localized_paths(self): manifest = generate_manifest() cdm_corpus = manifest.ctx.corpus entity = CdmEntityDefinition(cdm_corpus.ctx, 'entityName', None) entity.explanation = 'The explanation of the entity' create_document_for_entity(cdm_corpus, entity) cdm_entity = CdmEntityDefinition(cdm_corpus.ctx, 'cdm_entityName', None) create_document_for_entity(cdm_corpus, cdm_entity, 'cdm') localized_entity_declaration = manifest.entities.append(entity) cdm_entity_declaration = manifest.entities.append(cdm_entity) self.assertEqual('The explanation of the entity', localized_entity_declaration.explanation) self.assertEqual('entityName.cdm.json/entityName', localized_entity_declaration.entity_path) self.assertEqual('entityName', localized_entity_declaration.entity_name) self.assertEqual('cdm:/cdm_entityName.cdm.json/cdm_entityName', cdm_entity_declaration.entity_path) self.assertEqual('entityName', localized_entity_declaration.entity_name) self.assertEqual(2, len(manifest.entities)) self.assertEqual(localized_entity_declaration, manifest.entities[0]) self.assertEqual(cdm_entity_declaration, manifest.entities[1])
async def test_ent_ref_nonexistent(self): """Test that entity references that do not point to valid entities are reported as an error instead of triggering an exception""" expected_log_codes = {CdmLogCode.WARN_RESOLVE_OBJECT_FAILED, CdmLogCode.ERR_RESOLVE_REFERENCE_FAILURE} corpus = TestHelper.get_local_corpus(self.tests_sub_path, 'TestEntRefNonexistent', expected_codes=expected_log_codes) folder = corpus.storage._namespace_folders['local'] # type: CdmFolderDefinition doc = CdmDocumentDefinition(corpus.ctx, 'someDoc.cdm.json') folder.documents.append(doc) entity = CdmEntityDefinition(corpus.ctx, 'someEntity') ent_att = CdmEntityAttributeDefinition(corpus.ctx, 'entityAtt') ent_att.entity = CdmEntityReference(corpus.ctx, 'nonExistingEntity', True) entity.attributes.append(ent_att) doc.definitions.append(entity) resolvedEnt = await entity.create_resolved_entity_async('resolvedSomeEntity') self.assertIsNotNone(resolvedEnt)
def test_entity_attribute_source(self): """Tests if not setting the projection "source" on an entity attribute triggers an error log""" corpus = CdmCorpusDefinition() error_count = 0 def callback(level: 'CdmStatusLevel', message: str): nonlocal error_count error_count += 1 corpus.set_event_callback(callback, CdmStatusLevel.ERROR) projection = CdmProjection(corpus.ctx) entity_attribute = CdmEntityAttributeDefinition( corpus.ctx, 'attribute') entity_attribute.entity = CdmEntityReference(corpus.ctx, projection, False) # First case, a projection without source. projection.validate() self.assertEqual(1, error_count) error_count = 0 # Second case, a projection with a nested projection. inner_projection = CdmProjection(corpus.ctx) projection.source = CdmEntityReference(corpus.ctx, inner_projection, False) projection.validate() inner_projection.validate() self.assertEqual(1, error_count) error_count = 0 # Third case, a projection with an explicit entity definition. inner_projection.source = CdmEntityReference( corpus.ctx, CdmEntityDefinition(corpus.ctx, 'Entity'), False) projection.validate() inner_projection.validate() self.assertEqual(0, error_count) # Third case, a projection with a named reference. inner_projection.source = CdmEntityReference(corpus.ctx, 'Entity', False) projection.validate() inner_projection.validate() self.assertEqual(0, error_count)