示例#1
0
        public void Test_DefaultToRelationship(bool mapped, string json, string expect)
        {
            string jsonMember = "member";

            // Create a type mapping
            EntityType         entityTypeResource  = new EntityType( );
            EntityType         entityTypeResource2 = Entity.Get <EntityType>("test:allFields");
            Resource           referencedValue     = Factory.ScriptNameResolver.GetInstance("Test 10", entityTypeResource2.Id).As <Resource>( );
            Resource           defaultValue        = Factory.ScriptNameResolver.GetInstance("Test 11", entityTypeResource2.Id).As <Resource>( );
            ApiResourceMapping typeMapping         = new ApiResourceMapping( );

            typeMapping.MappedType = entityTypeResource;

            // Create a member mapping
            Relationship rel = new Relationship( );

            rel.FromType           = entityTypeResource;
            rel.ToType             = entityTypeResource2;
            rel.Cardinality_Enum   = CardinalityEnum_Enumeration.OneToOne;
            rel.ToTypeDefaultValue = defaultValue;
            ApiRelationshipMapping relMapping = new ApiRelationshipMapping( );

            relMapping.MappedRelationship = rel;
            relMapping.Name = jsonMember;

            entityTypeResource.Save( );
            rel.Save( );

            if (mapped)
            {
                typeMapping.ResourceMemberMappings.Add(relMapping.As <ApiMemberMapping>( ));
            }

            // Fill entity
            IEntity entity = RunTest(json, typeMapping);
            IEntity target = entity.GetRelationships(rel.Id, Direction.Forward).SingleOrDefault( );

            if (expect == "null")
            {
                Assert.That(target, Is.Null);
            }
            else
            {
                Assert.That(target, Is.Not.Null);
                if (expect == "default")
                {
                    Assert.That(target.Id, Is.EqualTo(defaultValue.Id));
                }
                else
                {
                    Assert.That(target.Id, Is.EqualTo(referencedValue.Id));
                }
            }
        }
示例#2
0
        public static ApiRelationshipMapping CreateApiRelationshipMapping(ApiResourceMapping resourceMapping, EntityRef relationship, string memberName, bool isReverse)
        {
            Relationship           relResource = Entity.Get <Relationship>(relationship);
            ApiRelationshipMapping mapping     = new ApiRelationshipMapping( );

            mapping.MappedRelationship       = relResource;
            mapping.MapRelationshipInReverse = isReverse;
            mapping.Name = memberName;
            resourceMapping.ResourceMemberMappings.Add(mapping.As <ApiMemberMapping>( ));

            return(mapping);
        }
示例#3
0
        public void Test_MandatoryRelationship(string mandatoryDefinedOn, bool provided, bool isNull)
        {
            // Create JSON
            string jsonMember = "member";
            string jsonData   = isNull ? "null" : "\"Test 10\"";
            string json       = "{\"" + jsonMember + "\":" + jsonData + "}";

            if (!provided)
            {
                json = json.Replace(jsonMember, "somethingelse");
            }

            // Create a type mapping
            EntityType         entityTypeResource  = new EntityType();
            EntityType         entityTypeResource2 = Entity.Get <EntityType>("test:allFields");
            ApiResourceMapping typeMapping         = new ApiResourceMapping();

            typeMapping.MappedType = entityTypeResource;

            // Create a member mapping
            Relationship rel = new Relationship();

            rel.FromType         = entityTypeResource;
            rel.ToType           = entityTypeResource2;
            rel.Cardinality_Enum = CardinalityEnum_Enumeration.OneToOne;
            ApiRelationshipMapping relMapping = new ApiRelationshipMapping();

            relMapping.MappedRelationship = rel;
            relMapping.Name = jsonMember;
            typeMapping.ResourceMemberMappings.Add(relMapping.As <ApiMemberMapping>());
            if (mandatoryDefinedOn == "Mapping")
            {
                relMapping.ApiMemberIsRequired = true;
            }
            if (mandatoryDefinedOn == "Schema")
            {
                rel.RelationshipIsMandatory = true;
            }
            entityTypeResource.Save( );

            // Fill entity
            if (provided && !isNull)
            {
                IEntity entity = RunTest(json, typeMapping);
            }
            else
            {
                Assert.Throws <ConnectorRequestException>(() => RunTest(json, typeMapping), "E1010 '" + jsonMember + "' value is required.");
            }
        }