Пример #1
0
        public async Task EditMethodShouldChangeDescriptionAndModifiedOn(string creationDescription, string editedDescription)
        {
            var options = new DbContextOptionsBuilder <ForumDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            var db = new ForumDbContext(options);
            var usersServiceMock     = new Mock <IUsersService>();
            var dateTimeProviderMock = new Mock <IDateTimeProvider>();

            dateTimeProviderMock.Setup(dtp => dtp.Now()).Returns(new DateTime(2020, 3, 27));

            await db.Replies.AddAsync(new Reply
            {
                Description = creationDescription,
                CreatedOn   = dateTimeProviderMock.Object.Now()
            });

            await db.SaveChangesAsync();

            var repliesService = new RepliesService(db, null, usersServiceMock.Object, dateTimeProviderMock.Object);
            await repliesService.EditAsync(1, editedDescription);

            var expected = new Reply
            {
                Description = editedDescription,
                ModifiedOn  = dateTimeProviderMock.Object.Now()
            };

            var actual = await db.Replies.FirstOrDefaultAsync();

            expected.Description.Should().BeSameAs(actual.Description);
            expected.ModifiedOn.Should().Be(actual.ModifiedOn);
        }
Пример #2
0
        public async Task GetAuthorIdByIdMethodShouldReturnCorrectId()
        {
            var guid = Guid.NewGuid().ToString();

            var options = new DbContextOptionsBuilder <ForumDbContext>()
                          .UseInMemoryDatabase(guid)
                          .Options;

            var db = new ForumDbContext(options);
            var usersServiceMock     = new Mock <IUsersService>();
            var dateTimeProviderMock = new Mock <IDateTimeProvider>();

            dateTimeProviderMock.Setup(dtp => dtp.Now()).Returns(new DateTime(2020, 3, 27));

            await db.Replies.AddAsync(new Reply
            {
                Description = "Test",
                AuthorId    = guid,
                CreatedOn   = dateTimeProviderMock.Object.Now()
            });

            await db.SaveChangesAsync();

            var repliesService = new RepliesService(db, null, usersServiceMock.Object, dateTimeProviderMock.Object);
            var authorId       = await repliesService.GetAuthorIdByIdAsync(1);

            authorId.Should().BeSameAs(guid);
        }
Пример #3
0
        public async Task IsExistingMethodShouldReturnTrueIfExists()
        {
            var options = new DbContextOptionsBuilder <ForumDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            var db = new ForumDbContext(options);
            var usersServiceMock     = new Mock <IUsersService>();
            var dateTimeProviderMock = new Mock <IDateTimeProvider>();

            dateTimeProviderMock.Setup(dtp => dtp.Now()).Returns(new DateTime(2020, 3, 27));

            await db.Replies.AddAsync(new Reply
            {
                Description = "Test",
                CreatedOn   = dateTimeProviderMock.Object.Now()
            });

            await db.SaveChangesAsync();

            var repliesService = new RepliesService(db, null, usersServiceMock.Object, dateTimeProviderMock.Object);
            var isExisting     = await repliesService.IsExistingAsync(1);

            isExisting.Should().BeTrue();
        }
Пример #4
0
        public async Task EditAsync_WithCorrectData_ShouldSuccessfullyEdit()
        {
            var testContent = "TestContent";

            // Arrange
            var context         = ApplicationDbContextInMemoryFactory.InitializeContext();
            var replyRepository = new EfDeletableEntityRepository <Reply>(context);
            var repliesService  = new RepliesService(replyRepository);

            var inputModel = new CreateReplyInputModel()
            {
                Content = testContent,
            };

            await repliesService.CreateAsync(inputModel);

            var reply = replyRepository.All().FirstOrDefault(c => c.Content == testContent);

            // Act
            var expectedReplyContent = "Edited_TestContent";
            await repliesService.EditAsync(reply.Id, expectedReplyContent);

            var actualReplyContent = reply.Content;

            // Assert
            reply = await replyRepository.GetByIdWithDeletedAsync(reply.Id);

            Assert.Equal(expectedReplyContent, actualReplyContent);
        }
        public void SetUp()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString());

            this.repository     = new EfDeletableEntityRepository <Reply>(new ApplicationDbContext(options.Options));
            this.repliesService = new RepliesService(this.repository);
            AutoMapperConfig.RegisterMappings(typeof(TestReply).Assembly);
        }
Пример #6
0
        public async Task GetAuthorIdByIdMethodShouldReturnNullIfReplyIsNotFound()
        {
            var options = new DbContextOptionsBuilder <ForumDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            var db = new ForumDbContext(options);
            var usersServiceMock     = new Mock <IUsersService>();
            var dateTimeProviderMock = new Mock <IDateTimeProvider>();

            var repliesService = new RepliesService(db, null, usersServiceMock.Object, dateTimeProviderMock.Object);
            var authorId       = await repliesService.GetAuthorIdByIdAsync(1);

            authorId.Should().BeNull();
        }
Пример #7
0
        public async Task IsExistingMethodShouldReturnFalseIfNotExists()
        {
            var options = new DbContextOptionsBuilder <ForumDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            var db = new ForumDbContext(options);
            var usersServiceMock     = new Mock <IUsersService>();
            var dateTimeProviderMock = new Mock <IDateTimeProvider>();

            var repliesService = new RepliesService(db, null, usersServiceMock.Object, dateTimeProviderMock.Object);
            var isExisting     = await repliesService.IsExistingAsync(1);

            isExisting.Should().BeFalse();
        }
