public async Task TestCascadeDeleteCompanySomeQuotesDifferentUserIdOk() { //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.Quotes.First().UserId = Guid.NewGuid(); context.SaveChanges(); var config = new ConfigCascadeDeleteWithUserId(context); var service = new CascadeSoftDelServiceAsync <ICascadeSoftDelete>(config); //ATTEMPT var status = await service.SetCascadeSoftDeleteAsync(company); //VERIFY status.IsValid.ShouldBeTrue(status.GetAllErrors()); status.Result.ShouldEqual(1 + 3 + 3); status.Message.ShouldEqual("You have soft deleted an entity and its 6 dependents"); context.Quotes.IgnoreQueryFilters().Count(x => x.SoftDeleteLevel != 0).ShouldEqual(3); } }
public async Task 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 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.ResetCascadeSoftDeleteAsync(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 TestCascadeDeleteOneQuoteOk() { //SETUP var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using var context = new CascadeSoftDelDbContext(options); context.Database.EnsureCreated(); context.Add(new Customer { CompanyName = "xxx", MoreInfo = new CustomerInfo() }); context.SaveChanges(); context.ChangeTracker.Clear(); var config = new ConfigCascadeDeleteWithUserId(context); var service = new CascadeSoftDelService <ICascadeSoftDelete>(config); //ATTEMPT var status = service.SetCascadeSoftDeleteViaKeys <Customer>(1); //VERIFY status.IsValid.ShouldBeTrue(status.GetAllErrors()); context.Companies.Count().ShouldEqual(0); context.CompanyInfos.Count().ShouldEqual(1); }
public async Task 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 CascadeSoftDelServiceAsync <ICascadeSoftDelete>(config); var numInnerSoftDelete = (await service.SetCascadeSoftDeleteAsync(context.Employees.Single(x => x.Name == "ProjectManager1"))).Result; numInnerSoftDelete.ShouldEqual(3 + 3); var numOuterSoftDelete = (await service.SetCascadeSoftDeleteAsync(context.Employees.Single(x => x.Name == "CTO"))).Result; numOuterSoftDelete.ShouldEqual(4 + 3); Employee.ShowHierarchical(ceo, x => _output.WriteLine(x), false); //ATTEMPT var status = await service.ResetCascadeSoftDeleteAsync(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 async Task 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 CascadeSoftDelServiceAsync <ICascadeSoftDelete>(config); var numSoftDeleted = (await service.SetCascadeSoftDeleteAsync(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 CascadeSoftDelServiceAsync <ICascadeSoftDelete>(config); //ATTEMPT var status = await service.ResetCascadeSoftDeleteAsync(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 static Company SeedCompanyWithQuotes(CascadeSoftDelDbContext context, Guid userId, string companyName = "Company1") { var company = new Company { CompanyName = companyName, UserId = userId, Quotes = new HashSet <Quote>() }; for (int i = 0; i < 4; i++) { company.Quotes.Add( new Quote { Name = $"quote{i}", UserId = userId, PriceInfo = new QuotePrice { UserId = userId, Price = i } } ); } context.Add(company); context.SaveChanges(); return(company); }
public async Task 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 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.ResetCascadeSoftDeleteAsync(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 TestSetSoftDeleteOk() { //SETUP var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using var context = new CascadeSoftDelDbContext(options); context.Database.EnsureCreated(); var shadowClass = new ShadowCascadeDelClass(); context.Add(shadowClass); context.SaveChanges(); context.ChangeTracker.Clear(); var config = new ConfigCascadeDeleteShadowDel(context); var service = new CascadeSoftDelService <IShadowCascadeSoftDelete>(config); //ATTEMPT var status = service.SetCascadeSoftDeleteViaKeys <ShadowCascadeDelClass>(shadowClass.Id); //VERIFY status.IsValid.ShouldBeTrue(status.GetAllErrors()); status.Result.ShouldEqual(1); context.ShadowCascadeDelClasses.Count().ShouldEqual(0); context.ShadowCascadeDelClasses.IgnoreQueryFilters().Count().ShouldEqual(1); }
public void TestResetCascadeDeleteCompanySomeQuotesDifferentUserIdOk() { //SETUP var userId = Guid.NewGuid(); var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); options.StopNextDispose(); 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 ConfigCascadeDeleteShadowDel(CascadeSoftDelDbContext context) : base(context) { GetSoftDeleteValue = entity => (byte)context.Entry(entity).Property("SoftDeleteLevel").CurrentValue; QuerySoftDeleteValue = entity => EF.Property <byte>(entity, "SoftDeleteLevel"); SetSoftDeleteValue = (entity, value) => context.Entry(entity).Property("SoftDeleteLevel").CurrentValue = value; }
public ConfigCascadeDeleteWithUserId(CascadeSoftDelDbContext context) : base(context) { GetSoftDeleteValue = entity => entity.SoftDeleteLevel; SetSoftDeleteValue = (entity, value) => { entity.SoftDeleteLevel = value; }; OtherFilters.Add(typeof(IUserId), entity => ((IUserId)entity).UserId == context.UserId); }
public async Task 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 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.HardDeleteSoftDeletedEntriesAsync(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.ChangeTracker.Clear(); context.Employees.IgnoreQueryFilters().Count().ShouldEqual(4); } }
public void TestCascadeDeleteOneQuoteThenStockCheckOk() { //SETUP var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using (var context = new CascadeSoftDelDbContext(options)) { context.Database.EnsureCreated(); var customer = Customer.SeedCustomerWithQuotes(context, Guid.Empty); var config = new ConfigCascadeDeleteWithUserId(context); var service = new CascadeSoftDelService <ICascadeSoftDelete>(config); var status = service.SetCascadeSoftDelete(customer.Quotes.First()); status.IsValid.ShouldBeTrue(status.GetAllErrors()); //ATTEMPT var requiredProducts = context.Set <LineItem>().ToList() .GroupBy(x => x.ProductSku, y => y.NumProduct) .ToDictionary(x => x.Key, y => y.Sum()); //VERIFY foreach (var productSku in requiredProducts.Keys) { _output.WriteLine($"{productSku}: {requiredProducts [productSku]} needed."); } } }
public void TestCascadeSoftDeleteEmployeeSoftDelInfoNoCallSaveChangesOk() { //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 status = service.SetCascadeSoftDelete(ceo.WorksFromMe.First(), false); context.Employees.IgnoreQueryFilters().Count(x => x.SoftDeleteLevel != 0).ShouldEqual(0); context.SaveChanges(); context.Employees.IgnoreQueryFilters().Count(x => x.SoftDeleteLevel != 0).ShouldEqual(7); //VERIFY //Employee.ShowHierarchical(ceo, x => _output.WriteLine(x), false); status.Result.ShouldEqual(7 + 6); status.IsValid.ShouldBeTrue(status.GetAllErrors()); status.Message.ShouldEqual("You have soft deleted an entity and its 12 dependents"); } }
public void TestConvertFuncToQueryOk() { //SETUP var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using (var context = new CascadeSoftDelDbContext(options)) { context.Database.EnsureCreated(); var ceo = Employee.SeedEmployeeSoftDel(context); ceo.SoftDeleteLevel = 1; ceo.WorksFromMe.First().SoftDeleteLevel = 1; context.SaveChanges(); Expression <Func <ICascadeSoftDelete, byte> > expression = entity => entity.SoftDeleteLevel; var parameter = Expression.Parameter(typeof(ICascadeSoftDelete), expression.Parameters.Single().Name); var left = Expression.Invoke(expression, parameter); var right = Expression.Constant((byte)1, typeof(byte)); var equal = Expression.Equal(left, right); var dynamicFilter = Expression.Lambda <Func <ICascadeSoftDelete, bool> >(equal, parameter); //ATTEMPT var query = context.Employees.IgnoreQueryFilters() .Where(dynamicFilter).Cast <Employee>() .Select(x => x.Name); var result = query.ToList(); //VERIFY _output.WriteLine(query.ToQueryString()); result.Count.ShouldEqual(2); } }
public void TestCascadeSoftDeleteExistingSoftDeleteEmployeeSoftDelOk() { //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 preNumSoftDeleted = service.SetCascadeSoftDelete(ceo.WorksFromMe.First().WorksFromMe.First()).Result; Employee.ShowHierarchical(ceo, x => _output.WriteLine(x), false); //ATTEMPT var status = service.SetCascadeSoftDelete(ceo.WorksFromMe.First()); //VERIFY Employee.ShowHierarchical(ceo, x => _output.WriteLine(x), false); status.IsValid.ShouldBeTrue(status.GetAllErrors()); preNumSoftDeleted.ShouldEqual(3 + 3); status.Result.ShouldEqual(4 + 3); 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, 1, 2, 2, 2, 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, 2, 3, 3, 3, 4 }); } }
public void TestRegisterServiceViaProvidedMethodTestOk() { //SETUP var options1 = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>(); var context1 = new SingleSoftDelDbContext(options1); var options2 = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); var context2 = new CascadeSoftDelDbContext(options2); //ATTEMPT var services = new ServiceCollection(); services.AddScoped(x => context1); services.AddScoped(x => context2); var logs = services.RegisterSoftDelServicesAndYourConfigurations(); var serviceProvider = services.BuildServiceProvider(); //VERIFY foreach (var log in logs) { _output.WriteLine(log); } var service1 = serviceProvider.GetRequiredService <SingleSoftDeleteService <ISingleSoftDelete> >(); var service2 = serviceProvider.GetRequiredService <SingleSoftDeleteService <ISingleSoftDeletedDDD> >(); var service3 = serviceProvider.GetRequiredService <CascadeSoftDelService <ICascadeSoftDelete> >(); }
public void TestCircularLoopCascadeSoftDeleteEmployeeSoftDelOk() { //SETUP var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using (var context = new CascadeSoftDelDbContext(options)) { context.Database.EnsureCreated(); var ceo = Employee.SeedEmployeeSoftDel(context); var devEntry = context.Employees.Single(x => x.Name == "dev1a"); devEntry.WorksFromMe = new List <Employee> { devEntry.Manager.Manager }; var config = new ConfigCascadeDeleteWithUserId(context); var service = new CascadeSoftDelService <ICascadeSoftDelete>(config); //ATTEMPT var status = service.SetCascadeSoftDelete(context.Employees.Single(x => x.Name == "CTO")); //VERIFY status.IsValid.ShouldBeTrue(status.GetAllErrors()); status.Result.ShouldEqual(7 + 6); } }
public void TestResetCascadeSoftOfPreviousDeleteInfo() { //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); 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")); //VERIFY status.IsValid.ShouldBeTrue(status.GetAllErrors()); status.Result.ShouldEqual(7 + 6); status.Message.ShouldEqual("You have recovered an entity and its 12 dependents"); } }
public void TestCascadeDeleteOneQuoteOk() { //SETUP var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using (var context = new CascadeSoftDelDbContext(options)) { context.Database.EnsureCreated(); var customer = Customer.SeedCustomerWithQuotes(context, Guid.Empty); var config = new ConfigCascadeDeleteWithUserId(context); var service = new CascadeSoftDelService <ICascadeSoftDelete>(config); //ATTEMPT var status = service.SetCascadeSoftDelete(customer.Quotes.First()); //VERIFY status.IsValid.ShouldBeTrue(status.GetAllErrors()); status.Message.ShouldEqual("You have soft deleted an entity and its 5 dependents"); context.ChangeTracker.Clear(); context.Companies.Count().ShouldEqual(1); context.Quotes.Count().ShouldEqual(3); context.Set <LineItem>().Count().ShouldEqual(3 * 4); context.Set <QuotePrice>().Count().ShouldEqual(3); status.Result.ShouldEqual(1 + 4 + 1); context.Set <LineItem>().IgnoreQueryFilters().Count(x => x.SoftDeleteLevel != 0).ShouldEqual(4); } }
public void TestCascadeDeleteCompanySomeQuotePriceDifferentUserIdOk() { //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().PriceInfo.UserId = Guid.NewGuid(); context.SaveChanges(); 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 + 3 + (4 * 4)); status.Message.ShouldEqual("You have soft deleted an entity and its 23 dependents"); context.Set <QuotePrice>().IgnoreQueryFilters().Count(x => x.SoftDeleteLevel != 0).ShouldEqual(3); } }
public void TestCascadeSoftDeleteEmployeeSoftDelInfoOk() { //SETUP var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using (var context = new CascadeSoftDelDbContext(options)) { context.Database.EnsureCreated(); var ceo = Employee.SeedEmployeeSoftDel(context); Employee.ShowHierarchical(ceo, x => _output.WriteLine(x), false); var config = new ConfigCascadeDeleteWithUserId(context); var service = new CascadeSoftDelService <ICascadeSoftDelete>(config); //ATTEMPT var status = service.SetCascadeSoftDelete(ceo.WorksFromMe.First()); //VERIFY status.IsValid.ShouldBeTrue(status.GetAllErrors()); status.Result.ShouldEqual(7 + 6); status.Message.ShouldEqual("You have soft deleted an entity and its 12 dependents"); context.ChangeTracker.Clear(); _output.WriteLine("---------------------\nAfter Cascade SoftDelete of the CTO and his/her staff."); context.Employees.Count().ShouldEqual(4); var allEmployees = context.Employees .Include(x => x.WorksFromMe) .ToList(); var readCeo = allEmployees.Single(x => x.Name == "CEO"); Employee.ShowHierarchical(readCeo, x => _output.WriteLine(x), false); } }
public void TestCascadeDeleteQuoteDoesNotSoftDeleteCompanyOk() { //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(); context.SaveChanges(); var config = new ConfigCascadeDeleteWithUserId(context); var service = new CascadeSoftDelService <ICascadeSoftDelete>(config); //ATTEMPT var status = service.SetCascadeSoftDelete(customer.Quotes.First()); //VERIFY status.IsValid.ShouldBeTrue(status.GetAllErrors()); status.Result.ShouldEqual(1 + 1 + 4); status.Message.ShouldEqual("You have soft deleted an entity and its 5 dependents"); context.Companies.Count().ShouldEqual(1); context.Quotes.Count().ShouldEqual(3); } }
public void TestDisconnectedCascadeSoftDeleteEmployeeSoftDelOk(bool readEveryTime) { //SETUP var logs = new List <string>(); var options = SqliteInMemory.CreateOptionsWithLogTo <CascadeSoftDelDbContext>(log => logs.Add(log)); using (var context = new CascadeSoftDelDbContext(options)) { context.Database.EnsureCreated(); Employee.SeedEmployeeSoftDel(context); context.ChangeTracker.Clear(); var config = new ConfigCascadeDeleteWithUserId(context) { ReadEveryTime = readEveryTime }; var service = new CascadeSoftDelService <ICascadeSoftDelete>(config); //ATTEMPT logs.Clear(); var status = service.SetCascadeSoftDelete(context.Employees.Single(x => x.Name == "CTO")); //VERIFY status.IsValid.ShouldBeTrue(status.GetAllErrors()); logs.Count(x => _selectMatchRegex.IsMatch(x)).ShouldEqual(7); status.Result.ShouldEqual(7 + 6); context.Employees.Count().ShouldEqual(4); context.Employees.IgnoreQueryFilters().Count().ShouldEqual(11); context.Contracts.Count().ShouldEqual(3); context.Contracts.IgnoreQueryFilters().Count().ShouldEqual(9); } }
public async Task TestHardDeleteCascadeDeleteCompanySomeQuotesDifferentUserIdOk() { //SETUP var userId = Guid.NewGuid(); var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using (var context = new CascadeSoftDelDbContext(options, userId)) { context.Database.EnsureCreated(); var company = Customer.SeedCustomerWithQuotes(context, userId); Customer.SeedCustomerWithQuotes(context, Guid.Empty, "Other company"); context.SaveChanges(); var config = new ConfigCascadeDeleteWithUserId(context); var service = new CascadeSoftDelServiceAsync <ICascadeSoftDelete>(config); (await service.SetCascadeSoftDeleteAsync(company)).Result.ShouldEqual(1 + 4 + 4 + (4 * 4)); //ATTEMPT var status = await service.HardDeleteSoftDeletedEntriesAsync(company); //VERIFY status.IsValid.ShouldBeTrue(status.GetAllErrors()); status.Result.ShouldEqual(1 + 4 + 4 + (4 * 4)); status.Message.ShouldEqual("You have hard deleted an entity and its 24 dependents"); context.ChangeTracker.Clear(); context.Quotes.Count().ShouldEqual(0); context.Quotes.IgnoreQueryFilters().Count().ShouldEqual(4); } }
public void TestCascadeSoftDeleteTwoLevelOk() { //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 numInnerSoftDelete = service.SetCascadeSoftDelete(context.Employees.Single(x => x.Name == "ProjectManager1")).Result; numInnerSoftDelete.ShouldEqual(3 + 3); var status = service.SetCascadeSoftDelete(context.Employees.Single(x => x.Name == "CTO")); //VERIFY Employee.ShowHierarchical(ceo, x => _output.WriteLine(x), false); status.IsValid.ShouldBeTrue(status.GetAllErrors()); status.Result.ShouldEqual(4 + 3); context.Employees.Count().ShouldEqual(4); context.Employees.IgnoreQueryFilters().Count().ShouldEqual(11); context.Employees.IgnoreQueryFilters().Select(x => x.SoftDeleteLevel).Where(x => x > 0) //.ToList().ForEach(x => _output.WriteLine(x.ToString())); .ToArray().ShouldEqual(new byte[] { 1, 1, 2, 2, 2, 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) //.ToList().ForEach(x => _output.WriteLine(x.ToString())); .ToArray().ShouldEqual(new byte[] { 2, 2, 3, 3, 3, 4 }); } }
public void TestManualSoftDeleteEmployeeSoftDelOk() { //SETUP var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using (var context = new CascadeSoftDelDbContext(options)) { context.Database.EnsureCreated(); var ceo = Employee.SeedEmployeeSoftDel(context); //ATTEMPT ceo.WorksFromMe.First().SoftDeleteLevel = 1; context.SaveChanges(); //VERIFY context.Employees.Count().ShouldEqual(10); } }
public void TestCreateEmployeeSoftDelOk() { //SETUP var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using (var context = new CascadeSoftDelDbContext(options)) { context.Database.EnsureCreated(); //ATTEMPT var ceo = Employee.SeedEmployeeSoftDel(context); //VERIFY context.Employees.Count().ShouldEqual(11); context.Contracts.Count().ShouldEqual(9); Employee.ShowHierarchical(ceo, x => _output.WriteLine(x), false); } }
public async Task TestCascadeDeleteEmployeeSoftDelOk() { //SETUP var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using (var context = new CascadeSoftDelDbContext(options)) { context.Database.EnsureCreated(); var ceo = Employee.SeedEmployeeSoftDel(context); //ATTEMPT context.Remove(ceo.WorksFromMe.First()); await context.SaveChangesAsync(); //VERIFY context.Employees.Count().ShouldEqual(4); context.Contracts.Count().ShouldEqual(3); } }
public void TestSeedCompanyWithQuotesOk() { //SETUP var userId = Guid.NewGuid(); var options = SqliteInMemory.CreateOptions <CascadeSoftDelDbContext>(); using (var context = new CascadeSoftDelDbContext(options, userId)) { context.Database.EnsureCreated(); //ATTEMPT var customer = Customer.SeedCustomerWithQuotes(context, userId); //VERIFY context.Companies.Count().ShouldEqual(1); context.Quotes.Count().ShouldEqual(4); context.Set <QuotePrice>().Count().ShouldEqual(4); } }