public void GetLongestServingEmployees()
        {
            Employee e1 = new Employee { HireDate = new DateTime(2003, 1, 1) };
            Employee e2 = new Employee { HireDate = new DateTime(2001, 1, 1) };
            Employee e3 = new Employee { HireDate = new DateTime(2000, 1, 1) };
            Employee e4 = new Employee { HireDate = new DateTime(2002, 1, 1) };

            // The following employee verifies GetLongestServingEmployees does not return terminated employees
            Employee e5 = new Employee { HireDate = new DateTime(1999, 1, 1), TerminationDate = DateTime.Today };

            using (FakeEmployeeContext ctx = new FakeEmployeeContext(new Employee[] { e1, e2, e3, e4, e5 }, new Department[] { }))
            {
                EmployeeRepository rep = new EmployeeRepository(ctx);

                // Select a subset
                List<Employee> result = rep.GetLongestServingEmployees(2).ToList();
                Assert.AreEqual(2, result.Count, "Expected two items in result.");
                Assert.AreSame(e3, result[0], "Incorrect item at position 0.");
                Assert.AreSame(e2, result[1], "Incorrect item at position 1.");

                // Select more than are present
                result = rep.GetLongestServingEmployees(50).ToList();
                Assert.AreEqual(4, result.Count, "Expected four items in result.");
            }
        }
        public void InitializeWithData()
        {
            Employee e1 = new Employee();
            Employee e2 = new Employee();

            Department d1 = new Department();
            Department d2 = new Department();

            using (FakeEmployeeContext ctx = new FakeEmployeeContext(new Employee[] { e1, e2 }, new Department[] { d1, d2 }))
            {
                UnitOfWork unit = new UnitOfWork(ctx);
                ObservableCollection <DepartmentViewModel> departments = new ObservableCollection <DepartmentViewModel>(ctx.Departments.Select(d => new DepartmentViewModel(d)));
                ObservableCollection <EmployeeViewModel>   employees   = new ObservableCollection <EmployeeViewModel>();
                foreach (var e in ctx.Employees)
                {
                    employees.Add(new EmployeeViewModel(e, employees, departments, unit));
                }

                EmployeeWorkspaceViewModel vm = new EmployeeWorkspaceViewModel(employees, departments, unit);

                Assert.IsNotNull(vm.CurrentEmployee, "Current employee should be set if there are departments.");
                Assert.AreSame(employees, vm.AllEmployees, "ViewModel should expose the same instance of the Employee collection so that changes outside the ViewModel are reflected.");
                Assert.AreSame(employees, vm.AllEmployees[0].ManagerLookup, "ViewModel should expose the same instance of the Employee collection to children so that changes outside the ViewModel are reflected.");
                Assert.AreSame(departments, vm.AllEmployees[0].DepartmentLookup, "ViewModel should expose the same instance of the Department collection to children so that changes outside the ViewModel are reflected.");
            }
        }
Пример #3
0
        public void IndexOrdersByName()
        {
            var context = new FakeEmployeeContext {
                Departments =
                {
                    new Department {
                        Name = "BBB"
                    },
                    new Department {
                        Name = "AAA"
                    },
                    new Department {
                        Name = "ZZZ"
                    },
                }
            };

            var target = new DepartmentController(context);
            var result = target.Index();

            Assert.IsInstanceOfType(result.ViewData.Model, typeof(IEnumerable <Department>));

            var departments = (IEnumerable <Department>)result.ViewData.Model;

            Assert.AreEqual("AAA", departments.ElementAt(0).Name);
            Assert.AreEqual("BBB", departments.ElementAt(1).Name);
            Assert.AreEqual("ZZZ", departments.ElementAt(2).Name);
        }
Пример #4
0
        public void GetLongestServingEmployees()
        {
            Employee e1 = new Employee {
                HireDate = new DateTime(2003, 1, 1)
            };
            Employee e2 = new Employee {
                HireDate = new DateTime(2001, 1, 1)
            };
            Employee e3 = new Employee {
                HireDate = new DateTime(2000, 1, 1)
            };
            Employee e4 = new Employee {
                HireDate = new DateTime(2002, 1, 1)
            };

            // 以下雇员验证 GetLongestServingEmployees 不返回已离职的雇员。
            Employee e5 = new Employee {
                HireDate = new DateTime(1999, 1, 1), TerminationDate = DateTime.Today
            };

            using (FakeEmployeeContext ctx = new FakeEmployeeContext(new Employee[] { e1, e2, e3, e4, e5 }, new Department[] { }))
            {
                EmployeeRepository rep = new EmployeeRepository(ctx);

                // 选择子集
                List <Employee> result = rep.GetLongestServingEmployees(2).ToList();
                Assert.AreEqual(2, result.Count, "Expected two items in result.");
                Assert.AreSame(e3, result[0], "Incorrect item at position 0.");
                Assert.AreSame(e2, result[1], "Incorrect item at position 1.");

                // 所选项多于现有项
                result = rep.GetLongestServingEmployees(50).ToList();
                Assert.AreEqual(4, result.Count, "Expected four items in result.");
            }
        }
