[InlineData("cktan", "Fragrant Garlic", "Fragrant Garlic (Small)", "Spice", 1.50, 100.0, "g", -1)] // stock
        public async Task Create_plant_details_bad_request_reponse(string userId, string name, string description, string category, decimal price, decimal weight, string unit, int stock)
        {
            //Arrange
            var plantDetailsContext = new PlantListingContext(_dbOptions);

            var invalidCreatePlantDetailsViewModel = new CreateEditPlantDetailsViewModel()
            {
                PlantDetailsId = 0,
                Name           = name,
                Description    = description,
                Category       = category,
                Price          = price,
                Weight         = weight,
                Unit           = unit,
                Stock          = stock,
                UserId         = userId
            };

            //Act
            var plantDetailsController = new PlantListingController(plantDetailsContext, _mockPlantImageService.Object);
            var actionResult           = await plantDetailsController.CreatePlantDetails(invalidCreatePlantDetailsViewModel);

            //Assert
            Assert.IsType <BadRequestResult>(actionResult.Result);
        }
        public async Task Update_plant_details_success()
        {
            //Arrange
            var plantDetailsContext = new PlantListingContext(_dbOptions);

            var userId = "wpkeoh";
            var updatePlantDetailsViewModel = new CreateEditPlantDetailsViewModel()
            {
                PlantDetailsId = 5,
                Name           = "Fragrant Garlic",
                Description    = "Fragrant Garlic (Small)",
                Category       = "Spice",
                Price          = 1.50m,
                Weight         = 100.0m,
                Unit           = "g",
                Stock          = 100,
                UserId         = userId
            };

            //Act
            var plantDetailsController = new PlantListingController(plantDetailsContext, _mockPlantImageService.Object);
            var actionResult           = await plantDetailsController.UpdatePlantDetails(updatePlantDetailsViewModel);

            //Assert
            Assert.IsType <NoContentResult>(actionResult);
        }
        public async Task Update_plant_details_not_found_reponse()
        {
            //Arrange
            var plantDetailsContext = new PlantListingContext(_dbOptions);

            var userId = "cktan";
            var NotFoundUpdatePlantDetailsViewModel = new CreateEditPlantDetailsViewModel()
            {
                PlantDetailsId = 99,
                Name           = "Bean sprouts",
                Description    = "Organic bean sprouts",
                Category       = "Vegetable",
                Price          = 0.80m,
                Weight         = 200.0m,
                Unit           = "g",
                Stock          = 100,
                UserId         = userId
            };

            //Act
            var plantDetailsController = new PlantListingController(plantDetailsContext, _mockPlantImageService.Object);
            var actionResult           = await plantDetailsController.UpdatePlantDetails(NotFoundUpdatePlantDetailsViewModel);

            //Assert
            Assert.IsType <NotFoundObjectResult>(actionResult);
        }
        public async Task Get_plant_details_success()
        {
            //Arrange
            var plantDetailsContext = new PlantListingContext(_dbOptions);

            var plantDetailsId      = 3;
            var expectedName        = "Japanese Cucumber";
            var expectedDescription = "Green color fruit";
            var expectedCategory    = "Fruit";
            var expectedPrice       = 1.00m;
            var expectedWeight      = 500.0m;
            var expectedUnit        = "g";
            var expectedStock       = 50;
            var expectedUserId      = "mgkoh";

            //Act
            var plantDetailsController = new PlantListingController(plantDetailsContext, _mockPlantImageService.Object);
            var actionResult           = await plantDetailsController.GetPlantDetails(plantDetailsId);

            //Assert
            Assert.IsType <ActionResult <PlantDetailsViewModel> >(actionResult);
            Assert.Equal(plantDetailsId, actionResult.Value.PlantDetailsId);
            Assert.Equal(expectedName, actionResult.Value.Name);
            Assert.Equal(expectedDescription, actionResult.Value.Description);
            Assert.Equal(expectedCategory, actionResult.Value.Category);
            Assert.Equal(expectedPrice, actionResult.Value.Price);
            Assert.Equal(expectedWeight, actionResult.Value.Weight);
            Assert.Equal(expectedUnit, actionResult.Value.Unit);
            Assert.Equal(expectedStock, actionResult.Value.Stock);
            Assert.Equal(expectedUserId, actionResult.Value.UserId);
        }
        [InlineData(5, 2, 0)] // test pagination
        public async Task Get_plant_listing_success(int pageSize, int pageIndex, int expectedCount)
        {
            //Arrange
            var plantDetailsContext = new PlantListingContext(_dbOptions);

            //Act
            var plantDetailsController = new PlantListingController(plantDetailsContext, _mockPlantImageService.Object);
            var actionResult           = await plantDetailsController.GetPlantListing(pageSize, pageIndex);

            //Assert
            Assert.IsType <ActionResult <PaginatedItemsViewModel <PlantDetailsViewModel> > >(actionResult);
            Assert.Equal(expectedCount, actionResult.Value.Data.Count());
        }
        public async Task Search_plant_listing_bad_request_response()
        {
            //Arrange
            var plantDetailsContext = new PlantListingContext(_dbOptions);

            //Act
            var plantDetailsController = new PlantListingController(plantDetailsContext, _mockPlantImageService.Object);
            var actionResult           = await plantDetailsController.SearchPlantListing(null);

            //Assert
            Assert.IsType <ActionResult <PaginatedItemsViewModel <PlantDetailsViewModel> > >(actionResult);
            Assert.IsType <BadRequestObjectResult>(actionResult.Result);
        }
        public async Task Get_plant_details_bad_request_response(long plantDetailsId)
        {
            //Arrange
            var plantDetailsContext = new PlantListingContext(_dbOptions);

            //Act
            var plantDetailsController = new PlantListingController(plantDetailsContext, _mockPlantImageService.Object);
            var actionResult           = await plantDetailsController.GetPlantDetails(plantDetailsId);

            //Assert
            Assert.IsType <ActionResult <PlantDetailsViewModel> >(actionResult);
            Assert.IsType <BadRequestObjectResult>(actionResult.Result);
        }
        public async Task Get_plant_listing_by_plantDetailsIds_bad_request_response(string plantDetailsIds, int pageSize, int pageIndex)
        {
            //Arrange
            var plantDetailsContext = new PlantListingContext(_dbOptions);

            //Act
            var plantDetailsController = new PlantListingController(plantDetailsContext, _mockPlantImageService.Object);
            var actionResult           = await plantDetailsController.GetPlantListingByPlantDetailsIds(plantDetailsIds, pageSize, pageIndex);

            //Assert
            Assert.IsType <ActionResult <PaginatedItemsViewModel <PlantDetailsViewModel> > >(actionResult);
            Assert.IsType <BadRequestObjectResult>(actionResult.Result);
        }
