Esempio n. 1
0
        public void AddTaskAndDataForDatabase_CorrectInput_UsereShouldBeAddedToDatabaseAndTaskShouldBeRunCorectly()
        {
            var         databaseFactory = new DatabaseFactory();
            IUnitOfWork unitOfWork      = new UnitOfWork(databaseFactory);

            List <string> Names = new List <string>()
            {
                "Reza", "MohammadReza"
            };

            var enlistTask = new EnlistTask
                             (
                new Task(() => Names.Add("Gholi")),
                new Task(() => Names.Remove("Gholi"))
                             );

            var secondEnlistTask = new EnlistTask
                                   (
                new Task(() => Names.Add("Qamar")),
                new Task(() => Names.Remove("Qamar"))
                                   );

            unitOfWork.AddToQueue(enlistTask);
            unitOfWork.AddToQueue(secondEnlistTask);
            unitOfWork.Commit();

            Assert.AreEqual(Names.Where(P => P == "Gholi").FirstOrDefault(), "Gholi");
            Assert.AreEqual(Names.Where(P => P == "Qamar").FirstOrDefault(), "Qamar");
        }
Esempio n. 2
0
        public void AddTaskAndDataForDatabase_ExceptionInjectedInTaskAndIncorectInput_AllTasksAndDatabaseShouldBeRollback()
        {
            IUnitOfWork unitOfWork = new UnitOfWork(_databaseFactory);

            User User = new User()
            {
                FirstName = "Reza",
                LastName  = "Alizadeh",
                Password  = "******",
            };

            List <string> Names = new List <string>()
            {
                "Reza", "MohammadReza"
            };

            var enlistTask = new EnlistTask
                             (
                new Task(() =>
            {
                _userRepository.Add(User);
            }),
                new Task(() =>
            {
                _userRepository.Delete(User);
            })
                             );

            unitOfWork.AddToQueue(enlistTask);

            var exceptionTask = new EnlistTask
                                (
                new Task(() => throw new Exception(nameof(Exception))),
                new Task(() => { })
                                );

            unitOfWork.AddToQueue(enlistTask);

            try
            {
                unitOfWork.Commit();
            }
            catch
            {
                Assert.IsNull(Names.Where(P => P == "Gholi").FirstOrDefault());
                Assert.IsEmpty(_userRepository.GetMany(x => x.FirstName == "Reza"));
            }
        }
Esempio n. 3
0
        public void AddTaskAndDataForDatabase_ExceptionInjectedInTask_AllTasksAndDatabaseShouldBeRollback()
        {
            IUnitOfWork unitOfWork = new UnitOfWork(_databaseFactory);

            User User = new User()
            {
                FirstName = "Reza",
                LastName  = "Alizadeh",
                Password  = "******",
                Document  = new Document()
                {
                    Title = "Test"
                }
            };

            var addUser = new EnlistTask
                          (
                new Task(() => { _userRepository.Add(User); }),
                new Task(() => { _userRepository.Delete(User); })
                          );

            unitOfWork.AddToQueue(addUser);

            List <string> Names = new List <string>()
            {
                "Reza", "MohammadReza"
            };

            var addName = new EnlistTask
                          (
                new Task(() => { Names.Add("Qamar"); throw new Exception(); }),
                new Task(() => Names.Remove("Qamar"))
                          );

            unitOfWork.AddToQueue(addName);

            try
            {
                unitOfWork.Commit();
                Assert.NotNull(null);
            }
            catch
            {
                Assert.IsEmpty(_userRepository.GetMany(x => x.FirstName == "Reza"));
                Assert.IsNull(Names.Where(P => P == "Gholi").FirstOrDefault());
            }
        }
Esempio n. 4
0
        public void AddTaskAndNullDatabase_ExceptionInjectedInput_AllTasksShouldBeRollback()
        {
            var         databaseFactory = new DatabaseFactory();
            IUnitOfWork unitOfWork      = new UnitOfWork(databaseFactory);

            var usersRepository = new UserRepository(databaseFactory);

            List <string> Names = new List <string>()
            {
                "Reza", "MohammadReza"
            };

            var enlistTask = new EnlistTask
                             (
                new Task(() => { Names.Add("Qamar"); }),
                new Task(() => Names.Remove("Qamar"))
                             );

            unitOfWork.AddToQueue(enlistTask);

            var enlistTask2 = new EnlistTask(
                new Task(() =>
            {
                Names.Add("Gholi");
                throw new Exception();
            }), new Task(() => Names.Remove("Gholi")));

            unitOfWork.AddToQueue(enlistTask2);

            try
            {
                unitOfWork.Commit();
                Assert.NotNull(null);
            }
            catch (Exception)
            {
                Assert.IsNull(Names.Where(P => P == "Gholi").FirstOrDefault());
                Assert.IsNull(Names.Where(P => P == "Qamar").FirstOrDefault());
            }
        }
Esempio n. 5
0
        public void AddTaskAndDataForDatabase_ExceptionInjectedInDataBase_AllTasksAndDatabaseShouldBeRollback()
        {
            var         databaseFactory = new DatabaseFactory();
            IUnitOfWork unitOfWork      = new UnitOfWork(databaseFactory);

            var usersRepository = new UserRepository(databaseFactory);

            User User = new User()
            {
                FirstName = "AliiReza",
                LastName  = "Alizadeh",
                Password  = "******",
            };

            usersRepository.Add(User);

            List <string> Names = new List <string>()
            {
                "Reza", "MohammadReza"
            };

            var enlistTask = new EnlistTask
                             (
                new Task(() => { Names.Add("Qamar"); }),
                new Task(() => Names.Remove("Qamar"))
                             );

            unitOfWork.AddToQueue(enlistTask);

            try
            {
                unitOfWork.Commit();
            }
            catch (Exception)
            {
                Assert.IsNull(Names.Where(P => P == "Gholi").FirstOrDefault());
                Assert.IsEmpty(usersRepository.GetMany(x => x.FirstName == "AliiReza"));
            }
        }