async def test_entity_proj_using_object_model(self):
        """Test for creating a projection with an AddAttributeGroup operation on an entity definition using the object model"""
        test_name = 'test_entity_proj_using_object_model'
        corpus = ProjectionTestUtils.get_corpus(test_name, self.tests_subpath)
        local_root = corpus.storage.fetch_root_folder('local')

        # Create an entity
        entity = ProjectionTestUtils.create_entity(corpus, local_root)  # type: CdmEntityDefinition

        # Create a projection
        projection = ProjectionTestUtils.create_projection(corpus, local_root)  # type: CdmProjection

        # Create an AddAttributeGroup operation
        add_att_group_op = corpus.make_object(CdmObjectType.OPERATION_ADD_ATTRIBUTE_GROUP_DEF)  # type: CdmOperationAddAttributeGroup
        add_att_group_op.attribute_group_name = 'PersonAttributeGroup'
        projection.operations.append(add_att_group_op)

        # Create an entity reference to hold this projection
        projection_entity_ref = corpus.make_object(CdmObjectType.ENTITY_REF, None)  # type: CdmEntityReference
        projection_entity_ref.explicit_reference = projection

        # Set the entity's ExtendEntity to be the projection
        entity.extends_entity = projection_entity_ref

        # Resolve the entity
        resolved_entity = await entity.create_resolved_entity_async('Resolved_{}.cdm.json'.format(entity.entity_name), None, local_root)

        # Verify correctness of the resolved attributes after running the AddAttributeGroup operation
        # Original set of attributes: ['id', 'name', 'value', 'date']
        att_group_definition = self.validate_attribute_group(resolved_entity.attributes, 'PersonAttributeGroup')
        self.assertEqual(4, len(att_group_definition.members))
        self.assertEqual('id', att_group_definition.members[0].name)
        self.assertEqual('name', att_group_definition.members[1].name)
        self.assertEqual('value', att_group_definition.members[2].name)
        self.assertEqual('date', att_group_definition.members[3].name)
Exemplo n.º 2
0
    async def test_nested_proj_using_object_model(self):
        """Test for creating nested projections with ExcludeAttributes operations using the object model"""
        corpus = ProjectionTestUtils.get_local_corpus(
            self.tests_subpath, 'test_nested_proj_using_object_model')
        corpus.storage.mount(
            'local',
            LocalAdapter(
                TestHelper.get_actual_output_folder_path(
                    self.tests_subpath,
                    'test_nested_proj_using_object_model')))
        local_root = corpus.storage.fetch_root_folder('local')

        # Create an entity
        entity = ProjectionTestUtils.create_entity(corpus, local_root)

        # Create a projection
        projection = ProjectionTestUtils.create_projection(corpus, local_root)

        # Create an ExcludeAttributes operation
        exclude_attrs_op = corpus.make_object(
            CdmObjectType.OPERATION_EXCLUDE_ATTRIBUTES_DEF)
        exclude_attrs_op.exclude_attributes = ['id', 'date']
        projection.operations.append(exclude_attrs_op)

        # Create an entity reference to hold this projection
        projection_entity_ref = corpus.make_object(CdmObjectType.ENTITY_REF,
                                                   None)
        projection_entity_ref.explicit_reference = projection

        # Create another projection that uses the previous projection as its source
        projection2 = corpus.make_object(CdmObjectType.PROJECTION_DEF)
        projection2.source = projection_entity_ref

        # Create an ExcludeAttributes operation
        exclude_attrs_op2 = corpus.make_object(
            CdmObjectType.OPERATION_EXCLUDE_ATTRIBUTES_DEF)
        exclude_attrs_op2.exclude_attributes = ['value']
        projection2.operations.append(exclude_attrs_op2)

        # Create an entity reference to hold this projection
        projection_entity_ref2 = corpus.make_object(CdmObjectType.ENTITY_REF,
                                                    None)
        projection_entity_ref2.explicit_reference = projection2

        # Create an entity attribute that contains this projection and add this to the entity
        entity_attribute = corpus.make_object(
            CdmObjectType.ENTITY_ATTRIBUTE_DEF, 'TestEntityAttribute')
        entity_attribute.entity = projection_entity_ref2
        entity.attributes.append(entity_attribute)

        # Resolve the entity
        resolved_entity = await entity.create_resolved_entity_async(
            'Resolved_{}.cdm.json'.format(entity.entity_name), None,
            local_root)

        # Verify correctness of the resolved attributes after running the ExcludeAttributes operations
        # Original set of attributes: ['id', 'name', 'value', 'date']
        # Excluded attributes: ['id', 'date'], ['value']
        self.assertEqual(1, len(resolved_entity.attributes))
        self.assertEqual('name', resolved_entity.attributes[0].name)
