Пример #1
0
        public async Task AddReviewShouldCreateANewReviewToRecipe()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;

            AutoMapperConfig.RegisterMappings(Assembly.Load("CookingBook.Web.ViewModels"));
            var dbContext = new ApplicationDbContext(options);

            var recipeRepo            = new EfDeletableEntityRepository <Recipe>(dbContext);
            var nutritionRepo         = new EfDeletableEntityRepository <NutritionValue>(dbContext);
            var productRepo           = new EfDeletableEntityRepository <Product>(dbContext);
            var userRepo              = new EfDeletableEntityRepository <ApplicationUser>(dbContext);
            var service               = new RecipesService(recipeRepo, nutritionRepo, productRepo, userRepo);
            var category              = new Category();
            var nutrValue             = new NutritionValue();
            var user                  = new ApplicationUser();
            var prod                  = new Collection <RecipeByIdProductsViewModel>();
            var recipeCreateViewModel = new RecipeCreateViewModel
            {
                CategoryId     = 1,
                CookProcedure  = "cookProc",
                Photo          = "photo",
                Serving        = 1,
                Title          = "addNew",
                CookTime       = 2,
                NutritionValue = new RecipeCreateNutritionValuesViewModel
                {
                    Calories = 1, Carbohydrates = 1, Fats = 1, Fiber = 1, Protein = 1, Salt = 1, Sugar = 1,
                },
                Products = new List <RecipeCreateProductsViewModel>(),
            };
            string       userId       = "trayan";
            StringValues sv           = new StringValues("one");
            StringValues sk           = new StringValues("1");
            var          recipeResult = await service.CreateAsync(recipeCreateViewModel, userId, sv, sk);

            var model = new ReviewForRecipeViewModel
            {
                Comment   = "commentOne",
                RecipeId  = recipeResult,
                Score     = 5,
                UserId    = "trayan",
                CreatedOn = DateTime.Now,
            };
            var reviewResult = await service.AddReview(model);

            Assert.IsType <string>(reviewResult);
            Assert.NotEmpty(reviewResult);
            Assert.NotNull(reviewResult);
            Assert.True(dbContext.Reviews.Any());
        }
Пример #2
0
        public async Task EditByAdminShouldChangeRecipe()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;

            AutoMapperConfig.RegisterMappings(Assembly.Load("CookingBook.Web.ViewModels"));
            var dbContext             = new ApplicationDbContext(options);
            var recipeRepo            = new EfDeletableEntityRepository <Recipe>(dbContext);
            var nutritionRepo         = new EfDeletableEntityRepository <NutritionValue>(dbContext);
            var productRepo           = new EfDeletableEntityRepository <Product>(dbContext);
            var userRepo              = new EfDeletableEntityRepository <ApplicationUser>(dbContext);
            var service               = new RecipesService(recipeRepo, nutritionRepo, productRepo, userRepo);
            var category              = new Category();
            var nutrValue             = new NutritionValue();
            var user                  = new ApplicationUser();
            var prod                  = new Collection <RecipeByIdProductsViewModel>();
            var recipeCreateViewModel = new RecipeCreateViewModel
            {
                CategoryId     = 1,
                CookProcedure  = "cookProc",
                Photo          = "photo",
                Serving        = 1,
                Title          = "addNew",
                CookTime       = 2,
                NutritionValue = new RecipeCreateNutritionValuesViewModel
                {
                    Calories = 1, Carbohydrates = 1, Fats = 1, Fiber = 1, Protein = 1, Salt = 1, Sugar = 1
                },
                Products = new List <RecipeCreateProductsViewModel>(),
            };
            string       userId       = "trayan";
            StringValues sv           = new StringValues("one");
            StringValues sk           = new StringValues("1");
            var          recipeResult = await service.CreateAsync(recipeCreateViewModel, userId, sv, sk);

            var model = new AdminRecipeViewModel
            {
                Id            = recipeResult,
                CategoryId    = 5,
                CookProcedure = "five",
                CookTime      = 5,
                Photo         = "fifthPhoto",
                Serving       = 5,
                Title         = "fifthEdit",
            };

            await service.EditByAdmin(model);

            Assert.Equal(5, dbContext.Recipes.FirstOrDefault(x => x.Id == recipeResult).CategoryId);
        }
Пример #3
0
        public async Task GetByIdShouldReturnTheExpectedType()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;

            AutoMapperConfig.RegisterMappings(Assembly.Load("CookingBook.Web.ViewModels"));
            var dbContext = new ApplicationDbContext(options);

            var category  = new Category();
            var nutrValue = new NutritionValue();
            var user      = new ApplicationUser();
            var prod      = new Collection <RecipeByIdProductsViewModel>();

            dbContext.Recipes.Add(new Recipe
            {
                Id         = "a", CreatedOn = DateTime.Parse("2008-11-01T19:31:00.0000000Z"), Title = "newTitle1",
                CategoryId = 1,
            });
            dbContext.Recipes.Add(new Recipe
            {
                Id         = "b", CreatedOn = DateTime.Parse("2008-11-01T19:32:00.0000000Z"), Title = "newTitle2",
                CategoryId = 1,
            });
            dbContext.Recipes.Add(new Recipe
            {
                Id         = "c", CreatedOn = DateTime.Parse("2008-11-01T19:33:00.0000000Z"), Title = "newTitle3",
                CategoryId = 2,
            });
            await dbContext.SaveChangesAsync();

            var recipeRepo    = new EfDeletableEntityRepository <Recipe>(dbContext);
            var nutritionRepo = new EfDeletableEntityRepository <NutritionValue>(dbContext);
            var productRepo   = new EfDeletableEntityRepository <Product>(dbContext);
            var userRepo      = new EfDeletableEntityRepository <ApplicationUser>(dbContext);
            var service       = new RecipesService(recipeRepo, nutritionRepo, productRepo, userRepo);

            Assert.IsType <RecipeDTO>(service.GetById <RecipeDTO>("a"));
            Assert.IsNotType <RecipeDTO>(service.GetById <RecipeDTO>("asd"));
        }