Пример #5
0
        public void GetLongestServingEmployees()
        {
            Employee e1 = new Employee {
                HireDate = new DateTime(2003, 1, 1)
            };
            Employee e2 = new Employee {
                HireDate = new DateTime(2001, 1, 1)
            };
            Employee e3 = new Employee {
                HireDate = new DateTime(2000, 1, 1)
            };
            Employee e4 = new Employee {
                HireDate = new DateTime(2002, 1, 1)
            };

            // The following employee verifies GetLongestServingEmployees does not return terminated employees
            Employee e5 = new Employee {
                HireDate = new DateTime(1999, 1, 1), TerminationDate = DateTime.Today
            };

            using (FakeEmployeeContext ctx = new FakeEmployeeContext(new Employee[] { e1, e2, e3, e4, e5 }, new Department[] { }))
            {
                EmployeeRepository rep = new EmployeeRepository(ctx);

                // Select a subset
                List <Employee> result = rep.GetLongestServingEmployees(2).ToList();
                Assert.AreEqual(2, result.Count, "Expected two items in result.");
                Assert.AreSame(e3, result[0], "Incorrect item at position 0.");
                Assert.AreSame(e2, result[1], "Incorrect item at position 1.");

                // Select more than are present
                result = rep.GetLongestServingEmployees(50).ToList();
                Assert.AreEqual(4, result.Count, "Expected four items in result.");
            }
        }
Пример #6
0
        public void GetLongestServingEmployees()
        {
            Employee e1 = new Employee { HireDate = new DateTime(2003, 1, 1) };
            Employee e2 = new Employee { HireDate = new DateTime(2001, 1, 1) };
            Employee e3 = new Employee { HireDate = new DateTime(2000, 1, 1) };
            Employee e4 = new Employee { HireDate = new DateTime(2002, 1, 1) };

            // 以下雇员验证 GetLongestServingEmployees 不返回已离职的雇员。
            Employee e5 = new Employee { HireDate = new DateTime(1999, 1, 1), TerminationDate = DateTime.Today };

            using (FakeEmployeeContext ctx = new FakeEmployeeContext(new Employee[] { e1, e2, e3, e4, e5 }, new Department[] { }))
            {
                EmployeeRepository rep = new EmployeeRepository(ctx);

                // 选择子集
                List<Employee> result = rep.GetLongestServingEmployees(2).ToList();
                Assert.AreEqual(2, result.Count, "Expected two items in result.");
                Assert.AreSame(e3, result[0], "Incorrect item at position 0.");
                Assert.AreSame(e2, result[1], "Incorrect item at position 1.");

                // 所选项多于现有项
                result = rep.GetLongestServingEmployees(50).ToList();
                Assert.AreEqual(4, result.Count, "Expected four items in result.");
            }
        }
        /// <summary>
        /// 创建基于虚设上下文的 ViewModel
        /// </summary>
        /// <param name="ctx">视图模型基于的上下文</param>
        /// <returns>新的 ViewModel</returns>
        private static DepartmentWorkspaceViewModel BuildViewModel(FakeEmployeeContext ctx)
        {
            UnitOfWork unit = new UnitOfWork(ctx);
            ObservableCollection <DepartmentViewModel> departments = new ObservableCollection <DepartmentViewModel>(ctx.Departments.Select(d => new DepartmentViewModel(d)));
            DepartmentWorkspaceViewModel vm = new DepartmentWorkspaceViewModel(departments, unit);

            return(vm);
        }
 public void Initialization()
 {
     using (FakeEmployeeContext ctx = new FakeEmployeeContext())
     {
         Assert.IsNotNull(ctx.Employees, "Constructor did not not initialize Employees ObjectSet.");
         Assert.IsNotNull(ctx.Departments, "Constructor did not initialize Departments ObjectSet.");
         Assert.IsNotNull(ctx.ContactDetails, "Constructor did not initialize ContactDetails ObjectSet.");
     }
 }
 public void NullArgumentChecks()
 {
     using (FakeEmployeeContext ctx = new FakeEmployeeContext())
     {
         Utilities.CheckNullArgumentException(() => { var c = new FakeEmployeeContext(null, new Department[0]); c.Dispose(); }, "employees", "ctor");
         Utilities.CheckNullArgumentException(() => { var c = new FakeEmployeeContext(new Employee[0], null); c.Dispose(); }, "departments", "ctor");
         Utilities.CheckNullArgumentException(() => { ctx.IsObjectTracked(null); }, "entity", "IsObjectTracked");
     }
 }