Exemplo n.º 3
0
    async def test_conditional_proj_using_object_model(self):
        """Test for creating a projection with an AddCountAttribute operation and a condition using the object model"""
        corpus = ProjectionTestUtils.get_local_corpus(self.tests_subpath, 'test_conditional_proj_using_object_model')
        corpus.storage.mount('local', LocalAdapter(TestHelper.get_actual_output_folder_path(self.tests_subpath, 'test_conditional_proj_using_object_model')))
        local_root = corpus.storage.fetch_root_folder('local')

        # Create an entity
        entity = ProjectionTestUtils.create_entity(corpus, local_root)

        # Create a projection with a condition that states the operation should only execute when the resolution directive is 'referenceOnly'
        projection = ProjectionTestUtils.create_projection(corpus, local_root)
        projection.condition = 'referenceOnly==True'

        # Create an AddCountAttribute operation
        add_count_attr_op = corpus.make_object(CdmObjectType.OPERATION_ADD_COUNT_ATTRIBUTE_DEF)
        add_count_attr_op.count_attribute = corpus.make_object(CdmObjectType.TYPE_ATTRIBUTE_DEF, 'testCount')
        add_count_attr_op.count_attribute.data_type = corpus.make_ref(CdmObjectType.DATA_TYPE_REF, 'integer', True)
        projection.operations.append(add_count_attr_op)

        # Create an entity reference to hold this projection
        projection_entity_ref = corpus.make_object(CdmObjectType.ENTITY_REF, None)
        projection_entity_ref.explicit_reference = projection

        # Create an entity attribute that contains this projection and add this to the entity
        entity_attribute = corpus.make_object(CdmObjectType.ENTITY_ATTRIBUTE_DEF, 'TestEntityAttribute')
        entity_attribute.entity = projection_entity_ref
        entity.attributes.append(entity_attribute)

        # Create resolution options with the 'referenceOnly' directive
        res_opt = ResolveOptions(entity.in_document)
        res_opt.directives = AttributeResolutionDirectiveSet(set(['referenceOnly']))

        # Resolve the entity with 'referenceOnly'
        resolved_entity_with_reference_only = await entity.create_resolved_entity_async('Resolved_{}.cdm.json'.format(entity.entity_name), res_opt, local_root)

        # Verify correctness of the resolved attributes after running the AddCountAttribute operation
        # Original set of attributes: ["id", "name", "value", "date"]
        # Count attribute: "testCount"
        self.assertEqual(5, len(resolved_entity_with_reference_only.attributes))
        self.assertEqual('id', resolved_entity_with_reference_only.attributes[0].name)
        self.assertEqual('name', resolved_entity_with_reference_only.attributes[1].name)
        self.assertEqual('value', resolved_entity_with_reference_only.attributes[2].name)
        self.assertEqual('date', resolved_entity_with_reference_only.attributes[3].name)
        self.assertEqual('testCount', resolved_entity_with_reference_only.attributes[4].name)
        self.assertIsNotNone(resolved_entity_with_reference_only.attributes[4].applied_traits.item('is.linkedEntity.array.count'))

        # Now resolve the entity with the 'structured' directive
        res_opt.directives = AttributeResolutionDirectiveSet(set(['structured']))
        resolved_entity_with_structured = await entity.create_resolved_entity_async('Resolved_{}.cdm.json'.format(entity.entity_name), res_opt, local_root)

        # Verify correctness of the resolved attributes after running the AddCountAttribute operation
        # Original set of attributes: ["id", "name", "value", "date"]
        # No Count attribute added, condition was false
        self.assertEqual(4, len(resolved_entity_with_structured.attributes))
        self.assertEqual('id', resolved_entity_with_structured.attributes[0].name)
        self.assertEqual('name', resolved_entity_with_structured.attributes[1].name)
        self.assertEqual('value', resolved_entity_with_structured.attributes[2].name)
        self.assertEqual('date', resolved_entity_with_structured.attributes[3].name)
