Example #1
0
        public void ShouldGetProductById()
        {
            _createRequestValidation = new CreateRequestValidation();
            _deleteRequestValidation = new DeleteRequestValidation();
            _findRequestValidation   = new FindRequestValidation();
            _updateRequestValidation = new UpdateRequestValidation();

            _validation = new ProductValidation(_createRequestValidation, _findRequestValidation, _updateRequestValidation, _deleteRequestValidation);

            ProductFindRequest request = new ProductFindRequest();

            request.Id = 1;

            var mock = new Mock <IProduct>();

            mock.Setup(x => x.ReadSingle(request))
            .Returns(new Product
            {
                ProductId   = 1,
                Name        = "Milk",
                Category    = Category.Milk,
                Price       = 50,
                Discount    = 20,
                Description = "Milk from latvia"
            });

            _victim = new ProductService(mock.Object, _validation);
            Product actual = _victim.FindById(request).FoundProduct;

            string expected = "Milk";

            Assert.Equal(expected, actual.Name);
        }
        public void ShouldReturnErrorCodes(string name, long category_id, decimal price, decimal discount, string description, long file_id, ProductValidationErrors expectedError)
        {
            CreateRequestValidation createRequestValidation = new CreateRequestValidation();
            FindRequestValidation   findRequestValidation   = new FindRequestValidation();
            UpdateRequestValidation updateRequestValidation = new UpdateRequestValidation();
            DeleteRequestValidation deleteRequestValidation = new DeleteRequestValidation();

            ProductValidation validation = new ProductValidation(createRequestValidation, findRequestValidation,
                                                                 updateRequestValidation, deleteRequestValidation);

            ProductCreateRequest request = new ProductCreateRequest
            {
                Name        = name,
                Category_Id = category_id,
                Price       = price,
                Discount    = discount,
                Description = description,
                File_Id     = file_id
            };

            var listErrors = validation.CreateRequestValidation.Validate(request);

            foreach (var error in listErrors)
            {
                Assert.Equal(expectedError, error);
            }
        }
Example #3
0
 public ProductValidation(CreateRequestValidation createRequestValidation, FindRequestValidation findRequestValidation,
                          UpdateRequestValidation updateRequestValidation, DeleteRequestValidation deleteRequestValidation)
 {
     CreateRequestValidation = createRequestValidation;
     FindRequestValidation   = findRequestValidation;
     UpdateRequestValidation = updateRequestValidation;
     DeleteRequestValidation = deleteRequestValidation;
 }
Example #4
0
        public void ShouldUpdateById()
        {
            _createRequestValidation = new CreateRequestValidation();
            _deleteRequestValidation = new DeleteRequestValidation();
            _findRequestValidation   = new FindRequestValidation();
            _updateRequestValidation = new UpdateRequestValidation();

            _validation = new ProductValidation(_createRequestValidation, _findRequestValidation, _updateRequestValidation, _deleteRequestValidation);

            ProductUpdateRequest request = new ProductUpdateRequest();

            request.Id          = 2;
            request.Name        = "Pork";
            request.Category    = Category.Meat;
            request.Price       = 150;
            request.Discount    = 50;
            request.Description = "Pork from Nigeria.";

            var mock = new Mock <IProduct>();

            mock.Setup(x => x.Update(request))
            .Returns(new Product
            {
                ProductId   = 2,
                Name        = "Pork",
                Category    = Category.Meat,
                Price       = 150,
                Discount    = 50,
                Description = "Pork from Nigeria."
            });

            _victim = new ProductService(mock.Object, _validation);
            Product actual = _victim.UpdateById(request).UpdatedProduct;

            string  expectedName     = "Pork";
            decimal expectedPrice    = 150;
            decimal expectedDiscount = 50;

            Assert.Equal(expectedName, actual.Name);
            Assert.Equal(expectedPrice, actual.Price);
            Assert.Equal(expectedDiscount, actual.Discount);
        }
Example #5
0
        public void ShouldGetAllProducts()
        {
            _createRequestValidation = new CreateRequestValidation();
            _deleteRequestValidation = new DeleteRequestValidation();
            _findRequestValidation   = new FindRequestValidation();
            _updateRequestValidation = new UpdateRequestValidation();

            _validation = new ProductValidation(_createRequestValidation, _findRequestValidation, _updateRequestValidation, _deleteRequestValidation);

            var mock = new Mock <IProduct>();

            mock.Setup(x => x.ReadAll())
            .Returns(listProducts);

            _victim = new ProductService(mock.Object, _validation);
            List <Product> actual = _victim.FindAll().ListOfFoundProducts;

            int expected = 3;

            Assert.Equal(expected, actual.Count);
        }
Example #6
0
        public void ShouldReturnCreatedProduct()
        {
            _createRequestValidation = new CreateRequestValidation();
            _deleteRequestValidation = new DeleteRequestValidation();
            _findRequestValidation   = new FindRequestValidation();
            _updateRequestValidation = new UpdateRequestValidation();

            _validation = new ProductValidation(_createRequestValidation, _findRequestValidation, _updateRequestValidation, _deleteRequestValidation);

            var mockDB = new Mock <IProduct>();

            mockDB.Setup(x => x.Create(getProduct()))
            .Returns(getProduct());

            _victim = new ProductService(mockDB.Object, _validation);

            var actual   = _victim.AddProduct(request());
            var expected = getProduct();

            Assert.Equal(expected, actual.Product);
        }
Example #7
0
        public void ShouldDeleteById()
        {
            _createRequestValidation = new CreateRequestValidation();
            _deleteRequestValidation = new DeleteRequestValidation();
            _findRequestValidation   = new FindRequestValidation();
            _updateRequestValidation = new UpdateRequestValidation();

            _validation = new ProductValidation(_createRequestValidation, _findRequestValidation, _updateRequestValidation, _deleteRequestValidation);

            ProductDeleteRequest request = new ProductDeleteRequest();

            request.Id = 2;

            var mock = new Mock <IProduct>();

            mock.Setup(x => x.Delete(request))
            .Returns(true);

            _victim = new ProductService(mock.Object, _validation);

            Assert.True(_victim.Delete(request).HasDeleted);
        }