Пример #8
0
        public async Task CreateMethodShouldAddReplyInDatabase(string description, int?parentId, int postId)
        {
            var guid = Guid.NewGuid().ToString();

            var options = new DbContextOptionsBuilder <ForumDbContext>()
                          .UseInMemoryDatabase(guid)
                          .Options;

            var db = new ForumDbContext(options);
            var usersServiceMock     = new Mock <IUsersService>();
            var dateTimeProviderMock = new Mock <IDateTimeProvider>();

            var repliesService = new RepliesService(db, null, usersServiceMock.Object, dateTimeProviderMock.Object);
            await repliesService.CreateAsync(description, parentId, postId, guid);

            db.Replies.Should().HaveCount(1);
        }
Пример #9
0
        public async Task DeleteByIdAsync_WithIncorrectData_ShouldThrowArgumentNullException()
        {
            var incorrectId = Guid.NewGuid().ToString();

            // Arrange
            var context         = ApplicationDbContextInMemoryFactory.InitializeContext();
            var replyRepository = new EfDeletableEntityRepository <Reply>(context);
            var repliesService  = new RepliesService(replyRepository);

            // Act

            // Assert
            await Assert.ThrowsAsync <ArgumentException>(async() =>
            {
                await repliesService.DeleteByIdAsync(incorrectId);
            });
        }
Пример #10
0
        public async Task CreateAsync_ShouldSuccessfullyCreate()
        {
            // Arrange
            var context         = ApplicationDbContextInMemoryFactory.InitializeContext();
            var replyRepository = new EfDeletableEntityRepository <Reply>(context);
            var repliesService  = new RepliesService(replyRepository);

            var inputModel = new CreateReplyInputModel()
            {
                Content = "TestContent",
            };

            // Act
            var expectedRepliesCount = 1;
            await repliesService.CreateAsync(inputModel);

            var actualRepliesCount = replyRepository.All().Count();

            // Assert
            Assert.Equal(expectedRepliesCount, actualRepliesCount);
        }
Пример #11
0
        public async Task MakeBestAnswerMethodShouldChangeIsBestAnswer(bool isBestAnswer)
        {
            var options = new DbContextOptionsBuilder <ForumDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            var db = new ForumDbContext(options);
            var usersServiceMock     = new Mock <IUsersService>();
            var dateTimeProviderMock = new Mock <IDateTimeProvider>();

            dateTimeProviderMock.Setup(dtp => dtp.Now()).Returns(new DateTime(2020, 3, 27));

            await db.Replies.AddAsync(new Reply
            {
                Description  = "Test",
                IsBestAnswer = isBestAnswer,
                CreatedOn    = dateTimeProviderMock.Object.Now()
            });

            await db.SaveChangesAsync();

            var repliesService = new RepliesService(db, null, usersServiceMock.Object, dateTimeProviderMock.Object);
            await repliesService.MakeBestAnswerAsync(1);

            var expected = new Reply
            {
                Id           = 1,
                Description  = "Test",
                IsBestAnswer = !isBestAnswer,
                CreatedOn    = dateTimeProviderMock.Object.Now()
            };

            var actual = await db.Replies.FirstOrDefaultAsync();

            expected.Id.Should().Be(actual.Id);
            expected.Description.Should().Be(actual.Description);
            expected.IsBestAnswer.Should().Be(actual.IsBestAnswer);
            expected.CreatedOn.Should().BeSameDateAs(actual.CreatedOn);
        }
Пример #12
0
        public async Task CreateMethodShouldAddRightReplyInDatabase(string description, int?parentId, int postId)
        {
            var guid = Guid.NewGuid().ToString();

            var options = new DbContextOptionsBuilder <ForumDbContext>()
                          .UseInMemoryDatabase(guid)
                          .Options;

            var db = new ForumDbContext(options);
            var usersServiceMock     = new Mock <IUsersService>();
            var dateTimeProviderMock = new Mock <IDateTimeProvider>();

            dateTimeProviderMock.Setup(dtp => dtp.Now()).Returns(new DateTime(2020, 3, 27));

            var repliesService = new RepliesService(db, null, usersServiceMock.Object, dateTimeProviderMock.Object);
            await repliesService.CreateAsync(description, parentId, postId, guid);

            var expected = new Reply
            {
                Id          = 1,
                Description = description,
                ParentId    = parentId,
                PostId      = postId,
                AuthorId    = guid,
                CreatedOn   = dateTimeProviderMock.Object.Now(),
            };

            var actual = await db.Replies.FirstOrDefaultAsync();

            expected.Id.Should().Be(actual.Id);
            expected.Description.Should().Be(actual.Description);
            expected.ParentId.Should().Be(actual.ParentId);
            expected.PostId.Should().Be(actual.PostId);
            expected.AuthorId.Should().Be(actual.AuthorId);
            expected.CreatedOn.Should().BeSameDateAs(actual.CreatedOn);
        }
Пример #13
0
 public RepliesController(RepliesService service)
 {
     _service = service;
 }