Exemple #1
0
        public void AttemptingToChangeKeyWhenItHasBeenSet_InvalidOperationException()
        {
            TestDomain.Entities.BasicEntity firstEntity = new TestDomain.Entities.BasicEntity();

            firstEntity.Id = 1;
            firstEntity.Id = 2;
        }
Exemple #2
0
        public void AttemptingToChangeKeyWhenItHasBeenSet_InvalidOperationException()
        {
            TestDomain.Entities.BasicEntity firstEntity = new TestDomain.Entities.BasicEntity();

            firstEntity.Id = 1;
            firstEntity.Id = 2;
        }
Exemple #3
0
        public void ComparingEntityWithNull_ReturnsFalse_NoException()
        {
            TestDomain.Entities.BasicEntity firstEntity = new TestDomain.Entities.BasicEntity();

            firstEntity.Id = 1;

            firstEntity.Name = "EntityOne";

            var expected = true;
            var actual   = firstEntity != null;

            Assert.IsTrue(actual == expected, message: "The entity should not be equal to null");
        }
Exemple #4
0
        public void ComparingEntityWithItselfAsObjectUsingEquals_ReturnsTrue_NoException()
        {
            TestDomain.Entities.BasicEntity firstEntity = new TestDomain.Entities.BasicEntity();

            firstEntity.Id = 1;

            firstEntity.Name = "EntityOne";

            var expected = true;
            var actual   = firstEntity.Equals((object)firstEntity);

            Assert.IsTrue(actual == expected, message: "The entity should be equal to itself when using equals even when casted as object (same reference)");
        }
Exemple #5
0
        public void ComparingEntityWithDifferentObjectUsingEquals_ReturnsFalse_NoException()
        {
            TestDomain.Entities.BasicEntity firstEntity = new TestDomain.Entities.BasicEntity();

            firstEntity.Id = 1;

            firstEntity.Name = "EntityOne";

            var expected = true;
            var actual   = !firstEntity.Equals(new object());

            Assert.IsTrue(actual == expected, message: "The entity should not be equal to an entity of different type");
        }
Exemple #6
0
        public void ComparingEntityWithItself_ReturnsTrue_NoException()
        {
            TestDomain.Entities.BasicEntity firstEntity = new TestDomain.Entities.BasicEntity();

            firstEntity.Id = 1;

            firstEntity.Name = "EntityOne";

            var expected = true;
#pragma warning disable CS1718 // Comparison made to same variable
            var actual = firstEntity == firstEntity;
#pragma warning restore CS1718 // Comparison made to same variable

            Assert.IsTrue(actual == expected, message: "The entity should be equal to itself");
        }
Exemple #7
0
        public void ComparingEntityWithItself_ReturnsTrue_NoException()
        {
            TestDomain.Entities.BasicEntity firstEntity = new TestDomain.Entities.BasicEntity();

            firstEntity.Id = 1;

            firstEntity.Name = "EntityOne";

            var expected = true;

#pragma warning disable CS1718 // Comparison made to same variable
            var actual = firstEntity == firstEntity;
#pragma warning restore CS1718 // Comparison made to same variable

            Assert.IsTrue(actual == expected, message: "The entity should be equal to itself");
        }
Exemple #8
0
        public void Provided2EntitiesWithDifferentKeyAndSameProps_AreNotEqual_NoException()
        {
            TestDomain.Entities.BasicEntity firstEntity  = new TestDomain.Entities.BasicEntity();
            TestDomain.Entities.BasicEntity secondEntity = new TestDomain.Entities.BasicEntity();

            firstEntity.Id  = 1;
            secondEntity.Id = 2;

            firstEntity.Name  = "SameProp";
            secondEntity.Name = "SameProp";

            var expected = true;
            var actual   = firstEntity != secondEntity;

            Assert.IsTrue(actual == expected, message: "Both entities should not be equal due to not having the same key");
        }
