Beispiel #1
0
        public async void Menu_GetSalesAsync_Test()
        {
            var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>()
                          .UseInMemoryDatabase(databaseName: "Menu_GetSalesAsync_Test")
                          .Options;
            int testValue = 10;

            using (var context = new DreamFoodDeliveryContext(options))
            {
                _dishes[0].Sale = 0;
                _dishes[1].Sale = testValue;
                context.AddRange(_dishes);
                await context.SaveChangesAsync();
            }

            using (var context = new DreamFoodDeliveryContext(options))
            {
                var service = new MenuService(_mapper, context);

                var resultPositive = await service.GetSalesAsync();

                resultPositive.IsSuccess.Should().BeTrue();
                resultPositive.Data.Count().Should().Be(1);
                foreach (var item in resultPositive.Data)
                {
                    item.Sale.Should().Be(testValue);
                }
            }
        }
        public async void Dishes_GetByIdAsync_PositiveAndNegative_Test()
        {
            var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>()
                          .UseInMemoryDatabase(databaseName: "Dishes_GetByIdAsync_PositiveAndNegative_Test")
                          .Options;

            using (var context = new DreamFoodDeliveryContext(options))
            {
                context.AddRange(_dishes);
                await context.SaveChangesAsync();
            }

            using (var context = new DreamFoodDeliveryContext(options))
            {
                var dish = await context.Dishes.AsNoTracking().FirstOrDefaultAsync();

                var service = new DishService(_mapper, context, _tagService);

                var resultPositive = await service.GetByIdAsync(dish.Id.ToString());

                var resultNegative = await service.GetByIdAsync(new Guid().ToString());

                resultPositive.IsSuccess.Should().BeTrue();
                resultPositive.Data.Name.Should().BeEquivalentTo(dish.Name);
                resultPositive.Data.Composition.Should().BeEquivalentTo(dish.Composition);
                resultPositive.Data.Description.Should().BeEquivalentTo(dish.Description);
                resultPositive.Data.Weight.Should().BeEquivalentTo(dish.Weight);

                resultNegative.IsSuccess.Should().BeFalse();
                resultNegative.Data.Should().BeNull();
            }
        }
        public async void Dihes_AddAsync_Positive_Test()
        {
            var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>()
                          .UseInMemoryDatabase(databaseName: "Dihes_AddAsync_Positive_Test")
                          .Options;

            using (var context = new DreamFoodDeliveryContext(options))
            {
                var tagService = new TagService(_mapper, context);
                var service    = new DishService(_mapper, context, tagService);

                TagToAdd[] tags = new TagToAdd[] {
                    new TagToAdd
                    {
                        TagName = "New"
                    }
                };
                DishToAdd dish = new DishToAdd()
                {
                    Name        = "Name",
                    Composition = "Composition",
                    Description = "Description",
                    Weight      = "Weight",
                    TagNames    = new HashSet <TagToAdd>(tags)
                };

                var resultPositive = await service.AddAsync(dish);

                resultPositive.IsSuccess.Should().BeTrue();
                resultPositive.Data.Name.Should().BeEquivalentTo(dish.Name);
                resultPositive.Data.Composition.Should().BeEquivalentTo(dish.Composition);
                resultPositive.Data.Description.Should().BeEquivalentTo(dish.Description);
                resultPositive.Data.Weight.Should().BeEquivalentTo(dish.Weight);
            }
        }
Beispiel #4
0
        public async void Menu_GetAllAsync_Test()
        {
            var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>()
                          .UseInMemoryDatabase(databaseName: "Menu_GetAllAsync_Test")
                          .Options;

            // Run the test against one instance of the context
            using (var context = new DreamFoodDeliveryContext(options))
            {
                context.AddRange(_dishes);
                await context.SaveChangesAsync();
            }

            // Use a separate instance of the context to verify correct data was saved to database
            using (var context = new DreamFoodDeliveryContext(options))
            {
                var service = new MenuService(_mapper, context);

                var dishesInBase = await context.Dishes.AsNoTracking().ToListAsync();

                var result = await service.GetAllAsync();

                foreach (var item in dishesInBase)
                {
                    var itemFromResult = result.Data.Where(_ => _.Name.Equals(item.Name, StringComparison.OrdinalIgnoreCase)).Select(_ => _).FirstOrDefault();
                    itemFromResult.Should().NotBeNull();
                }
            }
        }