Пример #4
0
        public async Task GetAllShouldReturnNewestFiveRecipes()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;

            AutoMapperConfig.RegisterMappings(Assembly.Load("CookingBook.Web.ViewModels"));
            var dbContext = new ApplicationDbContext(options);

            var category  = new Category();
            var nutrValue = new NutritionValue();
            var user      = new ApplicationUser();
            var prod      = new Collection <RecipeByIdProductsViewModel>();

            dbContext.Recipes.Add(new Recipe
            {
                Id = "a", CreatedOn = DateTime.Parse("2008-11-01T19:31:00.0000000Z"), Title = "newTitle1",
                NutritionValueId = "a", CategoryId = 1, CookProcedure = "a", CookTime = 1, Photo = "a", Serving = 1,
                UserId           = "az", Category = category, NutritionValue = nutrValue, User = user,
            });
            dbContext.Recipes.Add(new Recipe
            {
                Id = "b", CreatedOn = DateTime.Parse("2008-11-01T19:32:00.0000000Z"), Title = "newTitle2",
                NutritionValueId = "a", CategoryId = 1, CookProcedure = "a", CookTime = 1, Photo = "a", Serving = 1,
                UserId           = "az", Category = category, NutritionValue = nutrValue, User = user,
            });
            dbContext.Recipes.Add(new Recipe
            {
                Id = "c", CreatedOn = DateTime.Parse("2008-11-01T19:33:00.0000000Z"), Title = "newTitle3",
                NutritionValueId = "a", CategoryId = 1, CookProcedure = "a", CookTime = 1, Photo = "a", Serving = 1,
                UserId           = "az", Category = category, NutritionValue = nutrValue, User = user,
            });
            dbContext.Recipes.Add(new Recipe
            {
                Id = "d", CreatedOn = DateTime.Parse("2008-11-01T19:34:00.0000000Z"), Title = "newTitle4",
                NutritionValueId = "a", CategoryId = 1, CookProcedure = "a", CookTime = 1, Photo = "a", Serving = 1,
                UserId           = "az", Category = category, NutritionValue = nutrValue, User = user,
            });
            dbContext.Recipes.Add(new Recipe
            {
                Id = "e", CreatedOn = DateTime.Parse("2008-11-01T19:35:00.0000000Z"), Title = "newTitle5",
                NutritionValueId = "a", CategoryId = 1, CookProcedure = "a", CookTime = 1, Photo = "a", Serving = 1,
                UserId           = "az", Category = category, NutritionValue = nutrValue, User = user,
            });
            dbContext.Recipes.Add(new Recipe
            {
                Id = "f", CreatedOn = DateTime.Parse("2008-11-01T19:36:00.0000000Z"), Title = "newTitle6",
                NutritionValueId = "a", CategoryId = 1, CookProcedure = "a", CookTime = 1, Photo = "a", Serving = 1,
                UserId           = "az", Category = category, NutritionValue = nutrValue, User = user,
            });
            dbContext.Recipes.Add(new Recipe
            {
                Id = "g", CreatedOn = DateTime.Parse("2008-11-01T19:37:00.0000000Z"), Title = "newTitle7",
                NutritionValueId = "a", CategoryId = 1, CookProcedure = "a", CookTime = 1, Photo = "a", Serving = 1,
                UserId           = "az", Category = category, NutritionValue = nutrValue, User = user,
            });
            dbContext.Recipes.Add(new Recipe
            {
                Id = "h", CreatedOn = DateTime.Parse("2008-11-01T19:38:00.0000000Z"), Title = "newTitle8",
                NutritionValueId = "a", CategoryId = 1, CookProcedure = "a", CookTime = 1, Photo = "a", Serving = 1,
                UserId           = "az", Category = category, NutritionValue = nutrValue, User = user,
            });
            await dbContext.SaveChangesAsync();

            var recipeRepo    = new EfDeletableEntityRepository <Recipe>(dbContext);
            var nutritionRepo = new EfDeletableEntityRepository <NutritionValue>(dbContext);
            var productRepo   = new EfDeletableEntityRepository <Product>(dbContext);
            var userRepo      = new EfDeletableEntityRepository <ApplicationUser>(dbContext);
            var service       = new RecipesService(recipeRepo, nutritionRepo, productRepo, userRepo);

            Assert.Equal(6, service.GetNewestFiveRecipes <RecipeDTO>().Count());
        }