public void State_Interceptor_Should_Mark_Many_To_Many_Entity_State(bool setState) { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var nw = new MockNorthwind(); var employee = nw.Employees[0]; var territory1 = employee.Territories[0]; employee.Territories = new List <Territory> { territory1 }; territory1.TrackingState = TrackingState.Modified; // Act var state = setState ? EntityState.Unchanged : null as EntityState?; context .WithStateChangeInterceptor <Territory>((e, r) => state) .ApplyChanges(employee); // Assert var expectedState = setState ? EntityState.Unchanged : EntityState.Modified; Assert.Equal(expectedState, context.Entry(territory1).State); }
public void Deleting_Parent_And_Children_Should_Mark_Children_As_Deleted() { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var product = new Product { ProductId = 1, CategoryId = 1, ProductName = "TestProduct" }; var category = new Category { CategoryId = 1, CategoryName = "TestCategory", Products = new List <Product> { product } }; // Act context.Entry(product).State = EntityState.Unchanged; context.Entry(category).State = EntityState.Unchanged; context.Entry(product).State = EntityState.Deleted; context.Entry(category).State = EntityState.Deleted; // Assert Assert.Equal(EntityState.Deleted, context.Entry(product).State); }
public void State_Interceptor_Should_Mark_Many_To_Many_Entity_State_With_RelationType(RelationshipType?relationType) { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var nw = new MockNorthwind(); var employee = nw.Employees[0]; var territory1 = employee.Territories[0]; employee.Territories = new List <Territory> { territory1 }; employee.TrackingState = TrackingState.Unchanged; territory1.TrackingState = TrackingState.Added; // Act var state = relationType == RelationshipType.ManyToMany ? EntityState.Added : null as EntityState?; context .WithStateChangeInterceptor <Territory>((e, r) => r == RelationshipType.ManyToMany ? state : null) .ApplyChanges(employee); // Assert var expectedState = relationType == RelationshipType.ManyToMany ? EntityState.Added : EntityState.Unchanged; Assert.Equal(expectedState, context.Entry(territory1).State); }
public LoadRelatedEntitiesTests() { // Make sure that reference data exists for tables with non-idendity keys using (var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions)) { // Test Customers EnsureTestCustomer(context, TestCustomerId1, TestTerritoryId1); EnsureTestCustomer(context, TestCustomerId2, TestTerritoryId2); // Test Customer Settings EnsureTestCustomerSetting(context, TestCustomerId1); EnsureTestCustomerSetting(context, TestCustomerId2); // Test Territories EnsureTestTerritory(context, TestTerritoryId1); EnsureTestTerritory(context, TestTerritoryId2); EnsureTestTerritory(context, TestTerritoryId3); // Test Product Infos EnsureTestProductInfo(context, ProductInfo1A, ProductInfo1B); EnsureTestProductInfo(context, ProductInfo2A, ProductInfo2B); // Save changes context.SaveChanges(); } }
public void State_Interceptor_Should_Mark_One_To_One_Entity_State_With_RelationType(RelationshipType?relationType) { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var nw = new MockNorthwind(); var customer = nw.Customers[0]; var setting = customer.CustomerSetting = new CustomerSetting { CustomerId = customer.CustomerId, Setting = "Setting1", Customer = customer }; setting.TrackingState = TrackingState.Modified; // Act var state = relationType == RelationshipType.OneToOne ? EntityState.Unchanged : null as EntityState?; context .WithStateChangeInterceptor <CustomerSetting>((e, r) => r == RelationshipType.OneToOne ? state : null) .ApplyChanges(customer); // Assert var expectedState = relationType == RelationshipType.OneToOne ? EntityState.Unchanged : EntityState.Modified; Assert.Equal(expectedState, context.Entry(setting).State); }
public void State_Interceptor_Should_Mark_Many_To_One_MultiLevel_Entities_States(bool setState1, bool setState2) { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var nw = new MockNorthwind(); var order = nw.Orders[0]; var customer = order.Customer; order.TrackingState = TrackingState.Modified; customer.TrackingState = TrackingState.Modified; // Act var states = new[] { setState1?EntityState.Unchanged : null as EntityState?, setState2 ? EntityState.Unchanged : null as EntityState?, }; context .WithStateChangeInterceptor <Order>((e, r) => states[0]) .WithStateChangeInterceptor <Customer>((e, r) => states[1]) .ApplyChanges(order); // Assert var expectedStates = new[] { setState1?EntityState.Unchanged : EntityState.Modified, setState2 ? EntityState.Unchanged : EntityState.Modified, }; Assert.Equal(expectedStates[0], context.Entry(order).State); Assert.Equal(expectedStates[1], context.Entry(customer).State); }
public void Interceptor_Should_Not_Change_Entities_States() { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var nw = new MockNorthwind(); int[] ids = { 4, 5, 6 }; var products = nw.Products.Where(e => ids.Contains(e.ProductId)).ToList(); products.ForEach(e => e.TrackingState = TrackingState.Modified); var cache = new Dictionary <int, EntityState> { { 1, EntityState.Added }, { 2, EntityState.Deleted }, { 3, EntityState.Unchanged }, }; // Act context .WithInterceptor(new ProductCacheStateInterceptor(cache)) .ApplyChanges(products); // Assert Assert.Equal(EntityState.Modified, context.Entry(products[0]).State); Assert.Equal(EntityState.Modified, context.Entry(products[1]).State); Assert.Equal(EntityState.Modified, context.Entry(products[2]).State); }
public async void LoadRelatedEntitiesAsync_Load_All_Should_Populate_Order_With_Customer() { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var order = CreateTestOrders(context)[0]; // Act await context.LoadRelatedEntitiesAsync(order, true); // Assert Assert.NotNull(order.Customer); Assert.Equal(order.CustomerId, order.Customer.CustomerId); }
public async void LoadRelatedEntitiesAsync_Should_Populate_Employee_Territories_With_Area() { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var employee = CreateTestEmployees(context)[0]; employee.TrackingState = TrackingState.Added; // Act await context.LoadRelatedEntitiesAsync(employee); // Assert Assert.False(employee.Territories.Any(t => t.Area == null)); Assert.False(employee.Territories.Any(t => t.AreaId != t.Area.AreaId)); }
public async void LoadRelatedEntitiesAsync_Should_Populate_Multiple_Orders_With_Customer() { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var orders = CreateTestOrders(context); orders.ForEach(o => o.TrackingState = TrackingState.Added); // Act await context.LoadRelatedEntitiesAsync(orders); // Assert Assert.False(orders.Any(o => o.Customer == null)); Assert.False(orders.Any(o => o.Customer.CustomerId != o.CustomerId)); }
public void LoadRelatedEntities_Should_Populate_Order_With_Customer_With_Territory() { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var order = CreateTestOrders(context)[0]; order.TrackingState = TrackingState.Added; // Act context.LoadRelatedEntities(order); // Assert Assert.NotNull(order.Customer.Territory); Assert.Equal(order.Customer.TerritoryId, order.Customer.Territory.TerritoryId); }
public async void LoadRelatedEntitiesAsync_Should_Populate_Order_With_Customer_With_Setting() { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var order = CreateTestOrders(context)[0]; order.TrackingState = TrackingState.Added; // Act await context.LoadRelatedEntitiesAsync(order); // Assert Assert.NotNull(order.Customer.CustomerSetting); Assert.Equal(order.Customer.CustomerId, order.Customer.CustomerSetting.CustomerId); }
public void LoadRelatedEntities_Should_Populate_Product_With_HolidayPromo() { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var product = CreateTestProductsWithPromos(context)[0]; product.TrackingState = TrackingState.Added; // Act context.LoadRelatedEntities(product); // Assert Assert.NotNull(product.HolidayPromo); Assert.Equal(product.PromoId, product.HolidayPromo.PromoId); }
public void Deleting_Parent_Should_Delete_Children() { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var product = new Product { ProductName = "TestProduct" }; var category = new Category { CategoryName = "TestCategory", Products = new List <Product> { product } }; context.Categories.Add(category); context.SaveChanges(); int categoryId = category.CategoryId; int productId = product.ProductId; context.Entry(product).State = EntityState.Detached; context.Entry(category).State = EntityState.Detached; product = new Product { ProductId = productId, CategoryId = categoryId, ProductName = "TestProduct" }; category = new Category { CategoryId = categoryId, CategoryName = "TestCategory", Products = new List <Product> { product } }; // Act // First mark child and parent as unchanged (to attach) context.Entry(product).State = EntityState.Unchanged; context.Entry(category).State = EntityState.Unchanged; // Then mark child and parent as deleted context.Entry(product).State = EntityState.Deleted; context.Entry(category).State = EntityState.Deleted; context.SaveChanges(); // Assert Assert.Equal(EntityState.Detached, context.Entry(category).State); Assert.Equal(EntityState.Detached, context.Entry(product).State); }
public void State_Interceptor_Should_Mark_Many_To_Many_Entities_States(bool setState1, bool setState2) { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var nw = new MockNorthwind(); var employee = nw.Employees[0]; var territory1 = employee.Territories[0]; var territory2 = employee.Territories[1]; employee.Territories = new List <Territory> { territory1, territory2 }; territory1.TrackingState = TrackingState.Modified; territory2.TrackingState = TrackingState.Modified; // Act var states = new[] { setState1?EntityState.Unchanged : null as EntityState?, setState2 ? EntityState.Unchanged : null as EntityState?, }; context .WithStateChangeInterceptor <Territory>((e, r) => { if (e.TerritoryId == "01581") { return(states[0]); } if (e.TerritoryId == "01730") { return(states[1]); } return(null); }) .ApplyChanges(employee); // Assert var expectedStates = new[] { setState1?EntityState.Unchanged : EntityState.Modified, setState2 ? EntityState.Unchanged : EntityState.Modified, }; Assert.Equal(expectedStates[0], context.Entry(territory1).State); Assert.Equal(expectedStates[1], context.Entry(territory2).State); }
public void LoadRelatedEntities_Should_Populate_Order_With_Customer_With_Setting() { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var order = CreateTestOrders(context)[0]; order.TrackingState = TrackingState.Added; // Act context.LoadRelatedEntities(order); // Assert Assert.IsNotNull(order.Customer.CustomerSetting); Assert.AreEqual(order.Customer.CustomerId, order.Customer.CustomerSetting.CustomerId); Assert.IsNotNull(order.Customer.CustomerSetting.Customer); Assert.IsTrue(ReferenceEquals(order.Customer, order.Customer.CustomerSetting.Customer)); }
public void State_Interceptor_Should_Mark_Multiple_Entities_States(bool setState1, bool setState2) { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var products = new List <Product> { new Product { ProductId = 1, TrackingState = TrackingState.Added }, new Product { ProductId = 2, TrackingState = TrackingState.Deleted } }; // Act var states = new[] { setState1?EntityState.Modified : null as EntityState?, setState2 ? EntityState.Unchanged : null as EntityState?, }; context .WithStateChangeInterceptor <Product>((e, r) => { if (e.ProductId == 1) { return(states[0]); } if (e.ProductId == 2) { return(states[1]); } return(null); }) .ApplyChanges(products); // Assert var expectedStates = new[] { setState1?EntityState.Modified : EntityState.Added, setState2 ? EntityState.Unchanged : EntityState.Deleted, }; Assert.Equal(expectedStates[0], context.Entry(products[0]).State); Assert.Equal(expectedStates[1], context.Entry(products[1]).State); }
public void WithStateChangeInterceptor_Should_Not_Mutate_Pool() { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var product = new Product(); bool calledX = false, calledY = false; var poolX = context.WithStateChangeInterceptor <Product>((e, r) => { calledX = true; return(null); }); var poolXY = poolX.WithStateChangeInterceptor <Product>((e, r) => { calledY = true; return(null); }); // Act poolX.ApplyChanges(product); // Assert Assert.True(calledX); Assert.False(calledY); }
public async void LoadRelatedEntitiesAsync_Load_All_Should_Populate_Order_Details_With_Product_With_Category() { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var order = CreateTestOrders(context)[0]; // Act await context.LoadRelatedEntitiesAsync(order, true); // Assert var details = order.OrderDetails; Assert.False(details.Any(d => d.Product == null)); Assert.False(details.Any(d => d.Product.ProductId != d.ProductId)); Assert.False(details.Any(d => d.Product.Category == null)); Assert.False(details.Any(d => d.Product.Category.CategoryId != d.Product.CategoryId)); }
public void LoadRelatedEntities_Should_Populate_Multiple_Employees_Territories_With_Area() { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var employees = CreateTestEmployees(context); employees.ForEach(e => e.TrackingState = TrackingState.Added); // Act context.LoadRelatedEntities(employees); // Assert Assert.False(employees[0].Territories.Any(t => t.Area == null)); Assert.False(employees[0].Territories.Any(t => t.AreaId != t.Area.AreaId)); Assert.False(employees[1].Territories.Any(t => t.Area == null)); Assert.False(employees[1].Territories.Any(t => t.AreaId != t.Area.AreaId)); }
public void State_Interceptor_Should_Mark_One_To_Many_Entities_States(bool setState1, bool setState2) { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var order = new MockNorthwind().Orders[0]; var detail1 = order.OrderDetails[0]; var detail2 = order.OrderDetails[1]; detail1.TrackingState = TrackingState.Modified; detail2.TrackingState = TrackingState.Modified; // Act var states = new[] { setState1?EntityState.Unchanged : null as EntityState?, setState2 ? EntityState.Unchanged : null as EntityState?, }; context .WithStateChangeInterceptor <OrderDetail>((e, r) => { if (e.OrderDetailId == 1) { return(states[0]); } if (e.OrderDetailId == 2) { return(states[1]); } return(null); }) .ApplyChanges(order); // Assert var expectedStates = new[] { setState1?EntityState.Unchanged : EntityState.Modified, setState2 ? EntityState.Unchanged : EntityState.Modified, }; Assert.Equal(expectedStates[0], context.Entry(detail1).State); Assert.Equal(expectedStates[1], context.Entry(detail2).State); }
public void LoadRelatedEntities_Should_Populate_Order_With_Added_Details_With_Product_With_Category() { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var order = CreateTestOrders(context)[0]; order.OrderDetails.ForEach(d => d.TrackingState = TrackingState.Added); // Act context.LoadRelatedEntities(order); // Assert var details = order.OrderDetails; Assert.False(details.Any(d => d.Product == null)); Assert.False(details.Any(d => d.Product.ProductId != d.ProductId)); Assert.False(details.Any(d => d.Product.Category == null)); Assert.False(details.Any(d => d.Product.Category.CategoryId != d.Product.CategoryId)); }
public void Modifying_Parent_Should_Keep_Children_Unchanged() { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var product = new Product { ProductName = "TestProduct" }; var category = new Category { CategoryName = "TestCategory", Products = new List <Product> { product } }; context.Categories.Add(category); context.SaveChanges(); int categoryId = category.CategoryId; int productId = product.ProductId; context.Entry(product).State = EntityState.Detached; context.Entry(category).State = EntityState.Detached; product = new Product { ProductId = productId, CategoryId = categoryId, ProductName = "TestProduct" }; category = new Category { CategoryId = categoryId, CategoryName = "TestCategory_Changed", Products = new List <Product> { product } }; // Act context.Entry(product).State = EntityState.Unchanged; context.Entry(category).State = EntityState.Modified; context.SaveChanges(); // Assert Assert.Equal(EntityState.Unchanged, context.Entry(product).State); }
public void State_Interceptor_Should_Change_Single_Entity_State(bool setState) { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var product = new Product(); product.TrackingState = TrackingState.Modified; // Act var state = setState ? EntityState.Unchanged : null as EntityState?; context .WithStateChangeInterceptor <Product>((e, r) => state) .ApplyChanges(product); // Assert var expectedState = setState ? EntityState.Unchanged : EntityState.Modified; Assert.Equal(expectedState, context.Entry(product).State); }
public async void LoadRelatedEntitiesAsync_Should_Populate_Multiple_Orders_Details_With_Product_With_Category() { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var orders = CreateTestOrders(context); orders.ForEach(o => o.TrackingState = TrackingState.Added); // Act await context.LoadRelatedEntitiesAsync(orders); // Assert var details1 = orders[0].OrderDetails; var details2 = orders[1].OrderDetails; var allDetails = orders.SelectMany(o => o.OrderDetails).ToList(); Assert.False(allDetails.Any(d => d.Product == null)); Assert.False(allDetails.Any(d => d.Product.Category == null)); Assert.False(details1.Any(d => d.Product.ProductId != d.ProductId)); Assert.False(details2.Any(d => d.Product.Category.CategoryId != d.Product.CategoryId)); }
public void State_Interceptor_Should_Mark_One_To_Many_Entity_State_With_Deleted_Parent(bool setState) { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var order = new MockNorthwind().Orders[0]; var detail1 = order.OrderDetails[0]; order.TrackingState = TrackingState.Deleted; // Act var state = setState ? EntityState.Modified : null as EntityState?; context .WithStateChangeInterceptor <OrderDetail>((e, r) => state) .ApplyChanges(order); // Assert var expectedState = setState ? EntityState.Modified : EntityState.Deleted; Assert.Equal(expectedState, context.Entry(detail1).State); }
public void State_Interceptor_Should_Mark_Many_To_One_Entity_State(bool setState) { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var nw = new MockNorthwind(); var order = nw.Orders[0]; var customer = order.Customer; customer.TrackingState = TrackingState.Modified; // Act var state = setState ? EntityState.Unchanged : null as EntityState?; context .WithStateChangeInterceptor <Customer>((e, r) => state) .ApplyChanges(order); // Assert var expectedState = setState ? EntityState.Unchanged : EntityState.Modified; Assert.Equal(expectedState, context.Entry(customer).State); }
public void State_Interceptor_Should_Mark_One_To_Many_Entity_State_With_RelationType(RelationshipType?relationType) { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var order = new MockNorthwind().Orders[0]; var detail1 = order.OrderDetails[0]; detail1.TrackingState = TrackingState.Modified; // Act var state = relationType == RelationshipType.OneToMany ? EntityState.Unchanged : null as EntityState?; context .WithStateChangeInterceptor <OrderDetail>((e, r) => r == RelationshipType.OneToMany ? state : null) .ApplyChanges(order); // Assert var expectedState = relationType == RelationshipType.OneToMany ? EntityState.Unchanged : EntityState.Modified; Assert.Equal(expectedState, context.Entry(detail1).State); }
public void Adding_Parent_Should_Add_Children() { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var product = new Product { ProductName = "TestProduct" }; var category = new Category { CategoryName = "TestCategory", Products = new List <Product> { product } }; // Act context.Entry(category).State = EntityState.Added; context.SaveChanges(); // Assert Assert.Equal(EntityState.Unchanged, context.Entry(product).State); }
public void Deleting_Child_From_Modified_Parent_Should_Keep_Parent_Modified() { // Arrange var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions); var product = new Product { ProductName = "TestProduct" }; var category = new Category { CategoryName = "TestCategory", Products = new List <Product> { product } }; // Act context.Entry(category).State = EntityState.Modified; context.Entry(product).State = EntityState.Deleted; // Assert Assert.Equal(EntityState.Modified, context.Entry(category).State); }