Beispiel #5
0
        public async void Menu_GetByNameAsync_PositiveAndNegative_Test()
        {
            var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>()
                          .UseInMemoryDatabase(databaseName: "Menu_GetByNameAsync_PositiveAndNegative_Test")
                          .Options;
            string testString = "NameTest";

            using (var context = new DreamFoodDeliveryContext(options))
            {
                _dishes[0].Name = testString;
                _dishes[1].Name = testString;

                context.AddRange(_dishes);
                await context.SaveChangesAsync();
            }

            using (var context = new DreamFoodDeliveryContext(options))
            {
                var service = new MenuService(_mapper, context);

                var resultPositive = await service.GetByNameAsync(testString);

                var resultNegative = await service.GetByNameAsync(testString + testString);

                resultPositive.IsSuccess.Should().BeTrue();
                foreach (var item in resultPositive.Data)
                {
                    item.Name.Should().BeEquivalentTo(testString);
                }

                resultNegative.IsSuccess.Should().BeFalse();
                resultNegative.Data.Should().BeNull();
            }
        }
 public BasketService(IMapper mapper, UserManager <AppUser> userManager, DreamFoodDeliveryContext context, IDishService dishService)
 {
     _context     = context;
     _userManager = userManager;
     _mapper      = mapper;
     _dishService = dishService;
 }
        public async void Dishes_RemoveByIdAsync_PositiveAndNegative_Test()
        {
            var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>()
                          .UseInMemoryDatabase(databaseName: "Dishes_RemoveByIdAsync_PositiveAndNegative_Test")
                          .Options;

            using (var context = new DreamFoodDeliveryContext(options))
            {
                context.AddRange(_dishes);
                await context.SaveChangesAsync();
            }

            using (var context = new DreamFoodDeliveryContext(options))
            {
                var dish = await context.Dishes.AsNoTracking().FirstOrDefaultAsync();

                var service = new DishService(_mapper, context, _tagService);

                var resultPositive = await service.RemoveByIdAsync(dish.Id.ToString());

                var resultNegative = await service.RemoveByIdAsync(new Guid().ToString());

                resultPositive.IsSuccess.Should().BeTrue();
                resultPositive.Message.Should().BeNull();

                resultNegative.IsSuccess.Should().BeFalse();
                resultNegative.Message.Should().Contain(ExceptionConstants.DISH_WAS_NOT_FOUND);
            }
        }
        public async void Reviews_RemoveAllAsync_Test()
        {
            var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>()
                          .UseInMemoryDatabase(databaseName: "Reviews_RemoveAllAsync_Test")
                          .Options;

            using (var context = new DreamFoodDeliveryContext(options))
            {
                context.AddRange(_reviews);
                int sum = 0;
                for (int i = 0; i < _reviews.Count; i++)
                {
                    sum += _reviews[i].Rating.Value;
                }
                RatingDB rating = new RatingDB()
                {
                    Sum   = sum,
                    Count = _reviews.Count
                };
                context.Add(rating);
                await context.SaveChangesAsync();
            }

            using (var context = new DreamFoodDeliveryContext(options))
            {
                var service = new ReviewService(_mapper, context);

                var resultPositive = await service.RemoveAllAsync();

                resultPositive.IsSuccess.Should().BeTrue();
                resultPositive.Message.Should().BeNull();
            }
        }
Beispiel #9
0
        public async void Menu_GetByPriceAsync_PositiveAndNegative_Test()
        {
            var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>()
                          .UseInMemoryDatabase(databaseName: "Menu_GetByPriceAsync_PositiveAndNegative_Test")
                          .Options;
            double testValue = 250;

            using (var context = new DreamFoodDeliveryContext(options))
            {
                _dishes[0].Price = testValue;
                _dishes[1].Price = testValue;
                context.AddRange(_dishes);
                await context.SaveChangesAsync();
            }

            using (var context = new DreamFoodDeliveryContext(options))
            {
                var service = new MenuService(_mapper, context);

                var resultPositive = await service.GetByPriceAsync(testValue - 10, testValue + 10);

                var resultNegative = await service.GetByPriceAsync(testValue - 20, testValue - 10);

                resultPositive.IsSuccess.Should().BeTrue();
                foreach (var item in resultPositive.Data)
                {
                    item.Price.Should().Be(testValue);
                }

                resultNegative.IsSuccess.Should().BeFalse();
                resultNegative.Data.Should().BeNull();
            }
        }