Пример #10
0
 public void Initialization()
 {
     using (FakeEmployeeContext ctx = new FakeEmployeeContext())
     {
         Assert.IsNotNull(ctx.Employees, "Constructor did not not initialize Employees ObjectSet.");
         Assert.IsNotNull(ctx.Departments, "Constructor did not initialize Departments ObjectSet.");
         Assert.IsNotNull(ctx.ContactDetails, "Constructor did not initialize ContactDetails ObjectSet.");
     }
 }
 public void CheckNullArgumentExceptions()
 {
     using (FakeEmployeeContext ctx = new FakeEmployeeContext())
     {
         UnitOfWork unit = new UnitOfWork(ctx);
         Utilities.CheckNullArgumentException(() => { new DepartmentWorkspaceViewModel(null, unit); }, "departments", "ctor");
         Utilities.CheckNullArgumentException(() => { new DepartmentWorkspaceViewModel(new ObservableCollection<DepartmentViewModel>(), null); }, "unitOfWork", "ctor");
     }
 }
 public void CheckNullArgumentExceptions()
 {
     using (FakeEmployeeContext ctx = new FakeEmployeeContext())
     {
         UnitOfWork unit = new UnitOfWork(ctx);
         Utilities.CheckNullArgumentException(() => { new DepartmentWorkspaceViewModel(null, unit); }, "departments", "ctor");
         Utilities.CheckNullArgumentException(() => { new DepartmentWorkspaceViewModel(new ObservableCollection <DepartmentViewModel>(), null); }, "unitOfWork", "ctor");
     }
 }
        public void AddWhenEmployeeNotSelected()
        {
            using (FakeEmployeeContext ctx = BuildContextWithData())
            {
                EmployeeWorkspaceViewModel vm = BuildViewModel(ctx);

                vm.CurrentEmployee = null;
                TestAddEmployee(ctx, vm);
            }
        }
Пример #14
0
        public void GetAllDepartmentsEmpty()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                DepartmentRepository rep = new DepartmentRepository(ctx);

                IEnumerable <Department> result = rep.GetAllDepartments();
                Assert.AreEqual(0, result.Count());
            }
        }
        public void AddWhenDepartmentNotSelected()
        {
            using (FakeEmployeeContext ctx = BuildContextWithData())
            {
                DepartmentWorkspaceViewModel vm = BuildViewModel(ctx);

                vm.CurrentDepartment = null;
                TestAddDepartment(ctx, vm);
            }
        }
Пример #16
0
        public void GetAllEmployeesEmpty()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                EmployeeRepository rep = new EmployeeRepository(ctx);

                IEnumerable <Employee> result = rep.GetAllEmployees();
                Assert.AreEqual(0, result.Count());
            }
        }
        public void GetAllDepartmentsEmpty()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                DepartmentRepository rep = new DepartmentRepository(ctx);

                IEnumerable<Department> result = rep.GetAllDepartments();
                Assert.AreEqual(0, result.Count());
            }
        }
        public void GetAllEmployeesEmpty()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                EmployeeRepository rep = new EmployeeRepository(ctx);

                IEnumerable<Employee> result = rep.GetAllEmployees();
                Assert.AreEqual(0, result.Count());
            }
        }
Пример #19
0
        public void DisposeCalled()
        {
            bool called = false;
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                ctx.DisposeCalled += (sender, e) => { called = true; };
            }

            Assert.IsTrue(called, "Dispose did not raise DisposeCalled event.");
        }
 public void SaveCalled()
 {
     using (FakeEmployeeContext ctx = new FakeEmployeeContext())
     {
         bool called = false;
         ctx.SaveCalled += (sender, e) => { called = true; };
         ctx.Save();
         Assert.IsTrue(called, "Save did not raise SaveCalled event.");
     }
 }
        public void AddEmployee()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork unit = new UnitOfWork(ctx);

                Employee emp = new Employee();
                unit.AddEmployee(emp);
                Assert.IsTrue(ctx.Employees.Contains(emp), "Employee was not added to underlying context.");
            }
        }
        public void DisposeCalled()
        {
            bool called = false;

            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                ctx.DisposeCalled += (sender, e) => { called = true; };
            }

            Assert.IsTrue(called, "Dispose did not raise DisposeCalled event.");
        }
        /// <summary>
        /// 使用现有工作单元创建基于虚设上下文的 ViewModel
        /// </summary>
        /// <param name="ctx">视图模型基于的上下文</param>
        /// <param name="unit">当前工作单元</param>
        /// <returns>新的 ViewModel</returns>
        private static EmployeeWorkspaceViewModel BuildViewModel(FakeEmployeeContext ctx, UnitOfWork unit)
        {
            ObservableCollection<DepartmentViewModel> departments = new ObservableCollection<DepartmentViewModel>(ctx.Departments.Select(d => new DepartmentViewModel(d)));
            ObservableCollection<EmployeeViewModel> employees = new ObservableCollection<EmployeeViewModel>();
            foreach (var e in ctx.Employees)
            {
                employees.Add(new EmployeeViewModel(e, employees, departments, unit));
            }

            return new EmployeeWorkspaceViewModel(employees, departments, unit);
        }
        public void AddDepartment()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork unit = new UnitOfWork(ctx);

                Department dep = new Department();
                unit.AddDepartment(dep);
                Assert.IsTrue(ctx.Departments.Contains(dep), "Department was not added to underlying context.");
            }
        }
        public void RemoveEmployee()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork unit = new UnitOfWork(ctx);
                Employee   emp  = new Employee();
                unit.AddEmployee(emp);

                unit.RemoveEmployee(emp);
                Assert.IsFalse(ctx.Employees.Contains(emp), "Employee was not removed from underlying context.");
            }
        }
        public void RemoveDepartment()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork unit = new UnitOfWork(ctx);
                Department dep  = new Department();
                unit.AddDepartment(dep);

                unit.RemoveDepartment(dep);
                Assert.IsFalse(ctx.Departments.Contains(dep), "Department was not removed from underlying context.");
            }
        }
