public async Task DoesReviewIdExistShouldReturnFalseIfNotFound() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); await db.AddRangeAsync( new Review { Description = "description1", AuthorId = "author1", BookId = 1, }, new Review { Description = "description2", AuthorId = "author1", BookId = 1, ParentId = 1, }); await db.SaveChangesAsync(); var reviewsService = new ReviewsService(db); var result = await reviewsService.DoesReviewIdExistAsync(5); Assert.False(result); }
public async Task EditReviewShouldWorkCorrectly(string description, ReadingProgress progress, bool thisEdition) { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); await db.AddAsync( new Review { Description = "description1", AuthorId = "author1", BookId = 1, ReadingProgress = ReadingProgress.Unknown, ThisEdition = false, }); await db.SaveChangesAsync(); var reviewsService = new ReviewsService(db); await reviewsService.EditReviewAsync(1, description, progress, thisEdition); var result = await db.Reviews.FirstOrDefaultAsync(); Assert.NotNull(result.ModifiedOn); Assert.Equal(description, result.Description); Assert.Equal(progress, result.ReadingProgress); Assert.Equal(thisEdition, result.ThisEdition); }
public async Task AreReviewsAboutSameBookShouldReturnFalseIfDeleted() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); await db.AddRangeAsync( new Review { Description = "description1", AuthorId = "author1", BookId = 1, ReadingProgress = ReadingProgress.Finished, IsDeleted = true, }, new Review { Description = "description2", AuthorId = "author2", BookId = 1, ReadingProgress = ReadingProgress.Finished, }); await db.SaveChangesAsync(); var reviewsService = new ReviewsService(db); var result = await reviewsService.AreReviewsAboutSameBookAsync(1, 1); Assert.False(result); }
public async Task GetAuthorIdByIdShouldReturnNullIfDeleted() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); await db.AddRangeAsync( new Review { Description = "description1", AuthorId = "author1", BookId = 1, IsDeleted = true, }, new Review { Description = "description2", AuthorId = "author1", BookId = 1, ParentId = 1, }); await db.SaveChangesAsync(); var reviewsService = new ReviewsService(db); var result = await reviewsService.GetAuthorIdByIdAsync(1); Assert.Null(result); }
public async Task GetAllLanguagesShouldReturnTheCorrectCount() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); var languages = new List <EditionLanguage>(); for (int i = 1; i <= 10; i++) { languages.Add( new EditionLanguage { Name = $"test{i}", }); } await db.EditionLanguages.AddRangeAsync(languages); await db.SaveChangesAsync(); var languagesService = new EditionLanguagesService(db); var result = await languagesService.GetAllLanguagesAsync <LanguageTestModel>(); Assert.Equal(10, result.Count()); }
public async Task GetChildrenReviewsToReviewsAsyncShouldNotReturnReviewsWithOtherBookId() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); await db.AddRangeAsync( new Review { Description = "description1", AuthorId = "author1", BookId = 1, }, new Review { Description = "description2", AuthorId = "author1", BookId = 1, }, new Review { Description = "description3", AuthorId = "author1", BookId = 1, ParentId = 1, }, new Review { Description = "description4", AuthorId = "author1", BookId = 2, ParentId = 2, }, new Review { Description = "description4", AuthorId = "author1", BookId = 1, ParentId = 3, }, new Review { Description = "description4", AuthorId = "author1", BookId = 1, ParentId = 4, }); await db.SaveChangesAsync(); var reviewsService = new ReviewsService(db); var ids = new List <int> { 1, 2 }; var result = await reviewsService.GetChildrenReviewsToReviewsAsync <ReviewTestModel>(ids, 1); Assert.Equal(2, result.Count()); }
public async Task EditGenreShouldWorkCorrectly(string name, string description) { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); await db.Genres.AddAsync( new Genre { Name = "test", Description = "description", }); await db.SaveChangesAsync(); var genresService = new GenresService(db); await genresService.EditGenreAsync(1, name, description); var result = await db.Genres.FirstOrDefaultAsync(); Assert.Equal(name, result.Name); Assert.Equal(description, result.Description); Assert.NotNull(result.ModifiedOn); }
public async Task DoesEditionLanguageExistShouldReturnFalseWhenNotFound() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); await db.EditionLanguages.AddRangeAsync( new EditionLanguage { Name = "test1", }, new EditionLanguage { Name = "test2", }, new EditionLanguage { Name = "test3", }); await db.SaveChangesAsync(); var languagesService = new EditionLanguagesService(db); Assert.False(await languagesService.DoesEditionLanguageIdExistAsync(5)); }
public async Task GetGenreShouldReturnCorrectGenre() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); await db.Genres.AddRangeAsync( new Genre { Name = "test1", Description = "description1", }, new Genre { Name = "test2", Description = "description2", }, new Genre { Name = "test3", Description = "description3", IsDeleted = true, }); await db.SaveChangesAsync(); var genresService = new GenresService(db); var result = await genresService.GetGenreByIdAsync <GenreTestModel>(1); Assert.Equal("test1", result.Name); Assert.Equal("description1", result.Description); }
public async Task GetRandomGenresShouldNotReturnDeletedGenres() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); await db.Genres.AddRangeAsync( new Genre { Name = "test1", Description = "description1", IsDeleted = true, }, new Genre { Name = "test2", Description = "description2", IsDeleted = true, }, new Genre { Name = "test3", Description = "description3", }); await db.SaveChangesAsync(); var genresService = new GenresService(db); var result = await genresService.GetRandomGenresAsync <GenreTestModel>(2); Assert.Single(result); }
public async Task GetGenresCountShouldReturnCorrectCount() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); await db.Genres.AddRangeAsync( new Genre { Name = "test1", Description = "description1", }, new Genre { Name = "test2", Description = "description2", }, new Genre { Name = "test3", Description = "description3", }); await db.SaveChangesAsync(); var genresService = new GenresService(db); var result = await genresService.GetGenresCountAsync(); Assert.Equal(3, result); }
public async Task DeleteGenreShouldSetIsDeletedAndDeletedOn() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); await db.Genres.AddAsync( new Genre { Name = "test", Description = "description", }); await db.SaveChangesAsync(); var genresService = new GenresService(db); await genresService.DeleteGenreByIdAsync(1); var result = await db.Genres.FirstOrDefaultAsync(); Assert.True(result.IsDeleted); Assert.NotNull(result.DeletedOn); }
public async Task GetAllTagsMethodShouldReturnCorrectCountWhenBothTakeAndSkipAreGiven() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); var tags = new List <Tag>(); for (int i = 1; i <= 10; i++) { tags.Add(new Tag { Name = $"test{i}", CreatedOn = DateTime.UtcNow, }); } await db.AddRangeAsync(tags); await db.SaveChangesAsync(); var tagsService = new TagsService(db); var resultTags = await tagsService.GetAllTagsAsync <TagTestModel>(5, 5); Assert.Equal(5, resultTags.ToList().Count()); }
public async Task GetAllTagsMethodShouldReturnZeroIfTagsAreDeleted() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); var tags = new List <Tag>(); for (int i = 1; i <= 10; i++) { tags.Add(new Tag { Name = $"test{i}", CreatedOn = DateTime.UtcNow, IsDeleted = true, DeletedOn = DateTime.UtcNow, }); } await db.AddRangeAsync(tags); await db.SaveChangesAsync(); var tagsService = new TagsService(db); var resultTags = await tagsService.GetAllTagsAsync <TagTestModel>(); Assert.Empty(resultTags.ToList()); }
public async Task GetAllReviewsByAuthorIdShouldReturnAllReviews() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); await db.AddRangeAsync( new Review { Description = "description1", AuthorId = "author1", }, new Review { Description = "description1", AuthorId = "author1", }, new Review { Description = "description1", AuthorId = "author1", }); await db.SaveChangesAsync(); var reviewsService = new ReviewsService(db); var result = await reviewsService.GetAllReviewsByAuthorIdAsync <ReviewTestModel>("author1"); Assert.Equal(3, result.Count()); }
public async Task GetAuthorByIdShouldReturnNullWhenDeleted() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); await db.Authors.AddAsync( new Author { FirstName = "first1", SecondName = "second1", LastName = "last1", IsDeleted = true, DeletedOn = DateTime.UtcNow, }); await db.SaveChangesAsync(); var authorsService = new AuthorsService(db); var result = await authorsService.GetAuthorByIdAsync <AuthorTestModel>(1); Assert.Null(result); }
public async Task GetTagsCountMethodShouldReturnTheCorrectCountIfThereAreDeletedTags() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); await db.Tags.AddRangeAsync( new Tag { Name = "test1", CreatedOn = DateTime.UtcNow, }, new Tag { Name = "test2", CreatedOn = DateTime.UtcNow, IsDeleted = true, DeletedOn = DateTime.UtcNow, }, new Tag { Name = "test3", CreatedOn = DateTime.UtcNow, }); await db.SaveChangesAsync(); var tagsService = new TagsService(db); Assert.Equal(2, await tagsService.GetTagsCountAsync()); }
public async Task GetAverageRatingByBookIdShouldZeroWhenBookIdIsInvalid() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); var ratings = new List <StarRating>(); for (int i = 1; i <= 2; i++) { ratings.Add( new StarRating { Rate = 5, UserId = "userId", BookId = 1, }); } await db.StarRatings.AddRangeAsync(ratings); await db.SaveChangesAsync(); var ratingsService = new StarRatingsService(db); var result = await ratingsService.GetAverageRatingByBookIdAsync(5); Assert.Equal(0, result); }
public async Task CreateRatingMethodShouldNotChangeCountIfRatingExists() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); await db.StarRatings.AddAsync( new StarRating { Rate = 1, UserId = "userId", BookId = 1, }); await db.SaveChangesAsync(); var starRatingsService = new StarRatingsService(db); await starRatingsService.CreateRatingAsync(5, "userId", 1); var result = await db.StarRatings.CountAsync(); Assert.Equal(1, result); }
public async Task GetAllRatesByUserIdShouldReturnZeroWhenInvalidUserId() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); var rates = new List <StarRating>(); for (int i = 1; i <= 10; i++) { rates.Add(new StarRating { Rate = i, UserId = "userId", BookId = i, }); } await db.StarRatings.AddRangeAsync(rates); await db.SaveChangesAsync(); var ratingsService = new StarRatingsService(db); var result = await ratingsService.GetAllRatesByUserIdAsync <RatingTestModel>("id"); Assert.Empty(result); }
public async Task GetAllRatesByUserIdShouldReturnCorrectCountWhenTakeAndSkipAreUsed() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); var rates = new List <StarRating>(); for (int i = 1; i <= 10; i++) { rates.Add(new StarRating { Rate = i, UserId = "userId", BookId = i, }); } await db.StarRatings.AddRangeAsync(rates); await db.SaveChangesAsync(); var ratingsService = new StarRatingsService(db); var result = await ratingsService.GetAllRatesByUserIdAsync <RatingTestModel>("userId", 5, 5); Assert.Equal(5, result.Count()); }
public async Task SeedAsync(AlexandriaDbContext dbContext, IServiceProvider serviceProvider) { if (dbContext == null) { throw new ArgumentNullException(nameof(dbContext)); } if (serviceProvider == null) { throw new ArgumentNullException(nameof(serviceProvider)); } var logger = serviceProvider.GetService <ILoggerFactory>().CreateLogger(typeof(ApplicationDbContextSeeder)); var seeders = new List <ISeeder> { new RolesSeeder(), new TagsSeeder(), new GenresSeeder(), new AwardsSeeder(), new CountriesSeeder(), new EditionLanguagesSeeder(), new AdminSeeder(), }; foreach (var seeder in seeders) { await seeder.SeedAsync(dbContext, serviceProvider); await dbContext.SaveChangesAsync(); logger.LogInformation($"Seeder {seeder.GetType().Name} done."); } }
public async Task GetRatesCountByBookIdShouldReturnZeroWhenInvalidBookId() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); var rates = new List <StarRating>(); for (int i = 1; i <= 10; i++) { rates.Add(new StarRating { Rate = i, UserId = i.ToString(), BookId = 1, }); } await db.AddRangeAsync(rates); await db.SaveChangesAsync(); var ratingsService = new StarRatingsService(db); var result = await ratingsService.GetRatesCountByBookIdAsync(3); Assert.Equal(0, result); }
public async Task CreateUserFollowerShouldDeleteUserFollowerIfExisting() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); await db.UserFollowers.AddAsync( new UserFollower { UserId = "user1", FollowerId = "user2", }); await db.SaveChangesAsync(); var userFollowerService = new UserFollowersService(db); await userFollowerService.CreateUserFollowerAsync("user1", "user2"); var result = await db.UserFollowers.FirstOrDefaultAsync(); Assert.True(result.IsDeleted); Assert.NotNull(result.DeletedOn); }
public async Task AddTagsToBookMethodShouldNotAddBookTagsWhichExist() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); await db.BookTags.AddRangeAsync( new BookTag { BookId = 1, TagId = 1, }, new BookTag { BookId = 1, TagId = 2, }); await db.SaveChangesAsync(); var bookTagsService = new BookTagsService(db); var addTags = new List <int> { 1, 2, 3, 4, 5, 5, 5, 5, 5, 5 }; await bookTagsService.AddTagsToBookAsync(1, addTags); Assert.Equal(5, await db.BookTags.CountAsync()); }
public async Task DoesAwardExistMethodShouldReturnTrueIfExists() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); var awards = new List <Award> { new Award { Name = "test1", CreatedOn = DateTime.UtcNow, }, new Award { Name = "test2", CreatedOn = DateTime.UtcNow, }, new Award { Name = "test3", CreatedOn = DateTime.UtcNow, }, }; await db.Awards.AddRangeAsync(awards); await db.SaveChangesAsync(); var awardsService = new AwardsService(db); Assert.True(await awardsService.DoesAwardIdExistAsync(1)); }
public async Task GetAllAwardsShouldReturnZeroWhenAwardsAreDeleted() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); var awards = new List <Award>(); for (int i = 1; i <= 10; i++) { awards.Add( new Award { Name = $"test{i}", CreatedOn = DateTime.UtcNow, IsDeleted = true, DeletedOn = DateTime.UtcNow, }); } await db.Awards.AddRangeAsync(awards); await db.SaveChangesAsync(); var awardsService = new AwardsService(db); var result = await awardsService.GetAllAwardsAsync <AwardTestModel>(); Assert.Empty(result); }
public async Task GetReviewByIdShouldReturnCorrectReview() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); await db.AddRangeAsync( new Review { Description = "description1", AuthorId = "author1", BookId = 1, }, new Review { Description = "description2", AuthorId = "author1", BookId = 1, ParentId = 1, }); await db.SaveChangesAsync(); var reviewsService = new ReviewsService(db); var result = await reviewsService.GetReviewByIdAsync <ReviewTestModel>(2); Assert.Equal("description2", result.Description); Assert.NotNull(result.ParentId); }
public async Task DeleteReviewShouldSetIsDeletedAndDeletedOn() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); await db.AddRangeAsync( new Review { Description = "description1", AuthorId = "author1", BookId = 1, ReadingProgress = ReadingProgress.Finished, }, new Review { Description = "description2", AuthorId = "author2", BookId = 1, ReadingProgress = ReadingProgress.Finished, }); await db.SaveChangesAsync(); var reviewsService = new ReviewsService(db); await reviewsService.DeleteReviewByIdAsync(1); var result = await db.Reviews.FirstOrDefaultAsync(); Assert.NotNull(result.DeletedOn); Assert.True(result.IsDeleted); }
public async Task GetAllReviewsByAuthorIdShouldReturnCollectionWithCorrectOrder() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); await db.AddRangeAsync( new Review { Description = "description1", AuthorId = "author1", Likes = new List <Like> { new Like { ReviewId = 1, UserId = "1", IsLiked = true } }, }, new Review { Description = "description2", AuthorId = "author1", Likes = new List <Like> { new Like { ReviewId = 2, UserId = "1", IsLiked = false } }, }, new Review { Description = "description3", AuthorId = "author1", Likes = new List <Like> { new Like { ReviewId = 3, UserId = "1", IsLiked = true }, new Like { ReviewId = 3, UserId = "1", IsLiked = true } }, }, new Review { Description = "description4", AuthorId = "author1", }); await db.SaveChangesAsync(); var reviewsService = new ReviewsService(db); var result = await reviewsService.GetAllReviewsByAuthorIdAsync <ReviewTestModel>("author1"); var resultReviews = result.ToArray(); Assert.Equal(4, resultReviews.Count()); Assert.Equal("description3", resultReviews[0].Description); Assert.Equal("description1", resultReviews[1].Description); }