Beispiel #10
0
        public async void Tags_GetByIdAsync_PositiveAndNegative_Test()
        {
            var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>()
                          .UseInMemoryDatabase(databaseName: "Tags_GetByIdAsync_PositiveAndNegative_Test")
                          .Options;

            using (var context = new DreamFoodDeliveryContext(options))
            {
                context.AddRange(_tags);
                await context.SaveChangesAsync();
            }

            using (var context = new DreamFoodDeliveryContext(options))
            {
                var tag = await context.Tags.AsNoTracking().FirstOrDefaultAsync();

                var service = new TagService(_mapper, context);

                var resultPositive = await service.GetByIdAsync(tag.Id.ToString());

                var resultNegative = await service.GetByIdAsync(new Guid().ToString());

                resultPositive.IsSuccess.Should().BeTrue();
                resultPositive.Data.TagName.Should().BeEquivalentTo(tag.TagName);

                resultNegative.IsSuccess.Should().BeFalse();
                resultNegative.Data.Should().BeNull();
            }
        }
 public OrderService(IMapper mapper, UserManager <AppUser> userManager, DreamFoodDeliveryContext context, IDishService dishService, IEmailSenderService emailSender)
 {
     _context     = context;
     _userManager = userManager;
     _mapper      = mapper;
     _emailSender = emailSender;
     _dishService = dishService;
 }
 public AdminService(IMapper mapper, DreamFoodDeliveryContext context, UserManager <AppUser> userManager, IUserService userService, IEmailSenderService emailSender)
 {
     _context     = context;
     _mapper      = mapper;
     _userManager = userManager;
     _userService = userService;
     _emailSender = emailSender;
 }
        public async void Reviews_UpdateAsync_PositiveAndNegative_Test()
        {
            var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>()
                          .UseInMemoryDatabase(databaseName: "Reviews_UpdateAsync_PositiveAndNegative_Test")
                          .Options;

            using (var context = new DreamFoodDeliveryContext(options))
            {
                context.AddRange(_reviews);
                int sum = 0;
                for (int i = 0; i < _reviews.Count; i++)
                {
                    sum += _reviews[i].Rating.Value;
                }
                RatingDB rating = new RatingDB()
                {
                    Sum   = sum,
                    Count = _reviews.Count
                };
                context.Add(rating);
                await context.SaveChangesAsync();
            }

            using (var context = new DreamFoodDeliveryContext(options))
            {
                var review = await context.Reviews.AsNoTracking().FirstOrDefaultAsync();

                var rating = await context.Rating.AsNoTracking().FirstOrDefaultAsync();

                var service = new ReviewService(_mapper, context);

                ReviewToUpdate updateReview = new ReviewToUpdate()
                {
                    Id       = review.Id.ToString(),
                    Headline = "Headline",
                    Content  = "Content",
                    Rating   = 1
                };

                ReviewToUpdate failReview = new ReviewToUpdate()
                {
                    Id       = new Guid().ToString(),
                    Headline = "Headline",
                    Content  = "Content",
                    Rating   = 0
                };

                var resultPositive = await service.UpdateAsync(updateReview);

                var resultNegative = await service.UpdateAsync(failReview);

                resultPositive.IsSuccess.Should().BeTrue();
                resultPositive.Data.Headline.Should().BeEquivalentTo(updateReview.Headline);
                resultPositive.Data.Headline.Should().NotBeEquivalentTo(review.Headline);

                resultNegative.IsSuccess.Should().BeFalse();
            }
        }
        public async void Reviews_RemoveAllByUserIdAsync_PositiveAndNegative_Test()
        {
            var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>()
                          .UseInMemoryDatabase(databaseName: "Reviews_RemoveAllByUserIdAsync_PositiveAndNegative_Test")
                          .Options;

            UserDB userWithReviews;

            using (var context = new DreamFoodDeliveryContext(options))
            {
                context.AddRange(_users);
                await context.SaveChangesAsync();

                userWithReviews = context.Users.AsNoTracking().FirstOrDefault();

                foreach (var review in _reviews)
                {
                    review.UserId  = userWithReviews.Id;
                    review.OrderId = new Guid();
                }
                int sum = 0;
                for (int i = 0; i < _reviews.Count; i++)
                {
                    sum += _reviews[i].Rating.Value;
                }
                RatingDB rating = new RatingDB()
                {
                    Sum   = sum,
                    Count = _reviews.Count
                };
                context.Add(rating);
                context.AddRange(_reviews);
                await context.SaveChangesAsync();
            }

            using (var context = new DreamFoodDeliveryContext(options))
            {
                //var review = await context.Reviews.AsNoTracking().FirstOrDefaultAsync();

                var service = new ReviewService(_mapper, context);

                var resultPositive = await service.RemoveAllByUserIdAsync(userWithReviews.Id.ToString());

                var resultNegative = await service.RemoveAllByUserIdAsync(new Guid().ToString());

                resultPositive.IsSuccess.Should().BeTrue();
                resultPositive.Message.Should().BeNull();

                resultNegative.IsSuccess.Should().BeFalse();
                resultNegative.Message.Should().Contain(ExceptionConstants.REVIEWS_WERE_NOT_FOUND);
            }
        }
Beispiel #15
0
        public async void Menu_GetByTagIndexAsync_PositiveAndNegative_Test()
        {
            var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>()
                          .UseInMemoryDatabase(databaseName: "Menu_GetByTagIndexAsync_PositiveAndNegative_Test")
                          .Options;
            string tagTestName       = "new";
            string tagTestSecondName = "old";

            using (var context = new DreamFoodDeliveryContext(options))
            {
                TagDB[] tags = new TagDB[]
                {
                    new TagDB
                    {
                        TagName = tagTestName
                    },
                    new TagDB
                    {
                        TagName = tagTestSecondName
                    },
                };
                DishDB dish = new DishDB
                {
                    Name = "dish"
                };

                context.AddRange(tags);
                context.Add(dish);
                DishTagDB dishTag = new DishTagDB
                {
                    TagId  = tags.FirstOrDefault(x => x.TagName == tagTestName).Id,
                    DishId = dish.Id
                };
                context.Add(dishTag);

                await context.SaveChangesAsync();
            }

            using (var context = new DreamFoodDeliveryContext(options))
            {
                var service = new MenuService(_mapper, context);

                var resultPositive = await service.GetByTagIndexAsync(tagTestName);

                var resultNegative = await service.GetByTagIndexAsync(tagTestSecondName);

                resultPositive.IsSuccess.Should().BeTrue();
                resultNegative.IsSuccess.Should().BeFalse();
            }
        }
