public async Task Post_Success(int rateValue, string comment)
        {
            // Arrange
            var dbContext = _fixture.Context;
            var mapper    = MapperMock.Get();
            var user      = NewDatas.NewUser();
            var product   = NewDatas.NewProduct();
            var rating    = NewDatas.NewRating();

            rating.User    = user;
            rating.Product = product;

            dbContext.Products.Add(product);
            await dbContext.SaveChangesAsync();

            dbContext.Users.Add(user);
            await dbContext.SaveChangesAsync();

            dbContext.Ratings.Add(rating);
            await dbContext.SaveChangesAsync();

            var ratingsService    = new RatingService(dbContext);
            var ratingsController = new RatingsController(ratingsService, mapper);
            // Act
            var result = await ratingsController.PostRating(user.Id, product.productId, rateValue, comment);

            // Assert
            var ratingResult = Assert.IsType <CreatedAtActionResult>(result.Result);

            Assert.NotNull(ratingResult.Value as Rating);
        }
Exemple #2
0
        public async Task Put_Success()
        {
            // Arrange
            var dbContext        = _fixture.Context;
            var mapper           = MapperMock.Get();
            var fileService      = FileServiceMock.FilesService();
            var productToPut     = NewDatas.NewProductPutRequest();
            var productBeforePut = NewDatas.NewProduct(); productBeforePut.productId = 10; //set productId in order to update
            var productAfterPut  = NewDatas.ProductAfterPut();
            var category         = NewDatas.NewCategory();

            await dbContext.Categories.AddAsync(category);

            await dbContext.Products.AddAsync(productBeforePut);

            await dbContext.SaveChangesAsync(); productToPut.categoryId = category.categoryId;
            var productsService    = new ProductService(dbContext, fileService, mapper);
            var productsController = new ProductsController(productsService, fileService);
            // Act
            var result = await productsController.PutProduct(10, productToPut);

            // Assert
            Assert.IsType <OkObjectResult>(result.Result);

            Assert.Equal(productBeforePut.productName, productAfterPut.productName);
            Assert.Equal(productBeforePut.productPrice, productAfterPut.productPrice);
            Assert.Equal(productBeforePut.productDescription, productAfterPut.productDescription);
            Assert.Equal(productBeforePut.Images.Count, productAfterPut.Images.Count);
            Assert.Equal(productBeforePut.Category, category);
        }