Пример #27
0
        public void InitializationWithSuppliedCollections()
        {
            Department dep = new Department();
            ContactDetail det = new Phone();
            Employee emp = new Employee { ContactDetails = new List<ContactDetail> { det } };

            using (FakeEmployeeContext ctx = new FakeEmployeeContext(new Employee[] { emp }, new Department[] { dep }))
            {
                Assert.IsTrue(ctx.Employees.Contains(emp), "Constructor did not add supplied Employees to public ObjectSet.");
                Assert.IsTrue(ctx.Departments.Contains(dep), "Constructor did not add supplied Departments to public ObjectSet.");
                Assert.IsTrue(ctx.ContactDetails.Contains(det), "Constructor did not add supplied ContactDetails to public ObjectSet.");
            }
        }
        public void AddContactDetailFromDefaultConstructor()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork unit = new UnitOfWork(ctx);
                Employee   emp  = new Employee();
                unit.AddEmployee(emp);

                ContactDetail cd = new Address();
                unit.AddContactDetail(emp, cd);
                Assert.IsTrue(ctx.ContactDetails.Contains(cd), "ContactDetail was not added to underlying context.");
            }
        }
        public void DeleteCommandOnlyAvailableWhenEmployeeSelected()
        {
            using (FakeEmployeeContext ctx = BuildContextWithData())
            {
                EmployeeWorkspaceViewModel vm = BuildViewModel(ctx);

                vm.CurrentEmployee = null;
                Assert.IsFalse(vm.DeleteEmployeeCommand.CanExecute(null), "Delete command should be disabled when no employee is selected.");

                vm.CurrentEmployee = vm.AllEmployees.First();
                Assert.IsTrue(vm.DeleteEmployeeCommand.CanExecute(null), "Delete command should be enabled when employee is selected.");
            }
        }
Пример #30
0
        public void AddContactDetailFromDefaultConstructor()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork unit = new UnitOfWork(ctx);
                Employee emp = new Employee();
                unit.AddEmployee(emp);

                ContactDetail cd = new Address();
                unit.AddContactDetail(emp, cd);
                Assert.IsTrue(ctx.ContactDetails.Contains(cd), "ContactDetail was not added to underlying context.");
            }
        }
        public void DeleteCommandOnlyAvailableWhenDepartmentSelected()
        {
            using (FakeEmployeeContext ctx = BuildContextWithData())
            {
                DepartmentWorkspaceViewModel vm = BuildViewModel(ctx);

                vm.CurrentDepartment = null;
                Assert.IsFalse(vm.DeleteDepartmentCommand.CanExecute(null), "Delete command should be disabled when no department is selected.");

                vm.CurrentDepartment = vm.AllDepartments.First();
                Assert.IsTrue(vm.DeleteDepartmentCommand.CanExecute(null), "Delete command should be enabled when department is selected.");
            }
        }
        public void InitializeWithData()
        {
            using (FakeEmployeeContext ctx = BuildContextWithData())
            {
                UnitOfWork unit = new UnitOfWork(ctx);
                ObservableCollection <DepartmentViewModel> departments = new ObservableCollection <DepartmentViewModel>(ctx.Departments.Select(d => new DepartmentViewModel(d)));
                DepartmentWorkspaceViewModel vm = new DepartmentWorkspaceViewModel(departments, unit);

                Assert.IsNotNull(vm.CurrentDepartment, "Current department should be set if there are departments.");
                Assert.AreSame(departments, vm.AllDepartments, "ViewModel should expose the same instance of the collection so that changes outside the ViewModel are reflected.");
                Assert.IsNotNull(vm.AddDepartmentCommand, "AddDepartmentCommand should be initialized");
                Assert.IsNotNull(vm.DeleteDepartmentCommand, "DeleteDepartmentCommand should be initialized");
            }
        }
Пример #33
0
        public void SaveCommand()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork unit = new UnitOfWork(ctx);
                IDepartmentRepository departmentRepository = new DepartmentRepository(ctx);
                IEmployeeRepository employeeRepository = new EmployeeRepository(ctx);
                MainViewModel main = new MainViewModel(unit, departmentRepository, employeeRepository);

                bool called = false;
                ctx.SaveCalled += (sender, e) => { called = true; };
                main.SaveCommand.Execute(null);
                Assert.IsTrue(called, "SaveCommand should result in save on underlying UnitOfWork.");
            }
        }
