public void Shop_Method_Returns_Correct_View() { // Arrange var productMock = new Mock <IProductData>(); var configurationMock = new Mock <IConfiguration>(); configurationMock.SetupGet(x => x[It.IsAny <string>()]).Returns("3"); var products = new PagedProductDto(); products.Products = new List <ProductDto> { new ProductDto() { Id = 1, Name = "Test", ImageUrl = "TestImage.jpg", Order = 0, Price = 10, Brand = new BrandDto() { Id = 1, Name = "TestBrand" } }, new ProductDto() { Id = 2, Name = "Test2", ImageUrl = "TestImage2.jpg", Order = 1, Price = 22, Brand = new BrandDto() { Id = 1, Name = "TestBrand" } } }; productMock.Setup(p => p.GetProducts(It.IsAny <ProductFilter>())).Returns(products); var controller = new CatalogController(productMock.Object, configurationMock.Object); // Act var result = controller.Shop(1, 5); // Assert var viewResult = Xunit.Assert.IsType <ViewResult>(result); var model = Xunit.Assert.IsAssignableFrom <CatalogViewModel>( viewResult.ViewData.Model); Xunit.Assert.Equal(5, model.BrandId); Xunit.Assert.Equal(1, model.SectionId); }
public PagedProductDto GetProducts(ProductFilter productFilter = null) { IQueryable <Product> query = _db.Products .Include(p => p.Brand) .Include(p => p.Section) .AsQueryable(); if (productFilter is null) { return new PagedProductDto { Products = query.Select(p => p.Map()).AsEnumerable(), TotalCount = query.Count() } } ; if (productFilter.BrandId != null) { query = query.Where(p => p.BrandId == productFilter.BrandId); } if (productFilter.SectionId != null) { query = query.Where(p => p.SectionId == productFilter.SectionId); } var model = new PagedProductDto { TotalCount = query.Count() }; if (productFilter.PageSize != null) { model.Products = query .Skip((productFilter.Page - 1) * productFilter.PageSize.Value) .Take(productFilter.PageSize.Value) .Select(p => p.Map()) .AsEnumerable(); } else { model.Products = query.Select(p => p.Map()).AsEnumerable(); } return(model); }
public PagedProductDto GetProducts(ProductFilter filter) { var query = _context.Products .Include("Brand") .Include("Section") .Where(p => !p.IsDelete) .AsQueryable(); if (filter.BrandId.HasValue) { query = query.Where(p => p.BrandId.HasValue && p.BrandId.Value.Equals(filter.BrandId.Value)); } if (filter.SectionId.HasValue) { query = query.Where(p => p.SectionId.Equals(filter.SectionId)); } if (filter.Ids != null && filter.Ids.Count > 0) { query = query.Where(p => filter.Ids.Contains(p.Id)); } var model = new PagedProductDto { TotalCount = query.Count() }; if (filter.PageSize.HasValue) { model.Products = _mapper.Map <IEnumerable <ProductDto> >(query .OrderBy(c => c.Order) .Skip((filter.Page - 1) * filter.PageSize.Value) .Take(filter.PageSize.Value)); } else { model.Products = _mapper.Map <IEnumerable <ProductDto> >(query .OrderBy(c => c.Order)); } return(model); }
public void CartService_TransformCart_WorksCorrect() { // Arrange var cart = new Cart { Items = new List <CartItem> { new CartItem { ProductId = 1, Quantity = 4 } } }; var products = new PagedProductDto { Products = new List <ProductDto> { new ProductDto { Id = 1, ImageUrl = "", Name = "Test", Order = 0, Price = 1.11m, } } }; var productData = new Mock <IProductService>(); productData.Setup(c => c.GetProducts(It.IsAny <ProductFilter>())).Returns(products); var cartStore = new Mock <ICartStore>(); cartStore.Setup(c => c.Cart).Returns(cart); var cartService = new CartService(productData.Object, cartStore.Object); // Act var result = cartService.TransformCart(); // Assert Assert.Equal(4, result.ItemsCount); Assert.Equal(1.11m, result.Items.First().Key.Price); }
public void CartService_TransformCart_WorksCorrect() { var cart = new Cart { Items = new List <CartItem> { new CartItem { ProductId = 1, Quantity = 4 } } }; var products = new PagedProductDto { Products = new List <ProductDto> { new ProductDto { Id = 1, ImageUrl = "image.jpg", Name = "Test", Order = 0, Price = 1.11m, } }, TotalCount = 1 }; var product_data_mock = new Mock <IProductData>(); product_data_mock.Setup(c => c.GetProducts(It.IsAny <ProductFilter>())).Returns(products); var cart_store_mock = new Mock <ICartStore>(); cart_store_mock.Setup(c => c.Cart).Returns(cart); var cart_service = new CookieCartService(product_data_mock.Object, cart_store_mock.Object); var result = cart_service.TransformCart(); Assert.Equal(4, result.ItemsCount); Assert.Equal(1.11m, result.Items.First().Key.Price); }
public PagedProductDto GetProducts(ProductFilter filter) { var products = _context.Products .Include(p => p.Brand) .Include(p => p.Section) .Where(p => !p.IsDeleted) .AsQueryable(); if (filter.SectionId.HasValue) { products = products.Where(x => x.SectionId == filter.SectionId.Value); } if (filter.BrandId.HasValue) { products = products.Where(x => x.BrandId == filter.BrandId.Value); } var model = new PagedProductDto { TotalCount = products.Count() }; if (filter.PageSize != null) // если указан размер страницы { model.Products = products .Skip((filter.Page - 1) * (int)filter.PageSize) .Take((int)filter.PageSize) .Select(p => p.ToDto()) .ToList(); } else // иначе работаем по старой логике { model.Products = products .Select(p => p.ToDto()) .ToList(); } return(model); }
public void CartService_TransformCart_WorksCorrect() { var cart = new Cart { Items = new List <CartItem> { new CartItem() { ProductId = 1, Quantity = 4 } } }; var model = new PagedProductDto { Products = new List <ProductDto> { new ProductDto { Id = 1, ImageUrl = "", Name = "Test", Order = 0, Price = 1.11m, } }, TotalCount = 1 }; _productMock.Setup(c => c.GetProducts(It.IsAny <ProductFilter>())).Returns(model); _cartStore.Setup(c => c.Cart).Returns(cart); var result = _cartService.TransformCart(); Assert.Equal(4, result.ItemsCount); Assert.Equal(1.11m, result.Items.First().Key.Price); }
public PagedProductDto GetProducts(ProductFilter filter) { var query = _context.Products.Include("Brand").Include("Section").AsQueryable(); if (filter.Ids != null && filter.Ids.Count > 0) { query = query.Where(q => filter.Ids.Contains(q.Id)); } if (filter.BrandId.HasValue) { query = query.Where(c => c.BrandId.HasValue && c.BrandId.Value.Equals(filter.BrandId.Value)); } if (filter.SectionId.HasValue) { query = query.Where(c => c.SectionId.Equals(filter.SectionId.Value)); } query = query.Where(c => c.IsDelete == false); var model = new PagedProductDto { TotalCount = query.Count() }; if (filter.PageSize.HasValue) { model.Products = query.OrderBy(c => c.Order).Skip((filter.Page - 1) * filter.PageSize.Value).Take(filter.PageSize.Value) .Select(p => new ProductDto { Id = p.Id, Name = p.Name, Order = p.Order, Price = p.Price, ImageUrl = p.ImageUrl, Brand = p.BrandId.HasValue ? new BrandDto() { Id = p.Brand.Id, Name = p.Brand.Name } : null, Section = new SectionDto() { Id = p.SectionId, Name = p.Section.Name } }).ToList(); } else { model.Products = query.OrderBy(c => c.Order).Select(p => new ProductDto { Id = p.Id, Name = p.Name, Order = p.Order, Price = p.Price, ImageUrl = p.ImageUrl, Brand = p.BrandId.HasValue ? new BrandDto() { Id = p.Brand.Id, Name = p.Brand.Name } : null, Section = new SectionDto() { Id = p.SectionId, Name = p.Section.Name } }).ToList(); } return(model); }