Exemple #9
0
        public async Task Get_weight_units_success()
        {
            //Arrange
            var plantDetailsContext = new PlantListingContext(_dbOptions);
            var expectedCount       = 3;

            //Act
            var weightUnitsController = new WeightUnitsController(plantDetailsContext);
            var actionResult          = await weightUnitsController.GetWeightUnits();

            //Assert
            Assert.IsType <ActionResult <IEnumerable <WeightUnit> > >(actionResult);
            Assert.Equal(expectedCount, actionResult.Value.Count());
        }
        public async Task Get_plant_categories_success()
        {
            //Arrange
            var plantDetailsContext = new PlantListingContext(_dbOptions);
            var expectedCount       = 5;

            //Act
            var plantCategoriesController = new PlantCategoriesController(plantDetailsContext);
            var actionResult = await plantCategoriesController.GetPlantCategories();

            //Assert
            Assert.IsType <ActionResult <IEnumerable <PlantCategory> > >(actionResult);
            Assert.Equal(expectedCount, actionResult.Value.Count());
        }
        public async Task Create_plant_details_success()
        {
            //Arrange
            var plantDetailsContext = new PlantListingContext(_dbOptions);

            var userId = "cktan";
            var validCreatePlantDetailsViewModel = new CreateEditPlantDetailsViewModel()
            {
                Name        = "Bean sprouts",
                Description = "Organic bean sprouts",
                Category    = "Vegetable",
                Price       = 0.80m,
                Weight      = 100.0m,
                Unit        = "g",
                Stock       = 100,
                UserId      = userId
            };

            var expectedPlantDetailsId = 9;
            var expectedName           = "Bean sprouts";
            var expectedDescription    = "Organic bean sprouts";
            var expectedCategory       = "Vegetable";
            var expectedPrice          = 0.80m;
            var expectedWeight         = 100.0m;
            var expectedUnit           = "g";
            var expectedStock          = 100;
            var expectedUserId         = "cktan";

            //Act
            var plantDetailsController = new PlantListingController(plantDetailsContext, _mockPlantImageService.Object);
            var actionResult           = await plantDetailsController.CreatePlantDetails(validCreatePlantDetailsViewModel);

            //Assert
            Assert.IsType <CreatedAtActionResult>(actionResult.Result);
            Assert.Equal("GetPlantDetails", (actionResult.Result as CreatedAtActionResult).ActionName);
            Assert.IsType <PlantDetailsViewModel>((actionResult.Result as CreatedAtActionResult).Value);

            PlantDetailsViewModel actualViewModel = (actionResult.Result as CreatedAtActionResult).Value as PlantDetailsViewModel;

            Assert.Equal(expectedPlantDetailsId, actualViewModel.PlantDetailsId);
            Assert.Equal(expectedName, actualViewModel.Name);
            Assert.Equal(expectedDescription, actualViewModel.Description);
            Assert.Equal(expectedCategory, actualViewModel.Category);
            Assert.Equal(expectedPrice, actualViewModel.Price);
            Assert.Equal(expectedWeight, actualViewModel.Weight);
            Assert.Equal(expectedUnit, actualViewModel.Unit);
            Assert.Equal(expectedStock, actualViewModel.Stock);
            Assert.Equal(expectedUserId, actualViewModel.UserId);
        }
