public void Invoke_ReaderRead()
        {
            //Arrange
            var stubProductService = new Mock <IProductService>();
            var mockReader         = new Mock <IReader>();
            var stubWriter         = new Mock <IWriter>();
            var testedCommand      = new SearchProductCommand
                                         (mockReader.Object, stubWriter.Object, stubProductService.Object);


            //Act
            testedCommand.ExecuteThisCommand();

            //Assert
            mockReader.Verify(x => x.Read(), Times.Once);
        }
        public void Invoke_WriterWriteLine()
        {
            //Arrange
            var stubProductService = new Mock <IProductService>();
            var stubReader         = new Mock <IReader>();
            var stubWriter         = new Mock <IWriter>();
            var testedCommand      = new SearchProductCommand
                                         (stubReader.Object, stubWriter.Object, stubProductService.Object);


            //Act
            testedCommand.ExecuteThisCommand();

            //Assert
            stubWriter.Verify(x => x.WriteLine(It.IsAny <string>()), Times.Once);
        }
Beispiel #3
0
        public async Task <IActionResult> Index()
        {
            // Default price and max price.
            var defaultMinPrice = double.Parse(_configuration["Default:MinPrice"]);
            var defaultMaxPrice = double.Parse(_configuration["Default:MaxPrice"]);

            // Get all the default colors to show on the page.
            var defaultColors = await _mediator.Send(new SearchProductAllColorsCommand());

            // Create command for getting all products.
            var command = new SearchProductCommand(
                defaultMinPrice,
                defaultMaxPrice,
                defaultColors.Select(x => x.Id).ToList());

            // Execute and search for produts.
            var result = await _mediator.Send(command);

            // Order the result by lowest price.
            result = result.OrderBy(x => x.Inventory.Price).ToList();

            // Prepare view model for display.
            var viewModel = new ShopSearchViewModel()
            {
                DefaultColors   = defaultColors.ToList(),
                DefaultMinPrice = defaultMinPrice,
                DefaultMaxPrice = defaultMaxPrice,

                Colors   = defaultColors.Select(x => x.Id).ToList(),
                MaxPrice = defaultMaxPrice,
                MinPrice = defaultMinPrice,
                Products = result.Select(x => new ShopSearchProductViewModel()
                {
                    Name    = x.Name,
                    Price   = x.Inventory.Price,
                    Picture = x.Picture,
                    Id      = x.Id,
                    Color   = x.Color
                })
            };

            return(View("Index", viewModel));
        }
        public void GetProductsSellingPrice_IfMatchExist()
        {
            //Arrange
            var stubProductService = new Mock <IProductService>();
            var stubReader         = new Mock <IReader>();
            var stubWriter         = new Mock <IWriter>();
            var mockProduct        = new Mock <IProductModel>();

            stubProductService.Setup(x => x.FindProductByName(It.IsAny <string>()))
            .Returns(mockProduct.Object);
            var testedCommand = new SearchProductCommand
                                    (stubReader.Object, stubWriter.Object, stubProductService.Object);

            //Act
            testedCommand.ExecuteThisCommand();

            //Assert
            mockProduct.Verify(x => x.SellingPrice, Times.Once);
        }
Beispiel #5
0
 public MainWindowViewModel()
 {
     SearchCommand = new SearchProductCommand(this);
     GoToStore     = new GoToStoreCommand(this);
 }
        public async Task SearchProductComandHandler_ContainsProduct_RightProduct(double minPrice, double maxPrice, List <int> colors)
        {
            var option = new DbContextOptionsBuilder <ApplicationDbContext>()
                         .UseInMemoryDatabase(databaseName: "SearchProductComandHandler_ContainsProduct_RightProduct")
                         .Options;

            using (var context = new ApplicationDbContext(option))
            {
                // Clear the in memory database.
                context.Database.EnsureDeleted();

                // Create unit of work for populating data.
                IUnitOfWork unitOfWork = new UnitOfWork <ApplicationDbContext>(context);

                // Add color.
                var color = unitOfWork.Repository.Add(new Color()
                {
                    Id = 1
                });

                // Add product.
                var product = unitOfWork.Repository.Add(new Product()
                {
                    ColorId   = color.Id,
                    Inventory = new InventoryProduct()
                    {
                        Price = 5000
                    }
                });

                // Save changes.
                await unitOfWork.SaveChanges();
            }

            using (var contex = new ApplicationDbContext(option))
            {
                // Crete unit of work to use in testing.
                IUnitOfWork unitOfwork = new UnitOfWork <ApplicationDbContext>(contex);

                // Source for cancelation.
                CancellationTokenSource source = new CancellationTokenSource();

                // Command to execute.
                var command = new SearchProductCommand(minPrice, maxPrice, colors);

                // Handler to test.
                var handler = new SearchProductCommandHandler(unitOfwork);

                // Test handler.
                var results = await handler.Handle(command, source.Token);

                // Check if list is empty.
                Assert.NotEmpty(results);

                foreach (var result in results)
                {
                    if (colors.Any())
                    {
                        Assert.Contains(result.ColorId, colors);
                    }

                    Assert.True(result.Inventory.Price <= maxPrice && result.Inventory.Price >= minPrice);
                }
            }
        }