Пример #34
0
        public void ExternalAddToDepartmentLookup()
        {
            using (FakeEmployeeContext ctx = BuildContextWithData())
            {
                EmployeeViewModel vm = BuildViewModel(ctx);

                DepartmentViewModel currentDepartment = vm.Department;
                DepartmentViewModel newDepartment     = new DepartmentViewModel(new Department());

                vm.DepartmentLookup.Add(newDepartment);
                Assert.IsTrue(vm.DepartmentLookup.Contains(newDepartment), "New department should have been added to DepartmentLookup.");
                Assert.AreSame(currentDepartment, vm.Department, "Assigned Department should not have changed.");
                Assert.IsFalse(ctx.IsObjectTracked(newDepartment.Model), "ViewModel is not responsible for adding departments created externally.");
            }
        }
        public void SaveCommand()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork            unit = new UnitOfWork(ctx);
                IDepartmentRepository departmentRepository = new DepartmentRepository(ctx);
                IEmployeeRepository   employeeRepository   = new EmployeeRepository(ctx);
                MainViewModel         main = new MainViewModel(unit, departmentRepository, employeeRepository);

                bool called = false;
                ctx.SaveCalled += (sender, e) => { called = true; };
                main.SaveCommand.Execute(null);
                Assert.IsTrue(called, "SaveCommand should result in save on underlying UnitOfWork.");
            }
        }
        public void InitializeWithEmptyData()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork unit = new UnitOfWork(ctx);
                ObservableCollection <DepartmentViewModel> departments = new ObservableCollection <DepartmentViewModel>();
                ObservableCollection <EmployeeViewModel>   employees   = new ObservableCollection <EmployeeViewModel>();
                EmployeeWorkspaceViewModel vm = new EmployeeWorkspaceViewModel(employees, departments, unit);

                Assert.AreSame(employees, vm.AllEmployees, "ViewModel should expose the same instance of the collection so that changes outside the ViewModel are reflected.");
                Assert.IsNull(vm.CurrentEmployee, "Current employee should not be set if there are no department.");
                Assert.IsNotNull(vm.AddEmployeeCommand, "AddEmployeeCommand should be initialized");
                Assert.IsNotNull(vm.DeleteEmployeeCommand, "DeleteEmployeeCommand should be initialized");
            }
        }
Пример #37
0
        public void DeleteContactDetailOnlyAvailableWhenDetailIsSelected()
        {
            using (FakeEmployeeContext ctx = BuildContextWithData())
            {
                EmployeeViewModel             vm = BuildViewModel(ctx);
                List <ContactDetailViewModel> originalDetails = vm.ContactDetails.ToList();


                vm.CurrentContactDetail = null;
                Assert.IsFalse(vm.DeleteContactDetailCommand.CanExecute(null), "DeleteContactDetailCommand should be disabled when no detail is selected.");

                vm.CurrentContactDetail = vm.ContactDetails.First();
                Assert.IsTrue(vm.DeleteContactDetailCommand.CanExecute(null), "DeleteContactDetailCommand should be enabled when a detail is selected.");
            }
        }
Пример #38
0
        public void ExternalRemoveManagerLookup()
        {
            using (FakeEmployeeContext ctx = BuildContextWithData())
            {
                EmployeeViewModel vm = BuildViewModel(ctx);

                EmployeeViewModel currentManager = vm.ManagerLookup.First();
                EmployeeViewModel toDelete       = vm.ManagerLookup.Skip(1).First();
                vm.Manager = currentManager;

                vm.ManagerLookup.Remove(toDelete);
                Assert.IsFalse(vm.ManagerLookup.Contains(toDelete), "Employee should have been removed from ManagerLookup.");
                Assert.AreSame(currentManager, vm.Manager, "Assigned Manager should not have changed.");
                Assert.IsTrue(ctx.IsObjectTracked(toDelete.Model), "ViewModel is not responsible for deleting Employees removed externally.");
            }
        }
Пример #39
0
        public void ExternalRemoveDepartmentLookup()
        {
            using (FakeEmployeeContext ctx = BuildContextWithData())
            {
                EmployeeViewModel vm = BuildViewModel(ctx);

                DepartmentViewModel currentDepartment = vm.DepartmentLookup.First();
                DepartmentViewModel toDelete          = vm.DepartmentLookup.Skip(1).First();
                vm.Department = currentDepartment;

                vm.DepartmentLookup.Remove(toDelete);
                Assert.IsFalse(vm.DepartmentLookup.Contains(toDelete), "Department should have been removed from DepartmentLookup.");
                Assert.AreSame(currentDepartment, vm.Department, "Assigned Department should not have changed.");
                Assert.IsTrue(ctx.IsObjectTracked(toDelete.Model), "ViewModel is not responsible for deleting departments removed externally.");
            }
        }
Пример #40
0
        public void ExternalAddToManagerLookup()
        {
            using (FakeEmployeeContext ctx = BuildContextWithData())
            {
                UnitOfWork        unit = new UnitOfWork(ctx);
                EmployeeViewModel vm   = BuildViewModel(ctx, unit);

                EmployeeViewModel currentManager = vm.Manager;
                EmployeeViewModel newManager     = new EmployeeViewModel(new Employee(), vm.ManagerLookup, vm.DepartmentLookup, unit);

                vm.ManagerLookup.Add(newManager);
                Assert.IsTrue(vm.ManagerLookup.Contains(newManager), "New department should have been added to ManagerLookup.");
                Assert.AreSame(currentManager, vm.Manager, "Assigned Manager should not have changed.");
                Assert.IsFalse(ctx.IsObjectTracked(newManager.Model), "ViewModel is not responsible for adding Employees created externally.");
            }
        }