Exemple #3
0
        public async Task GetCate_Success()
        {
            // get MOCK
            var mapper = MapperMock.Get();
            // initial mock data
            var dbContext = _fixture.Context;

            dbContext.TypeProducts.Add(new TypeProduct {
                Name = "Product Type"
            });
            var category = new Category {
                Name = "Product Category", TypeProductId = 1
            };

            dbContext.Categories.Add(category);
            await dbContext.SaveChangesAsync();

            // create dependency
            var cateDao     = new CategoryDAO(dbContext);
            var cateService = new CategoryService(cateDao, mapper);
            // test
            var result = cateService.GetList(1);

            Assert.NotEmpty(result);
        }
        public async void GetAll_Success()
        {
            // Arrange
            var dbContext = _fixture.Context;
            var mapper    = MapperMock.Get();
            var user      = NewDatas.NewUser();
            var product   = NewDatas.NewProduct();
            var rating    = NewDatas.NewRating();

            rating.User    = user;
            rating.Product = product;

            dbContext.Products.Add(product);
            await dbContext.SaveChangesAsync();

            dbContext.Users.Add(user);
            await dbContext.SaveChangesAsync();

            dbContext.Ratings.Add(rating);
            await dbContext.SaveChangesAsync();

            var ratingsService    = new RatingService(dbContext);
            var ratingsController = new RatingsController(ratingsService, mapper);
            // Act
            var result = await ratingsController.GetRatings();

            // Assert
            var ratingResult = Assert.IsType <OkObjectResult>(result.Result);

            Assert.NotEmpty(ratingResult.Value as IEnumerable <Rating>);
        }
        public async Task GetSingle_Success()
        {
            var dbContext   = _fixture.Context;
            var mapper      = MapperMock.Get();
            var fileService = FileServiceMock.FilesService();
            var user        = NewDatas.NewUser();
            await dbContext.Users.AddAsync(user);

            await dbContext.SaveChangesAsync();

            var product1 = NewDatas.NewProduct();
            var product2 = NewDatas.NewProduct();
            var product3 = NewDatas.NewProduct();
            await dbContext.Products.AddRangeAsync(product1, product2, product3);

            await dbContext.SaveChangesAsync();

            var order = NewDatas.NewOrder();

            dbContext.Orders.Add(order);
            await dbContext.SaveChangesAsync();

            var orderDetail1 = NewDatas.NewOrderDetail();

            orderDetail1.productId = product1.productId;
            orderDetail1.orderId   = order.orderId;
            var orderDetail2 = NewDatas.NewOrderDetail();

            orderDetail2.productId = product2.productId;
            orderDetail2.orderId   = order.orderId;
            var orderDetail3 = NewDatas.NewOrderDetail();

            orderDetail3.productId = product3.productId;
            orderDetail3.orderId   = order.orderId;
            await dbContext.OrderDetails.AddRangeAsync(orderDetail1, orderDetail2, orderDetail3);

            await dbContext.SaveChangesAsync();

            order.orderDetails = new List <OrderDetail> {
                orderDetail1, orderDetail2, orderDetail3
            };
            order.user = user;
            await dbContext.SaveChangesAsync();

            var ordersService    = new OrderService(dbContext, mapper);
            var ordersController = new OrdersController(ordersService, mapper, fileService);
            // Act
            var result = await ordersController.GetOrder(order.orderId);

            // Assert
            var ordersResult = Assert.IsType <OkObjectResult>(result.Result);

            Assert.NotNull(ordersResult.Value as Order);
        }
        public async void GetAll_Success()
        {
            // Arrange
            var dbContext = _fixture.Context;
            var mapper    = MapperMock.Get();

            var user = NewDatas.NewUser();
            await dbContext.Users.AddAsync(user);

            await dbContext.SaveChangesAsync();

            var product1 = NewDatas.NewProduct();
            var product2 = NewDatas.NewProduct();
            var product3 = NewDatas.NewProduct();
            await dbContext.Products.AddRangeAsync(product1, product2, product3);

            await dbContext.SaveChangesAsync();

            #region create intial order data
            var fileService = FileServiceMock.FilesService();
            var order       = NewDatas.NewOrder();
            dbContext.Orders.Add(order);
            await dbContext.SaveChangesAsync();

            var orderDetail1 = NewDatas.NewOrderDetail();
            orderDetail1.productId = product1.productId;
            orderDetail1.orderId   = order.orderId;
            var orderDetail2 = NewDatas.NewOrderDetail();
            orderDetail2.productId = product2.productId;
            orderDetail2.orderId   = order.orderId;
            var orderDetail3 = NewDatas.NewOrderDetail();
            orderDetail3.productId = product3.productId;
            orderDetail3.orderId   = order.orderId;
            await dbContext.OrderDetails.AddRangeAsync(orderDetail1, orderDetail2, orderDetail3);

            await dbContext.SaveChangesAsync();

            order.orderDetails = new List <OrderDetail> {
                orderDetail1, orderDetail2, orderDetail3
            };
            order.user = user;
            await dbContext.SaveChangesAsync();

            #endregion
            var ordersService    = new OrderService(dbContext, mapper);
            var ordersController = new OrdersController(ordersService, mapper, fileService);
            // Act
            var result = await ordersController.GetOrders();

            // Assert
            var ordersResult = Assert.IsType <OkObjectResult>(result.Result);
            Assert.NotEmpty(ordersResult.Value as IEnumerable <OrderResponse>);
        }
Exemple #7
0
        public void GetAll_Success()
        {
            // Arrange
            var dbContext         = _fixture.Context;
            var BlobService       = BlobServiceMock.BlobService();
            var mapper            = MapperMock.Get();
            var productRepository = new ProductRepository(dbContext, BlobService, mapper);
            var productController = new ProductController(productRepository);
            // Act
            var result = productController.GetProducts();

            // Assert
            Assert.IsType <OkObjectResult>(result.Result);
        }
