Beispiel #1
0
        public void test_delete_of_department_should_delete_the_underlying_employee()
        {
            //Arrange
            using (ICommandRepository <Department> departmentCommandRepository = GetCommandRepositoryInstance <Department>())
                using (ICommandRepository <Employee> employeeCommandRepository = GetCommandRepositoryInstance <Employee>())
                    using (IQueryableRepository <Employee> employeeQueryableRepository = GetQueryableRepositoryInstance <Employee>())
                    {
                        //Arrange
                        Department departmentFake = FakeData.GetDepartmentFake();

                        Employee employeeFake = FakeData.GetEmployeeFake();
                        employeeFake.EmployeeName = "XYZ";
                        employeeFake.DeptID       = departmentFake.Id;
                        employeeFake.Department   = departmentFake;

                        //Action
                        //Employee insert will automatically insert the department object before inserting this employee object
                        //since department object is marked as required in the employee map.
                        employeeCommandRepository.Insert(employeeFake);

                        // Should delete the employee object automatically since the map is defined so
                        // (WillCascadeOnDelete is set to true).
                        departmentCommandRepository.Delete(departmentFake);

                        //Assert
                        employeeQueryableRepository.Count().Should().Be(0);
                    };
        }
Beispiel #2
0
        /// <summary>
        /// Count all data of specified type.
        /// </summary>
        /// <typeparam name="TValue">data type</typeparam>
        /// <param name="repository">queryable repository</param>
        /// <returns>total found items</returns>
        public static long Count <TValue>(this IQueryableRepository <TValue> repository)
            where TValue : IDataSource
        {
            Contract.Requires(repository != null);

            return(repository.Count <TValue>(null));
        }
Beispiel #3
0
        public void test_insert_multiple_employees_without_any_explicit_transaction_scope_should_be_saved()
        {
            //Arrange
            using (ICommandRepository <Department> departmentCommandRepository = GetCommandRepositoryInstance <Department>())
                using (ICommandRepository <Employee> employeeCommandRepository = GetCommandRepositoryInstance <Employee>())
                    using (IQueryableRepository <Department> departmentQueryableRepository = GetQueryableRepositoryInstance <Department>())
                        using (IQueryableRepository <Employee> employeeQueryableRepository = GetQueryableRepositoryInstance <Employee>())
                        {
                            //Arrange
                            Department departmentFake = FakeData.GetDepartmentFake();

                            Employee managerEmployeeFake = FakeData.GetEmployeeFake();
                            managerEmployeeFake.EmployeeName = "XYZ";
                            managerEmployeeFake.DeptID       = departmentFake.Id;
                            managerEmployeeFake.Department   = departmentFake;

                            Employee subEmployeeFake = FakeData.GetEmployeeFake(2);
                            subEmployeeFake.DeptID     = departmentFake.Id;
                            subEmployeeFake.Department = departmentFake;
                            subEmployeeFake.ManagerId  = managerEmployeeFake.Id;
                            subEmployeeFake.Manager    = managerEmployeeFake;

                            //Action
                            //Considering cascading inserts, here the order in which the list is inserted with different employee
                            //objects is important. Since department object is defined as required in the EmployeMap class,
                            //Effort(just like EF) automatically inserts the department object before inserting the employee objects.
                            //But again in EmployeeMap class, Manager is marked as optional, so manager and the employees under him or her needs to
                            //be explicitly inserted in proper order.Also here, EF doesn't ensure that if managerEmployeeFake gets
                            //saved successfully and then subEmployeeFake doesn't get saved, then all the operations will be
                            //rolled back (at the DB level).

                            //And all these(inserting department object first, then manager object and then sub emloyee object), happens
                            // internally within the same transaction (just like EF6) and one doesn't need to use the UnitOfWork class
                            // (available in this project).
                            employeeCommandRepository.Insert(new List <Employee> {
                                managerEmployeeFake, subEmployeeFake
                            });

                            //Assert
                            departmentQueryableRepository.Count().Should().Be(1);
                            employeeQueryableRepository.Count().Should().Be(2);
                            employeeQueryableRepository.First().EmployeeName.Should().Be("XYZ");
                            employeeQueryableRepository.Single(x => x.EmployeeName == "Sandip").Id.Should().Be(2);

                            // LINQ to Entities doesn't support the method "Last" and so will throw an exception.That's one of
                            // the differences w.r.t LINQ to Objects.If tested with some mock(s) then the below code won't throw any
                            // exception since internally mocks uses LINQ to Objects (atleast as far as LINQ in general is concerned)
                            // and so true behaviour won't reflect using mocks.

                            Action action = () => employeeQueryableRepository.Last();
                            action.ShouldThrow <NotSupportedException>();
                        };
        }
Beispiel #4
0
        public void test_insert_multiple_employees_alongwith_department_with_explicit_transaction_scope_with_exception_thrown_in_between_should_rollback()
        {
            //Arrange
            IUnitOfWork unitOfWorkWithExceptionToBeThrown = GetUnitOfWorkInstance(true);

            using (ICommandRepository <Department> departmentCommandRepository = GetCommandRepositoryInstance <Department>(unitOfWorkWithExceptionToBeThrown))
                using (ICommandRepository <Employee> employeeCommandRepository = GetCommandRepositoryInstance <Employee>(unitOfWorkWithExceptionToBeThrown))
                    using (IQueryableRepository <Department> departmentQueryableRepository = GetQueryableRepositoryInstance <Department>())
                        using (IQueryableRepository <Employee> employeeQueryableRepository = GetQueryableRepositoryInstance <Employee>())
                        {
                            //Arrange
                            Department departmentFake  = FakeData.GetDepartmentFake();
                            Department departmentFake2 = FakeData.GetDepartmentFake(2);

                            Employee managerEmployeeFake = FakeData.GetEmployeeFake();
                            managerEmployeeFake.EmployeeName = "XYZ";
                            managerEmployeeFake.DeptID       = departmentFake.Id;
                            managerEmployeeFake.Department   = departmentFake;

                            Employee subEmployeeFake = FakeData.GetEmployeeFake(2);
                            subEmployeeFake.DeptID     = departmentFake.Id;
                            subEmployeeFake.Department = departmentFake;
                            subEmployeeFake.ManagerId  = managerEmployeeFake.Id;
                            subEmployeeFake.Manager    = managerEmployeeFake;

                            //Action
                            /// Order of operations of different instances of same type or different types needs to be handled at
                            /// the Business or Service Layer.
                            employeeCommandRepository.Insert(new List <Employee> {
                                managerEmployeeFake, subEmployeeFake
                            });
                            departmentCommandRepository.Insert(departmentFake2);
                            unitOfWorkWithExceptionToBeThrown.Commit();

                            //Assert
                            departmentQueryableRepository.Count().Should().Be(0);
                            employeeQueryableRepository.Count().Should().Be(0);
                        };
        }