Exemplo n.º 1
0
        public void Test_Build_With_2RelProps_ShouldCreateRelDefWithTwoProps()
        {
            //---------------Set up test pack-------------------
            string relationshipName = "R" + GetRandomString();
            string propertyName     = "P" + GetRandomString();
            string relatedPropName  = "P" + GetRandomString();
            string propertyName2    = "P" + GetRandomString();
            string relatedPropName2 = "P" + GetRandomString();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------

            var multipleRelationshipDef = new RelationshipsBuilderStub <Car>().WithMultipleRelationship <Car>(relationshipName)
                                          .WithCompositeRelationshipKey()
                                          .WithRelProp(propertyName, relatedPropName)
                                          .WithRelProp(propertyName2, relatedPropName2)
                                          .EndCompositeRelationshipKey()
                                          .Build();

            //---------------Test Result -----------------------
            Assert.AreEqual(relationshipName, multipleRelationshipDef.RelationshipName);
            Assert.AreEqual(2, multipleRelationshipDef.RelKeyDef.Count);
            IRelPropDef relPropDef = multipleRelationshipDef.RelKeyDef[propertyName];

            Assert.IsNotNull(relPropDef);
            Assert.AreEqual(propertyName, relPropDef.OwnerPropertyName);
            Assert.AreEqual(relatedPropName, relPropDef.RelatedClassPropName);
            relPropDef = multipleRelationshipDef.RelKeyDef[propertyName2];
            Assert.IsNotNull(relPropDef);
            Assert.AreEqual(propertyName2, relPropDef.OwnerPropertyName);
            Assert.AreEqual(relatedPropName2, relPropDef.RelatedClassPropName);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Maps the <see cref="ReflectionWrappers.PropertyWrapper"/> to a <see cref="IRelationshipDef"/>.
        /// </summary>
        /// <returns></returns>
        public IRelationshipDef MapOneToOne()
        {
            if (!MustBeMapped())
            {
                return(null);
            }
            CheckReverseRelationshipValid();

            var relatedClassType            = PropertyWrapper.RelatedClassType.UnderlyingType;
            DeleteParentAction deleteAction = GetDeleteAction();

            var relDef = new SingleRelationshipDef(this.PropertyWrapper.Name, relatedClassType
                                                   , new RelKeyDef(), true, deleteAction)
            {
                OwningBOHasForeignKey   = this.OwningBoHasForeignKey,
                ReverseRelationshipName = this.ReverseRelationshipName
            };

            SetRelationshipType(relDef);
            relDef.SetAsOneToOne();
            IRelPropDef relPropDef = this.CreateRelPropDef();

            relDef.RelKeyDef.Add(relPropDef);
            return(relDef);
        }
Exemplo n.º 3
0
        public void TestContainsPropDef()
        {
            Assert.IsTrue(mRelKeyDef.Contains("Prop"));
            IRelPropDef lPropDef = mRelKeyDef["Prop"];

            Assert.AreEqual("Prop", lPropDef.OwnerPropertyName);
        }
        public void Test_Map_WhenNotHasFKPropDefined_AndIsOneToMany_ShouldFKProp()
        {
            //---------------Set up test pack-------------------
            Type           boWith12M          = typeof(FakeBOWithMultipleRel);
            Type           boWithNoDefinedRel = typeof(FakeBOWithNoRelationship);
            FakeTypeSource source             = new FakeTypeSource(
                new[] { boWith12M, boWithNoDefinedRel });

            AllClassesAutoMapper allClassesAutoMapper = new AllClassesAutoMapper(source);

            //---------------Assert Precondition----------------
            Assert.AreEqual(2, source.GetTypes().Count());
            //---------------Execute Test ----------------------
            ClassDefCol classDefCol = allClassesAutoMapper.Map();
            //---------------Test Result -----------------------
            IClassDef cDefWith12M = classDefCol[boWith12M];

            cDefWith12M.RelationshipDefCol.ShouldHaveCount(1);
            IRelationshipDef relationshipDef = cDefWith12M.RelationshipDefCol.First();

            Assert.IsInstanceOf(typeof(MultipleRelationshipDef), relationshipDef);

            IClassDef cDefNoDefinedRel = classDefCol[boWithNoDefinedRel];

            IRelationshipDef reverseRelDef     = cDefNoDefinedRel.RelationshipDefCol[relationshipDef.ReverseRelationshipName];
            IRelPropDef      reverseRelPropDef = reverseRelDef.RelKeyDef.First();

            cDefNoDefinedRel.PropDefcol.ShouldContain(propDef => propDef.PropertyName == reverseRelPropDef.OwnerPropertyName);
        }
Exemplo n.º 5
0
 /// <summary>
 /// Removes a Related Property definition from the key
 /// </summary>
 /// <param name="relPropDef">The Related Property Definition to remove</param>
 protected void Remove(IRelPropDef relPropDef)
 {
     if (Contains(relPropDef))
     {
         _relPropDefs.Remove(relPropDef.OwnerPropertyName);
     }
 }
Exemplo n.º 6
0
 /// <summary>
 /// Adds the related property definition to this key, as long as
 /// a property by that name has not already been added.
 /// </summary>
 /// <param name="relPropDef">The RelPropDef object to be added.</param>
 /// <exception cref="HabaneroArgumentException">Thrown if the
 /// argument passed is null</exception>
 public virtual void Add(IRelPropDef relPropDef)
 {
     if (relPropDef == null)
     {
         throw new HabaneroArgumentException("relPropDef",
                                             "ClassDef-Add. You cannot add a null prop def to a classdef");
     }
     if (!Contains(relPropDef))
     {
         _relPropDefs.Add(relPropDef.OwnerPropertyName, relPropDef);
     }
 }
Exemplo n.º 7
0
        public void TestThisIndexerException()
        {
            //---------------Set up test pack-------------------
            RelKeyDef relKeyDef = new RelKeyDef();

            //---------------Execute Test ----------------------
            try
            {
                IRelPropDef relPropDef = relKeyDef["prop"];
                Assert.Fail("Expected to throw an InvalidPropertyNameException");
            }
            //---------------Test Result -----------------------
            catch (InvalidPropertyNameException ex)
            {
                StringAssert.Contains("In a relationship property definition, the property with the name 'prop' does not exist in the collection of properties", ex.Message);
            }
        }
Exemplo n.º 8
0
        public void Test_IsCompulsory_WhenOwnerPropDefNull_ShouldRetFalse()
        {
            //---------------Set up test pack-------------------
            FakeSingleRelationshipDef relationshipDef = new FakeSingleRelationshipDef();
            var         relKeyDef  = new RelKeyDef();
            IRelPropDef relPropDef = MockRepository.GenerateStub <IRelPropDef>();

            relPropDef.Stub(def => def.OwnerPropertyName).Return(TestUtil.GetRandomString());
            relKeyDef.Add(relPropDef);
            relationshipDef.SetRelKeyDef(relKeyDef);
            relationshipDef.OwningBOHasForeignKey = true;
            //---------------Assert Precondition----------------
            Assert.IsTrue(relationshipDef.OwningBOHasForeignKey);
            Assert.IsNull(relPropDef.OwnerPropDef);
            //---------------Execute Test ----------------------
            bool isCompulsory = relationshipDef.IsCompulsory;

            //---------------Test Result -----------------------
            Assert.IsFalse(isCompulsory, "Rel Should not be compulsory");
        }
Exemplo n.º 9
0
        public void Test_Build_WithRelProp_ShouldCreateRelDefWithOneProp()
        {
            //---------------Set up test pack-------------------
            const string relationshipName = "Drivers";
            const string propertyName     = "VehicleID";
            const string relatedPropName  = "CarID";
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var multipleRelationshipDef      = new RelationshipsBuilderStub <Car>().WithMultipleRelationship(c => c.Drivers).WithRelProp(propertyName, relatedPropName);
            IRelationshipDef relationshipDef = multipleRelationshipDef.Build();


            //---------------Test Result -----------------------
            Assert.AreEqual(relationshipName, relationshipDef.RelationshipName);
            Assert.AreEqual(1, relationshipDef.RelKeyDef.Count);
            IRelPropDef relPropDef = relationshipDef.RelKeyDef[propertyName];

            Assert.IsNotNull(relPropDef);
            Assert.AreEqual(propertyName, relPropDef.OwnerPropertyName);
            Assert.AreEqual(relatedPropName, relPropDef.RelatedClassPropName);
        }
Exemplo n.º 10
0
        ///<summary>
        /// Creates a Reverse Relationship when required.
        ///</summary>
        ///<param name="classDefCol"></param>
        ///<param name="classDef"></param>
        ///<param name="relationship"></param>
        ///<returns></returns>
        public static IRelationshipDef CreateReverseRelationship(ClassDefCol classDefCol, IClassDef classDef, IRelationshipDef relationship)
        {
            IRelationshipDef rel = relationship;

            if (!ContainsRelatedClass(relationship, classDefCol))
            {
                return(null);
            }

            IClassDef relatedClassDef          = RelatedObjectClassDef(classDefCol, relationship);
            bool      foundReverseRelationship = relatedClassDef.RelationshipDefCol.Any(
                def => def.RelationshipName == rel.ReverseRelationshipName);

            if (foundReverseRelationship)
            {
                return(null);
            }

            IRelationshipDef newReverseRelDef = CreateReverseRelDef(rel, classDef);


            relatedClassDef.RelationshipDefCol.Add(newReverseRelDef);
            IRelPropDef relPropDef = relationship.RelKeyDef.FirstOrDefault();

            if (relPropDef != null)
            {
                var reverseRelPropDef = new RelPropDef(relPropDef.RelatedClassPropName, relPropDef.OwnerPropertyName);
                newReverseRelDef.RelKeyDef.Add(reverseRelPropDef);
                bool hasPropDef = relatedClassDef.PropDefColIncludingInheritance.Any(
                    propDef => propDef.PropertyName == reverseRelPropDef.OwnerPropertyName);
                if (!hasPropDef)
                {
                    var fkPropDef = new PropDef(reverseRelPropDef.OwnerPropertyName, typeof(Guid), PropReadWriteRule.ReadWrite, null);
                    relatedClassDef.PropDefcol.Add(fkPropDef);
                }
            }
            return(newReverseRelDef);
        }
        public void Test_Map_WhenNotHasReverseRelDefined_ShouldCreateRelProp()
        {
            //---------------Set up test pack-------------------
            Type           boWithM21          = typeof(FakeManyToOneBoRelNoFK);
            Type           boWithNoDefinedRel = typeof(FakeBOWithNoRelationship);
            FakeTypeSource source             = new FakeTypeSource(
                new[] { boWithM21, boWithNoDefinedRel });

            AllClassesAutoMapper allClassesAutoMapper = new AllClassesAutoMapper(source);

            //---------------Assert Precondition----------------
            Assert.AreEqual(2, source.GetTypes().Count());
            //---------------Execute Test ----------------------
            ClassDefCol classDefCol = allClassesAutoMapper.Map();
            //---------------Test Result -----------------------
            IClassDef cDefWithM21 = classDefCol[boWithM21];

            cDefWithM21.RelationshipDefCol.ShouldHaveCount(1);
            IRelationshipDef relationshipDef = cDefWithM21.RelationshipDefCol.FirstOrDefault();

            Assert.IsNotNull(relationshipDef);
            relationshipDef.RelKeyDef.ShouldHaveCount(1);
            IRelPropDef relPropDef = relationshipDef.RelKeyDef.FirstOrDefault();

            Assert.IsNotNull(relPropDef);

            IClassDef cDefNoDefinedRel = classDefCol[boWithNoDefinedRel];

            IRelationshipDef reverseRelDef = cDefNoDefinedRel.RelationshipDefCol[relationshipDef.ReverseRelationshipName];

            reverseRelDef.RelKeyDef.ShouldHaveCount(1);
            IRelPropDef revereRelPropDef = reverseRelDef.RelKeyDef.FirstOrDefault();

            Assert.IsNotNull(revereRelPropDef, "ReverseRelationship ShouldHave Been Created");
            Assert.AreEqual(relPropDef.OwnerPropertyName, revereRelPropDef.RelatedClassPropName);
            Assert.AreEqual(relPropDef.RelatedClassPropName, revereRelPropDef.OwnerPropertyName);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Adds the related property definition to this key, as long as
        /// a property by that name has not already been added.
        /// </summary>
        /// <param name="relPropDef">The RelPropDef object to be added.</param>
        /// <exception cref="HabaneroArgumentException">Thrown if the
        /// argument passed is null</exception>
        public virtual void Add(IRelPropDef relPropDef)
        {
            if (relPropDef == null)
            {
				throw new HabaneroArgumentException("relPropDef",
                                                   "ClassDef-Add. You cannot add a null prop def to a classdef");
            }
            if (!Contains(relPropDef))
            {
                _relPropDefs.Add(relPropDef.OwnerPropertyName, relPropDef);
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// Returns true if the specified property is found.
        /// </summary>
		/// <param name="relPropDef">The Related Property Definition to search for</param>
		/// <returns>Returns true if found, false if not</returns>
		internal protected bool Contains(IRelPropDef relPropDef)
        {
			return (_relPropDefs.ContainsKey(relPropDef.OwnerPropertyName));
        }
Exemplo n.º 14
0
		/// <summary>
		/// Removes a Related Property definition from the key
		/// </summary>
		/// <param name="relPropDef">The Related Property Definition to remove</param>
		protected void Remove(IRelPropDef relPropDef)
		{
			if (Contains(relPropDef))
			{
				_relPropDefs.Remove(relPropDef.OwnerPropertyName);
			}
		}
Exemplo n.º 15
0
 private bool PropDefIsCompulsory(IRelPropDef def)
 {
     return (def.OwnerPropDef != null && def.OwnerPropDef.Compulsory)
            || (this.OwningClassDef != null
                && this.OwningClassDef.GetPropDef(def.OwnerPropertyName).Compulsory);
 }
Exemplo n.º 16
0
 internal static bool DoKeyPropsMatch(this IRelPropDef relPropDef, IRelPropDef reverseRelPropDef)
 {
     return relPropDef.OwnerPropertyName == reverseRelPropDef.RelatedClassPropName
            && relPropDef.RelatedClassPropName == reverseRelPropDef.OwnerPropertyName;
 }
Exemplo n.º 17
0
 internal static bool DoKeyPropsMatch(this IRelPropDef relPropDef, IRelPropDef reverseRelPropDef)
 {
     return(relPropDef.OwnerPropertyName == reverseRelPropDef.RelatedClassPropName &&
            relPropDef.RelatedClassPropName == reverseRelPropDef.OwnerPropertyName);
 }
Exemplo n.º 18
0
 private bool PropDefIsCompulsory(IRelPropDef def)
 {
     return((def.OwnerPropDef != null && def.OwnerPropDef.Compulsory) ||
            (this.OwningClassDef != null &&
             this.OwningClassDef.GetPropDef(def.OwnerPropertyName).Compulsory));
 }
Exemplo n.º 19
0
 /// <summary>
 /// Returns true if the specified property is found.
 /// </summary>
 /// <param name="relPropDef">The Related Property Definition to search for</param>
 /// <returns>Returns true if found, false if not</returns>
 internal protected bool Contains(IRelPropDef relPropDef)
 {
     return(_relPropDefs.ContainsKey(relPropDef.OwnerPropertyName));
 }