public async Task AddAsyncShouldReturnFalseWhenProductIdDoesNotExistUsingMoq() { var favouriteProductsRepository = new Mock <IRepository <UserFavouriteProduct> >(); var productsRepository = new Mock <IDeletableEntityRepository <Product> >(); var productImages = new List <ProductImage> { new ProductImage { ImageUrl = "TestImageUrl1" }, new ProductImage { ImageUrl = "TestImageUrl2" }, }; var productsList = new List <Product> { new Product { Id = "TestProductId", Name = "TestProductName", Price = 42, Images = productImages, Reviews = new List <UserProductReview>() }, }; productsRepository.Setup(r => r.AllAsNoTracking()).Returns(productsList.AsQueryable()); var service = new FavouritesService(favouriteProductsRepository.Object, productsRepository.Object, null); Assert.False(await service.AddAsync("InvalidId", "TestUserId")); productsRepository.Verify(x => x.AllAsNoTracking(), Times.Once); }
public static void Initialise(IPlatformServices platformServices, ISecrets secrets) { PlatformServices = platformServices; ViewModelFactory = new ViewModelFactory(); DeezerSession = new DeezerSession(new HttpClientHandler()); AuthenticationService = new AuthenticationService(DeezerSession, platformServices.SecureStorage); FavouritesService = new FavouritesService(DeezerSession, AuthenticationService); // TODO: Need to work out how to dispose of services when they are dying off Register <IGenreListDataController>(new GenreListDataController(DeezerSession)); Register <IWhatsNewDataController>(new WhatsNewDataController(DeezerSession)); Register <IChartsDataController>(new ChartsDataController(DeezerSession)); Register <ISearchDataController>(new SearchDataController(DeezerSession)); Register <ITracklistDataController>(new TracklistDataController(DeezerSession)); Register <IArtistOverviewDataController>(new ArtistOverviewDataController(DeezerSession)); Register <IUserOverviewDataController>(new UserOverviewDataController(DeezerSession)); Register <IMyDeezerDataController>(new MyDeezerDataController(DeezerSession, AuthenticationService, FavouritesService)); Register <IOAuthClient>(new OAuthClient(secrets.AppId, secrets.AppSecret, secrets.OAuthRedirectUri, secrets.DesiredPermissions)); }
public void GetCountShouldReturnEmptyCollectionWithNonExistingUserIdUsingMoq() { var repository = new Mock <IRepository <UserFavouriteProduct> >(); var productsList = new List <UserFavouriteProduct> { new UserFavouriteProduct { Id = "TestId1", UserId = "TestUserId1" }, new UserFavouriteProduct { Id = "TestId2", UserId = "TestUserId2" }, new UserFavouriteProduct { Id = "TestId3", UserId = "TestUserId3" }, }; repository.Setup(r => r.AllAsNoTracking()).Returns(productsList.AsQueryable()); var service = new FavouritesService(repository.Object, null, null); Assert.Equal(0, service.GetCount("TestUserId4")); repository.Verify(x => x.AllAsNoTracking(), Times.Once); }
public void AddMovieToFavourite_Should_ThrowWhenMovieDoNotExist() { var db = new TFContext(this.DatabaseSimulator()); var favouriteService = new FavouritesService(db); Assert.ThrowsException <InexistingEntityException> (() => favouriteService.AddMovieToFavourite(1, userIdToUse)); }
public async Task DeleteAsyncShouldReturnFalseWhenFavouriteProductIdDoesNotExistsUsingMoq() { var favouriteProductsRepository = new Mock <IRepository <UserFavouriteProduct> >(); var productsRepository = new Mock <IDeletableEntityRepository <Product> >(); var store = new Mock <IUserStore <ApplicationUser> >(); var userManager = new Mock <UserManager <ApplicationUser> >(store.Object, null, null, null, null, null, null, null, null); var productImages = new List <ProductImage> { new ProductImage { ImageUrl = "TestImageUrl1" }, new ProductImage { ImageUrl = "TestImageUrl2" }, }; var productsList = new List <Product> { new Product { Id = "TestProductId", Name = "TestProductName", Price = 42, Images = productImages, Reviews = new List <UserProductReview>() }, new Product { Id = "TestProductId2", Name = "TestProductName", Price = 42, Images = productImages, Reviews = new List <UserProductReview>() }, }; var favouriteProductsList = new List <UserFavouriteProduct> { new UserFavouriteProduct { Id = "TestId1", UserId = "TestUserId1", Product = productsList.ElementAt(0), ProductId = "TestProductId" }, new UserFavouriteProduct { Id = "TestId2", UserId = "TestUserId2" }, new UserFavouriteProduct { Id = "TestId3", UserId = "TestUserId3" }, }; favouriteProductsRepository.Setup(r => r.AllAsNoTracking()).Returns(favouriteProductsList.AsQueryable()); productsRepository.Setup(r => r.AllAsNoTracking()).Returns(productsList.AsQueryable()); userManager.Setup(r => r.FindByIdAsync(It.IsAny <string>())) .Returns(async(string userId) => await Task.FromResult <ApplicationUser>(new ApplicationUser { Id = "TestUserId1" })); var service = new FavouritesService(favouriteProductsRepository.Object, productsRepository.Object, userManager.Object); Assert.False(await service.DeleteAsync("TestProductId2", "TestUserId1")); productsRepository.Verify(x => x.AllAsNoTracking(), Times.Once); userManager.Verify(x => x.FindByIdAsync(It.IsAny <string>()), Times.Once); favouriteProductsRepository.Verify(x => x.AllAsNoTracking(), Times.Once); }
public async Task RemoveAsyncShouldThrowArgumentNullForNonExistingEntity() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "RemoveAsyncShouldWork").Options; var dbContext = new ApplicationDbContext(options); var repository = new EfDeletableEntityRepository <Favourite>(dbContext); var service = new FavouritesService(repository); await Assert.ThrowsAsync <ArgumentNullException>(() => service.Remove(1, "test")); }
public async Task AddAsyncShouldWorkCorrectlyUsingMoq() { var favouriteProductsRepository = new Mock <IRepository <UserFavouriteProduct> >(); var productsRepository = new Mock <IDeletableEntityRepository <Product> >(); var store = new Mock <IUserStore <ApplicationUser> >(); var userManager = new Mock <UserManager <ApplicationUser> >(store.Object, null, null, null, null, null, null, null, null); var productImages = new List <ProductImage> { new ProductImage { ImageUrl = "TestImageUrl1" }, new ProductImage { ImageUrl = "TestImageUrl2" }, }; var productsList = new List <Product> { new Product { Id = "TestProductId", Name = "TestProductName", Price = 42, Images = productImages, Reviews = new List <UserProductReview>() }, }; var favouriteProductsList = new List <UserFavouriteProduct>(); favouriteProductsRepository.Setup(r => r.SaveChangesAsync()).Verifiable(); favouriteProductsRepository.Setup(r => r.AddAsync(It.IsAny <UserFavouriteProduct>())) .Callback((UserFavouriteProduct item) => favouriteProductsList.Add(item)); productsRepository.Setup(r => r.AllAsNoTracking()).Returns(productsList.AsQueryable()); userManager.Setup(r => r.FindByIdAsync(It.IsAny <string>())) .Returns(async(string userId) => await Task.FromResult <ApplicationUser>(new ApplicationUser { Id = "TestUserId1" })); var service = new FavouritesService(favouriteProductsRepository.Object, productsRepository.Object, userManager.Object); Assert.True(await service.AddAsync("TestProductId", "TestUserId1")); Assert.Single(favouriteProductsList); Assert.Equal("TestProductId", favouriteProductsList.FirstOrDefault().ProductId); Assert.Equal("TestUserId1", favouriteProductsList.FirstOrDefault().UserId); productsRepository.Verify(x => x.AllAsNoTracking(), Times.Once); userManager.Verify(x => x.FindByIdAsync(It.IsAny <string>()), Times.Once); favouriteProductsRepository.Verify(x => x.AddAsync(It.IsAny <UserFavouriteProduct>()), Times.Once); favouriteProductsRepository.Verify(x => x.SaveChangesAsync()); }
public async Task AddAsyncShouldWork() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "AddAsyncShouldWork").Options; var dbContext = new ApplicationDbContext(options); var repository = new EfDeletableEntityRepository <Favourite>(dbContext); var service = new FavouritesService(repository); await service.AddAsync(1, "test"); Assert.True(repository.All().Any(x => x.PostId == 1 && x.UserId == "test")); }
public void GetAllGenericShouldMapCorrectlyWhenThereAreNoReviewsUsingMoq() { AutoMapperConfig.RegisterMappings(typeof(ErrorViewModel).GetTypeInfo().Assembly); var repository = new Mock <IRepository <UserFavouriteProduct> >(); var productImages = new List <ProductImage> { new ProductImage { ImageUrl = "TestImageUrl1" }, new ProductImage { ImageUrl = "TestImageUrl2" }, }; var product = new Product { Id = "TestProductId", Name = "TestProductName", Price = 42, Images = productImages, Reviews = new List <UserProductReview>(), }; var productsList = new List <UserFavouriteProduct> { new UserFavouriteProduct { Id = "TestId1", UserId = "TestUserId1", Product = product, ProductId = "TestProductId" }, new UserFavouriteProduct { Id = "TestId2", UserId = "TestUserId2" }, new UserFavouriteProduct { Id = "TestId3", UserId = "TestUserId3" }, }; repository.Setup(r => r.AllAsNoTracking()).Returns(productsList.AsQueryable()); var service = new FavouritesService(repository.Object, null, null); Assert.Equal("TestProductId", service.GetAll <FavouriteProductViewModel>("TestUserId1").FirstOrDefault().ProductId); Assert.Equal("TestProductName", service.GetAll <FavouriteProductViewModel>("TestUserId1").FirstOrDefault().ProductName); Assert.Equal(42, service.GetAll <FavouriteProductViewModel>("TestUserId1").FirstOrDefault().ProductPrice); Assert.Equal("TestImageUrl1", service.GetAll <FavouriteProductViewModel>("TestUserId1").FirstOrDefault().ImageUrl); Assert.Equal(0.00, service.GetAll <FavouriteProductViewModel>("TestUserId1").FirstOrDefault().AverageRating); repository.Verify(x => x.AllAsNoTracking(), Times.Exactly(5)); }
public async Task RemoveAsyncShouldWork() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "RemoveAsyncShouldWork").Options; var dbContext = new ApplicationDbContext(options); var repository = new EfDeletableEntityRepository <Favourite>(dbContext); var service = new FavouritesService(repository); await service.AddAsync(1, "test"); Assert.Single(repository.All()); await service.Remove(1, "test"); Assert.Empty(repository.All()); }
public void AddMovieToFavourite_Should_AddCorrect() { var db = new TFContext(this.DatabaseSimulator()); var favouriteService = new FavouritesService(db); var movie = new Movie() { Id = 1 }; db.Movies.Add(movie); db.SaveChanges(); favouriteService.AddMovieToFavourite(movie.Id, userIdToUse); Assert.AreEqual(1, db.MoviesUsers.Count(a => a.UserId == userIdToUse && a.MovieId == 1)); }
public void FindAllUserOrdersShouldReturnAllUserOrders() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: $"FindAllUserOrdersShouldReturnAllUserOrders_Orders_Database") .Options; var dbContext = new ApplicationDbContext(options); var mapper = this.SetUpAutoMapper(); var brandService = new BrandsService(dbContext, mapper); var categoriesService = new CategoriesService(dbContext, mapper); var usersService = new UsersService(dbContext, mapper); var productsService = new ProductsService(dbContext, mapper); var shoppingCartsService = new ShoppingCartsService(dbContext, productsService, usersService, mapper); var ordersService = new OrdersService(dbContext, shoppingCartsService, mapper); var favouritesService = new FavouritesService(dbContext, productsService, usersService, mapper); this.SeeDbdWithBrands(dbContext); this.SeedDbWithCategories(dbContext); var brands = brandService.FindAllBrands(); var categories = categoriesService.FindAllCategories(); var image = new Mock <IFormFile>(); this.SeedDbWithCountries(dbContext); this.SeedDbWithUserAndProduct(dbContext, productsService, mapper.Map <Category>(categories[0]), mapper.Map <Brand>(brands[0]), image.Object); var user = dbContext.Users.FirstOrDefault(x => x.UserName == "1"); var products = productsService.FindAllProducts(); var shoppingCarts = dbContext.ShoppingCarts; var shoppingCartss = dbContext.ShoppingCartProducts; var cart = this.SeedDbShoppingCartWithProducts(dbContext, user.UserName, products[0].Id); var model = this.CreateOrderCreateBindingModel(); var orderId = ordersService.CreateOrder(model, mapper.Map <ApplicationUserDTO>(user)); var orders = ordersService.FindAllUserOrders(mapper.Map <ApplicationUserDTO>(user)); Assert.True(orders.Count == 1); }
public void GetAllFavoritesByUserId_Should_ReturnAllFavourites() { var db = new TFContext(this.DatabaseSimulator()); var favouriteService = new FavouritesService(db); var movie = new Movie() { Id = 1 }; var secondMovie = new Movie() { Id = 2 }; var thridMovie = new Movie() { Id = 3 }; var movieUser = new MoviesUsers() { UserId = userIdToUse, Movie = movie }; var secondMovieUser = new MoviesUsers() { UserId = userIdToUse, Movie = secondMovie }; var thridMovieUser = new MoviesUsers() { UserId = userIdToUse, Movie = thridMovie }; db.Movies.Add(movie); db.Movies.Add(secondMovie); db.Movies.Add(thridMovie); db.MoviesUsers.AddRange(movieUser, secondMovieUser, thridMovieUser); db.SaveChanges(); Assert.AreEqual(3, favouriteService.GetAllFavoritesByUserId(userIdToUse).Count()); }
public async Task GetAllUserFavouritesGenericShouldWork() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "GetAllUserFavouritesGenericShouldWork").Options; var dbContext = new ApplicationDbContext(options); var user = new ApplicationUser() { Id = "randomId" }; var post = new Post() { Id = 1, Name = "random name", Price = 53222, Currency = Currency.LV, Mileage = 25123, Color = new Color(), EngineType = EngineType.Disel, Horsepower = 255, TransmissionType = TransmissionType.Automatic, ManufactureDate = DateTime.Now, Category = new Category(), }; dbContext.Users.Add(user); dbContext.Posts.Add(post); var favourite = new Favourite() { Post = post, User = user }; dbContext.Favourites.Add(favourite); await dbContext.SaveChangesAsync(); var repository = new EfDeletableEntityRepository <Favourite>(dbContext); var service = new FavouritesService(repository); Assert.Single(service.GetAllUserFavourites <FavouriteViewModel>("randomId")); }
public void AddMovieToFavourite_Should_ThrowWhenMovieIsAlreadyInFavourite() { var db = new TFContext(this.DatabaseSimulator()); var favouriteService = new FavouritesService(db); var movie = new Movie() { Id = 1 }; var movieUser = new MoviesUsers() { MovieId = 1, UserId = userIdToUse }; db.Movies.Add(movie); db.MoviesUsers.Add(movieUser); db.SaveChanges(); Assert.ThrowsException <EntityAlreadyExistingException> (() => favouriteService.AddMovieToFavourite(1, userIdToUse)); }
public async Task GetAllUserFavouritesShouldWork() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "GetAllUserFavouritesShouldWork").Options; var dbContext = new ApplicationDbContext(options); dbContext.Favourites.Add(new Favourite()); dbContext.Users.Add(new ApplicationUser() { Id = "TestId" }); dbContext.Favourites.Add(new Favourite() { UserId = "TestId", PostId = 5 }); await dbContext.SaveChangesAsync(); var repository = new EfDeletableEntityRepository <Favourite>(dbContext); var service = new FavouritesService(repository); Assert.Single(service.GetAllUserFavourites("TestId")); }
public ItemsController(SonorousContext context) { itemsServices = new ItemsService(context); favouritesService = new FavouritesService(context); }
public FavouriteController(ILogger <PointsController> logger, FavouritesService favouritesService, UserManager <ApplicationUser> userManager) { _logger = logger; _favouritesService = favouritesService; _userManager = userManager; }