Пример #41
0
        public void AddContactDetailToEmployeeOutsideUnitOfWork()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork unit = new UnitOfWork(ctx);
                Employee emp = new Employee();
                ContactDetail detail = new Email();

                try
                {
                    unit.AddContactDetail(emp, detail);
                    Assert.Fail("Adding a contact detail to an employee outside the Unit of Work did not throw.");
                }
                catch (InvalidOperationException ex)
                {
                    Assert.AreEqual("The supplied Employee is not part of this Unit of Work.", ex.Message);
                }
            }
        }
        public void CreateObject()
        {
            // Fake context should create the actual base type and not a type derived from it
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                object entity = ctx.CreateObject<Department>();
                Assert.AreEqual(typeof(Department), entity.GetType(), "Department did not get created.");

                entity = ctx.CreateObject<Employee>();
                Assert.AreEqual(typeof(Employee), entity.GetType(), "Employee did not get created.");

                entity = ctx.CreateObject<Email>();
                Assert.AreEqual(typeof(Email), entity.GetType(), "Email did not get created.");

                entity = ctx.CreateObject<Phone>();
                Assert.AreEqual(typeof(Phone), entity.GetType(), "Phone did not get created.");

                entity = ctx.CreateObject<Address>();
                Assert.AreEqual(typeof(Address), entity.GetType(), "Address did not get created.");
            }
        }
Пример #43
0
        public void CreateObject()
        {
            // 虚设上下文应该创建实际基类型,而不是从其派生的类型
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                object entity = ctx.CreateObject<Department>();
                Assert.AreEqual(typeof(Department), entity.GetType(), "Department did not get created.");

                entity = ctx.CreateObject<Employee>();
                Assert.AreEqual(typeof(Employee), entity.GetType(), "Employee did not get created.");

                entity = ctx.CreateObject<Email>();
                Assert.AreEqual(typeof(Email), entity.GetType(), "Email did not get created.");

                entity = ctx.CreateObject<Phone>();
                Assert.AreEqual(typeof(Phone), entity.GetType(), "Phone did not get created.");

                entity = ctx.CreateObject<Address>();
                Assert.AreEqual(typeof(Address), entity.GetType(), "Address did not get created.");
            }
        }
Пример #44
0
        public void AddContactDetailAlreadyInUnitOfWork()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork unit = new UnitOfWork(ctx);

                Employee emp = new Employee();
                ContactDetail detail = new Phone();
                unit.AddEmployee(emp);
                unit.AddContactDetail(emp, detail);

                try
                {
                    unit.AddContactDetail(emp, detail);
                    Assert.Fail("Adding an ContactDetail that was already added did not throw.");
                }
                catch (InvalidOperationException ex)
                {
                    Assert.AreEqual("The supplied Phone is already part of this Unit of Work.", ex.Message);
                }
            }
        }
        public void GetAllEmployees()
        {
            Employee e1 = new Employee();
            Employee e2 = new Employee();
            Employee e3 = new Employee();

            using (FakeEmployeeContext ctx = new FakeEmployeeContext(new Employee[] { e1, e2, e3 }, new Department[] { }))
            {
                EmployeeRepository rep = new EmployeeRepository(ctx);

                IEnumerable<Employee> result = rep.GetAllEmployees();

                Assert.IsNotInstanceOfType(
                    result,
                    typeof(IQueryable),
                    "Repositories should not return IQueryable as this allows modification of the query that gets sent to the store. ");

                Assert.AreEqual(3, result.Count());
                Assert.IsTrue(result.Contains(e1));
                Assert.IsTrue(result.Contains(e2));
                Assert.IsTrue(result.Contains(e3));
            }
        }
        public void GetAllDepartments()
        {
            Department d1 = new Department();
            Department d2 = new Department();
            Department d3 = new Department();

            using (FakeEmployeeContext ctx = new FakeEmployeeContext(new Employee[] { }, new Department[] { d1, d2, d3 }))
            {
                DepartmentRepository rep = new DepartmentRepository(ctx);

                IEnumerable<Department> result = rep.GetAllDepartments();

                Assert.IsNotInstanceOfType(
                    result,
                    typeof(IQueryable),
                    "Repositories should not return IQueryable as this allows modification of the query that gets sent to the store. ");

                Assert.AreEqual(3, result.Count());
                Assert.IsTrue(result.Contains(d1));
                Assert.IsTrue(result.Contains(d2));
                Assert.IsTrue(result.Contains(d3));
            }
        }
Пример #47
0
        public void NullArgumentChecks()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork unit = new UnitOfWork(ctx);

                Utilities.CheckNullArgumentException(() => { new UnitOfWork(null); }, "context", "ctor");

                Utilities.CheckNullArgumentException(() => { unit.AddEmployee(null); }, "employee", "AddEmployee");
                Utilities.CheckNullArgumentException(() => { unit.AddDepartment(null); }, "department", "AddDepartment");
                Utilities.CheckNullArgumentException(() => { unit.AddContactDetail(new Employee(), null); }, "detail", "AddContactDetail");
                Utilities.CheckNullArgumentException(() => { unit.AddContactDetail(null, new Phone()); }, "employee", "AddContactDetail");

                Utilities.CheckNullArgumentException(() => { unit.RemoveEmployee(null); }, "employee", "RemoveEmployee");
                Utilities.CheckNullArgumentException(() => { unit.RemoveDepartment(null); }, "department", "RemoveDepartment");
                Utilities.CheckNullArgumentException(() => { unit.RemoveContactDetail(null, new Phone()); }, "employee", "RemoveContactDetail");
                Utilities.CheckNullArgumentException(() => { unit.RemoveContactDetail(new Employee(), null); }, "detail", "RemoveContactDetail");
            }
        }
