public async Task Adding_Two_Products_Should_Also_Return_Two_Products() { IProductRepository repository = new ProductRepository(DbContextHelper.GetInMemory()); //ACT await repository.AddAsync(product1); await repository.AddAsync(product2); IEnumerable <Product> products = await repository.GetAllAsync(); //ASSERT products.Should().NotBeEmpty().And.HaveCount(2); }
public async Task Adding_A_Product_Should_Also_Return_The_Same_Product() { IProductRepository repository = new ProductRepository(DbContextHelper.GetInMemory()); //ACT await repository.AddAsync(product1); await repository.AddAsync(product2); Product exisitingProudct = await repository.GetAsync(product1.Id); //ASSERT exisitingProudct.Should().BeEquivalentTo(product1); }
public async Task add_async_should_add_async_on_product_repository() { //Arrange var logger = new Mock <ILogger <ProductRepository> >(); var options = new DbContextOptionsBuilder <AppDbContext>() .UseInMemoryDatabase(databaseName: "MovieListDatabase") .Options; var context = new AppDbContext(options); context.Product.Add(new Product(Guid.NewGuid() , 1 , 1 , "żółty" , "s" , Guid.Parse("b974a7cf-68ce-4b35-ab89-2101c253a670") , Guid.Parse("d37aa518-6978-434d-8c54-f88af0eea3c8") , Guid.Parse("20d0b89a-d37a-4f1d-b8bb-dc213c8cae5a") , new Guid() , 109.90)); context.SaveChanges(); //Act var newProduct = new Product(Guid.NewGuid(), 1, 1, "żółty", "s", Guid.Parse("b974a7cf-68ce-4b35-ab89-2101c253a670"), Guid.Parse("d37aa518-6978-434d-8c54-f88af0eea3c8"), Guid.Parse("20d0b89a-d37a-4f1d-b8bb-dc213c8cae5a"), new Guid(), 109.90); ProductRepository productRepository = new ProductRepository(logger.Object, context); await productRepository.AddAsync(newProduct); //Assert var existingProduct = await productRepository.GetByIdAsync(newProduct.Id); Assert.Equal(newProduct, existingProduct); }
public async Task Browse_Async_method_Should_Work_Correctly() { const int ProductQuantity = 100; IProductRepository repository = new ProductRepository(DbContextHelper.GetInMemory()); List <Product> generatedProducts = new ProductGenerator().Generate(ProductQuantity).ToList(); foreach (var product in generatedProducts) { await repository.AddAsync(product); } Product firstProduct = generatedProducts.First(); var @params = new PageProductQuery() { Subcategory = firstProduct.Subcategory.Slug, Category = firstProduct.Subcategory.Category.Slug, PageNumber = 2, PageSize = 30 }; //ACT PagedList <Product> productsFromRepo = await repository.BrowseAsync(@params.PageNumber, @params.PageSize, @params.Category, @params.Subcategory); //ASSERT productsFromRepo.Should().HaveCount(@params.PageSize); productsFromRepo.PageSize.Should().Be(@params.PageSize); productsFromRepo.PageNumber.Should().Be(@params.PageNumber); productsFromRepo.TotalCount.Should().Be(ProductQuantity); }
public async Task <IActionResult> Create([Bind("Id,Name")] Product categorie) { if (ModelState.IsValid) { await repository.AddAsync(categorie); return(RedirectToAction(nameof(Index))); } return(View(categorie)); }
public async Task <IActionResult> Create([Bind("Id,Name,Category")] Product product) { if (ModelState.IsValid) { await _productRepository.AddAsync(product); return(RedirectToAction(nameof(Index))); } return(View(product)); }
public async Task When_Deleting_Product_Should_Be_Deleted_Correctly() { IProductRepository repository = new ProductRepository(DbContextHelper.GetInMemory()); //ACT await repository.AddAsync(product1); await repository.AddAsync(product2); await repository.RemoveAsync(product1); var existstingProduct = await repository.GetAsync(product2.Id); var removedProduct = await repository.GetAsync(product1.Id); //ASSERT removedProduct.Should().BeNull(); existstingProduct.Should().BeEquivalentTo(product2); }
public void ImportData(IEnumerable <ProductModel> data) { var activity = new Activity(nameof(ImportData)); if (_diagnosticSource.IsEnabled(activity.OperationName)) { _diagnosticSource.StartActivity(activity, 1); } var countProducts = 0; var countModels = 0; var productId = 0; var modelId = 0; var lineIndex = 0; foreach (var item in data) { lineIndex++; try { item._id = ObjectId.GenerateNewId(); if (item.RecordType.Equals("PRODUCT", StringComparison.OrdinalIgnoreCase)) { productId++; item.Id = productId; ProductRepository.AddAsync(item).Wait(); countProducts++; } else { modelId++; item.Id = modelId; ModelRepository.AddAsync(item).Wait(); countModels++; } } catch (Exception ex) { _logger.LogError(ex, $"impossible d'importer la ligne {lineIndex}"); } } _diagnosticSource.StopActivity(activity, new { Products = countProducts, Models = countModels }); }
public async Task Test_Create_New_Product() { var newProduct = ProductBuilder.NewProductValues(); _output.WriteLine($"New Product: {newProduct.Name}"); var productFromRepo = await _productRepository.AddAsync(newProduct); Assert.True(productFromRepo.ProductId > 0); Assert.Equal(productFromRepo.Code, productFromRepo.Code); Assert.Equal(productFromRepo.Name, productFromRepo.Name); Assert.Equal(productFromRepo.Summary, productFromRepo.Summary); Assert.Equal(productFromRepo.UnitPrice, productFromRepo.UnitPrice); Assert.Equal(productFromRepo.UnitsInStock, productFromRepo.UnitsInStock); }
public async Task <IActionResult> Create([Bind("ProductId,ProductName,SupplierId,CategoryId,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued")] Product product) { if (ModelState.IsValid) { await repository.AddAsync(product); return(RedirectToAction(nameof(Index))); } IEnumerable <Category> categories = await new RepositoryBase <Category>().GetAllAsync(); IEnumerable <Supplier> suppliers = await new RepositoryBase <Supplier>().GetAllAsync(); ViewData["CategoryId"] = new SelectList(categories, "CategoryId", "CategoryName", product.CategoryId); ViewData["SupplierId"] = new SelectList(suppliers, "SupplierId", "CompanyName", product.SupplierId); return(View(product)); }
public async Task When_Updating_Product_Should_Be_Updated_Correctly() { IProductRepository repository = new ProductRepository(DbContextHelper.GetInMemory()); string newCode = "9031101"; //ACT await repository.AddAsync(product1); product1.SetCode(newCode); await repository.UpdateAsync(product1); var existstingProduct = await repository.GetAsync(product1.Id); //ASSERT existstingProduct.Should().NotBeNull(); existstingProduct.Code.Should().Be(newCode); }
public async Task AddAsync_WhenSaved_AddsProductToDatabase() { //Arrange var product = CreateProduct(); await using var dbContext = GetProductsContext(); var sut = new ProductRepository(dbContext); //Act var result = await sut.AddAsync(product); await sut.UnitOfWork.SaveChangesAsync(); //Assert Assert.Equal(product, await dbContext.Products.FindAsync(product.Id)); Assert.Equal(product, result); }
public async Task DbContext() { // Arrange _dbContext.Products.Add(new Product { Id = Guid.NewGuid(), Count = 1, Name = "a", Price = 3 }); _dbContext.SaveChanges(); var test = await _repo.GetAllAsync(); await _repo.AddAsync(new Product { Id = Guid.NewGuid(), Count = 1, Name = "a2", Price = 3 }); await _unitOfWork.CommitAsync(); var test1 = await _repo.GetAllAsync(); // Act //_repo.CreateBook(new Book()); var a = 5; }
public async void SaveProductTest() { IQueryable <Product> products = new List <Product> { new Product { Name = "Product 1", Price = 100m, Code = "P01" }, new Product { Name = "Product 2", Price = 200m, Code = "P02" } }.AsQueryable(); var mockSet = new Mock <DbSet <Product> >(); mockSet.As <IAsyncEnumerable <Product> >() .Setup(d => d.GetEnumerator()) .Returns(new AsyncEnumerator <Product>(products.GetEnumerator())); mockSet.As <IQueryable <Product> >().Setup(m => m.Provider).Returns(products.Provider); mockSet.As <IQueryable <Product> >().Setup(m => m.Expression).Returns(products.Expression); mockSet.As <IQueryable <Product> >().Setup(m => m.ElementType).Returns(products.ElementType); mockSet.As <IQueryable <Product> >().Setup(m => m.GetEnumerator()).Returns(products.GetEnumerator()); var mockContext = new Mock <ProductContext>(); mockContext.Setup(c => c.Products).Returns(mockSet.Object); var repository = new ProductRepository(mockContext.Object); var actual = await repository.AddAsync(products.FirstOrDefault()); Assert.Equal("P01", actual.Code); Assert.Equal("Product 1", actual.Name); Assert.Equal(100m, actual.Price); }
public void SeedDb() { Console.WriteLine("EnsureDeleted"); Database.EnsureDeleted(); SaveChanges(); Console.WriteLine("EnsureCreated"); Database.EnsureCreated(); SaveChanges(); // if (Database.IsNpgsql()) // Database.Migrate(); var type1 = new ProductType() { Code = "TypeCode1", Description = "Description1" }; var type2 = new ProductType() { Code = "TypeCode2", Description = "Description2" }; var unit1 = new ProductUnit() { Code = "item", Description = "One item/piece" }; var unit2 = new ProductUnit() { Code = "kg.", Description = "One kilogram." }; var category1 = new ProductCategory() { Code = "SKATES", Description = "In-line skates" }; var category2 = new ProductCategory() { Code = "WAX", Description = "Wax for street." }; var category3 = new ProductCategory() { Code = "ACC", Description = "Accessories" }; var category4 = new ProductCategory() { Code = "STREET", Description = "Street equipment" }; var category5 = new ProductCategory() { Code = "WHEEL", Description = "Wheels" }; Entry(type1).State = EntityState.Added; Entry(type2).State = EntityState.Added; Entry(unit1).State = EntityState.Added; Entry(unit2).State = EntityState.Added; Entry(category1).State = EntityState.Added; Entry(category2).State = EntityState.Added; Entry(category3).State = EntityState.Added; Entry(category4).State = EntityState.Added; Entry(category5).State = EntityState.Added; SaveChanges(); var valo1 = new Product() { Code = "VALOAB.1BLACK", Description = "Valo AB.1 2018 Black.", Price = 999, NextDelivery = DateTime.UtcNow.AddMonths(1).AddTicks(1), IsAvailable = false, ProductType = type1, Unit = unit1 }; valo1.Categories.Add(new ProductToCategory(0, category1.Id)); valo1.Categories.Add(new ProductToCategory(0, category4.Id)); var valo2 = new Product() { Code = "VALOAB.1CARBON", Description = "Valo AB.1 2018 Carbon Black Edition.", Price = 1499, NextDelivery = DateTime.UtcNow.AddMonths(1).AddTicks(1), IsAvailable = false, ProductType = type1, Unit = unit1 }; valo2.Categories.Add(new ProductToCategory(0, category1.Id)); valo2.Categories.Add(new ProductToCategory(0, category4.Id)); var wax1 = new Product() { Code = "WAX01", Description = "Super wax 1", Price = 10, NextDelivery = DateTime.UtcNow.AddMonths(1).AddTicks(1), IsAvailable = true, ProductType = type1, Unit = unit2 }; wax1.Categories.Add(new ProductToCategory(0, category2.Id)); wax1.Categories.Add(new ProductToCategory(0, category4.Id)); var wax2 = new Product() { Code = "WAX02", Description = "Super wax 2", Price = 50, NextDelivery = DateTime.UtcNow.AddMonths(1).AddTicks(1), IsAvailable = true, ProductType = type1, Unit = unit2 }; wax2.Categories.Add(new ProductToCategory(0, category2.Id)); wax2.Categories.Add(new ProductToCategory(0, category3.Id)); wax2.Categories.Add(new ProductToCategory(0, category4.Id)); var wheel1 = new Product() { Code = "UNDERCOVER", Description = "Undervcover wheels 80 mm.", Price = 50, NextDelivery = DateTime.UtcNow.AddMonths(1).AddTicks(1), IsAvailable = true, ProductType = type1, Unit = unit1 }; var wheel2 = new Product() { Code = "UNDERCOVER_RICHI", Description = "Undervcover Richi pro model wheels 80 mm.", Price = 90, NextDelivery = DateTime.UtcNow.AddMonths(1).AddTicks(1), IsAvailable = true, ProductType = type1, Unit = unit1 }; var wheel3 = new Product() { Code = "UNDERCOVER Aragon", Description = "Undervcover Aragon wheels 80 mm.", Price = 40, NextDelivery = DateTime.UtcNow.AddMonths(1).AddTicks(1), IsAvailable = false, ProductType = type1, Unit = unit1 }; var wheel4 = new Product() { Code = "UNDERCOVER BASIC", Description = "Undervcover basic wheels 80 mm.", Price = 10, NextDelivery = DateTime.UtcNow.AddMonths(1).AddTicks(1), IsAvailable = true, ProductType = type1, Unit = unit1 }; wheel1.Categories.Add(new ProductToCategory(0, category3.Id)); wheel1.Categories.Add(new ProductToCategory(0, category4.Id)); wheel1.Categories.Add(new ProductToCategory(0, category5.Id)); wheel2.Categories.Add(new ProductToCategory(0, category3.Id)); wheel2.Categories.Add(new ProductToCategory(0, category4.Id)); wheel2.Categories.Add(new ProductToCategory(0, category5.Id)); wheel3.Categories.Add(new ProductToCategory(0, category3.Id)); wheel3.Categories.Add(new ProductToCategory(0, category4.Id)); wheel3.Categories.Add(new ProductToCategory(0, category5.Id)); wheel4.Categories.Add(new ProductToCategory(0, category3.Id)); wheel4.Categories.Add(new ProductToCategory(0, category4.Id)); wheel4.Categories.Add(new ProductToCategory(0, category5.Id)); var productRepo = new ProductRepository(this); productRepo.AddAsync(valo1).GetAwaiter().GetResult(); productRepo.AddAsync(valo2).GetAwaiter().GetResult(); productRepo.AddAsync(wax1).GetAwaiter().GetResult(); productRepo.AddAsync(wax2).GetAwaiter().GetResult(); productRepo.AddAsync(wheel1).GetAwaiter().GetResult(); productRepo.AddAsync(wheel2).GetAwaiter().GetResult(); productRepo.AddAsync(wheel3).GetAwaiter().GetResult(); productRepo.AddAsync(wheel4).GetAwaiter().GetResult(); SaveChanges(); }