public void DoesNotAddTheRootObjectIfAllItemsAreInvalid() { companies.Add(new Company { Id = 1, Name = "Microsoft" }); var model = new AcquisitionModel { CompanyName = "Microsoft", Date = "1/1/2000", Items = new[] { new AcquisitionItemModel { ProductName = null, Quantity = "1", Price = "1" }, new AcquisitionItemModel { ProductName = "b", Quantity = "", Price = "2" }, new AcquisitionItemModel { ProductName = "c", Quantity = "3", Price = "" }, }, }; sut.AddAcquisition(model); acquisitions.Should().BeEmpty(); }
public void AddAcquisition(AcquisitionModel model) { var acquisition = ToEntity(model); if (acquisition == null) { return; } store.ExecuteCommand(commandFactory.AddAcquisition(acquisition)); store.SaveChanges(); // this updates the ProductId field on the items // get the stock records for these products var productIds = store .ExecuteQuery(queryFactory.GetProductsForItems(acquisition.Items)) .Select(it => it.Id) .ToList(); var stocks = store.ExecuteQuery(queryFactory.GetStocks(productIds)); foreach (var acquisitionItem in acquisition.Items) { UpdateStock(repository, stocks, acquisitionItem); } repository.SaveChanges(); }
private Acquisition ToEntity(AcquisitionModel model) { var productNames = model .Items .Select(it => it.ProductName) .Where(it => !it.IsNullOrEmpty()) .ToList(); var products = store.ExecuteQuery(queryFactory.FindProductsByName(productNames)).ToList(); var items = model .Items .Select(it => ToEntity(products, it)) .Where(it => it != null) .ToList(); if (!items.Any()) { return(null); } return(new Acquisition { Company = repository.FindOrAddCompanyByName(model.CompanyName), Date = model.Date.ParseDateNullable() ?? DateTime.Today, Items = items, }); }
public void AddsTheMissingProducts() { companies.Add(new Company { Id = 1, Name = "Microsoft" }); products.Add(new Product { Id = 1, Name = "abc", SalePrice = 12.34m }); stocks.Add(new Stock { Id = 1, ProductId = 1, Quantity = 22.35m }); var model = new AcquisitionModel { CompanyName = "Microsoft", Date = "2/3/2000", Items = new[] { new AcquisitionItemModel { ProductName = "abc", Quantity = "1.23", Price = "4" }, new AcquisitionItemModel { ProductName = "def", Quantity = "5.67", Price = "8" }, }, }; sut.AddAcquisition(model); products.Should().HaveCount(2); products[0].Name.Should().Be("abc"); products[1].Name.Should().Be("def"); }
public void AddsTheCompanyIfNeeded() { companies.Add(new Company { Id = 1, Name = "Microsoft" }); var model = new AcquisitionModel { CompanyName = "Google", Date = "2/3/2000", Items = new[] { new AcquisitionItemModel { ProductName = "abc", Quantity = "1.23", Price = "4" }, new AcquisitionItemModel { ProductName = "def", Quantity = "5.67", Price = "8" }, }, }; sut.AddAcquisition(model); companies.Should().HaveCount(2); companies[1].Name.Should().Be("Google"); }
public void CommitsTheTransaction() { companies.Add(new Company { Id = 1, Name = "Microsoft" }); products.Add(new Product { Id = 1, Name = "abc" }); products.Add(new Product { Id = 2, Name = "def", SalePrice = 12.34m }); stocks.Add(new Stock { Id = 1, ProductId = 1, Name = "abc", Quantity = 22.35m }); var model = new AcquisitionModel { CompanyName = "Microsoft", Date = "2/3/2000", Items = new[] { new AcquisitionItemModel { ProductName = "abc", Quantity = "1.23", Price = "4" }, new AcquisitionItemModel { ProductName = "def", Quantity = "5.67", Price = "8" }, }, }; sut.AddAcquisition(model); transaction.Verify(it => it.Commit()); }
public void PostCallsTheLogicToAddTheNewAcquisition() { var model = new AcquisitionModel(); sut.Create(model); logic.Verify(it => it.AddAcquisition(model)); }
public void IgnoresItemsWithInvalidFields() { companies.Add(new Company { Id = 1, Name = "Microsoft" }); products.Add(new Product { Id = 4, Name = "d" }); var model = new AcquisitionModel { CompanyName = "Microsoft", Date = "1/1/2000", Items = new[] { new AcquisitionItemModel { ProductName = null, Quantity = "1", Price = "1" }, new AcquisitionItemModel { ProductName = "b", Quantity = "", Price = "2" }, new AcquisitionItemModel { ProductName = "c", Quantity = "3", Price = "" }, new AcquisitionItemModel { ProductName = "d", Quantity = "4", Price = "4" }, }, }; sut.AddAcquisition(model); acquisitions.Should().HaveCount(1); var acquisition = acquisitions[0]; acquisition.ShouldBeEquivalentTo(new Acquisition { Id = 1, Company = new Company { Id = 1, Name = "Microsoft" }, Date = new DateTime(2000, 1, 1), Items = new Collection <AcquisitionItem> { new AcquisitionItem { Product = new Product { Id = 4, Name = "d" }, ProductId = 4, Quantity = 4.00m, Price = 4.00m, }, } }); }
private void CompareExpectedResult(AcquisitionModel acquisition) { Assert.Equal("D6AE96E1-62B1-4F43-91F6-680E3990D76D", acquisition.Properties.AcquisitionId); Assert.Equal("blob3", acquisition.Properties.Blob); Assert.Equal("blob1", acquisition.Properties.Container); Assert.Equal("\\\\JUSTISUN-BLOB2\\blob\\mystorageaccount1\\blob1\\c9c61ccf-9db4-4543-bb9f-39510f1c4591.pageblob", acquisition.Properties.FilePath); Assert.Equal("mystorageaccount1", acquisition.Properties.StorageAccountName); Assert.Equal(AcquisitionStatus.Success, acquisition.Properties.Status); Assert.Equal(0, acquisition.Properties.MaximumBlobSize); Assert.Equal(new Guid("d6ae96e1-62b1-4f43-91f6-680e3990d76b"), acquisition.Properties.TenantSubscriptionId); }
public ActionResult Create() { var model = new AcquisitionModel { Date = DateTime.Today.ToString(Constants.DATE_FORMAT), Items = new List <AcquisitionItemModel> { new AcquisitionItemModel() } }; return(View(model)); }
public void UpdatesTheStock() { companies.Add(new Company { Id = 1, Name = "Microsoft" }); products.Add(new Product { Id = 1, Name = "abc" }); products.Add(new Product { Id = 2, Name = "def", SalePrice = 12.34m }); stocks.Add(new Stock { Id = 1, ProductId = 1, Name = "abc", Quantity = 22.35m }); var model = new AcquisitionModel { CompanyName = "Microsoft", Date = "2/3/2000", Items = new[] { new AcquisitionItemModel { ProductName = "abc", Quantity = "1.23", Price = "4" }, new AcquisitionItemModel { ProductName = "def", Quantity = "5.67", Price = "8" }, }, }; sut.AddAcquisition(model); stocks.Should().HaveCount(2); stocks[0].Quantity.Should().Be(23.58m); var addedStock = stocks[1]; addedStock.ShouldBeEquivalentTo(new Stock { Id = 2, ProductId = 2, Name = "def", SalePrice = 12.34m, Quantity = 5.67m, PurchaseValue = 45.36m, SaleValue = 69.97m, }); }
public void AddAcquisition(AcquisitionModel model) { using (var repository = dbFactory.Invoke()) using (var transaction = repository.CreateTransaction()) { var productNames = model .Items .Select(it => it.ProductName) .Where(it => !it.IsNullOrEmpty()) .ToList(); var products = repository .Products .Where(it => productNames.Contains(it.Name)) .ToList(); var acquisition = ToEntity(repository, products, model); if (acquisition == null) { return; } repository.Acquisitions.Add(acquisition); repository.SaveChanges(); // this updates the ProductId field on the items // get the stock records for these products var stocks = GetStocks(repository.Stocks, acquisition.Items).ToList(); foreach (var acquisitionItem in acquisition.Items) { var item = acquisitionItem; Retry.Times(3, TimeSpan.FromMilliseconds(500), () => UpdateStock(repository, stocks, item)); } repository.SaveChanges(); transaction.Commit(); UpdateAllClients(); } }
public void AddsTheCorrectValuesToTheRepository() { companies.Add(new Company { Id = 1, Name = "Microsoft" }); products.Add(new Product { Id = 1, Name = "abc" }); products.Add(new Product { Id = 2, Name = "def" }); var model = new AcquisitionModel { CompanyName = "Microsoft", Date = "2/3/2000", Items = new[] { new AcquisitionItemModel { ProductName = "abc", Quantity = "1.23", Price = "4" }, new AcquisitionItemModel { ProductName = "def", Quantity = "5.67", Price = "8" }, }, }; sut.AddAcquisition(model); acquisitions.Should().HaveCount(1); var acquisition = acquisitions[0]; acquisition.ShouldBeEquivalentTo(new Acquisition { Id = 1, Company = new Company { Id = 1, Name = "Microsoft" }, Date = new DateTime(2000, 2, 3), Items = new Collection <AcquisitionItem> { new AcquisitionItem { Product = new Product { Id = 1, Name = "abc" }, ProductId = 1, Quantity = 1.23m, Price = 4.00m, }, new AcquisitionItem { Product = new Product { Id = 2, Name = "def" }, ProductId = 2, Quantity = 5.67m, Price = 8.00m, } } }); repository.Verify(it => it.SaveChanges()); }
public ActionResult Create(AcquisitionModel model) { logic.AddAcquisition(model); return(RedirectToAction("Create")); }
private static Acquisition ToEntity(Repository repository, IEnumerable <Product> products, AcquisitionModel model) { var items = model .Items .Select(it => ToEntity(repository, products, it)) .Where(it => it != null) .ToList(); if (!items.Any()) { return(null); } return(new Acquisition { Company = repository.Companies.FirstOrDefault(it => it.Name == model.CompanyName) ?? repository.Companies.Add(new Company { Name = model.CompanyName }), Date = model.Date.ParseDateNullable() ?? DateTime.Today, Items = items, }); }
public AcquisitionResponse(AcquisitionModel resource) : base(resource) { }