Beispiel #16
0
        public async void Basket_AddUpdateDishAsync_Positive_Test()
        {
            var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>()
                          .UseInMemoryDatabase(databaseName: "Basket_AddUpdateDishAsync_Positive_Test")
                          .Options;

            int count = 10;

            var storeManager = new Mock <IUserStore <AppUser> >();

            storeManager.Setup(x => x.FindByIdAsync(_userData.Id, CancellationToken.None))
            .ReturnsAsync(new AppUser()
            {
                PersonalDiscount = _userData.PersonalDiscount,
            });
            var mgr = new UserManager <AppUser>(storeManager.Object, null, null, null, null, null, null, null, null);


            using (var context = new DreamFoodDeliveryContext(options))
            {
                _user.IdFromIdentity = _userData.Id;
                context.Add(_user);
                context.AddRange(_dishes);
                BasketDB basket = new BasketDB()
                {
                    Id = _user.BasketId, UserId = _user.Id
                };
                context.Baskets.Add(basket);
                await context.SaveChangesAsync();
            }

            var storeDishService = new Mock <IDishService>();

            storeDishService.Setup(x => x.GetByIdAsync(_dishes.FirstOrDefault().Id.ToString(), CancellationToken.None))
            .ReturnsAsync(Result <DishView> .Ok(_mapper.Map <DishView>(_dishes.FirstOrDefault())));

            using (var context = new DreamFoodDeliveryContext(options))
            {
                var    dish = _dishes.FirstOrDefault();
                double basketPrice_first = (dish.Price * (100 - dish.Sale) / 100 * count * (100 - _userData.PersonalDiscount) / 100).Value;
                basketPrice_first = Math.Round(basketPrice_first, 2);
                var service        = new BasketService(_mapper, mgr, context, storeDishService.Object);
                var resultPositive = await service.AddUpdateDishAsync(dish.Id.ToString(), _userData.Id, count);

                resultPositive.IsSuccess.Should().BeTrue();
                resultPositive.Data.BasketCost.Should().Be(basketPrice_first);
            }
        }
Beispiel #17
0
        public async void User_GetUserProfileByIdFromIdentityAsync_Positive_Test()
        {
            var store = new Mock <IUserStore <AppUser> >();

            store.Setup(x => x.FindByIdAsync("11111111-222e-4276-82bf-7d0e0d12f1e9", CancellationToken.None))
            .ReturnsAsync(new AppUser()
            {
                Id               = "11111111-222e-4276-82bf-7d0e0d12f1e9",
                UserName         = "******",
                Email            = "*****@*****.**",
                EmailConfirmed   = true,
                PhoneNumber      = "123456789012",
                Address          = "My Adr",
                PersonalDiscount = 0,
                Name             = "Yana",
                Surname          = "Hersurname",
                Role             = "Admin",
                IsEmailConfirmed = true
            });

            var mgr       = new UserManager <AppUser>(store.Object, null, null, null, null, null, null, null, null);
            var optionsDB = new DbContextOptionsBuilder <DreamFoodDeliveryContext>()
                            .UseInMemoryDatabase(databaseName: "User_GetUserProfileByIdFromIdentityAsync_Positive_Test")
                            .Options;

            using (var contextDB = new DreamFoodDeliveryContext(optionsDB))
            {
                var userDB = new UserDB
                {
                    IdFromIdentity = "11111111 - 222e-4276 - 82bf - 7d0e0d12f1e9",
                };
                var userDB2 = new UserDB
                {
                    IdFromIdentity = "22222222 - 222e-4276 - 82bf - 7d0e0d12f1e9",
                };
                contextDB.Add(userDB);
                contextDB.Add(userDB2);
                await contextDB.SaveChangesAsync();
            }
            using (var contextDB = new DreamFoodDeliveryContext(optionsDB))
            {
                var service = new UserService(_mapper, contextDB, mgr, _emailSender, _emailBuilder);

                var result2 = await service.GetUserProfileByIdFromIdentityAsync("11111111-222e-4276-82bf-7d0e0d12f1e9");

                result2.Data.Should().NotBeNull();
            }
        }
        public async void Reviews_GetByUserIdAsync_PositiveAndNegative_Test()
        {
            var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>()
                          .UseInMemoryDatabase(databaseName: "Reviews_GetByUserIdAsync_PositiveAndNegative_Test")
                          .Options;

            UserDB userWithReviews;

            using (var context = new DreamFoodDeliveryContext(options))
            {
                context.AddRange(_users);
                await context.SaveChangesAsync();

                userWithReviews = context.Users.AsNoTracking().FirstOrDefault();

                foreach (var review in _reviews)
                {
                    review.UserId  = userWithReviews.Id;
                    review.OrderId = new Guid();
                }

                context.AddRange(_reviews);
                await context.SaveChangesAsync();
            }

            using (var context = new DreamFoodDeliveryContext(options))
            {
                var service = new ReviewService(_mapper, context);

                var reviewsInBase = await context.Reviews.AsNoTracking().ToListAsync();

                var userWithoutReviews = await context.Users.Where(_ => _.Id != userWithReviews.Id).FirstOrDefaultAsync();

                var resultPositive = await service.GetByUserIdAsync(userWithReviews.IdFromIdentity.ToString());

                var resultNegative = await service.GetByUserIdAsync(userWithoutReviews.IdFromIdentity.ToString());

                foreach (var review in reviewsInBase)
                {
                    resultPositive.Data
                    .Where(_ => _.Id == review.Id)
                    .FirstOrDefault()
                    .Should().NotBeNull();
                }

                resultNegative.Data.Should().BeNullOrEmpty();
            }
        }
