public async Task Add_WithCorrectData_ShouldProvideCorrectResult() { string onFalseErrorMessage = "Service method returned false."; string onNullErrorMessage = "The favorite product not found in database."; var context = ApplicationDbContextInMemoryFactory.InitializeContext(); // Gets the user(Usermanager accepts the id and returns the whole user) and productId only var user = this.GetUser(); var productId = this.GetProduct().Id; var userManager = UserManagerInitializer.InitializeMockedUserManager(); userManager.Setup(x => x.FindByIdAsync(user.Id)).ReturnsAsync(user); var favoritesService = new FavoritesService(context, userManager.Object); // Seed both user and product await this.SeedUser(context); await this.SeedProduct(context); var methodResult = await favoritesService.Add(productId, user.Id); Assert.True(methodResult, onFalseErrorMessage); var favoriteProductFromDb = context .UsersFavoriteProducts .SingleOrDefaultAsync(x => x.ProductId == productId && x.ApplicationUserId == user.Id); AssertExtensions.NotNullWithMessage(favoriteProductFromDb, onNullErrorMessage); }
public void DeleteShouldDeleteFavoriteProduct() { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: "Delete_Favorites_Database") .Options; var dbContext = new XeonDbContext(options); var username = "******"; dbContext.Users.Add(new XeonUser { UserName = username }); var productNameForDelete = "USB"; var products = new List <Product> { new Product { Name = productNameForDelete }, new Product { Name = "Phone Samsung" }, new Product { Name = "Phone Nokia" }, new Product { Name = "Phone Iphone" }, new Product { Name = "Tablet Galaxy" } }; dbContext.Products.AddRange(products); dbContext.SaveChanges(); var favoriteService = new FavoritesService(dbContext); foreach (var product in products.Take(3)) { favoriteService.Add(product.Id, username); } var productId = products.FirstOrDefault(x => x.Name == productNameForDelete).Id; favoriteService.Delete(productId, username); var userFavoriteProduxts = dbContext.Users .FirstOrDefault(x => x.UserName == username) .FavoriteProducts .ToList(); var isProductExist = userFavoriteProduxts.Any(x => x.Product.Name == productNameForDelete); Assert.Equal(2, userFavoriteProduxts.Count()); Assert.False(isProductExist); }
public void AddFavoriteProductWhithExistingProductShouldReturnFalse() { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: "AddExisting_Favorites_Database") .Options; var dbContext = new XeonDbContext(options); var username = "******"; dbContext.Users.Add(new XeonUser { UserName = username }); var productName = "USB"; dbContext.Products.Add(new Product { Name = productName }); dbContext.SaveChanges(); var product = dbContext.Products.FirstOrDefault(x => x.Name == productName); var favoriteService = new FavoritesService(dbContext); favoriteService.Add(product.Id, username); var isAddFavoriteProduct = favoriteService.Add(product.Id, username); var favoriteProducts = dbContext.Users .Include(x => x.FavoriteProducts) .FirstOrDefault(x => x.UserName == username) .FavoriteProducts; var favoriteProductsCount = favoriteProducts.Count(); Assert.False(isAddFavoriteProduct); Assert.Equal(1, favoriteProductsCount); }
//添加 public JsonResult Add(Enums.FavoritesType type, long contentId) { if (!serviceFavorites.IsExists(currentUser.UserId, type, contentId)) { var model = new Favorites { Type = type, ContentId = contentId, UserId = currentUser.UserId, AddTime = DateTime.Now }; serviceFavorites.Add(model); } result.code = 1; return(Json(result)); }
public void AllFavoriteShouldReturnAllFavoriteProducts() { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: "All_Favorites_Database") .Options; var dbContext = new XeonDbContext(options); var username = "******"; dbContext.Users.Add(new XeonUser { UserName = username }); var products = new List <Product> { new Product { Name = "USB" }, new Product { Name = "Phone Samsung" }, new Product { Name = "Phone Nokia" }, new Product { Name = "Phone Iphone" }, new Product { Name = "Tablet Galaxy" } }; dbContext.Products.AddRange(products); dbContext.SaveChanges(); var favoriteService = new FavoritesService(dbContext); foreach (var product in products.Take(3)) { favoriteService.Add(product.Id, username); } var favoriteProducts = favoriteService.All(username); Assert.Equal(3, favoriteProducts.Count()); }
public IHttpActionResult PostFavorite(Favorite favorite) { bool isUserInFavorites = _favoritesService.IsUserInFavorites(favorite); //if user is not in favorites if (!isUserInFavorites) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _favoritesService.Add(favorite); return(CreatedAtRoute("DefaultApi", new { id = favorite.FavoriteId }, favorite)); } return(CreatedAtRoute("DefaultApi", new { id = favorite.FavoriteId }, favorite)); }
public void AddFavoriteProductWithInvalidProductShouldReturnFalse() { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: "Add_Favorites_Database") .Options; var dbContext = new XeonDbContext(options); var username = "******"; dbContext.Users.Add(new XeonUser { UserName = username }); dbContext.SaveChanges(); var favoriteService = new FavoritesService(dbContext); var invalidProductId = 123; var isAddFavoriteProduct = favoriteService.Add(invalidProductId, username); Assert.False(isAddFavoriteProduct); }
public async Task Add_WithNonExistingProduct_MethodShouldReturnFalse() { string onTrueErrorMessage = "Method not returning false on non-existing product."; var context = ApplicationDbContextInMemoryFactory.InitializeContext(); // Gets the user(Usermanager accepts the id and returns the whole user) and a fake productId var user = this.GetUser(); var productId = "fakeProductId"; var userManager = UserManagerInitializer.InitializeMockedUserManager(); userManager.Setup(x => x.FindByIdAsync(user.Id)).ReturnsAsync(user); var favoritesService = new FavoritesService(context, userManager.Object); // Seed only user await this.SeedUser(context); var methodResult = await favoritesService.Add(productId, user.Id); Assert.False(methodResult, onTrueErrorMessage); }
public void AddFavoriteProductWithInvalidUserShouldReturnFalse() { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: "Add_Favorites_Database") .Options; var dbContext = new XeonDbContext(options); var invalidUsername = "******"; var productName = "USB"; dbContext.Products.Add(new Product { Name = productName }); dbContext.SaveChanges(); var product = dbContext.Products.FirstOrDefault(x => x.Name == productName); var favoriteService = new FavoritesService(dbContext); var isAddFavoriteProduct = favoriteService.Add(product.Id, invalidUsername); Assert.False(isAddFavoriteProduct); }