From 972b4fb83c2b8cbc45262072438f70a1d6ce1dd0 Mon Sep 17 00:00:00 2001 From: Artemy <109195690+stalcker2288969@users.noreply.github.com> Date: Sun, 22 Jun 2025 12:48:48 +0700 Subject: [PATCH] more tests and fichers for MessagesRepository --- .../Repositories/MessagesRepositoryTests.cs | 120 +++++++++++++++++- Govor.Data/Repositories/MessagesRepository.cs | 21 ++- 2 files changed, 133 insertions(+), 8 deletions(-) diff --git a/Govor.API.Tests/IntegrationTests/EF/Repositories/MessagesRepositoryTests.cs b/Govor.API.Tests/IntegrationTests/EF/Repositories/MessagesRepositoryTests.cs index a0428c4..3a561e6 100644 --- a/Govor.API.Tests/IntegrationTests/EF/Repositories/MessagesRepositoryTests.cs +++ b/Govor.API.Tests/IntegrationTests/EF/Repositories/MessagesRepositoryTests.cs @@ -14,10 +14,13 @@ public class MessagesRepositoryTests private Fixture _fixture; private DbContextOptions _options; private readonly IObjectValidator _messageValidator = new MessageValidator(); - + private int _testIteration = 0; + [SetUp] public void SetUp() { + _testIteration += 1; + _fixture = new Fixture(); _fixture.Behaviors @@ -28,7 +31,7 @@ public class MessagesRepositoryTests _fixture.Behaviors.Add(new OmitOnRecursionBehavior()); _options = new DbContextOptionsBuilder() - .UseInMemoryDatabase(databaseName: "DbGovor") + .UseInMemoryDatabase(databaseName: $"DbGovor_{nameof(MessagesRepositoryTests)}_{_testIteration}") .Options; } @@ -137,6 +140,119 @@ public class MessagesRepositoryTests [Test] public async Task Given_ValidReceiverId_When_FindByReceiverIdAsync_Then_ReturnMessages() { + // Arrange + var messages = _fixture.CreateMany(10).ToList(); + var receiverId =_fixture.Create(); + foreach (var message in messages) + { + message.RecipientId = receiverId; + } + + await using var context = new GovorDbContext(_options); + var messagesRepository = new MessagesRepository(context, _messageValidator); + + context.Messages.AddRange(messages); + await context.SaveChangesAsync(); + + // Act + var results = await messagesRepository.FindByReceiverIdAsync(receiverId); + + // Assert + Assert.That(results, Is.Not.Null); + Assert.That(results, Is.EquivalentTo(messages)); + } + + [Test] + public void Given_InvalidReceiverId_When_FindByReceiverIdAsync_Should_Throw_NotFoundByKeyException() + { + // Arrange + using var context = new GovorDbContext(_options); + var messagesRepository = new MessagesRepository(context, _messageValidator); + + // Act & Assert + Assert.ThrowsAsync>(async () => await messagesRepository.FindByReceiverIdAsync(_fixture.Create())); + } + + [Test] + public async Task Given_ValidSenderIdReceiverIdAndReceiverType_When_FindBySenderAndReceiverIdAsync_Then_ReturnMessages() + { + // Arrange + var messages = _fixture.CreateMany(10).ToList(); + + var receiverId =_fixture.Create(); + var senderId =_fixture.Create(); + RecipientType recipientType =_fixture.Create(); + + foreach (var message in messages) + { + message.RecipientId = receiverId; + message.SenderId = senderId; + message.RecipientType = recipientType; + } + + await using var context = new GovorDbContext(_options); + var messagesRepository = new MessagesRepository(context, _messageValidator); + + context.Messages.AddRange(messages); + await context.SaveChangesAsync(); + + // Act + var results = await messagesRepository.FindBySenderAndReceiverIdAsync(senderId, receiverId, recipientType); + + Assert.That(results, Is.Not.Null); + Assert.That(results, Is.EquivalentTo(messages)); + } + + [Test] + public async Task Given_InvalidSenderId_When_FindBySenderAndReceiverIdAsync_Should_Throw_NotFoundByKeyException() + { + // Arrange + var messages = _fixture.CreateMany(10).ToList(); + + var receiverId =_fixture.Create(); + var senderId =_fixture.Create(); + RecipientType recipientType =_fixture.Create(); + + foreach (var message in messages) + { + message.RecipientId = receiverId; + message.RecipientType = recipientType; + } + + await using var context = new GovorDbContext(_options); + var messagesRepository = new MessagesRepository(context, _messageValidator); + + context.Messages.AddRange(messages); + await context.SaveChangesAsync(); + + // Act & Assert + Assert.ThrowsAsync(async () => await messagesRepository.FindBySenderAndReceiverIdAsync(senderId, receiverId, recipientType)); + } + + [Test] + public async Task Given_InvalidReceiverId_When_FindBySenderAndReceiverIdAsync_Should_Throw_NotFoundByKeyException() + { + // Arrange + var messages = _fixture.CreateMany(10).ToList(); + + var receiverId =_fixture.Create(); + var senderId =_fixture.Create(); + RecipientType recipientType =_fixture.Create(); + + foreach (var message in messages) + { + message.SenderId = senderId; + message.RecipientType = recipientType; + } + + await using var context = new GovorDbContext(_options); + var messagesRepository = new MessagesRepository(context, _messageValidator); + + context.Messages.AddRange(messages); + await context.SaveChangesAsync(); + + // Act & Assert + Assert.ThrowsAsync(async () => await messagesRepository.FindBySenderAndReceiverIdAsync(senderId, receiverId, recipientType)); } } \ No newline at end of file diff --git a/Govor.Data/Repositories/MessagesRepository.cs b/Govor.Data/Repositories/MessagesRepository.cs index 31c7755..819fb9e 100644 --- a/Govor.Data/Repositories/MessagesRepository.cs +++ b/Govor.Data/Repositories/MessagesRepository.cs @@ -11,7 +11,6 @@ public class MessagesRepository : IMessagesRepository { private GovorDbContext _context; private IObjectValidator _validator; - public MessagesRepository(GovorDbContext context, IObjectValidator validator) { _context = context; @@ -44,16 +43,26 @@ public class MessagesRepository : IMessagesRepository .ToListOrThrowIfEmpty(new NotFoundByKeyException(senderId, "Messages with given sender id do not exist")); } - public Task> FindByReceiverIdAsync(Guid receiverId) + public async Task> FindByReceiverIdAsync(Guid receiverId) { - throw new NotImplementedException(); + return await _context.Messages + .AsNoTracking() + .Include(m => m.ReplyToMessage) + .Where(m => m.RecipientId == receiverId) + .ToListOrThrowIfEmpty(new NotFoundByKeyException(receiverId, "Messages with given recipient id do not exist")); } - public Task> FindBySenderAndReceiverIdAsync(Guid senderId, Guid receiverId, RecipientType recipientType = RecipientType.User) + public async Task> FindBySenderAndReceiverIdAsync(Guid senderId, Guid receiverId, RecipientType recipientType = RecipientType.User) { - throw new NotImplementedException(); + return await _context.Messages + .AsNoTracking() + .Include(m => m.ReplyToMessage) + .Where(m => m.SenderId == senderId + && m.RecipientId == receiverId + && m.RecipientType == recipientType) + .ToListOrThrowIfEmpty(new NotFoundException("No messages found in the database")); } - + public Task> FindBySentAtAsync(DateTime date) { throw new NotImplementedException();