Beispiel #19
0
        public async void User_CreateAccountAsyncById_Positive_Test()
        {
            var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>()
                          .UseInMemoryDatabase(databaseName: "User_CreateAccountAsyncById_Positive_Test")
                          .Options;

            var store = new Mock <IUserStore <AppUser> >();

            store.Setup(x => x.FindByIdAsync(It.IsAny <string>(), CancellationToken.None))
            .ReturnsAsync(new AppUser()
            {
                UserName         = "******",
                Email            = "*****@*****.**",
                EmailConfirmed   = true,
                PhoneNumber      = "123456789012",
                Address          = "My Adr",
                PersonalDiscount = 0,
                Name             = "Yana",
                Surname          = "Hersurname",
                Role             = "Admin",
                IsEmailConfirmed = true
            });

            var userManager = new UserManager <AppUser>(store.Object, null, null, null, null, null, null, null, null);

            var user = _users.FirstOrDefault();

            //var store = new Mock<UserService>();
            //store.Setup(x => x.GetUserProfileByIdFromIdentityAsync(user.IdFromIdentity))
            //    .ReturnsAsync(Result<UserProfile>.Ok(_mapper.Map<UserProfile>(_userProfile)));

            using (var context = new DreamFoodDeliveryContext(options))
            {
                var service = new UserService(_mapper, context, userManager, _emailSender, _emailBuilder);

                var resultPositive = await service.CreateAccountAsyncById(user.IdFromIdentity);

                resultPositive.IsSuccess.Should().BeTrue();
                resultPositive.Data.UserDTO.IdFromIdentity.Should().BeEquivalentTo(user.IdFromIdentity);
                resultPositive.Data.UserDTO.Id.Should().NotBeEmpty();
                resultPositive.Data.UserDTO.BasketId.Should().NotBeEmpty();
            }
        }
Beispiel #20
0
        public async void Menu_GetAllDishesByRequestAsync_PositiveAndNegative_Test()
        {
            var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>()
                          .UseInMemoryDatabase(databaseName: "Menu_GetAllDishesByRequestAsync_PositiveAndNegative_Test")
                          .Options;

            using (var context = new DreamFoodDeliveryContext(options))
            {
                _dishes[0].Sale = 10;
                _dishes[1].Sale = 10;

                context.AddRange(_dishes);
                await context.SaveChangesAsync();
            }

            using (var context = new DreamFoodDeliveryContext(options))
            {
                var service = new MenuService(_mapper, context);
                RequestParameters positiveParameters = new RequestParameters()
                {
                    Request    = "",
                    TagsNames  = Enumerable.Empty <string>(),
                    OnSale     = true,
                    LowerPrice = 0,
                    UpperPrice = 1500
                };
                RequestParameters negativeParameters = new RequestParameters()
                {
                    Request    = "testLine",
                    TagsNames  = Enumerable.Empty <string>(),
                    OnSale     = false,
                    LowerPrice = 0,
                    UpperPrice = 1500
                };
                var resultPositive = await service.GetAllDishesByRequestAsync(positiveParameters);

                var resultNegative = await service.GetAllDishesByRequestAsync(negativeParameters);

                resultPositive.IsSuccess.Should().BeTrue();
                resultNegative.Data.Should().BeEmpty();
            }
        }