Exemplo n.º 4
0
    async def test_entity_attribute_proj_using_object_model(self):
        """Test for creating a projection with an AddTypeAttribute operation on an entity attribute using the object model"""
        corpus = ProjectionTestUtils.get_local_corpus(
            self.tests_subpath,
            'test_entity_attribute_proj_using_object_model')
        corpus.storage.mount(
            'local',
            LocalAdapter(
                TestHelper.get_actual_output_folder_path(
                    self.tests_subpath,
                    'test_entity_attribute_proj_using_object_model')))
        local_root = corpus.storage.fetch_root_folder('local')

        # Create an entity
        entity = ProjectionTestUtils.create_entity(corpus, local_root)

        # Create a projection
        projection = ProjectionTestUtils.create_projection(corpus, local_root)

        # Create an AddTypeAttribute operation
        add_type_attr_op = corpus.make_object(
            CdmObjectType.OPERATION_ADD_TYPE_ATTRIBUTE_DEF)
        add_type_attr_op.type_attribute = corpus.make_object(
            CdmObjectType.TYPE_ATTRIBUTE_DEF, 'testType')
        add_type_attr_op.type_attribute.data_type = corpus.make_ref(
            CdmObjectType.DATA_TYPE_REF, 'entityName', True)
        projection.operations.append(add_type_attr_op)

        # Create an entity reference to hold this projection
        projection_entity_ref = corpus.make_object(CdmObjectType.ENTITY_REF,
                                                   None)
        projection_entity_ref.explicit_reference = projection

        # Create an entity attribute that contains this projection and add this to the entity
        entity_attribute = corpus.make_object(
            CdmObjectType.ENTITY_ATTRIBUTE_DEF, 'TestEntityAttribute')
        entity_attribute.entity = projection_entity_ref
        entity.attributes.append(entity_attribute)

        # Resolve the entity
        resolved_entity = await entity.create_resolved_entity_async(
            'Resolved_{}.cdm.json'.format(entity.entity_name), None,
            local_root)

        # Verify correctness of the resolved attributes after running the AddTypeAttribute operation
        # Original set of attributes: ["id", "name", "value", "date"]
        # Type attribute: "testType"
        self.assertEqual(5, len(resolved_entity.attributes))
        self.assertEqual('id', resolved_entity.attributes[0].name)
        self.assertEqual('name', resolved_entity.attributes[1].name)
        self.assertEqual('value', resolved_entity.attributes[2].name)
        self.assertEqual('date', resolved_entity.attributes[3].name)
        self.assertEqual('testType', resolved_entity.attributes[4].name)
        self.assertEqual(
            'is.linkedEntity.name',
            resolved_entity.attributes[4].applied_traits[4].named_reference)
    async def test_conditional_proj_using_object_model(self):
        """Test for creating a projection with an AddAttributeGroup operation and a condition using the object model"""
        test_name = 'test_conditional_proj_using_object_model'
        corpus = ProjectionTestUtils.get_corpus(test_name, self.tests_subpath)
        local_root = corpus.storage.fetch_root_folder('local')

        # Create an entity.
        entity = ProjectionTestUtils.create_entity(corpus, local_root)

        # Create a projection with a condition that states the operation should only execute when the resolution directive is 'structured'.
        projection = ProjectionTestUtils.create_projection(corpus, local_root)
        projection.condition = 'structured==true'

        # Create an AddAttributeGroup operation
        add_att_group_op = corpus.make_object(CdmObjectType.OPERATION_ADD_ATTRIBUTE_GROUP_DEF)
        add_att_group_op.attribute_group_name = 'PersonAttributeGroup'
        projection.operations.append(add_att_group_op)

        # Create an entity reference to hold this projection.
        projection_entity_ref = corpus.make_object(CdmObjectType.ENTITY_REF, None)  # type: CdmEntityReference
        projection_entity_ref.explicit_reference = projection

        # Create an entity attribute that contains this projection and add this to the entity.
        entity_attribute = corpus.make_object(CdmObjectType.ENTITY_ATTRIBUTE_DEF, 'TestEntityAttribute')  # type: CdmEntityAttributeDefinition
        entity_attribute.entity = projection_entity_ref
        entity.attributes.append(entity_attribute)

        # Create resolution options with the 'referenceOnly' directive.
        res_opt = ResolveOptions(entity.in_document)
        res_opt.directives = AttributeResolutionDirectiveSet({'referenceOnly'})

        # Resolve the entity with 'referenceOnly'
        resolved_entity_with_reference_only = await entity.create_resolved_entity_async('Resolved_{}.cdm.json'.format(entity.entity_name), res_opt, local_root)

        # Verify correctness of the resolved attributes after running the AddAttributeGroup operation
        # Original set of attributes: ['id', 'name', 'value', 'date']
        # Condition not met, keep attributes in flat list
        self.assertEqual(4, len(resolved_entity_with_reference_only.attributes))
        self.assertEqual('id', resolved_entity_with_reference_only.attributes[0].name)
        self.assertEqual('name', resolved_entity_with_reference_only.attributes[1].name)
        self.assertEqual('value', resolved_entity_with_reference_only.attributes[2].name)
        self.assertEqual('date', resolved_entity_with_reference_only.attributes[3].name)

        # Now resolve the entity with the 'structured' directive
        res_opt.directives = AttributeResolutionDirectiveSet({'structured'})
        resolved_entity_with_structured = await entity.create_resolved_entity_async('Resolved_{}.cdm.json'.format(entity.entity_name), res_opt, local_root)

        # Verify correctness of the resolved attributes after running the AddAttributeGroup operation
        # Original set of attributes: ['id', 'name', 'value', 'date']
        # Condition met, put all attributes in an attribute group
        att_group_definition = self.validate_attribute_group(resolved_entity_with_structured.attributes, 'PersonAttributeGroup')
        self.assertEqual(4, len(att_group_definition.members))
        self.assertEqual('id', att_group_definition.members[0].name)
        self.assertEqual('name', att_group_definition.members[1].name)
        self.assertEqual('value', att_group_definition.members[2].name)
        self.assertEqual('date', att_group_definition.members[3].name)
