public void Add_WithRequiredSubEntitiesAndRequiredUsedEntities_Succeeds()
        {
            //Arrange
            var repository = new EFRepositoryTester();
            repository.Initialize();

            var addingDate = DateTime.UtcNow;

            var manager = new Manager()
            {
                Director = director,
                ManagerTrackers = new List<ManagerTracker>()
                {
                    new ManagerTracker()
                },
                Office = office1,
                Computer = new Computer() { Data = "data", Admin = admin },
                Data = "data"
            };

            //Act
            repository.Add(new AddOperationParameters<Manager>()
            {
                Entity = manager,
                OperationDateTime = addingDate
            });

            //Assert
            var context = new EFContext();

            var managers = context.Managers.ToArray();
            var computers = context.Computers.ToArray();

            AssertUsingEntities();

            AssertSecondLevelUsedEntities();

            //Sub entities
            AssertComputers(addingDate, addingDate, "data", false);

            //Main entity
            Assert.AreEqual(1, managers.Length);
            Assert.AreEqual("data", managers[0].Data);
            Assert.AreEqual(addingDate, managers[0].CreatedOn);
            Assert.AreEqual(addingDate, managers[0].LastUpdateOn);
            Assert.AreEqual(null, managers[0].CarId);
            Assert.AreEqual(computers[0].Id, managers[0].ComputerId);
            Assert.AreEqual(null, managers[0].DescriptionId);
            Assert.AreEqual(null, managers[0].DirectorId);
            Assert.AreEqual(office1.Id, managers[0].OfficeId);

            //First level used entities
            AssertOffices();
        }
        public void Delete_WithAllSubEntitiesAndAllUsedEntities_Succeeds()
        {
            //Arrange
            var repository = new EFRepositoryTester();
            repository.Initialize();

            var addingDate = DateTime.UtcNow;

            var manager = new Manager()
            {
                Director = director,
                ManagerTrackers = new List<ManagerTracker>()
                {
                    new ManagerTracker()
                },
                Office = office1,
                Computer = new Computer() { Data = "data", Admin = admin },
                Things = new List<Thing>()
                {
                    new Thing()
                    {
                        Id = 1,
                        Admin = admin,
                        Data = "data"
                    },
                    new Thing()
                    {
                        Id = 2,
                        Admin = admin,
                        Data = "data"
                    }
                },
                Description = new Description()
                {
                    Data = "data"
                },
                Data = "data",
                Car = car1,
                Projects = new List<Project>()
                {
                    project1,
                    project2
                }
            };

            repository.Add(new AddOperationParameters<Manager>()
            {
                Entity = manager,
                OperationDateTime = addingDate
            });

            //Act
            director.Data = "data100";
            office1.Data = "data100";
            admin.Data = "data100";
            car1.Data = "data100";
            project1.Data = "data100";
            project2.Data = "data100";

            var deletingDate = DateTime.UtcNow;

            repository.Delete(new DeleteOperationParameters()
            {
                PrimaryKeys = new object[]
                {
                    manager.Id
                },
                OperationDateTime = deletingDate
            });

            //Assert
            var context = new EFContext();

            var managers = context.Managers.ToArray();
            var things = context.Things.ToArray();

            AssertUsingEntities();
            AssertSecondLevelUsedEntities();

            //Sub entities
            AssertComputers(addingDate, deletingDate, "data", true);

            AssertDescriptions(addingDate, deletingDate, "data", true);

            Assert.AreEqual(0, things.Length);

            //Main entity
            Assert.AreEqual(0, managers.Length);

            //First level used entities
            AssertOffices();

            AssertCars();

            AssertProjects(null, null);
        }
        public void Add_WithAllSubEntitiesAndAllUsedEntities_Succeeds()
        {
            //Arrange
            var repository = new EFRepositoryTester();
            repository.Initialize();

            var addingDate = DateTime.UtcNow;

            var manager = new Manager()
            {
                Director = director,
                ManagerTrackers = new List<ManagerTracker>()
                {
                    new ManagerTracker()
                },
                Office = office1,
                Computer = new Computer() { Data = "data", Admin = admin },
                Things = new List<Thing>()
                {
                    new Thing()
                    {
                        Id = 1,
                        Admin = admin,
                        Data = "data"
                    },
                    new Thing()
                    {
                        Id = 2,
                        Admin = admin,
                        Data = "data"
                    }
                },
                Description = new Description()
                {
                    Data = "data"
                },
                Data = "data",
                Car = car1,
                Projects = new List<Project>()
                {
                    project1,
                    project2
                }
            };

            //Act
            repository.Add(new AddOperationParameters<Manager>()
            {
                Entity = manager,
                OperationDateTime = addingDate
            });

            //Assert
            var context = new EFContext();

            var managers = context.Managers.ToArray();
            var computers = context.Computers.ToArray();
            var things = context.Things.ToArray();
            var descriptions = context.Descriptions.ToArray();

            AssertUsingEntities();
            AssertSecondLevelUsedEntities();

            //Sub entities
            AssertComputers(addingDate, addingDate, "data", false);

            AssertDescriptions(addingDate, addingDate, "data", false);

            Assert.AreEqual(2, things.Length);

            AssertThing(things[0], addingDate, addingDate, "data", managers[0].Id);

            AssertThing(things[1], addingDate, addingDate, "data", managers[0].Id);

            //Main entity
            Assert.AreEqual(1, managers.Length);
            Assert.AreEqual("data", managers[0].Data);
            Assert.AreEqual(addingDate, managers[0].CreatedOn);
            Assert.AreEqual(addingDate, managers[0].LastUpdateOn);
            Assert.AreEqual(car1.Id, managers[0].CarId);
            Assert.AreEqual(computers[0].Id, managers[0].ComputerId);
            Assert.AreEqual(descriptions[0].Id, managers[0].DescriptionId);
            Assert.AreEqual(null, managers[0].DirectorId);
            Assert.AreEqual(office1.Id, managers[0].OfficeId);

            //First level used entities
            AssertOffices();

            AssertCars();

            AssertProjects(manager.Id, manager.Id);
        }
        private void Add10Managers()
        {
            var repository = new EFRepositoryTester();
            repository.Initialize();

            var addingDate = DateTime.UtcNow;

            var manager = new Manager()
            {
                Director = director,
                ManagerTrackers = new List<ManagerTracker>()
                {
                    new ManagerTracker()
                },
                Office = office1,
                Computer = new Computer() { Data = "data", Admin = admin },
                Things = new List<Thing>()
                {
                    new Thing()
                    {
                        Id = 1,
                        Admin = admin,
                        Data = "data"
                    },
                    new Thing()
                    {
                        Id = 2,
                        Admin = admin,
                        Data = "data"
                    }
                },
                Description = new Description()
                {
                    Data = "data"
                },
                Data = "data",
                Car = car1,
                Projects = new List<Project>()
                {
                    project1,
                    project2
                }
            };

            for (int i = 0; i < 10; i++)
            {
                manager.Data = "data" + i;

                repository.Add(new AddOperationParameters<Manager>()
                {
                    Entity = manager,
                    OwnerId = i / 3L,
                    OperationDateTime = addingDate
                });
            }
        }
        public void Delete_WithRequiredSubEntitiesAndRequiredUsedEntities_Succeeds()
        {
            //Arrange
            var repository = new EFRepositoryTester();
            repository.Initialize();

            var addingDate = DateTime.UtcNow;

            var manager = new Manager()
            {
                Director = director,
                ManagerTrackers = new List<ManagerTracker>()
                {
                    new ManagerTracker()
                },
                Office = office1,
                Computer = new Computer() { Data = "data", Admin = admin },
                Data = "data"
            };

            repository.Add(new AddOperationParameters<Manager>()
            {
                Entity = manager,
                OperationDateTime = addingDate
            });

            //Act
            director.Data = "data100";
            office1.Data = "data100";
            admin.Data = "data100";

            var deletingDate = DateTime.UtcNow;

            repository.Delete(new DeleteOperationParameters()
            {
                PrimaryKeys = new object[]
                {
                    manager.Id
                },
                OperationDateTime = deletingDate
            });

            //Assert
            var context = new EFContext();

            var managers = context.Managers.ToArray();
            var computers = context.Computers.ToArray();

            AssertUsingEntities();
            AssertSecondLevelUsedEntities();

            //Sub entities
            AssertComputers(addingDate, deletingDate, "data", true);

            //Main entity
            Assert.AreEqual(0, managers.Length);

            //First level used entities
            AssertOffices();
        }
        public void Get_WithUsingEntities_IsUsedIsTrue()
        {
            //Arrange
            var repository = new EFRepositoryTester();
            repository.Initialize();

            var manager = new Manager()
            {
                Director = director,
                ManagerTrackers = new List<ManagerTracker>()
                {
                    new ManagerTracker()
                },
                Office = office1,
                Computer = new Computer() { Data = "data", Admin = admin },
                Things = new List<Thing>()
                {
                    new Thing()
                    {
                        Id = 1,
                        Admin = admin,
                        Data = "data"
                    },
                    new Thing()
                    {
                        Id = 2,
                        Admin = admin,
                        Data = "data"
                    }
                },
                Description = new Description()
                {
                    Data = "data"
                },
                Data = "data",
                Car = car1,
                Projects = new List<Project>()
                {
                    project1,
                    //project2
                }
            };

            repository.Add(new AddOperationParameters<Manager>()
            {
                Entity = manager,
                OperationDateTime = initializingDate
            });

            //Attach using entities
            var context = new EFContext();

            context.Directors.Attach(director);
            context.Managers.Attach(manager);
            director.Managers = new List<Manager>();
            director.Managers.Add(manager);

            context.ManagerTrackers.Add(new ManagerTracker()
            {
                ManagerId = manager.Id,
                Data = "data",
                CreatedOn = initializingDate,
                LastUpdateOn = initializingDate
            });

            context.SaveChanges();

            //Act
            var entities = repository.Get(new GetOperationParameters()
            {
                LoadSubEntities = true,
                LoadUsedEntities = true,
                LoadUsingEntities = true,
            });

            //Assert
            Assert.AreEqual(true, entities[0].IsUsed);
        }
        public void Get_WithAllRelatedEntities_ReturnsEntities()
        {
            //Arrange
            var repository = new EFRepositoryTester();
            repository.Initialize();

            var manager = new Manager()
            {
                Director = director,
                ManagerTrackers = new List<ManagerTracker>()
                {
                    new ManagerTracker()
                },
                Office = office1,
                Computer = new Computer() { Data = "data", Admin = admin },
                Things = new List<Thing>()
                {
                    new Thing()
                    {
                        Id = 1,
                        Admin = admin,
                        Data = "data"
                    },
                    new Thing()
                    {
                        Id = 2,
                        Admin = admin,
                        Data = "data"
                    }
                },
                Description = new Description()
                {
                    Data = "data"
                },
                Data = "data",
                Car = car1,
                Projects = new List<Project>()
                {
                    project1,
                    //project2
                }
            };

            repository.Add(new AddOperationParameters<Manager>()
            {
                Entity = manager,
                OperationDateTime = initializingDate
            });

            //Attach using entities
            var context = new EFContext();

            context.Directors.Attach(director);
            context.Managers.Attach(manager);
            director.Managers = new List<Manager>();
            director.Managers.Add(manager);

            context.ManagerTrackers.Add(new ManagerTracker()
            {
                ManagerId = manager.Id,
                Data = "data",
                CreatedOn = initializingDate,
                LastUpdateOn = initializingDate
            });

            context.SaveChanges();

            //Act
            var entities = repository.Get(new GetOperationParameters()
            {
                LoadSubEntities = true,
                LoadUsedEntities = true,
                LoadUsingEntities = true,
            });

            //Assert
            Assert.AreEqual(true, entities[0].IsUsed);

            AssertUsingEntities();

            AssertSecondLevelUsedEntities();

            //Sub entities
            Assert.AreEqual(1, entities.Length);
            Assert.AreEqual(initializingDate, entities[0].Computer.CreatedOn);
            Assert.AreEqual(initializingDate, entities[0].Computer.LastUpdateOn);
            Assert.AreEqual("data", entities[0].Computer.Data);
            Assert.AreEqual(admin.Id, entities[0].Computer.AdminId);

            Assert.AreEqual(initializingDate, entities[0].Description.CreatedOn);
            Assert.AreEqual(initializingDate, entities[0].Description.LastUpdateOn);
            Assert.AreEqual("data", entities[0].Description.Data);

            Assert.AreEqual(initializingDate, entities[0].Things[0].CreatedOn);
            Assert.AreEqual(initializingDate, entities[0].Things[0].LastUpdateOn);
            Assert.AreEqual("data", entities[0].Things[0].Data);
            Assert.AreEqual(admin.Id, entities[0].Things[0].AdminId);
            Assert.AreEqual(entities[0].Id, entities[0].Things[0].ManagerId);

            Assert.AreEqual(initializingDate, entities[0].Things[1].CreatedOn);
            Assert.AreEqual(initializingDate, entities[0].Things[1].LastUpdateOn);
            Assert.AreEqual("data", entities[0].Things[1].Data);
            Assert.AreEqual(admin.Id, entities[0].Things[1].AdminId);
            Assert.AreEqual(entities[0].Id, entities[0].Things[1].ManagerId);

            //Main entity
            Assert.AreEqual("data", entities[0].Data);
            Assert.AreEqual(initializingDate, entities[0].CreatedOn);
            Assert.AreEqual(initializingDate, entities[0].LastUpdateOn);
            Assert.AreEqual(car1.Id, entities[0].CarId);
            Assert.AreEqual(director.Id, entities[0].DirectorId);
            Assert.AreEqual(office1.Id, entities[0].OfficeId);

            //First level used entities
            Assert.AreEqual(office1.Id, entities[0].Office.Id);
            Assert.AreEqual(initializingDate, entities[0].Office.CreatedOn);
            Assert.AreEqual(initializingDate, entities[0].Office.LastUpdateOn);
            Assert.AreEqual("data", entities[0].Office.Data);

            Assert.AreEqual(admin.Id, entities[0].Car.AdminId);
            Assert.AreEqual(car1.Id, entities[0].Car.Id);
            Assert.AreEqual(initializingDate, entities[0].Car.CreatedOn);
            Assert.AreEqual(initializingDate, entities[0].Car.LastUpdateOn);
            Assert.AreEqual("data", entities[0].Car.Data);

            Assert.AreEqual(admin.Id, entities[0].Projects[0].AdminId);
            Assert.AreEqual(project1.Id, entities[0].Projects[0].Id);
            Assert.AreEqual(initializingDate, entities[0].Projects[0].CreatedOn);
            Assert.AreEqual(initializingDate, entities[0].Projects[0].LastUpdateOn);
            Assert.AreEqual("data", entities[0].Projects[0].Data);
            Assert.AreEqual(manager.Id, entities[0].Projects[0].ManagerId);

            //Assert.AreEqual(admin.Id, entities[0].Projects[1].AdminId);
            //Assert.AreEqual(project2.Id, entities[0].Projects[1].Id);
            //Assert.AreEqual(initializingDate, entities[0].Projects[1].CreatedOn);
            //Assert.AreEqual(initializingDate, entities[0].Projects[1].LastUpdateOn);
            //Assert.AreEqual("data", entities[0].Projects[1].Data);
            //Assert.AreEqual(false, entities[0].Projects[1].IsDeleted);
            //Assert.AreEqual(manager.Id, entities[0].Projects[1].ManagerId);

            //Using entities
            Assert.AreEqual(initializingDate, entities[0].ManagerTrackers[0].CreatedOn);
            Assert.AreEqual("data", entities[0].ManagerTrackers[0].Data);
            Assert.AreEqual(initializingDate, entities[0].ManagerTrackers[0].LastUpdateOn);
            Assert.AreEqual(manager.Id, entities[0].ManagerTrackers[0].ManagerId);

            //Get result
        }
        public void Get_WhenNoUsingEntities_IsUsedIsFalse()
        {
            //Arrange
            var repository = new EFRepositoryTester();
            repository.Initialize();

            var manager = new Manager()
            {
                Director = director,
                ManagerTrackers = new List<ManagerTracker>()
                {
                    new ManagerTracker()
                },
                Office = office1,
                Computer = new Computer() { Data = "data", Admin = admin },
                Things = new List<Thing>()
                {
                    new Thing()
                    {
                        Id = 1,
                        Admin = admin,
                        Data = "data"
                    },
                    new Thing()
                    {
                        Id = 2,
                        Admin = admin,
                        Data = "data"
                    }
                },
                Description = new Description()
                {
                    Data = "data"
                },
                Data = "data",
                Car = car1,
                Projects = new List<Project>()
                {
                    project1,
                    //project2
                }
            };

            repository.Add(new AddOperationParameters<Manager>()
            {
                Entity = manager,
                OperationDateTime = initializingDate
            });

            //Act
            var entities = repository.Get(new GetOperationParameters()
            {
                LoadSubEntities = true,
                LoadUsedEntities = true,
                LoadUsingEntities = true,
            });

            //Assert
            Assert.AreEqual(false, entities[0].IsUsed);
        }
        public void GetTotalCount_EntitiesExist_ReturnsEntitiesCount()
        {
            //Arrange
            var repository = new EFRepositoryTester();
            repository.Initialize();

            var addingDate = DateTime.UtcNow;

            var manager1 = new Manager()
            {
                Director = director,
                ManagerTrackers = new List<ManagerTracker>()
                {
                    new ManagerTracker()
                },
                Office = office1,
                Computer = new Computer() { Data = "data", Admin = admin },
                Things = new List<Thing>()
                {
                    new Thing()
                    {
                        Id = 1,
                        Admin = admin,
                        Data = "data"
                    },
                    new Thing()
                    {
                        Id = 2,
                        Admin = admin,
                        Data = "data"
                    }
                },
                Description = new Description()
                {
                    Data = "data"
                },
                Data = "data",
                Car = car1,
                Projects = new List<Project>()
                {
                    project1
                }
            };

            var manager2 = new Manager()
            {
                Director = director,
                ManagerTrackers = new List<ManagerTracker>()
                {
                    new ManagerTracker()
                },
                Office = office2,
                Computer = new Computer() { Data = "data", Admin = admin },
                Things = new List<Thing>()
                {
                    new Thing()
                    {
                        Id = 1,
                        Admin = admin,
                        Data = "data"
                    },
                    new Thing()
                    {
                        Id = 2,
                        Admin = admin,
                        Data = "data"
                    }
                },
                Description = new Description()
                {
                    Data = "data"
                },
                Data = "data",
                Car = car2,
                Projects = new List<Project>()
                {
                    project2
                }
            };

            repository.Add(new AddOperationParameters<Manager>()
            {
                Entity = manager1,
                OperationDateTime = addingDate
            });

            repository.Add(new AddOperationParameters<Manager>()
            {
                Entity = manager2,
                OperationDateTime = addingDate
            });

            repository.Delete(new DeleteOperationParameters()
            {
                OperationDateTime = DateTime.UtcNow,
                PrimaryKeys = new object[] { manager2.Id }
            });

            //Act
            var result = repository.GetTotalCount(new OperationParameters());

            //Assert
            Assert.AreEqual(1, result);
        }
        public void Update_WithAllSubEntitiesAndRequiredUsedEntities_UpdateAllEntities_Succeeds()
        {
            //Arrange
            var repository = new EFRepositoryTester();
            repository.Initialize();

            var addingDate = DateTime.UtcNow;

            var manager = new Manager()
            {
                Director = director,
                ManagerTrackers = new List<ManagerTracker>()
                {
                    new ManagerTracker()
                },
                Office = office1,
                Computer = new Computer() { Data = "data", Admin = admin },
                Things = new List<Thing>()
                {
                    new Thing()
                    {
                        Id = 1,
                        Admin = admin,
                        Data = "data"
                    },
                    new Thing()
                    {
                        Id = 2,
                        Admin = admin,
                        Data = "data"
                    }
                },
                Description = new Description()
                {
                    Data = "data"
                },
                Data = "data"
            };

            repository.Add(new AddOperationParameters<Manager>()
            {
                Entity = manager,
                OperationDateTime = addingDate
            });

            //Act
            director.Data = "data100";
            office1.Data = "data100";
            office2.Data = "data100";
            admin.Data = "data100";
            car1.Data = "data100";

            var updatingDate = DateTime.UtcNow;

            manager.Data = "data1";
            manager.Computer.Data = "data1";
            manager.Description.Data = "data1";
            manager.Office = office2;
            manager.Things.RemoveAt(1);
            manager.Things[0].Data = "data1";
            manager.Things.Add(new Thing() { Admin = admin, Data = "data" });

            repository.Update(new UpdateOperationParameters<Manager>()
            {
                Entity = manager,
                OperationDateTime = updatingDate
            });

            //Assert
            var context = new EFContext();

            var managers = context.Managers.ToArray();
            var computers = context.Computers.ToArray();
            var things = context.Things.ToArray();
            var descriptions = context.Descriptions.ToArray();

            AssertUsingEntities();
            AssertSecondLevelUsedEntities();

            //Sub entities
            AssertComputers(addingDate, updatingDate, "data1", false);

            AssertDescriptions(addingDate, updatingDate, "data1", false);

            Assert.AreEqual(2, things.Length);

            AssertThing(things[0], addingDate, updatingDate, "data1", managers[0].Id);

            AssertThing(things[1], updatingDate, updatingDate, "data", managers[0].Id);

            //Main entity
            Assert.AreEqual(1, managers.Length);
            Assert.AreEqual("data1", managers[0].Data);
            Assert.AreEqual(addingDate, managers[0].CreatedOn);
            Assert.AreEqual(updatingDate, managers[0].LastUpdateOn);
            Assert.AreEqual(null, managers[0].CarId);
            Assert.AreEqual(computers[0].Id, managers[0].ComputerId);
            Assert.AreEqual(descriptions[0].Id, managers[0].DescriptionId);
            Assert.AreEqual(null, managers[0].DirectorId);
            Assert.AreEqual(office2.Id, managers[0].OfficeId);

            //First level used entities
            AssertOffices();
        }