Exemple #9
0
        public void ComparingEntityWithEntityOfDifferentTypeEvenBeingSimilar_IsNotEqual_NoException()
        {
            TestDomain.Entities.BasicEntity          firstEntity     = new TestDomain.Entities.BasicEntity();
            TestDomain.Entities.DifferentBasicEntity differentEntity = new TestDomain.Entities.DifferentBasicEntity();

            firstEntity.Id     = 1;
            differentEntity.Id = 1;

            firstEntity.Name     = "BothEntitiesSameData";
            differentEntity.Name = "BothEntitiesSameData";

            var expected = true;
            var actual   = firstEntity != differentEntity;

            Assert.IsTrue(actual == expected, message: "The entity should not be equal to an entity of different type, regardless of them being identical in data");
        }
Exemple #10
0
        public void Provided2EntitiesWithDifferentKeyAndSameProps_AreNotEqual_NoException()
        {
            TestDomain.Entities.BasicEntity firstEntity = new TestDomain.Entities.BasicEntity();
            TestDomain.Entities.BasicEntity secondEntity = new TestDomain.Entities.BasicEntity();

            firstEntity.Id = 1;
            secondEntity.Id = 2;

            firstEntity.Name = "SameProp";
            secondEntity.Name = "SameProp";

            var expected = true;
            var actual = firstEntity != secondEntity;

            Assert.IsTrue(actual == expected, message: "Both entities should not be equal due to not having the same key");
        }
Exemple #11
0
        public void ComparingEntityWithDifferentObject_ReturnsFalse_NoException()
        {
            TestDomain.Entities.BasicEntity firstEntity = new TestDomain.Entities.BasicEntity();

            firstEntity.Id = 1;

            firstEntity.Name = "EntityOne";

            var expected = true;

#pragma warning disable CS0253 // Possible unintended reference comparison; right hand side needs cast
            var actual = firstEntity != new object();
#pragma warning restore CS0253 // Possible unintended reference comparison; right hand side needs cast

            Assert.IsTrue(actual == expected, message: "The entity should not be equal to an entity of different type");
        }
Exemple #12
0
        public void Provided2EntitiesWithSameKeyButDifferentProps_AreEqual_NoException()
        {
            TestDomain.Entities.BasicEntity firstEntity = new TestDomain.Entities.BasicEntity();
            TestDomain.Entities.BasicEntity secondEntity = new TestDomain.Entities.BasicEntity();

            firstEntity.Id = 1;
            secondEntity.Id = 1;

            firstEntity.Name = "EntityOne";
            secondEntity.Name = "EntityTwo";

            var expected = true;
            var actual = firstEntity == secondEntity;


            Assert.IsTrue(actual == expected, message: "Both entities should be equal due to having the same key");
        }
Exemple #13
0
        public void Provided2EntitiesWithSameKeyButDifferentProps_AreEqual_NoException()
        {
            TestDomain.Entities.BasicEntity firstEntity  = new TestDomain.Entities.BasicEntity();
            TestDomain.Entities.BasicEntity secondEntity = new TestDomain.Entities.BasicEntity();

            firstEntity.Id  = 1;
            secondEntity.Id = 1;

            firstEntity.Name  = "EntityOne";
            secondEntity.Name = "EntityTwo";

            var expected = true;
            var actual   = firstEntity == secondEntity;


            Assert.IsTrue(actual == expected, message: "Both entities should be equal due to having the same key");
        }
Exemple #14
0
        public void HashCollisionsAreWithinLimits_ReturnsTrue_NoException()
        {
            Dictionary <TestDomain.Entities.BasicEntity, string> entityDict = new Dictionary <TestDomain.Entities.BasicEntity, string>();
            object syncObject    = new object();
            int    errors        = 0;
            int    maxIterations = 1000000;

            Parallel.For(fromInclusive: 0, toExclusive: maxIterations, body: (iteration) =>
            {
                try
                {
                    TestDomain.Entities.BasicEntity entity = new TestDomain.Entities.BasicEntity();

                    entity.Id = iteration;

                    entity.Name = "Entity " + iteration;

                    lock (syncObject)
                    {
                        entityDict.Add(entity, iteration.ToString());
                    }
                }
                catch
                {
                    Interlocked.Increment(ref errors);
                }
            });


            var limit    = 1;
            var expected = true;
            var actual   = (errors < limit);

            Assert.IsTrue(actual == expected, message: "The hashing algoritm generates more than " + limit + " collision per " + maxIterations + " entities");
            Assert.IsTrue(entityDict.Count == maxIterations - errors, message: "Something wrong happened running the test, the right number of elements where not introduced in the dictionary");
        }