Exemple #8
0
        public async Task Post_Success()
        {
            // Arrange
            var dbContext     = _fixture.Context;
            var mapper        = MapperMock.Get();
            var fileService   = BlobServiceMock.BlobService();
            var productToPost = NewData.CreateProduct();

            var productRepository = new ProductRepository(dbContext, fileService, mapper);
            var productController = new ProductController(productRepository);
            // Act
            var result = await productController.CreateProduct(productToPost);

            // Assert
            Assert.IsType <CreatedAtActionResult>(result.Result);
        }
        public async Task Post_Success()
        {
            // Arrange
            var dbContext     = _fixture.Context;
            var mapper        = MapperMock.Get();
            var fileService   = FileServiceMock.FilesService();
            var productToPost = NewDatas.NewProductPostRequest();

            var productsService    = new ProductService(dbContext, fileService, mapper);
            var productsController = new ProductsController(productsService, fileService);
            // Act
            var result = await productsController.PostProduct(productToPost);

            // Assert
            Assert.IsType <CreatedAtActionResult>(result.Result);
        }
        public async Task Post_Success()
        {
            // Arrange
            var dbContext = _fixture.Context;
            var mapper    = MapperMock.Get();

            var fileService = FileServiceMock.FilesService();

            var user = NewDatas.NewUser();
            await dbContext.Users.AddAsync(user);

            await dbContext.SaveChangesAsync();

            var product1 = NewDatas.NewProduct();
            await dbContext.Products.AddAsync(product1);

            await dbContext.SaveChangesAsync();

            var orderCreateRequest = NewDatas.NewOrderDetailRequest();

            orderCreateRequest.productId = product1.productId;

            var ordersService    = new OrderService(dbContext, mapper);
            var ordersController = new OrdersController(ordersService, mapper, fileService);

            #region set controller user
            ordersController.ControllerContext = new ControllerContext
            {
                HttpContext = new DefaultHttpContext
                {
                    User = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
                    {
                        new Claim(ClaimTypes.Name, user.UserName),
                        new Claim(ClaimTypes.NameIdentifier, user.Id.ToString(CultureInfo.InvariantCulture))
                    }, "Bearer")
                                               )
                }
            };
            #endregion
            // Act
            var result = await ordersController.PostOrder(orderCreateRequest);

            // Assert
            var ordersResult = Assert.IsType <CreatedAtActionResult>(result.Result);
            Assert.NotNull(ordersResult.Value as Order);
        }
Exemple #11
0
        public async Task GetAll_Success()
        {
            // Arrange
            var dbContext   = _fixture.Context;
            var mapper      = MapperMock.Get();
            var fileService = FileServiceMock.FilesService();
            var product1    = NewDatas.NewProduct();

            dbContext.Products.Add(product1);
            dbContext.SaveChanges();

            var productsService    = new ProductService(dbContext, fileService, mapper);
            var productsController = new ProductsController(productsService, fileService);
            // Act
            var result = await productsController.GetProducts();

            // Assert
            Assert.IsType <OkObjectResult>(result.Result);
        }
Exemple #12
0
        public void PostCate_Success()
        {
            // get MOCK
            var mapper = MapperMock.Get();
            // initial mock data
            var dbContext = _fixture.Context;

            dbContext.TypeProducts.Add(new TypeProduct {
                Name = "Product Type"
            });
            // create dependency
            var cateDao = new CategoryDAO(dbContext);
            // test
            var category = new Category {
                Name = "Product Category Post", TypeProductId = 1
            };
            var result = cateDao.AddItem(category);

            var createAtAction = Assert.IsType <CategoryVM>(result);

            Assert.Equal("Product Category Post", createAtAction.Name);
        }
