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(); } }
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_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(); } }
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); } }
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); } }
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 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(); } } }