Beispiel #21
0
        public async void Tags_UpdateAsync_PositiveAndNegative_Test()
        {
            var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>()
                          .UseInMemoryDatabase(databaseName: "Tags_UpdateAsync_PositiveAndNegative_Test")
                          .Options;

            using (var context = new DreamFoodDeliveryContext(options))
            {
                context.AddRange(_tags);
                await context.SaveChangesAsync();
            }

            using (var context = new DreamFoodDeliveryContext(options))
            {
                var tag = await context.Tags.AsNoTracking().FirstOrDefaultAsync();

                var         service   = new TagService(_mapper, context);
                string      testName  = "TestName";
                TagToUpdate updateTag = new TagToUpdate()
                {
                    Id      = tag.Id.ToString(),
                    TagName = testName,
                };

                TagToUpdate failTag = new TagToUpdate()
                {
                    Id      = new Guid().ToString(),
                    TagName = testName,
                };

                var resultPositive = await service.UpdateAsync(updateTag);

                var resultNegative = await service.UpdateAsync(failTag);

                resultPositive.IsSuccess.Should().BeTrue();
                resultPositive.Data.TagName.Should().BeEquivalentTo(updateTag.TagName);
                resultPositive.Data.TagName.Should().NotBeEquivalentTo(tag.TagName);

                resultNegative.IsSuccess.Should().BeFalse();
            }
        }
        public async void Dishes_RemoveAllAsync_Test()
        {
            var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>()
                          .UseInMemoryDatabase(databaseName: "Dishes_RemoveAllAsync_Test")
                          .Options;

            using (var context = new DreamFoodDeliveryContext(options))
            {
                context.AddRange(_dishes);
                await context.SaveChangesAsync();
            }

            using (var context = new DreamFoodDeliveryContext(options))
            {
                var service = new DishService(_mapper, context, _tagService);

                var resultPositive = await service.RemoveAllAsync();

                resultPositive.IsSuccess.Should().BeTrue();
                resultPositive.Message.Should().BeNull();
            }
        }
Beispiel #23
0
        public async void Tags_AddAsync_Positive_Test()
        {
            var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>()
                          .UseInMemoryDatabase(databaseName: "Tags_AddAsync_Positive_Test")
                          .Options;

            using (var context = new DreamFoodDeliveryContext(options))
            {
                var    service = new TagService(_mapper, context);
                string newTag  = "NewTag";

                TagToAdd tag = new TagToAdd()
                {
                    TagName = newTag,
                };

                var resultPositive = await service.AddAsync(tag);

                resultPositive.IsSuccess.Should().BeTrue();
                resultPositive.Data.TagName.Should().BeEquivalentTo(tag.TagName);
            }
        }
Beispiel #24
0
        public async void Tags_GetAllTagsAsync_Test()
        {
            var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>()
                          .UseInMemoryDatabase(databaseName: "Tag_GetAllTagsAsync_Test")
                          .Options;

            // Run the test against one instance of the context
            using (var context = new DreamFoodDeliveryContext(options))
            {
                context.AddRange(_dishes);
                context.AddRange(_tags);
                DishTagDB dishTag = new DishTagDB
                {
                    TagId  = _tags.FirstOrDefault().Id,
                    DishId = _dishes.FirstOrDefault().Id,
                };
                context.Add(dishTag);
                await context.SaveChangesAsync();
            }

            // Use a separate instance of the context to verify correct data was saved to database
            using (var context = new DreamFoodDeliveryContext(options))
            {
                var service = new TagService(_mapper, context);

                var tagFromBase = await context.Tags.FirstOrDefaultAsync();

                var result = await service.GetAllTagsAsync();

                result.IsSuccess.Should().BeTrue();
                result.Data.Count().Should().Be(1);

                foreach (var item in result.Data)
                {
                    item.TagName.Should().Be(tagFromBase.TagName);
                }
            }
        }
        public async void Reviews_GetAllAsync_Test()
        {
            var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>()
                          .UseInMemoryDatabase(databaseName: "Reviews_GetAllAsync_Test")
                          .Options;

            // Run the test against one instance of the context
            using (var context = new DreamFoodDeliveryContext(options))
            {
                context.AddRange(_orders);
                await context.SaveChangesAsync();

                var order  = context.Orders.AsNoTracking().FirstOrDefault();
                var review = _reviews.FirstOrDefault();
                review.UserId  = order.UserId;
                review.OrderId = order.Id.GetValueOrDefault();

                context.AddRange(review);
                await context.SaveChangesAsync();
            }

            // Use a separate instance of the context to verify correct data was saved to database
            using (var context = new DreamFoodDeliveryContext(options))
            {
                var service = new ReviewService(_mapper, context);

                var reviewsInBase = await context.Reviews.AsNoTracking().ToListAsync();

                var result = await service.GetAllAsync(new PageRequest());

                foreach (var item in reviewsInBase)
                {
                    var itemFromResult = result.Data.Data.Where(_ => _.Headline.Equals(item.Headline, StringComparison.OrdinalIgnoreCase)).Select(_ => _).FirstOrDefault();
                    itemFromResult.Should().NotBeNull();
                }
            }
        }
