public async Task ApplyChanges_Should_Set_Graph_States() { // Arrange ITrackableRepository <Order> ordersRepo = new TrackableRepository <Order>(_fixture.Context); var order = await ordersRepo.FindAsync(1); ordersRepo.DetachEntities(order); order.OrderDetails[0].TrackingState = TrackingState.Unchanged; order.OrderDetails[1].TrackingState = TrackingState.Modified; order.OrderDetails[2].TrackingState = TrackingState.Deleted; var addedDetail = new OrderDetail { OrderDetailId = 4, OrderId = 1, ProductId = 3, Product = order.OrderDetails[2].Product, UnitPrice = 40, Quantity = 400, TrackingState = TrackingState.Added }; order.OrderDetails.Add(addedDetail); // Act ordersRepo.ApplyChanges(order); // Assert Assert.Equal(EntityState.Unchanged, _fixture.Context.Entry(order).State); Assert.Equal(EntityState.Unchanged, _fixture.Context.Entry(order.OrderDetails[0]).State); Assert.Equal(EntityState.Modified, _fixture.Context.Entry(order.OrderDetails[1]).State); Assert.Equal(EntityState.Deleted, _fixture.Context.Entry(order.OrderDetails[2]).State); Assert.Equal(EntityState.Added, _fixture.Context.Entry(order.OrderDetails[3]).State); }
public async Task Insert_Should_Create_Customer() { // Arrange IUnitOfWork unitOfWork = new UnitOfWork(_fixture.Context); var customerRepository = new TrackableRepository <Customer>(_fixture.Context); var customerDomainService = new CustomerDomainService(customerRepository, _mapper); const string customerId = "COMP1"; const string companyName = "Company 1"; var customer = new CustomerDomainModel { CustomerId = customerId, CompanyName = companyName }; // Act customerDomainService.Insert(customer); var savedChanges = await unitOfWork.SaveChangesAsync(); // Assert Assert.Equal(1, savedChanges); var newCustomer = await customerRepository.FindAsync(customerId); Assert.Equal(newCustomer.CustomerId, customerId); Assert.Equal(newCustomer.CompanyName, companyName); }
public async Task DeleteAsync_Should_Set_Entity_State_to_Deleted() { // Arrange ITrackableRepository <Product> productsRepo = new TrackableRepository <Product>(_fixture.Context); var product = await productsRepo.FindAsync(1); // Act await productsRepo.DeleteAsync(1); // Assert Assert.Equal(TrackingState.Deleted, product.TrackingState); }
public async Task Update_Should_Update_Customer() { // Arrange IUnitOfWork unitOfWork = new UnitOfWork(_fixture.Context); var customerRepository = new TrackableRepository <Customer>(_fixture.Context); var customerDomainService = new CustomerDomainService(customerRepository, _mapper); const string customerId = "BERGS"; const string companyName = "Eastern Connection 1"; var data = await customerRepository.FindAsync(customerId); customerRepository.Detach(data); var customerDomainModel = new CustomerDomainModel { CustomerId = data.CustomerId, CompanyName = companyName, ContactName = data.ContactName, ContactTitle = data.ContactTitle, Address = data.Address, City = data.City, Region = data.Region, PostalCode = data.PostalCode, Country = data.Country, Phone = data.Phone, Fax = data.Fax }; // Act customerDomainService.Update(customerDomainModel); var savedChanges = await unitOfWork.SaveChangesAsync(); // Assert Assert.Equal(1, savedChanges); var updatedCustomer = await customerRepository.FindAsync(customerId); Assert.Equal(data.Address, updatedCustomer.Address); Assert.Equal(customerDomainModel.CompanyName, companyName); }
public async Task ApplyChanges_Should_Set_Graph_States() { // Arrange ITrackableRepository <Order> ordersRepo = new TrackableRepository <Order>(_fixture.Context); var order = await ordersRepo.FindAsync(10248); ordersRepo.DetachEntities(order); // updating object in complex object graph order.OrderDetails[0].UnitPrice = 17; order.OrderDetails[0].TrackingState = TrackingState.Modified; // updating object in complex object graph order.OrderDetails[1].Quantity = 2; order.OrderDetails[1].TrackingState = TrackingState.Modified; // deleting object in complex object graph order.OrderDetails[2].TrackingState = TrackingState.Deleted; // adding object in complex object graph var addedDetail = new OrderDetail { //OrderDetailId = 4, OrderId = 1, ProductId = 3, Product = order.OrderDetails[2].Product, UnitPrice = 40, Quantity = 400, TrackingState = TrackingState.Added }; // Act order.OrderDetails.Add(addedDetail); // Act ordersRepo.ApplyChanges(order); // Assert Assert.Equal(EntityState.Unchanged, _fixture.Context.Entry(order).State); Assert.Equal(EntityState.Modified, _fixture.Context.Entry(order.OrderDetails[0]).State); Assert.Equal(EntityState.Modified, _fixture.Context.Entry(order.OrderDetails[1]).State); Assert.Equal(EntityState.Deleted, _fixture.Context.Entry(order.OrderDetails[2]).State); Assert.Equal(EntityState.Added, _fixture.Context.Entry(order.OrderDetails[3]).State); // Save changes to object graph with different TrackingStates throughout the object graph await _unitOfWork.SaveChangesAsync(); }
public async Task Service_Insert_Should_Insert_Into_Database() { // Arrange IUnitOfWork unitOfWork = new UnitOfWork(_fixture.Context); ITrackableRepository <Customer> customerRepository = new TrackableRepository <Customer>(_fixture.Context); ITrackableRepository <Order> orderRepository = new TrackableRepository <Order>(_fixture.Context); var customerService = new CustomerService(customerRepository, orderRepository); const string customerId = "COMP1"; const string companyName = "Company 1"; var customer = new Customer { CustomerId = customerId, CompanyName = companyName }; // Act customerService.Insert(customer); // Assert Assert.Equal(TrackableEntities.Common.Core.TrackingState.Added, customer.TrackingState); // Act var savedChanges = await unitOfWork.SaveChangesAsync(); // Assert Assert.Equal(1, savedChanges); // Act var newCustomer = await customerRepository.FindAsync(customerId); // Assert Assert.Equal(newCustomer.CustomerId, customerId); Assert.Equal(newCustomer.CompanyName, companyName); }