Exemple #15
0
        public void HashCollisionsAreWithinLimits_ReturnsTrue_NoException()
        {
            Dictionary<TestDomain.Entities.BasicEntity, string> entityDict = new Dictionary<TestDomain.Entities.BasicEntity, string>();
            object syncObject = new object();
            int errors = 0;
            int maxIterations = 1000000;

            Parallel.For(fromInclusive: 0, toExclusive: maxIterations, body: (iteration) =>
                     {
                         try
                         {
                             TestDomain.Entities.BasicEntity entity = new TestDomain.Entities.BasicEntity();

                             entity.Id = iteration;

                             entity.Name = "Entity " + iteration;

                             lock (syncObject)
                             {
                                 entityDict.Add(entity, iteration.ToString());
                             }
                         }
                         catch
                         {
                             Interlocked.Increment(ref errors);
                         }
                     });


            var limit = 1;
            var expected = true;
            var actual = (errors < limit);

            Assert.IsTrue(actual == expected, message: "The hashing algoritm generates more than " + limit + " collision per " + maxIterations + " entities");
            Assert.IsTrue(entityDict.Count == maxIterations - errors, message: "Something wrong happened running the test, the right number of elements where not introduced in the dictionary");
        }
Exemple #16
0
        public void ComparingEntityWithEntityOfDifferentTypeEvenBeingSimilar_IsNotEqual_NoException()
        {
            TestDomain.Entities.BasicEntity firstEntity = new TestDomain.Entities.BasicEntity();
            TestDomain.Entities.DifferentBasicEntity differentEntity = new TestDomain.Entities.DifferentBasicEntity();

            firstEntity.Id = 1;
            differentEntity.Id = 1;

            firstEntity.Name = "BothEntitiesSameData";
            differentEntity.Name = "BothEntitiesSameData";

            var expected = true;
            var actual = firstEntity != differentEntity;

            Assert.IsTrue(actual == expected, message: "The entity should not be equal to an entity of different type, regardless of them being identical in data");
        }
Exemple #17
0
        public void ComparingEntityWithItselfAsObjectUsingEquals_ReturnsTrue_NoException()
        {
            TestDomain.Entities.BasicEntity firstEntity = new TestDomain.Entities.BasicEntity();

            firstEntity.Id = 1;

            firstEntity.Name = "EntityOne";

            var expected = true;
            var actual = firstEntity.Equals((object)firstEntity);

            Assert.IsTrue(actual == expected, message: "The entity should be equal to itself when using equals even when casted as object (same reference)");
        }
Exemple #18
0
        public void ComparingEntityWithNullObjectUsingEquals_ReturnsFalse_NoException()
        {
            TestDomain.Entities.BasicEntity firstEntity = new TestDomain.Entities.BasicEntity();

            firstEntity.Id = 1;
            object nullObject = null;

            firstEntity.Name = "EntityOne";

            var expected = true;
            var actual = !firstEntity.Equals(nullObject);

            Assert.IsTrue(actual == expected, message: "The entity should not be equal to a null entity of different type");
        }
Exemple #19
0
        public void ComparingEntityWithNull_ReturnsFalse_NoException()
        {
            TestDomain.Entities.BasicEntity firstEntity = new TestDomain.Entities.BasicEntity();

            firstEntity.Id = 1;

            firstEntity.Name = "EntityOne";

            var expected = true;
            var actual = firstEntity != null;

            Assert.IsTrue(actual == expected, message: "The entity should not be equal to null");
        }
Exemple #20
0
        public void ComparingEntityWithDifferentObject_ReturnsFalse_NoException()
        {
            TestDomain.Entities.BasicEntity firstEntity = new TestDomain.Entities.BasicEntity();

            firstEntity.Id = 1;

            firstEntity.Name = "EntityOne";

            var expected = true;
#pragma warning disable CS0253 // Possible unintended reference comparison; right hand side needs cast
            var actual = firstEntity != new object();
#pragma warning restore CS0253 // Possible unintended reference comparison; right hand side needs cast

            Assert.IsTrue(actual == expected, message: "The entity should not be equal to an entity of different type");
        }