Exemple #1
0
        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);
        }
Exemple #2
0
        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);
        }
Exemple #3
0
        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);
        }
Exemple #4
0
        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);
        }
Exemple #5
0
        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());
        }
Exemple #6
0
        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);
        }
Exemple #7
0
        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);
        }
Exemple #8
0
        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());
        }
Exemple #9
0
        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());
        }
Exemple #10
0
        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());
        }
Exemple #11
0
        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);
        }
Exemple #12
0
        public async Task GetTopReviewsByBookIdShouldNotReturnDeletedAndReviewsWithParentId()
        {
            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,
            },
                new Review
            {
                Description = "description3",
                AuthorId    = "author1",
                BookId      = 1,
            },
                new Review
            {
                Description = "description4",
                AuthorId    = "author1",
                BookId      = 1,
                IsDeleted   = true,
            },
                new Review
            {
                Description = "description4",
                AuthorId    = "author1",
                BookId      = 2,
            });

            await db.SaveChangesAsync();

            var reviewsService = new ReviewsService(db);

            var result = await reviewsService.GetTopReviewsByBookIdAsync <ReviewTestModel>(1, 5);

            Assert.Equal(2, result.Count());
        }
Exemple #13
0
        public async Task GetChildrenReviewsByReviewIdShouldReturnCorrectResultWhenTakeAndSkipAreUsed()
        {
            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,
            },
                new Review
            {
                Description = "description3",
                AuthorId    = "author1",
                BookId      = 1,
                ParentId    = 1,
            },
                new Review
            {
                Description = "description4",
                AuthorId    = "author1",
                BookId      = 1,
                ParentId    = 1,
            });

            await db.SaveChangesAsync();

            var reviewsService = new ReviewsService(db);

            var result = await reviewsService.GetChildrenReviewsByReviewIdAsync <ReviewTestModel>(1, 5, 1);

            var resultReview = result.FirstOrDefault();

            Assert.Equal(2, result.Count());
            Assert.Equal("description3", resultReview.Description);
        }
Exemple #14
0
        public async Task GetChildrenReviewsCountShouldReturnCorrectCount()
        {
            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    = "author2",
                BookId      = 1,
                ParentId    = 1,
            },
                new Review
            {
                Description = "description3",
                AuthorId    = "author3",
                BookId      = 1,
                ParentId    = 1,
            },
                new Review
            {
                Description = "description4",
                AuthorId    = "author4",
                BookId      = 1,
                ParentId    = 1,
                IsDeleted   = true,
            });

            await db.SaveChangesAsync();

            var reviewsService = new ReviewsService(db);

            var result = await reviewsService.GetChildrenReviewsCountByReviewIdAsync(1);

            Assert.Equal(2, result);
        }