Exemplo n.º 6
0
    async def test_entity_attribute_proj_using_object_model(self):
        """Test for creating a projection with an ArrayExpansion operation on an entity attribute using the object model"""
        corpus = TestHelper.get_local_corpus(self.tests_subpath, 'test_entity_attribute_proj_using_object_model')
        corpus.storage.mount('local', LocalAdapter(TestHelper.get_actual_output_folder_path(self.tests_subpath, 'test_entity_attribute_proj_using_object_model')))
        local_root = corpus.storage.fetch_root_folder('local')

        # Create an entity
        entity = ProjectionTestUtils.create_entity(corpus, local_root)

        # Create a projection
        projection = ProjectionTestUtils.create_projection(corpus, local_root)

        # Create an ArrayExpansion operation
        array_expansion_op = corpus.make_object(CdmObjectType.OPERATION_ARRAY_EXPANSION_DEF)
        array_expansion_op.start_ordinal = 1
        array_expansion_op.end_ordinal = 2
        projection.operations.append(array_expansion_op)

        # Create an entity reference to hold this projection
        projection_entity_ref = corpus.make_object(CdmObjectType.ENTITY_REF, None)
        projection_entity_ref.explicit_reference = projection

        # Create another projection that does a rename so that we can see the expanded attributes in the final resolved entity
        projection2 = corpus.make_object(CdmObjectType.PROJECTION_DEF)
        projection2.source = projection_entity_ref

        # Create a RenameAttributes operation
        rename_attrs_op = corpus.make_object(CdmObjectType.OPERATION_RENAME_ATTRIBUTES_DEF)
        rename_attrs_op.rename_format = '{m}{o}'
        projection2.operations.append(rename_attrs_op)

        # Create an entity reference to hold this projection
        projection_entity_ref2 = corpus.make_object(CdmObjectType.ENTITY_REF, None)
        projection_entity_ref2.explicit_reference = projection2

        # Create an entity attribute that contains this projection and add this to the entity
        entity_attribute = corpus.make_object(CdmObjectType.ENTITY_ATTRIBUTE_DEF, 'TestEntityAttribute')
        entity_attribute.entity = projection_entity_ref2
        entity.attributes.append(entity_attribute)

        # Resolve the entity
        resolved_entity = await entity.create_resolved_entity_async('Resolved_{}.cdm.json'.format(entity.entity_name), None, local_root)

        # Verify correctness of the resolved attributes after running the projections
        # Original set of attributes: ["id", "name", "value", "date"]
        # Expand 1...2, renameFormat = {m}{o}
        self.assertEqual(8, len(resolved_entity.attributes))
        self.assertEqual('id1', resolved_entity.attributes[0].name)
        self.assertEqual('name1', resolved_entity.attributes[1].name)
        self.assertEqual('value1', resolved_entity.attributes[2].name)
        self.assertEqual('date1', resolved_entity.attributes[3].name)
        self.assertEqual('id2', resolved_entity.attributes[4].name)
        self.assertEqual('name2', resolved_entity.attributes[5].name)
        self.assertEqual('value2', resolved_entity.attributes[6].name)
        self.assertEqual('date2', resolved_entity.attributes[7].name)
    async def test_entity_proj_using_object_model(self):
        """Test for creating a projection with an ExcludeAttributes operation on an entity definition using the object model"""
        corpus = TestHelper.get_local_corpus(
            self.tests_subpath, 'test_entity_proj_using_object_model')
        corpus.storage.mount(
            'local',
            LocalAdapter(
                TestHelper.get_actual_output_folder_path(
                    self.tests_subpath,
                    'test_entity_proj_using_object_model')))
        local_root = corpus.storage.fetch_root_folder('local')

        # Create an entity
        entity = ProjectionTestUtils.create_entity(corpus, local_root)

        # Create a projection
        projection = ProjectionTestUtils.create_projection(corpus, local_root)

        # Create an ExcludeAttributes operation
        exclude_attrs_op = corpus.make_object(
            CdmObjectType.OPERATION_EXCLUDE_ATTRIBUTES_DEF)
        exclude_attrs_op.exclude_attributes.append('name')
        exclude_attrs_op.exclude_attributes.append('value')
        projection.operations.append(exclude_attrs_op)

        # Create an entity reference to hold this projection
        projection_entity_ref = corpus.make_object(CdmObjectType.ENTITY_REF,
                                                   None)
        projection_entity_ref.explicit_reference = projection

        # Set the entity's ExtendEntity to be the projection
        entity.extends_entity = projection_entity_ref

        # Resolve the entity
        resolved_entity = await entity.create_resolved_entity_async(
            'Resolved_{}.cdm.json'.format(entity.entity_name), None,
            local_root)

        # Verify correctness of the resolved attributes after running the ExcludeAttributes operation
        # Original set of attributes: ['id', 'name', 'value', 'date']
        # Excluded attributes: ['name', 'value']
        self.assertEqual(2, len(resolved_entity.attributes))
        self.assertEqual('id', resolved_entity.attributes[0].name)
        self.assertEqual('date', resolved_entity.attributes[1].name)
    async def test_conditional_proj_using_object_model(self):
        """Test for creating a projection with an ExcludeAttributes operation and a condition using the object model"""
        corpus = TestHelper.get_local_corpus(
            self.tests_subpath, 'test_conditional_proj_using_object_model')
        corpus.storage.mount(
            'local',
            LocalAdapter(
                TestHelper.get_actual_output_folder_path(
                    self.tests_subpath,
                    'test_conditional_proj_using_object_model')))
        local_root = corpus.storage.fetch_root_folder('local')

        # Create an entity
        entity = ProjectionTestUtils.create_entity(corpus, local_root)

        # Create a projection with a condition that states the operation should only execute when the resolution directive is 'referenceOnly'
        projection = ProjectionTestUtils.create_projection(corpus, local_root)
        projection.condition = 'referenceOnly==True'

        # Create an ExcludeAttributes operation
        exclude_attrs_op = corpus.make_object(
            CdmObjectType.OPERATION_EXCLUDE_ATTRIBUTES_DEF)
        exclude_attrs_op.exclude_attributes.append('id')
        exclude_attrs_op.exclude_attributes.append('date')
        projection.operations.append(exclude_attrs_op)

        # Create an entity reference to hold this projection
        projection_entity_ref = corpus.make_object(CdmObjectType.ENTITY_REF,
                                                   None)
        projection_entity_ref.explicit_reference = projection

        # Create an entity attribute that contains this projection and add this to the entity
        entity_attribute = corpus.make_object(
            CdmObjectType.ENTITY_ATTRIBUTE_DEF, 'TestEntityAttribute')
        entity_attribute.entity = projection_entity_ref
        entity.attributes.append(entity_attribute)

        # Create resolution options with the 'referenceOnly' directive
        res_opt = ResolveOptions(entity.in_document)
        res_opt.directives = AttributeResolutionDirectiveSet(
            set(['referenceOnly']))

        # Resolve the entity with 'referenceOnly'
        resolved_entity_with_reference_only = await entity.create_resolved_entity_async(
            'Resolved_{}.cdm.json'.format(entity.entity_name), res_opt,
            local_root)

        # Verify correctness of the resolved attributes after running the ExcludeAttributes operation
        # Original set of attributes: ['id', 'name', 'value', 'date']
        # Excluded attributes: ['id', 'date']
        self.assertEqual(2,
                         len(resolved_entity_with_reference_only.attributes))
        self.assertEqual(
            'name', resolved_entity_with_reference_only.attributes[0].name)
        self.assertEqual(
            'value', resolved_entity_with_reference_only.attributes[1].name)

        # Now resolve the entity with the 'structured' directive
        res_opt.directives = AttributeResolutionDirectiveSet(
            set(['structured']))
        resolved_entity_with_structured = await entity.create_resolved_entity_async(
            'Resolved_{}.cdm.json'.format(entity.entity_name), res_opt,
            local_root)

        # Verify correctness of the resolved attributes after running the ExcludeAttributes operation
        # Original set of attributes: ['id', 'name', 'value', 'date']
        # Excluded attributes: none, condition was false
        self.assertEqual(4, len(resolved_entity_with_structured.attributes))
        self.assertEqual('id',
                         resolved_entity_with_structured.attributes[0].name)
        self.assertEqual('name',
                         resolved_entity_with_structured.attributes[1].name)
        self.assertEqual('value',
                         resolved_entity_with_structured.attributes[2].name)
        self.assertEqual('date',
                         resolved_entity_with_structured.attributes[3].name)