Exemple #12
0
        public WeightUnitsControllerTest()
        {
            _dbOptions = new DbContextOptionsBuilder <PlantListingContext>()
                         .UseInMemoryDatabase(databaseName: "in-memory weight units")
                         .Options;

            using (var dbContext = new PlantListingContext(_dbOptions))
            {
                if (!dbContext.WeightUnits.Any())
                {
                    dbContext.WeightUnits.AddRange(PlantListingContextSeed.GetPreconfiguredWeightUnits());
                }

                dbContext.SaveChanges();
            }
        }
        public PlantCategoriesControllerTest()
        {
            _dbOptions = new DbContextOptionsBuilder <PlantListingContext>()
                         .UseInMemoryDatabase(databaseName: "in-memory plant categories")
                         .Options;

            using (var dbContext = new PlantListingContext(_dbOptions))
            {
                if (!dbContext.PlantCategories.Any())
                {
                    dbContext.PlantCategories.AddRange(PlantListingContextSeed.GetPreconfiguredPlantCategories());
                }

                dbContext.SaveChanges();
            }
        }
        public async Task Delete_plant_details_unauthorized_reponse(string userId)
        {
            //Arrange
            var plantDetailsContext = new PlantListingContext(_dbOptions_Delete);

            var plantDetailsId = 7;

            //Act
            var plantDetailsController = new PlantListingController(plantDetailsContext, _mockPlantImageService.Object);
            var actionResult           = await plantDetailsController.DeletePlantDetails(new DeletePlantDetailsViewModel()
            {
                UserId         = userId,
                PlantDetailsId = plantDetailsId
            });

            //Assert
            Assert.IsType <UnauthorizedResult>(actionResult);
        }
        public async Task Delete_plant_details_not_found_reponse()
        {
            //Arrange
            var plantDetailsContext = new PlantListingContext(_dbOptions_Delete);

            var plantDetailsId = 99;
            var userId         = "cktan";

            //Act
            var plantDetailsController = new PlantListingController(plantDetailsContext, _mockPlantImageService.Object);
            var actionResult           = await plantDetailsController.DeletePlantDetails(new DeletePlantDetailsViewModel()
            {
                UserId         = userId,
                PlantDetailsId = plantDetailsId
            });

            //Assert
            Assert.IsType <NotFoundObjectResult>(actionResult);
        }
        public async Task Delete_plant_details_success()
        {
            //Arrange
            var plantDetailsContext = new PlantListingContext(_dbOptions_Delete);

            var plantDetailsId = 8;
            var userId         = "wpkeoh";

            //Act
            var plantDetailsController = new PlantListingController(plantDetailsContext, _mockPlantImageService.Object);
            var actionResult           = await plantDetailsController.DeletePlantDetails(new DeletePlantDetailsViewModel()
            {
                UserId         = userId,
                PlantDetailsId = plantDetailsId
            });

            //Assert
            Assert.IsType <NoContentResult>(actionResult);
        }
        private void SeedDBContext(DbContextOptions <PlantListingContext> dbOptions)
        {
            using (var dbContext = new PlantListingContext(dbOptions))
            {
                if (!dbContext.PlantCategories.Any())
                {
                    dbContext.PlantCategories.AddRange(PlantListingContextSeed.GetPreconfiguredPlantCategories());
                }

                if (!dbContext.WeightUnits.Any())
                {
                    dbContext.WeightUnits.AddRange(PlantListingContextSeed.GetPreconfiguredWeightUnits());
                }

                if (!dbContext.PlantDetails.Any())
                {
                    dbContext.PlantDetails.AddRange(PlantListingContextSeed.GetPreconfiguredPlantDetails());
                }

                dbContext.SaveChanges();
            }
        }
 public WeightUnitsController(PlantListingContext context)
 {
     _context = context;
 }
Exemple #19
0
 public PlantCategoriesController(PlantListingContext context)
 {
     _context = context;
 }
 public PlantListingController(PlantListingContext context, IPlantImageService plantImageService)
 {
     _context           = context ?? throw new ArgumentNullException(nameof(context));
     _plantImageService = plantImageService ?? throw new ArgumentNullException(nameof(plantImageService));
 }