Ejemplo n.º 1
0
        public async Task UpdateCake_WithdDuplicateImageUrl_ShouldNotUpdatedProduct()
        {
            //Arange
            var db = this.SetDb();

            await this.SeedProducts(db);

            var repo = new Repository <Product>(db);

            var cakeService = new CakeService(null, repo, this.Mapper);

            var entity = repo.All().SingleOrDefault(p => p.Id == 1);

            db.Entry(entity).State = EntityState.Detached;

            var model = this.Mapper.Map <Product, EditAndDeleteViewModel>(entity);

            model.Image = "https://res.cloudinary.com/cakeit/image/upload/ar_1:1,c_fill,g_auto,e_art:hokusai/v1544136590/Chocolate_Drip_cake.jpg";

            //Act
            var result = await cakeService.UpdateCake(model);

            var actual = await cakeService.GetCakeById(1);

            string imageExpected = "https://res.cloudinary.com/cakeit/image/upload/ar_1:1,c_fill,g_auto,e_art:hokusai/v1544136591/Chocolate_and_Peanut_cake.jpg";
            string imageActual   = actual.Image;

            //Assert
            Assert.Equal(imageExpected, imageActual);
            Assert.Equal("Product with such image url already exists.", result);
        }
Ejemplo n.º 2
0
        //public IHttpActionResult DeleteCake(int id)
        //{
        //    var service = CreateCakeService();
        //    if (!service.DeleteCake(id))
        //        return InternalServerError();
        //    return Ok();
        //}
        // [Route("api/Cakes/{id}")]
        public IHttpActionResult GetById(int id)
        {
            CakeService cakeService = CreateCakeService();
            var         cake        = cakeService.GetCakeById(id);

            return(Ok(cake));
        }
Ejemplo n.º 3
0
        public async Task UpdateCake_WithdDuplicateName_ShouldNotUpdatedProduct()
        {
            //Arange
            var db = this.SetDb();

            await this.SeedProducts(db);

            var repo = new Repository <Product>(db);

            var cakeService = new CakeService(null, repo, this.Mapper);

            var entity = repo.All().SingleOrDefault(p => p.Id == 1);

            db.Entry(entity).State = EntityState.Detached;

            var model = this.Mapper.Map <Product, EditAndDeleteViewModel>(entity);

            model.Name = "Chocolate Drip Cake";

            //Act
            var result = await cakeService.UpdateCake(model);

            var actual = await cakeService.GetCakeById(1);

            string name = actual.Name;

            //Assert
            Assert.Equal("Chocolate Peanut Cake", name);
            Assert.Equal("Product with such name already exists.", result);
        }
Ejemplo n.º 4
0
        public async Task UpdateCake_WithValidData_ShouldSaveUpdatedProductInDb()
        {
            //Arange
            var db = this.SetDb();

            await this.SeedProducts(db);

            var repo = new Repository <Product>(db);

            var cakeService = new CakeService(null, repo, this.Mapper);

            var entity = repo.All().SingleOrDefault(p => p.Id == 1);

            db.Entry(entity).State = EntityState.Detached;

            entity.Price = 45.00m;

            //Act
            var result = await cakeService.UpdateCake(this.Mapper.Map <Product, EditAndDeleteViewModel>(entity));

            var actual = await cakeService.GetCakeById(1);

            decimal price = actual.Price;

            //Assert
            Assert.Equal(45, price);
        }
Ejemplo n.º 5
0
        public async Task GetCakesById_WithInValidId_ShouldReturnNull()
        {
            //Arange
            var db = this.SetDb();

            await this.SeedProducts(db);

            var repo = new Repository <Product>(db);

            var cakeService = new CakeService(null, repo, this.Mapper);

            //Act

            //Assert
            await Assert.ThrowsAnyAsync <NullReferenceException>(async() => await(cakeService.GetCakeById(3)));
        }
Ejemplo n.º 6
0
        public async Task GetCakesById_WithValidId_ShouldReturnProduct()
        {
            //Arange
            var db = this.SetDb();

            await this.SeedProducts(db);

            var repo = new Repository <Product>(db);

            var cakeService = new CakeService(null, repo, this.Mapper);

            //Act
            var result = await cakeService.GetCakeById(1);

            var productId = repo.All().SingleOrDefault(p => p.Name == result.Name).Id;

            //Assert
            Assert.Equal(1, productId);
            result.ShouldBeOfType <EditAndDeleteViewModel>();
        }
Ejemplo n.º 7
0
        public async Task DeleteCake_WithValidId_ShouldDeleteProduct()
        {
            //Arange
            var db = this.SetDb();

            await this.SeedProducts(db);

            var repo = new Repository <Product>(db);

            var cakeService = new CakeService(null, repo, this.Mapper);

            var cake = await cakeService.GetCakeById(1);

            //Act
            await cakeService.DeleteCake(cake);

            var actualProductsCount = repo.All().Where(p => p.IsDeleted == true).Count();;

            var expectedProductsCount = 1;

            //Assert
            Assert.Equal(expectedProductsCount, actualProductsCount);
        }