Exemple #13
0
        public async Task Put_Success()
        {
            // Arrange
            var    dbContext = _fixture.Context;
            var    mapper = MapperMock.Get();
            var    user = NewDatas.NewUser();
            var    product = NewDatas.NewProduct();
            var    rating = NewDatas.NewRating();
            string commentToPut, commentAfterPut;

            commentToPut = commentAfterPut = "Comment Changed";
            int valueToPut, valueAfterPut;

            valueToPut     = valueAfterPut = 5;
            rating.User    = user;
            rating.Product = product;

            dbContext.Products.Add(product);
            await dbContext.SaveChangesAsync();

            dbContext.Users.Add(user);
            await dbContext.SaveChangesAsync();

            dbContext.Ratings.Add(rating);
            await dbContext.SaveChangesAsync();

            var ratingsService    = new RatingService(dbContext);
            var ratingsController = new RatingsController(ratingsService, mapper);
            // Act
            var result = await ratingsController.PutRating(rating.ratingId, user.Id, product.productId, valueToPut, commentToPut);

            // Assert
            var ratingResult = Assert.IsType <OkObjectResult>(result);

            Assert.Equal((ratingResult.Value as Rating).rateValue, valueAfterPut);
            Assert.Equal((ratingResult.Value as Rating).comment, commentAfterPut);
        }
        public async Task GetSingle_Success(int id)
        {
            // Arrange
            var dbContext = _fixture.Context;
            var mapper    = MapperMock.Get();
            var user1     = NewDatas.NewUser();
            var user2     = NewDatas.NewUser();
            var product   = NewDatas.NewProduct();
            var rating    = NewDatas.NewRating();

            rating.User    = user1;
            rating.Product = product;
            var rating1 = NewDatas.NewRating();

            rating1.User    = user2;
            rating1.Product = product;

            dbContext.Products.Add(product);
            await dbContext.SaveChangesAsync();

            dbContext.Users.AddRange(user1, user2);
            await dbContext.SaveChangesAsync();

            dbContext.Ratings.AddRange(rating, rating1);
            await dbContext.SaveChangesAsync();

            var ratingsService    = new RatingService(dbContext);
            var ratingsController = new RatingsController(ratingsService, mapper);
            // Act
            var result = await ratingsController.GetRatingByProductId(id);

            // Assert
            var ratingResult = Assert.IsType <OkObjectResult>(result.Result);

            Assert.NotNull(ratingResult.Value);
        }
        public void PostCate_Success()
        {
            // get MOCK
            var mapper = MapperMock.Get();
            // initial mock data
            var dbContext = _fixture.Context;

            dbContext.TypeProducts.Add(new TypeProduct {
                Name = "Product Type"
            });
            // create dependency
            var cateDao     = new CategoryDAO(dbContext);
            var cateService = new CategoryService(cateDao, mapper);
            // test
            var categoryVM = new CategoryVM {
                Name = "Product Category Post", TypeProductId = 1
            };
            var cateController = new CategoryController(cateService);
            var result         = cateController.Create(categoryVM);

            var createAtAction = Assert.IsType <ActionResult <CategoryVM> >(result.Value);

            Assert.Equal("Product Category Post", createAtAction.Value.Name);
        }
Exemple #16
0
        public async Task Put_Success(int quantity)
        {
            // Arrange
            var dbContext = _fixture.Context;
            var mapper    = MapperMock.Get();

            var fileService = FileServiceMock.FilesService();

            var user = NewDatas.NewUser();
            await dbContext.Users.AddAsync(user);

            await dbContext.SaveChangesAsync();

            var product1 = NewDatas.NewProduct();
            await dbContext.Products.AddRangeAsync(product1);

            await dbContext.SaveChangesAsync();

            #region create intial order data
            var order = NewDatas.NewOrder();
            dbContext.Orders.Add(order);
            await dbContext.SaveChangesAsync();

            var orderDetail1 = NewDatas.NewOrderDetail();
            orderDetail1.productId = product1.productId;
            orderDetail1.orderId   = order.orderId;

            int initialQuantity = orderDetail1.quantity;

            await dbContext.OrderDetails.AddRangeAsync(orderDetail1);

            await dbContext.SaveChangesAsync();

            order.orderDetails = new List <OrderDetail> {
                orderDetail1
            };
            order.user = user;
            await dbContext.SaveChangesAsync();

            #endregion

            var orderCreateRequest = NewDatas.NewOrderDetailRequest();
            orderCreateRequest.productId = product1.productId;
            orderCreateRequest.quantity  = quantity;

            int afterPostQuantity = quantity;

            var ordersService    = new OrderService(dbContext, mapper);
            var ordersController = new OrdersController(ordersService, mapper, fileService);

            #region set controller user
            ordersController.ControllerContext = new ControllerContext
            {
                HttpContext = new DefaultHttpContext
                {
                    User = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
                    {
                        new Claim(ClaimTypes.Name, user.UserName),
                        new Claim(ClaimTypes.NameIdentifier, user.Id.ToString(CultureInfo.InvariantCulture))
                    }, "Bearer")
                                               )
                }
            };
            #endregion

            // Act
            var result = await ordersController.PostOrder(orderCreateRequest);

            // Assert
            var ordersResult         = Assert.IsType <CreatedAtActionResult>(result.Result);
            var productInOrderResult = (ordersResult.Value as Order).orderDetails.First(x => x.productId == product1.productId);
            Assert.Equal(productInOrderResult.quantity, initialQuantity + afterPostQuantity);
        }