public void TestHardDeleteCascadeSoftOfPreviousDeleteInfo() { //SETUP var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using (var context = new CascadeSoftDelDbContext(options)) { context.Database.EnsureCreated(); var ceo = Employee.SeedEmployeeSoftDel(context); var config = new ConfigCascadeDeleteWithUserId(context); var service = new CascadeSoftDelService <ICascadeSoftDelete>(config); var numSoftDeleted = service.SetCascadeSoftDelete(context.Employees.Single(x => x.Name == "CTO")).Result; numSoftDeleted.ShouldEqual(7 + 6); Employee.ShowHierarchical(ceo, x => _output.WriteLine(x), false); //ATTEMPT var status = service.HardDeleteSoftDeletedEntries(context.Employees.IgnoreQueryFilters().Single(x => x.Name == "CTO")); //VERIFY status.IsValid.ShouldBeTrue(status.GetAllErrors()); status.Result.ShouldEqual(7 + 6); status.Message.ShouldEqual("You have hard deleted an entity and its 12 dependents"); context.Employees.IgnoreQueryFilters().Count().ShouldEqual(4); } }
public void TestResetCascadeSoftDeletePartialOfPreviousDeleteDoesNothingOk() { //SETUP var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using (var context = new CascadeSoftDelDbContext(options)) { context.Database.EnsureCreated(); var ceo = Employee.SeedEmployeeSoftDel(context); var config = new ConfigCascadeDeleteWithUserId(context); var service = new CascadeSoftDelService <ICascadeSoftDelete>(config); var numSoftDeleted = service.SetCascadeSoftDelete(context.Employees.Single(x => x.Name == "CTO")).Result; numSoftDeleted.ShouldEqual(7 + 6); Employee.ShowHierarchical(ceo, x => _output.WriteLine(x), false); //ATTEMPT var status = service.ResetCascadeSoftDelete(context.Employees.IgnoreQueryFilters().Single(x => x.Name == "ProjectManager1")); //VERIFY Employee.ShowHierarchical(ceo, x => _output.WriteLine(x), false); status.IsValid.ShouldBeFalse(); status.GetAllErrors().ShouldEqual("This entry was soft deleted 1 level above here"); status.Result.ShouldEqual(0); } }
public void TestHardDeleteCascadeDeleteCompanySomeQuotesDifferentUserIdOk() { //SETUP var userId = Guid.NewGuid(); var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using (var context = new CascadeSoftDelDbContext(options, userId)) { context.Database.EnsureCreated(); var company = Company.SeedCompanyWithQuotes(context, userId); Company.SeedCompanyWithQuotes(context, Guid.Empty, "Other company"); context.SaveChanges(); var config = new ConfigCascadeDeleteWithUserId(context); var service = new CascadeSoftDelService <ICascadeSoftDelete>(config); service.SetCascadeSoftDelete(company).Result.ShouldEqual(1 + 4 + 4); //ATTEMPT var status = service.HardDeleteSoftDeletedEntries(company); //VERIFY status.IsValid.ShouldBeTrue(status.GetAllErrors()); status.Result.ShouldEqual(1 + 4 + 4); status.Message.ShouldEqual("You have hard deleted an entity and its 8 dependents"); context.Quotes.Count().ShouldEqual(0); context.Quotes.IgnoreQueryFilters().Count().ShouldEqual(4); } }
public void TestResetCascadeSoftOfPreviousDeleteNoCalSaveChangesOk() { //SETUP var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using (var context = new CascadeSoftDelDbContext(options)) { context.Database.EnsureCreated(); var ceo = Employee.SeedEmployeeSoftDel(context); var config = new ConfigCascadeDeleteWithUserId(context); var service = new CascadeSoftDelService <ICascadeSoftDelete>(config); var numSoftDeleted = service.SetCascadeSoftDelete(context.Employees.Single(x => x.Name == "CTO")).Result; numSoftDeleted.ShouldEqual(7 + 6); Employee.ShowHierarchical(ceo, x => _output.WriteLine(x), false); //ATTEMPT var status = service.ResetCascadeSoftDelete(context.Employees.IgnoreQueryFilters().Single(x => x.Name == "CTO"), false); context.Employees.Count().ShouldEqual(11 - 7); context.Contracts.Count().ShouldEqual(9 - 6); context.SaveChanges(); context.Employees.Count().ShouldEqual(11); context.Contracts.Count().ShouldEqual(9); //VERIFY status.IsValid.ShouldBeTrue(status.GetAllErrors()); status.Result.ShouldEqual(7 + 6); } }
public void TestResetCascadeDeleteCompanySomeQuotesDifferentUserIdOk() { //SETUP var userId = Guid.NewGuid(); var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using (var context = new CascadeSoftDelDbContext(options, userId)) { context.Database.EnsureCreated(); var customer = Customer.SeedCustomerWithQuotes(context, userId); customer.Quotes.First().UserId = Guid.NewGuid(); customer.Quotes.First().SoftDeleteLevel = 1; //Set to deleted context.SaveChanges(); var config = new ConfigCascadeDeleteWithUserId(context); var service = new CascadeSoftDelService <ICascadeSoftDelete>(config); service.SetCascadeSoftDelete(customer).Result.ShouldEqual(1 + 3 + 3 + (3 * 4)); } using (var context = new CascadeSoftDelDbContext(options, userId)) { var config = new ConfigCascadeDeleteWithUserId(context); var service = new CascadeSoftDelService <ICascadeSoftDelete>(config); //ATTEMPT var status = service.ResetCascadeSoftDelete(context.Companies.IgnoreQueryFilters().Single()); //VERIFY status.IsValid.ShouldBeTrue(status.GetAllErrors()); status.Result.ShouldEqual(1 + 3 + 3 + (3 * 4)); status.Message.ShouldEqual("You have recovered an entity and its 18 dependents"); context.Quotes.Count().ShouldEqual(3); } }
public void TestDisconnectedResetCascadeSoftDeleteEmployeeSoftDelOk() { //SETUP var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using (var context = new CascadeSoftDelDbContext(options)) { context.Database.EnsureCreated(); var ceo = Employee.SeedEmployeeSoftDel(context); var config = new ConfigCascadeDeleteWithUserId(context); var service = new CascadeSoftDelService <ICascadeSoftDelete>(config); var numSoftDeleted = service.SetCascadeSoftDelete(context.Employees.Single(x => x.Name == "CTO")).Result; numSoftDeleted.ShouldEqual(7 + 6); } using (var context = new CascadeSoftDelDbContext(options)) { var config = new ConfigCascadeDeleteWithUserId(context); var service = new CascadeSoftDelService <ICascadeSoftDelete>(config); //ATTEMPT var status = service.ResetCascadeSoftDelete(context.Employees.IgnoreQueryFilters().Single(x => x.Name == "CTO")); //VERIFY status.IsValid.ShouldBeTrue(status.GetAllErrors()); status.Result.ShouldEqual(7 + 6); context.Employees.Count().ShouldEqual(11); context.Contracts.Count().ShouldEqual(9); } }
public void TestResetCascadeSoftDeleteTwoLevelSoftDeleteThenResetTopOk() { //SETUP var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using (var context = new CascadeSoftDelDbContext(options)) { context.Database.EnsureCreated(); var ceo = Employee.SeedEmployeeSoftDel(context); var config = new ConfigCascadeDeleteWithUserId(context); var service = new CascadeSoftDelService <ICascadeSoftDelete>(config); var numInnerSoftDelete = service.SetCascadeSoftDelete(context.Employees.Single(x => x.Name == "ProjectManager1")).Result; numInnerSoftDelete.ShouldEqual(3 + 3); var numOuterSoftDelete = service.SetCascadeSoftDelete(context.Employees.Single(x => x.Name == "CTO")).Result; numOuterSoftDelete.ShouldEqual(4 + 3); Employee.ShowHierarchical(ceo, x => _output.WriteLine(x), false); //ATTEMPT var status = service.ResetCascadeSoftDelete(context.Employees.IgnoreQueryFilters().Single(x => x.Name == "CTO")); //VERIFY Employee.ShowHierarchical(ceo, x => _output.WriteLine(x), false); status.IsValid.ShouldBeTrue(status.GetAllErrors()); status.Result.ShouldEqual(4 + 3); var cto = context.Employees.Include(x => x.WorksFromMe).Single(x => x.Name == "CTO"); cto.WorksFromMe.Single(x => x.SoftDeleteLevel == 0).Name.ShouldEqual("ProjectManager2"); } }
public void TestCascadeSoftDeleteEmployeeSoftDelOneToOneOk() { //SETUP var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using (var context = new CascadeSoftDelDbContext(options)) { context.Database.EnsureCreated(); var ceo = Employee.SeedEmployeeSoftDel(context); var config = new ConfigCascadeDeleteWithUserId(context); var service = new CascadeSoftDelService <ICascadeSoftDelete>(config); //ATTEMPT var ex = Assert.Throws <InvalidOperationException>(() => service.SetCascadeSoftDelete(ceo.WorksFromMe.First().Contract)); //VERIFY ex.Message.ShouldEqual("You cannot soft delete a one-to-one relationship. It causes problems if you try to create a new version."); } }
public void TestGetSoftDeletedEntriesEmployeeSoftDeletedOk() { //SETUP var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using (var context = new CascadeSoftDelDbContext(options)) { context.Database.EnsureCreated(); var ceo = Employee.SeedEmployeeSoftDel(context); var config = new ConfigCascadeDeleteWithUserId(context); var service = new CascadeSoftDelService <ICascadeSoftDelete>(config); service.SetCascadeSoftDelete(ceo.WorksFromMe.First()).IsValid.ShouldBeTrue(); //ATTEMPT var softDeleted = service.GetSoftDeletedEntries <Employee>().ToList(); //VERIFY softDeleted.Count.ShouldEqual(1); softDeleted.Single().Name.ShouldEqual(ceo.WorksFromMe.First().Name); } }
public async Task TestCheckCascadeSoftDeleteNoSoftDeleteInfo() { //SETUP var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using (var context = new CascadeSoftDelDbContext(options)) { context.Database.EnsureCreated(); var ceo = Employee.SeedEmployeeSoftDel(context); var config = new ConfigCascadeDeleteWithUserId(context); var service = new CascadeSoftDelServiceAsync <ICascadeSoftDelete>(config); //ATTEMPT var status = await service.CheckCascadeSoftDeleteAsync(context.Employees.IgnoreQueryFilters().Single(x => x.Name == "ProjectManager1")); //VERIFY status.IsValid.ShouldBeFalse(); status.Result.ShouldEqual(0); status.GetAllErrors().ShouldEqual("This entry isn't soft deleted."); } }
public async Task TestCascadeSoftDeleteEmployeeSoftDelWithLoggingOk(bool readEveryTime, int selectCount) { //SETUP var logs = new List <string>(); var options = SqliteInMemory.CreateOptionsWithLogTo <CascadeSoftDelDbContext>(log => logs.Add(log)); using (var context = new CascadeSoftDelDbContext(options)) { context.Database.EnsureCreated(); var ceo = Employee.SeedEmployeeSoftDel(context); var config = new ConfigCascadeDeleteWithUserId(context) { ReadEveryTime = readEveryTime }; var service = new CascadeSoftDelServiceAsync <ICascadeSoftDelete>(config); //ATTEMPT logs.Clear(); var status = await service.SetCascadeSoftDeleteAsync(ceo.WorksFromMe.First()); //VERIFY status.IsValid.ShouldBeTrue(status.GetAllErrors()); logs.Count(x => _selectMatchRegex.IsMatch(x)).ShouldEqual(selectCount); status.Result.ShouldEqual(7 + 6); context.ChangeTracker.Clear(); context.Employees.Count().ShouldEqual(4); context.Employees.IgnoreQueryFilters().Count().ShouldEqual(11); context.Employees.IgnoreQueryFilters().Select(x => x.SoftDeleteLevel).Where(x => x > 0).ToArray() .ShouldEqual(new byte[] { 1, 2, 2, 3, 3, 3, 3 }); context.Contracts.Count().ShouldEqual(3); context.Contracts.IgnoreQueryFilters().Count().ShouldEqual(9); context.Employees.IgnoreQueryFilters().Select(x => x.Contract).Where(x => x != null) .Select(x => x.SoftDeleteLevel).Where(x => x > 0).ToArray() .ShouldEqual(new byte[] { 2, 3, 3, 4, 4, 4 }); } }
public void TestGetSoftDeletedEntriesCompanyOk() { //SETUP var userId = Guid.NewGuid(); var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using (var context = new CascadeSoftDelDbContext(options, userId)) { context.Database.EnsureCreated(); var customer = Customer.SeedCustomerWithQuotes(context, userId); var config = new ConfigCascadeDeleteWithUserId(context); var service = new CascadeSoftDelService <ICascadeSoftDelete>(config); var status = service.SetCascadeSoftDelete(customer); //ATTEMPT var softDeleted = service.GetSoftDeletedEntries <Customer>().ToList(); //VERIFY softDeleted.Count.ShouldEqual(1); softDeleted.Single().CompanyName.ShouldEqual(customer.CompanyName); } }
public void TestCascadeDeleteCompanyOk() { //SETUP var userId = Guid.NewGuid(); var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using (var context = new CascadeSoftDelDbContext(options, userId)) { context.Database.EnsureCreated(); var customer = Customer.SeedCustomerWithQuotes(context, userId); var config = new ConfigCascadeDeleteWithUserId(context); var service = new CascadeSoftDelService <ICascadeSoftDelete>(config); //ATTEMPT var status = service.SetCascadeSoftDelete(customer); //VERIFY status.IsValid.ShouldBeTrue(status.GetAllErrors()); status.Result.ShouldEqual(1 + 4 + 4 + (4 * 4)); status.Message.ShouldEqual("You have soft deleted an entity and its 24 dependents"); } }
public async Task TestGetSoftDeletedEntriesCompanyOk() { //SETUP var userId = Guid.NewGuid(); var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using (var context = new CascadeSoftDelDbContext(options, userId)) { context.Database.EnsureCreated(); var company = Company.SeedCompanyWithQuotes(context, userId); var config = new ConfigCascadeDeleteWithUserId(context); var service = new CascadeSoftDelServiceAsync <ICascadeSoftDelete>(config); var status = await service.SetCascadeSoftDeleteAsync(company); //ATTEMPT var softDeleted = await service.GetSoftDeletedEntries <Company>().ToListAsync(); //VERIFY softDeleted.Count.ShouldEqual(1); softDeleted.Single().CompanyName.ShouldEqual(company.CompanyName); } }
public void TestCascadeSoftDeleteEmployeeSoftDelInfoOk() { //SETUP var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using (var context = new CascadeSoftDelDbContext(options)) { context.Database.EnsureCreated(); var ceo = Employee.SeedEmployeeSoftDel(context); var config = new ConfigCascadeDeleteWithUserId(context); var service = new CascadeSoftDelService <ICascadeSoftDelete>(context, config); //ATTEMPT var status = service.SetCascadeSoftDelete(ceo.WorksFromMe.First()); //VERIFY Employee.ShowHierarchical(ceo, x => _output.WriteLine(x), false); status.IsValid.ShouldBeTrue(status.GetAllErrors()); status.Result.ShouldEqual(7 + 6); context.Employees.IgnoreQueryFilters().Count(x => x.SoftDeleteLevel != 0).ShouldEqual(7); status.Message.ShouldEqual("You have soft deleted an entity and its 12 dependents"); } }
public async Task TestCheckCascadeSoftOfPreviousDeleteInfo() { //SETUP var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using (var context = new CascadeSoftDelDbContext(options)) { context.Database.EnsureCreated(); var ceo = Employee.SeedEmployeeSoftDel(context); var config = new ConfigCascadeDeleteWithUserId(context); var service = new CascadeSoftDelServiceAsync <ICascadeSoftDelete>(config); var numSoftDeleted = (await service.SetCascadeSoftDeleteAsync(context.Employees.Single(x => x.Name == "CTO"))).Result; numSoftDeleted.ShouldEqual(7 + 6); Employee.ShowHierarchical(ceo, x => _output.WriteLine(x), false); //ATTEMPT var status = await service.CheckCascadeSoftDeleteAsync(context.Employees.IgnoreQueryFilters().Single(x => x.Name == "CTO")); //VERIFY status.Result.ShouldEqual(7 + 6); status.Message.ShouldEqual("Are you sure you want to hard delete this entity and its 12 dependents"); } }