public void GetProducts_When_Filtered_By_Ids_Returns_CorrectSet() { // given there are products with suppliers and categories var supplier = new Supplier { SupplierID = 1, CompanyName = "Supplier 1" }; var category = new Category { CategoryID = 1, CategoryName = "Category 1" }; var products = new List<Product>(); for (var i = 0; i < 100; i++) { var product = new Product { ProductID = i, ProductName = $"Product {i}", Category = category, Supplier = supplier }; products.Add(product); } var mockedUnitOfWork = new Mock<IUnitOfWork>(); var mockedProductRepository = new Mock<IRepository<Product>>(); mockedUnitOfWork.Setup(e => e.GetRepository<Product>()).Returns(mockedProductRepository.Object); mockedProductRepository.Setup(e => e.GetAll()).Returns(products.AsQueryable()); // when GetProducts is called with id filter var productsService = new ProductsService(mockedUnitOfWork.Object); var getProductsFilter = new GetProductsFilter {Ids = new[] {1, 7, 15, 22, 9}}; var productDescriptions = productsService.GetProducts(Int32.MaxValue, 1, getProductsFilter).Data; // then correct set of product descriptions are returned Assert.IsTrue(productDescriptions.Select(e => e.Id).Intersect(new[] { 1, 7, 15, 22, 9 }).Count() == 5, "Products with ids 1, 7, 15, 22, 9 are returned"); }
public QueryResult<ProductDetails> GetProducts(int entriesPerPage, int page, GetProductsFilter filter = null) { var result = new QueryResult<ProductDetails> { Page = page, PageSize = entriesPerPage }; var products = _unitOfWork.GetRepository<Product>().GetAll(); var filteredProducts = filter != null ? filter.Apply(products) : products; var productDetails = filteredProducts.Select(ProjectProductDetails); result.Total = productDetails.Count(); result.Data = productDetails .OrderBy(e => e.Id) .Page(entriesPerPage, page).ToList(); return result; }