Beispiel #26
0
        public async void Orders_GetAllAsync_Test()
        {
            var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>()
                          .UseInMemoryDatabase(databaseName: "Orders_GetAllAsync_Test")
                          .Options;

            // Run the test against one instance of the context
            using (var context = new DreamFoodDeliveryContext(options))
            {
                context.AddRange(_orders);
                _basketDishes[0].OrderId = _orders.FirstOrDefault().Id;
                _basketDishes[1].OrderId = _orders.LastOrDefault().Id;
                context.AddRange(_basketDishes);
                await context.SaveChangesAsync();
            }

            var storeDishService = new Mock <IDishService>();

            storeDishService.Setup(x => x.GetByIdAsync(It.IsAny <string>(), CancellationToken.None))
            .ReturnsAsync(Result <DishView> .Ok(_dishView));

            // Use a separate instance of the context to verify correct data was saved to database
            using (var context = new DreamFoodDeliveryContext(options))
            {
                var service     = new OrderService(_mapper, _userManager, context, storeDishService.Object, _emailSender);
                var orderInBase = await context.Orders.AsNoTracking().ToListAsync();

                var result = await service.GetAllAsync();

                foreach (var item in orderInBase)
                {
                    var itemFromResult = result.Data.Where(_ => _.Name.Equals(item.Name, StringComparison.OrdinalIgnoreCase)).Select(_ => _).FirstOrDefault();
                    itemFromResult.Should().NotBeNull();
                }
            }
        }
 public DishService(IMapper mapper, DreamFoodDeliveryContext context, ITagService tagService)
 {
     _context    = context;
     _mapper     = mapper;
     _tagService = tagService;
 }
