mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
MessageRepository + Tests
+ GetAllAsync rework + FindByIdAsync + rework FindBySenderAndReceiverIdAsync
This commit is contained in:
@@ -3,6 +3,7 @@ using Govor.Core.Infrastructure.Validators;
|
||||
using Govor.Core.Models;
|
||||
using Govor.Data;
|
||||
using Govor.Data.Repositories;
|
||||
using Govor.Data.Repositories.Exceptions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Govor.API.Tests.IntegrationTests.EF.Repositories;
|
||||
@@ -51,16 +52,42 @@ public class MessagesRepositoryTests
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Count, Is.EqualTo(messages.Count));
|
||||
Assert.That(result.Select(u => u.Id), Is.EquivalentTo(messages.Select(u => u.Id)));
|
||||
Assert.That(result.Select(u => u.SentAt), Is.EquivalentTo(messages.Select(u => u.SentAt)));
|
||||
Assert.That(result.Select(r => r.SenderId), Is.EquivalentTo(messages.Select(u => u.SenderId)));
|
||||
Assert.That(result.Select(r => r.RecipientId), Is.EquivalentTo(messages.Select(u => u.RecipientId)));
|
||||
Assert.That(result.Select(r => r.RecipientType), Is.EquivalentTo(messages.Select(u => u.RecipientType)));
|
||||
Assert.That(result, Is.EquivalentTo(messages));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Given_EmptySetDb_When_GetAllMessages_Should_Throw_NotFoundException()
|
||||
{
|
||||
using var context = new GovorDbContext(_options);
|
||||
var messagesRepository = new MessagesRepository(context, _messageValidator);
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<NotFoundException>(async () => await messagesRepository.GetAllAsync());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidMessageId_When_FindMessageById_Then_ReturnMessage()
|
||||
{
|
||||
var messages = _fixture.CreateMany<Message>(10);
|
||||
var id = messages.First().Id;
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var messagesRepository = new MessagesRepository(context, _messageValidator);
|
||||
|
||||
context.Messages.AddRange(messages);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var result = await messagesRepository.FindByIdAsync(id);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Is.EqualTo(messages.First()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidMessageId_When_FindById_Should_Throw_NotFoundByKeyException()
|
||||
{
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var messagesRepository = new MessagesRepository(context, _messageValidator);
|
||||
|
||||
Assert.ThrowsAsync<NotFoundByKeyException<Guid>>(async () => await messagesRepository.FindByIdAsync(_fixture.Create<Guid>()));
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,21 @@ public class Message
|
||||
|
||||
public Guid? ReplyToMessageId { get; set; }
|
||||
public Message? ReplyToMessage { get; set; } // navigation
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
if (obj is not Message other) return false;
|
||||
|
||||
return Id == other.Id &&
|
||||
SenderId == other.SenderId &&
|
||||
RecipientId == other.RecipientId &&
|
||||
RecipientType == other.RecipientType &&
|
||||
EncryptedContent == other.EncryptedContent &&
|
||||
SentAt == other.SentAt &&
|
||||
IsEdited == other.IsEdited &&
|
||||
EditedAt == other.EditedAt &&
|
||||
ReplyToMessageId == other.ReplyToMessageId;
|
||||
}
|
||||
}
|
||||
|
||||
public enum RecipientType
|
||||
|
||||
@@ -8,6 +8,6 @@ public interface IMessagesReader
|
||||
Task<Message> FindByIdAsync(Guid messageId);
|
||||
Task<List<Message>> FindBySenderIdAsync(Guid senderId);
|
||||
Task<List<Message>> FindByReceiverIdAsync(Guid receiverId);
|
||||
Task<List<Message>> FindBySenderAndReceiverIdAsync(Guid senderId, Guid receiverId);
|
||||
Task<List<Message>> FindBySenderAndReceiverIdAsync(Guid senderId, Guid receiverId, RecipientType recipientType = RecipientType.User);
|
||||
Task<List<Message>> FindBySentAtAsync(DateTime date);
|
||||
}
|
||||
@@ -22,13 +22,17 @@ public class MessagesRepository : IMessagesRepository
|
||||
{
|
||||
return await _context.Messages
|
||||
.AsNoTracking()
|
||||
.Where(x => true)
|
||||
.ToListOrThrowIfEmpty(new NotFoundException("Messages in Database not exists"));
|
||||
.Include(m => m.ReplyToMessage)
|
||||
.ToListOrThrowIfEmpty(new NotFoundException("No messages found in the database"));
|
||||
}
|
||||
|
||||
public Task<Message> FindByIdAsync(Guid messageId)
|
||||
public async Task<Message> FindByIdAsync(Guid messageId)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
return await _context.Messages
|
||||
.AsNoTracking()
|
||||
.Include(m => m.ReplyToMessage)
|
||||
.FirstOrDefaultAsync(m => m.Id == messageId)
|
||||
?? throw new NotFoundByKeyException<Guid>(messageId, "Message with given id does not exist");
|
||||
}
|
||||
|
||||
public Task<List<Message>> FindBySenderIdAsync(Guid senderId)
|
||||
@@ -41,7 +45,7 @@ public class MessagesRepository : IMessagesRepository
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<List<Message>> FindBySenderAndReceiverIdAsync(Guid senderId, Guid receiverId)
|
||||
public Task<List<Message>> FindBySenderAndReceiverIdAsync(Guid senderId, Guid receiverId, RecipientType recipientType = RecipientType.User)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ public class UsersRepository : IUsersRepository
|
||||
{
|
||||
return await _context.Users
|
||||
.AsNoTracking()
|
||||
.Where(x => true)
|
||||
.ToListOrThrowIfEmpty(new NotFoundException("Users in Database not exists"));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user