Exemplo n.º 9
0
    async def test_conditional_proj_using_object_model(self):
        """Test for creating a projection with an AddArtifactAttribute operation and a condition using the object model"""
        test_name = 'test_conditional_proj_using_object_model'
        corpus = ProjectionTestUtils.get_local_corpus(self.tests_subpath,
                                                      test_name)
        local_root = corpus.storage.fetch_root_folder('local')
        corpus.storage.mount('traitGroup',
                             LocalAdapter(self.trait_group_file_path))

        # Create an entity.
        entity = ProjectionTestUtils.create_entity(corpus, local_root)
        entity.in_document.imports.append('traitGroup:/TraitGroup.cdm.json')

        # Create a projection with a condition that states the operation should only execute when the resolution directive is 'structured'.
        projection = ProjectionTestUtils.create_projection(
            corpus, local_root)  # type: CdmProjection
        projection.condition = 'structured==true'
        projection.run_sequentially = True

        # Create an AlterTraits operation
        alter_traits_op_1 = corpus.make_object(
            CdmObjectType.OPERATION_ALTER_TRAITS_DEF
        )  # type: CdmOperationAlterTraits
        alter_traits_op_1.traits_to_add = CdmCollection(
            corpus.ctx, alter_traits_op_1, CdmObjectType.TRAIT_REF)
        alter_traits_op_1.traits_to_add.append(
            corpus.make_ref(CdmObjectType.TRAIT_REF, "means.TraitG100", True))
        alter_traits_op_1.traits_to_add.append(
            corpus.make_ref(CdmObjectType.TRAIT_GROUP_REF, "JobTitleBase",
                            True))
        alter_traits_op_1.traits_to_remove = CdmCollection(
            corpus.ctx, alter_traits_op_1, CdmObjectType.TRAIT_REF)
        alter_traits_op_1.traits_to_remove.append(
            corpus.make_ref(CdmObjectType.TRAIT_REF, "means.TraitG300", True))
        projection.operations.append(alter_traits_op_1)

        alter_traits_op_2 = corpus.make_object(
            CdmObjectType.OPERATION_ALTER_TRAITS_DEF
        )  # type: CdmOperationAlterTraits
        trait_g4 = corpus.make_ref(CdmObjectType.TRAIT_REF, "means.TraitG4",
                                   True)  # type: CdmTraitReference
        trait_g4.arguments.append('precision', '5')
        trait_g4.arguments.append('scale', '15')
        alter_traits_op_2.traits_to_add = CdmCollection(
            corpus.ctx, alter_traits_op_2, CdmObjectType.TRAIT_REF)
        alter_traits_op_2.traits_to_add.append(trait_g4)
        alter_traits_op_2.apply_to = []
        alter_traits_op_2.apply_to.append('name')
        projection.operations.append(alter_traits_op_2)

        # Create an entity reference to hold this projection.
        projection_entity_ref = corpus.make_object(
            CdmObjectType.ENTITY_REF, None)  # type: CdmEntityReference
        projection_entity_ref.explicit_reference = projection

        # Create an entity attribute that contains this projection and add this to the entity.
        entity_attribute = corpus.make_object(
            CdmObjectType.ENTITY_ATTRIBUTE_DEF,
            'TestEntityAttribute')  # type: CdmEntityAttributeDefinition
        entity_attribute.entity = projection_entity_ref
        entity.attributes.append(entity_attribute)

        # Create resolution options with the 'referenceOnly' directive.
        res_opt = ResolveOptions(entity.in_document)
        res_opt.directives = AttributeResolutionDirectiveSet({'referenceOnly'})

        # Resolve the entity with 'referenceOnly'
        resolved_entity_with_reference_only = await entity.create_resolved_entity_async(
            'Resolved_{}.cdm.json'.format(entity.entity_name), res_opt,
            local_root)

        # Original set of attributes: ['id', 'name', 'value', 'date']
        # Condition not met, no traits are added
        self.assertEqual(4,
                         len(resolved_entity_with_reference_only.attributes))
        self.validate_trait(resolved_entity_with_reference_only.attributes[0],
                            'id', False, True)
        self.validate_trait(resolved_entity_with_reference_only.attributes[1],
                            'name', False, True)
        self.validate_trait(resolved_entity_with_reference_only.attributes[2],
                            'value', False, True)
        self.validate_trait(resolved_entity_with_reference_only.attributes[3],
                            'date', False, True)

        # Now resolve the entity with the 'structured' directive
        res_opt.directives = AttributeResolutionDirectiveSet({'structured'})
        resolved_entity_with_structured = await entity.create_resolved_entity_async(
            'Resolved_{}.cdm.json'.format(entity.entity_name), res_opt,
            local_root)

        # Original set of attributes: ['id', 'name', 'value', 'date']
        # Condition met, new traits are added
        self.assertEqual(4, len(resolved_entity_with_structured.attributes))
        self.validate_trait(resolved_entity_with_structured.attributes[0],
                            'id')
        self.validate_trait(resolved_entity_with_structured.attributes[1],
                            'name', True)
        self.validate_trait(resolved_entity_with_structured.attributes[2],
                            'value')
        self.validate_trait(resolved_entity_with_structured.attributes[3],
                            'date')