Beispiel #28
0
        public async void Admin_GetAllAsync_Test()
        {
            var users = new List <AppUser>();

            users.Add(new AppUser()
            {
                Id               = "11111111-222e-4276-82bf-7d0e0d12f1e9",
                UserName         = "******",
                Email            = "*****@*****.**",
                EmailConfirmed   = true,
                PhoneNumber      = "123456789012",
                Address          = "My Adr",
                PersonalDiscount = 0,
                Name             = "Yana",
                Surname          = "Hersurname",
                Role             = "Admin",
                IsEmailConfirmed = true
            });

            users.Add(new AppUser()
            {
                Id               = "22222222-222e-4276-82bf-7d0e0d12f1e9",
                UserName         = "******",
                Email            = "*****@*****.**",
                EmailConfirmed   = true,
                PhoneNumber      = "123456789012",
                Address          = "My Adr2",
                PersonalDiscount = 0,
                Name             = "NAAAAA",
                Surname          = "AAAAAAAAAAA",
                Role             = "User",
                IsEmailConfirmed = true
            });

            var mock = users.AsQueryable().BuildMock();

            var mockUserManager   = new Mock <UserManager <AppUser> >();
            var mockUserStore     = new Mock <IUserStore <AppUser> >();
            var mockUserRoleStore = mockUserStore.As <IQueryableUserStore <AppUser> >();

            mockUserManager.Setup(x => x.Users).Returns(mock.Object);
            mockUserRoleStore.Setup(x => x.Users).Returns(mock.Object);
            var manager = new UserManager <AppUser>(mockUserStore.Object, null, null, null, null, null, null, null, null);

            //UserManager<UserData> manager = mockUserManager.Object;

            //Mock<EntityFrameworkQueryableExtensions> mockToList = new Mock<EntityFrameworkQueryableExtensions>();
            //mockToList.Setup(x => x.To).Returns(GetTestUsers());

            ////mockUser.Setup(userManager => userManager.FindByIdAsync(It.IsAny<string>())).ReturnsAsync(new IdentityUser { ... });
            //mockUser.Setup(p => p.Users).Returns(GetTestUsers());
            ////mockUser.Setup(userManager => userManager.IsInRoleAsync(It.IsAny<User>(), "SweetTooth")).ReturnsAsync(true);
            //UserManager<User> manager = mockUser.Object;

            //var userManagerMocked = new Mock<UserManager<User>>();
            //userManagerMocked.Setup(p => p.Users.ToListAsync(CancellationToken.None)).ReturnsAsync(GetTestUsers());
            ////userManagerMocked.Setup(p => p.Users.ToList()).Returns(GetTestUsers());
            //UserManager<User> manager = userManagerMocked.Object;

            //var store = new Mock<UserManager<User>>();
            //store.Setup(x => x.Users.ToListAsync(CancellationToken.None)).ReturnsAsync(GetTestUsers());
            //UserManager<User> manager = store.Object;

            //var MockUserManager = Microsoft.AspNet.Identity.Test.MockHelpers.MockUserManager<User>();

            //var mockUserStore = new Mock<IUserStore<UserData>>();
            //var mockUserRoleStore = mockUserStore.As<IQueryableUserStore<UserData>>();
            ////mockUserRoleStore.Setup(x => x.Users).Returns(mock.Object);

            //var manager = new UserManager<UserData>(mockUserStore.Object, null, null, null, null, null, null, null, null);



            //mockUserRoleStore.Setup(x => x.Users.ToListAsync(CancellationToken.None)).ReturnsAsync(GetTestUsers());

            //var userManager = new UserManager<User>(mockUserStore.Object);

            //store.Setup(x => x.IsInRoleAsync(User, "Admin")).ReturnsAsync(GetTestUsers());



            var optionsDB = new DbContextOptionsBuilder <DreamFoodDeliveryContext>()
                            .UseInMemoryDatabase(databaseName: "Admin_GetAllAsync_Test")
                            .Options;

            //var options = new DbContextOptionsBuilder<UserContext>()
            //    .UseInMemoryDatabase(databaseName: "Admin_GetAllAsync_Test")
            //    .Options;

            // Run the test against one instance of the context
            //using (var context = new UserContext(options))
            using (var contextDB = new DreamFoodDeliveryContext(optionsDB))
            {
                //var user = new User
                //{
                //    Email = SuperAdminData.EMAIL,
                //    UserName = SuperAdminData.USER_NAME,
                //    PersonalDiscount = 0,
                //    Role = "Admin",
                //    IsEmailConfirmed = false
                //};
                var userDB = new UserDB
                {
                    IdFromIdentity = "11111111-222e-4276-82bf-7d0e0d12f1e9",
                };
                var userDB2 = new UserDB
                {
                    IdFromIdentity = "22222222-222e-4276-82bf-7d0e0d12f1e9",
                };
                //context.Add(user);
                //await context.SaveChangesAsync();
                contextDB.Add(userDB);
                contextDB.Add(userDB2);
                await contextDB.SaveChangesAsync();
            }

            var store = new Mock <IUserService>();

            store.Setup(x => x.GetUserProfileByIdFromIdentityAsync(It.IsAny <string>())) //"11111111-222e-4276-82bf-7d0e0d12f1e9"
            .ReturnsAsync(Result <UserProfile> .Ok(_userProfile));
            //store.Setup(x => x.GetUserProfileByIdFromIdentityAsync("22222222-222e-4276-82bf-7d0e0d12f1e9")) // It.IsAny<string>()  - give exception
            //    .ReturnsAsync(Result<UserProfile>.Ok(_mapper.Map<UserProfile>(_userProfile)));

            // Use a separate instance of the context to verify correct data was saved to database
            //using (var context = new UserContext(options))
            using (var contextDB = new DreamFoodDeliveryContext(optionsDB))
            {
                //var userService = new UserService(_mapper, contextDB, manager, _emailSender, _emailBuilder);
                var service = new AdminService(_mapper, contextDB, manager, store.Object, _emailSender);

                //var usersInBase = await context.Users.AsNoTracking().ToListAsync();
                //var usersInBase = GetTestUsers();
                var usersInBaseDB = await contextDB.Users.AsNoTracking().ToListAsync();

                var result = await service.GetAllAsync();

                foreach (var user in usersInBaseDB)
                {
                    var itemFromResult = result.Data.Where(_ => _.UserDTO.Id == user.Id).Select(_ => _).FirstOrDefault();
                    itemFromResult.Should().NotBeNull();
                }
            }
        }
 public ReviewService(IMapper mapper, DreamFoodDeliveryContext reviewContext)
 {
     _context = reviewContext;
     _mapper  = mapper;
 }
        public async void Dishes_UpdateAsync_PositiveAndNegative_Test()
        {
            var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>()
                          .UseInMemoryDatabase(databaseName: "Dishes_UpdateAsync_PositiveAndNegative_Test")
                          .Options;

            using (var context = new DreamFoodDeliveryContext(options))
            {
                context.AddRange(_dishes);
                await context.SaveChangesAsync();
            }

            using (var context = new DreamFoodDeliveryContext(options))
            {
                var dish = await context.Dishes.AsNoTracking().FirstOrDefaultAsync();

                var tagService = new TagService(_mapper, context);
                var service    = new DishService(_mapper, context, tagService);

                TagToAdd[] tags = new TagToAdd[] {
                    new TagToAdd
                    {
                        TagName = "New"
                    }
                };

                DishToUpdate updateDish = new DishToUpdate()
                {
                    Id          = dish.Id,
                    Name        = "Name",
                    Composition = "Composition",
                    Description = "Description",
                    Weight      = "Weight",
                    TagNames    = new HashSet <TagToAdd>(tags)
                };

                DishToUpdate failDish = new DishToUpdate()
                {
                    Id          = Guid.NewGuid(),
                    Name        = "Name",
                    Composition = "Composition",
                    Description = "Description",
                    Weight      = "Weight",
                    TagNames    = new HashSet <TagToAdd>(tags)
                };

                var resultPositive = await service.UpdateAsync(updateDish);

                var resultNegative = await service.UpdateAsync(failDish);

                resultPositive.IsSuccess.Should().BeTrue();
                resultPositive.Data.Name.Should().BeEquivalentTo(updateDish.Name);
                resultPositive.Data.Composition.Should().BeEquivalentTo(updateDish.Composition);
                resultPositive.Data.Description.Should().BeEquivalentTo(updateDish.Description);
                resultPositive.Data.Weight.Should().BeEquivalentTo(updateDish.Weight);
                resultPositive.Data.Name.Should().NotBeEquivalentTo(dish.Name);
                resultPositive.Data.Composition.Should().NotBeEquivalentTo(dish.Composition);
                resultPositive.Data.Description.Should().NotBeEquivalentTo(dish.Description);
                resultPositive.Data.Weight.Should().NotBeEquivalentTo(dish.Weight);

                resultNegative.IsSuccess.Should().BeFalse();
            }
        }