Пример #48
0
        public void RemoveContactDetail()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork unit = new UnitOfWork(ctx);

                Employee emp = new Employee();
                ContactDetail detail = new Phone();
                unit.AddEmployee(emp);
                unit.AddContactDetail(emp, detail);

                unit.RemoveContactDetail(emp, detail);
                Assert.IsFalse(ctx.ContactDetails.Contains(detail), "ContactDetail was not removed from underlying context.");
                Assert.IsFalse(
                    emp.ContactDetails.Contains(detail),
                    "ContactDetail is still in collection on Employee after being removed via Unit of Work.");
            }
        }
Пример #49
0
        public void RemoveEmployee()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork unit = new UnitOfWork(ctx);
                Employee emp = new Employee();
                unit.AddEmployee(emp);

                unit.RemoveEmployee(emp);
                Assert.IsFalse(ctx.Employees.Contains(emp), "Employee was not removed from underlying context.");
            }
        }
Пример #50
0
        public void RemoveEmployeeOutsideUnitOfWork()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork unit = new UnitOfWork(ctx);

                try
                {
                    unit.RemoveEmployee(new Employee());
                    Assert.Fail("Removing an Employee that was not added to Unit of Work did not throw.");
                }
                catch (InvalidOperationException ex)
                {
                    Assert.AreEqual("The supplied Employee is not part of this Unit of Work.", ex.Message);
                }
            }
        }
Пример #51
0
        public void RemoveEmployeeWithReports()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork unit = new UnitOfWork(ctx);
                Employee emp = new Employee();
                Employee man = new Employee();
                unit.AddEmployee(emp);
                unit.AddEmployee(man);
                emp.Manager = man;

                unit.RemoveEmployee(man);
                Assert.IsFalse(ctx.Employees.Contains(man), "Employee was not removed from underlying context.");
                Assert.AreEqual(0, man.Reports.Count, "Employee was not removed from managers reports.");
                Assert.IsNull(emp.Manager, "Manager property on Employee was not cleared.");
            }
        }
Пример #52
0
        public void IsObjectTracked()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                Employee e = new Employee();
                Assert.IsFalse(ctx.IsObjectTracked(e), "IsObjectTracked should be false when entity is not in added.");
                ctx.Employees.AddObject(e);
                Assert.IsTrue(ctx.IsObjectTracked(e), "IsObjectTracked should be true when entity is added.");

                Department d = new Department();
                Assert.IsFalse(ctx.IsObjectTracked(d), "IsObjectTracked should be false when entity is not in added.");
                ctx.Departments.AddObject(d);
                Assert.IsTrue(ctx.IsObjectTracked(d), "IsObjectTracked should be true when entity is added.");

                ContactDetail c = new Phone();
                Assert.IsFalse(ctx.IsObjectTracked(c), "IsObjectTracked should be false when entity is not in added.");
                ctx.ContactDetails.AddObject(c);
                Assert.IsTrue(ctx.IsObjectTracked(c), "IsObjectTracked should be true when entity is added.");
            }
        }
Пример #53
0
        public void AddDepartment()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork unit = new UnitOfWork(ctx);

                Department dep = new Department();
                unit.AddDepartment(dep);
                Assert.IsTrue(ctx.Departments.Contains(dep), "Department was not added to underlying context.");
            }
        }
Пример #54
0
 public void NullArgumentChecks()
 {
     using (FakeEmployeeContext ctx = new FakeEmployeeContext())
     {
         Utilities.CheckNullArgumentException(() => { var c = new FakeEmployeeContext(null, new Department[0]); c.Dispose(); }, "employees", "ctor");
         Utilities.CheckNullArgumentException(() => { var c = new FakeEmployeeContext(new Employee[0], null); c.Dispose(); }, "departments", "ctor");
         Utilities.CheckNullArgumentException(() => { ctx.IsObjectTracked(null); }, "entity", "IsObjectTracked");
     }
 }
 /// <summary>
 /// Creates a ViewModel based on a fake context
 /// </summary>
 /// <param name="ctx">Context to base view model on</param>
 /// <returns>The new ViewModel</returns>
 private static EmployeeViewModel BuildViewModel(FakeEmployeeContext ctx)
 {
     return BuildViewModel(ctx, new UnitOfWork(ctx));
 }