Exemplo n.º 10
0
    async def test_conditional_proj_using_object_model(self):
        """Test for creating a projection with an AddArtifactAttribute operation and a condition using the object model"""
        test_name = 'test_conditional_proj_using_object_model'
        corpus = ProjectionTestUtils.get_local_corpus(self.tests_subpath,
                                                      test_name)
        local_root = corpus.storage.fetch_root_folder('local')

        # Create an entity.
        entity = ProjectionTestUtils.create_entity(corpus, local_root)

        # Create a projection with a condition that states the operation should only execute when the resolution directive is 'structured'.
        projection = ProjectionTestUtils.create_projection(corpus, local_root)
        projection.condition = 'structured==true'

        # Create an AddArtifactAttribute operation
        add_artifact_attribute_op = corpus.make_object(
            CdmObjectType.OPERATION_ADD_ARTIFACT_ATTRIBUTE_DEF)
        add_artifact_attribute_op.new_attribute = corpus.make_object(
            CdmObjectType.TYPE_ATTRIBUTE_DEF, "newName")
        add_artifact_attribute_op.new_attribute.data_type = corpus.make_ref(
            CdmObjectType.DATA_TYPE_REF, "string", True)
        projection.operations.append(add_artifact_attribute_op)

        # Create an entity reference to hold this projection.
        projection_entity_ref = corpus.make_object(
            CdmObjectType.ENTITY_REF, None)  # type: CdmEntityReference
        projection_entity_ref.explicit_reference = projection

        # Create an entity attribute that contains this projection and add this to the entity.
        entity_attribute = corpus.make_object(
            CdmObjectType.ENTITY_ATTRIBUTE_DEF,
            'TestEntityAttribute')  # type: CdmEntityAttributeDefinition
        entity_attribute.entity = projection_entity_ref
        entity.attributes.append(entity_attribute)

        # Create resolution options with the 'referenceOnly' directive.
        res_opt = ResolveOptions(entity.in_document)
        res_opt.directives = AttributeResolutionDirectiveSet({'referenceOnly'})

        # Resolve the entity with 'referenceOnly'
        resolved_entity_with_reference_only = await entity.create_resolved_entity_async(
            'Resolved_{}.cdm.json'.format(entity.entity_name), res_opt,
            local_root)

        # Original set of attributes: ['id', 'name', 'value', 'date']
        # Condition not met, keep attributes in flat list
        self.assertEqual(4,
                         len(resolved_entity_with_reference_only.attributes))
        self.assertEqual(
            'id', resolved_entity_with_reference_only.attributes[0].name)
        self.assertEqual(
            'name', resolved_entity_with_reference_only.attributes[1].name)
        self.assertEqual(
            'value', resolved_entity_with_reference_only.attributes[2].name)
        self.assertEqual(
            'date', resolved_entity_with_reference_only.attributes[3].name)

        # Now resolve the entity with the 'structured' directive
        res_opt.directives = AttributeResolutionDirectiveSet({'structured'})
        resolved_entity_with_structured = await entity.create_resolved_entity_async(
            'Resolved_{}.cdm.json'.format(entity.entity_name), res_opt,
            local_root)

        # Original set of attributes: ['id', 'name', 'value', 'date']
        # Condition met, keep attributes in flat list and add the new attribute "newName" all attributes at the end
        self.assertEqual(5, len(resolved_entity_with_structured.attributes))
        self.assertEqual('id',
                         resolved_entity_with_structured.attributes[0].name)
        self.assertEqual('name',
                         resolved_entity_with_structured.attributes[1].name)
        self.assertEqual('value',
                         resolved_entity_with_structured.attributes[2].name)
        self.assertEqual('date',
                         resolved_entity_with_structured.attributes[3].name)
        self.assertEqual('newName',
                         resolved_entity_with_structured.attributes[4].name)