Пример #56
0
 public void SaveCalled()
 {
     using (FakeEmployeeContext ctx = new FakeEmployeeContext())
     {
         bool called = false;
         ctx.SaveCalled += (sender, e) => { called = true; };
         ctx.Save();
         Assert.IsTrue(called, "Save did not raise SaveCalled event.");
     }
 }
        public void ReferencesGetAndSet()
        {
            // Scalar properties are inherited from BasicEmployeeViewModel and are already tested
            Department d1 = new Department();
            Department d2 = new Department();

            Employee e1 = new Employee();
            Employee e2 = new Employee();
            Employee employee = new Employee { Department = d1, Manager = e1 };
            employee.ContactDetails.Add(new Phone());
            employee.ContactDetails.Add(new Email());

            using (FakeEmployeeContext ctx = new FakeEmployeeContext(new Employee[] { e1, e2, employee }, new Department[] { d1, d2 }))
            {
                UnitOfWork unit = new UnitOfWork(ctx);

                DepartmentViewModel dvm1 = new DepartmentViewModel(d1);
                DepartmentViewModel dvm2 = new DepartmentViewModel(d2);
                ObservableCollection<DepartmentViewModel> departments = new ObservableCollection<DepartmentViewModel> { dvm1, dvm2 };

                ObservableCollection<EmployeeViewModel> employees = new ObservableCollection<EmployeeViewModel>();
                EmployeeViewModel evm1 = new EmployeeViewModel(e1, employees, departments, unit);
                EmployeeViewModel evm2 = new EmployeeViewModel(e2, employees, departments, unit);
                EmployeeViewModel employeeViewModel = new EmployeeViewModel(employee, employees, departments, unit);
                employees.Add(evm1);
                employees.Add(evm2);
                employees.Add(employeeViewModel);

                // Test initial references are surfaced in ViewModel
                Assert.AreEqual(evm1, employeeViewModel.Manager, "ViewModel did not return ViewModel representing current manager.");
                Assert.AreEqual(e1, employeeViewModel.Manager.Model, "ViewModel did not return ViewModel representing current manager.");
                Assert.AreEqual(dvm1, employeeViewModel.Department, "ViewModel did not return ViewModel representing current department.");
                Assert.AreEqual(d1, employeeViewModel.Department.Model, "ViewModel did not return ViewModel representing current department.");
                Assert.AreEqual(2, employeeViewModel.ContactDetails.Count, "Contact details have not been populated on ViewModel.");

                // Test changing properties updates Model and raises PropertyChanged
                string lastProperty;
                employeeViewModel.PropertyChanged += (sender, e) => { lastProperty = e.PropertyName; };

                lastProperty = null;
                employeeViewModel.Department = dvm2;
                Assert.AreEqual("Department", lastProperty, "Setting Department property did not raise correct PropertyChanged event.");
                Assert.AreEqual(d2, employee.Department, "Setting Department property in ViewModel is not reflected in Model.");

                lastProperty = null;
                employeeViewModel.Manager = evm2;
                Assert.AreEqual("Manager", lastProperty, "Setting Manager property did not raise correct PropertyChanged event.");
                Assert.AreEqual(e2, employee.Manager, "Setting Manager property in ViewModel is not reflected in Model.");

                // Test ViewModel returns current value from model
                employee.Manager = e1;
                Assert.AreEqual(evm1, employeeViewModel.Manager, "ViewModel did not return correct manager when model was updated outside of ViewModel.");
                employee.Department = d1;
                Assert.AreEqual(dvm1, employeeViewModel.Department, "ViewModel did not return correct department when model was updated outside of ViewModel.");

                // Test ViewModel returns current value from model when set to null
                employee.Manager = null;
                Assert.AreEqual(null, employeeViewModel.Manager, "ViewModel did not return correct manager when model was updated outside of ViewModel.");
                employee.Department = null;
                Assert.AreEqual(null, employeeViewModel.Department, "ViewModel did not return correct department when model was updated outside of ViewModel.");
            }
        }
Пример #58
0
        public void RemoveDepartment()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork unit = new UnitOfWork(ctx);
                Department dep = new Department();
                unit.AddDepartment(dep);

                unit.RemoveDepartment(dep);
                Assert.IsFalse(ctx.Departments.Contains(dep), "Department was not removed from underlying context.");
            }
        }
        /// <summary>
        /// Creates a ViewModel based on a fake context using an existing unit of work
        /// </summary>
        /// <param name="ctx">Context to base view model on</param>
        /// <param name="unit">Current unit of work</param>
        /// <returns>The new ViewModel</returns>
        private static EmployeeViewModel BuildViewModel(FakeEmployeeContext ctx, UnitOfWork unit)
        {
            ObservableCollection<DepartmentViewModel> departments = new ObservableCollection<DepartmentViewModel>(ctx.Departments.Select(d => new DepartmentViewModel(d)));
            ObservableCollection<EmployeeViewModel> employees = new ObservableCollection<EmployeeViewModel>();
            foreach (var e in ctx.Employees)
            {
                employees.Add(new EmployeeViewModel(e, employees, departments, unit));
            }

            return employees[0];
        }
Пример #60
0
        public void RemoveDepartmentWithEmployees()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork unit = new UnitOfWork(ctx);
                Department dep = new Department();
                Employee emp = new Employee();
                unit.AddDepartment(dep);
                unit.AddEmployee(emp);
                emp.Department = dep;

                unit.RemoveDepartment(dep);
                Assert.IsFalse(ctx.Departments.Contains(dep), "Department was not removed from underlying context.");
                Assert.IsNull(emp.Department, "Employee.Department property has not been nulled when deleting department.");
                Assert.IsNull(emp.DepartmentId, "Employee.DepartmentId property has not been nulled when deleting department.");
                Assert.AreEqual(0, dep.Employees.Count, "Department.Employees collection was not cleared when deleting department.");
            }
        }