mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
Finished MessageRepository
+ Tests
This commit is contained in:
@@ -255,4 +255,143 @@ public class MessagesRepositoryTests
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<NotFoundException>(async () => await messagesRepository.FindBySenderAndReceiverIdAsync(senderId, receiverId, recipientType));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidDateTime_When_FindByValidDateTimeAsync_Then_ReturnMessages()
|
||||
{
|
||||
// Assert
|
||||
var messages = _fixture.CreateMany<Message>(10).ToList();
|
||||
var dateTime = _fixture.Create<DateTime>();
|
||||
|
||||
foreach (var message in messages)
|
||||
{
|
||||
message.SentAt = dateTime;
|
||||
}
|
||||
|
||||
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.FindBySentAtAsync(dateTime);
|
||||
|
||||
// Assert
|
||||
Assert.That(results, Is.Not.Null);
|
||||
Assert.That(results, Is.EquivalentTo(messages));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidMessage_When_AddAsync_Then_MessageAdded()
|
||||
{
|
||||
// Arrange
|
||||
var message = _fixture.Create<Message>();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var messagesRepository = new MessagesRepository(context, _messageValidator);
|
||||
|
||||
// Act
|
||||
messagesRepository.AddAsync(message);
|
||||
|
||||
// Assert
|
||||
Assert.That(context.Messages.Count, Is.EqualTo(1));
|
||||
Assert.That(context.Messages.First(), Is.EqualTo(message));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidMessage_When_AddAsync_Should_Throw_AdditionException()
|
||||
{
|
||||
// Arrange
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var messagesRepository = new MessagesRepository(context, _messageValidator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<AdditionException>(async () => await messagesRepository.AddAsync(default));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ExistMessage_When_Exist_Then_ReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
var message = _fixture.Create<Message>();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var messagesRepository = new MessagesRepository(context, _messageValidator);
|
||||
|
||||
context.Messages.Add(message);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result = messagesRepository.Exist(message);
|
||||
var result2 = messagesRepository.Exist(message.Id);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.True);
|
||||
Assert.That(result2, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_NotExistMessage_When_Exist_Then_ReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
var message = _fixture.Create<Message>();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var messagesRepository = new MessagesRepository(context, _messageValidator);
|
||||
|
||||
// Act
|
||||
var result = messagesRepository.Exist(message);
|
||||
var result2 = messagesRepository.Exist(message.Id);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.False);
|
||||
Assert.That(result2, Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_NotEqualMessage_When_Exist_Then_ReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
var message = _fixture.Create<Message>();
|
||||
var message2 = _fixture.Create<Message>();
|
||||
message.Id = message2.Id;
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var messagesRepository = new MessagesRepository(context, _messageValidator);
|
||||
|
||||
context.Messages.Add(message);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result = messagesRepository.Exist(message2);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidMessage_When_Exist_Should_Throw_InvalidObjectException()
|
||||
{
|
||||
// Arrange
|
||||
var message = _fixture.Create<Message>();
|
||||
message.SentAt = DateTime.MinValue;
|
||||
message.RecipientId = Guid.Empty;
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var messagesRepository = new MessagesRepository(context, _messageValidator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<InvalidObjectException<Message>>(() => messagesRepository.Exist(message));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Given_NullMessage_When_Exist_Should_Throw_InvalidObjectException()
|
||||
{
|
||||
using var context = new GovorDbContext(_options);
|
||||
var messagesRepository = new MessagesRepository(context, _messageValidator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<InvalidObjectException<Message>>(() => messagesRepository.Exist(default(Message)));
|
||||
}
|
||||
}
|
||||
@@ -272,7 +272,7 @@ public class UsersRepositoryTests
|
||||
var userRepository = new UsersRepository(context, _userValidator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<AdditionUserException>(async () => await userRepository.AddAsync(user));
|
||||
Assert.ThrowsAsync<AdditionException>(async () => await userRepository.AddAsync(user));
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -4,5 +4,6 @@ namespace Govor.Core.Repositories.Messages;
|
||||
|
||||
public interface IMessagesExist
|
||||
{
|
||||
bool Exist(Guid messageId);
|
||||
bool Exist(Message message);
|
||||
}
|
||||
@@ -4,7 +4,7 @@ namespace Govor.Core.Repositories.Messages;
|
||||
|
||||
public interface IMessagesWriter
|
||||
{
|
||||
void Add(Message message);
|
||||
void Update(Message message);
|
||||
void Delete(Guid messageId);
|
||||
Task AddAsync(Message message);
|
||||
Task UpdateAsync(Message message);
|
||||
Task RemoveAsync(Guid messageId);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace Govor.Data.Repositories.Exceptions;
|
||||
|
||||
public class AdditionException(string s, Exception ex)
|
||||
: Exception(s, ex);
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace Govor.Data.Repositories.Exceptions;
|
||||
|
||||
public class RemoveException(string s, Exception exception)
|
||||
: Exception(s, exception);
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace Govor.Data.Repositories.Exceptions;
|
||||
|
||||
public class UpdateException(string s, Exception ex)
|
||||
: Exception(s, ex);
|
||||
@@ -63,28 +63,108 @@ public class MessagesRepository : IMessagesRepository
|
||||
.ToListOrThrowIfEmpty(new NotFoundException("No messages found in the database"));
|
||||
}
|
||||
|
||||
public Task<List<Message>> FindBySentAtAsync(DateTime date)
|
||||
public async Task<List<Message>> FindBySentAtAsync(DateTime date)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
return await _context.Messages
|
||||
.AsNoTracking()
|
||||
.Include(m => m.ReplyToMessage)
|
||||
.Where(m => m.SentAt == date)
|
||||
.ToListOrThrowIfEmpty(new NotFoundByKeyException<DateTime>(date, "Messages sent at date do not exist"));
|
||||
}
|
||||
|
||||
public void Add(Message message)
|
||||
public async Task AddAsync(Message message)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
try
|
||||
{
|
||||
_validator.Validate(message);
|
||||
|
||||
_context.Messages.Add(message);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (InvalidObjectException<Message> ex)
|
||||
{
|
||||
throw new AdditionException("Message with given data invalid", ex);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new AdditionException("Cannot add message", ex);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Test this block
|
||||
public async Task UpdateAsync(Message message)
|
||||
{
|
||||
try
|
||||
{
|
||||
_validator.Validate(message);
|
||||
|
||||
var rowsAffected = await _context.Messages
|
||||
.Where(m => m.Id == message.Id)
|
||||
.ExecuteUpdateAsync(u => u
|
||||
.SetProperty(m => m.SenderId, message.SenderId)
|
||||
.SetProperty(m => m.RecipientId, message.RecipientId)
|
||||
.SetProperty(m => m.SentAt, message.SentAt)
|
||||
.SetProperty(m => m.RecipientType, message.RecipientType)
|
||||
.SetProperty(m => m.EncryptedContent, message.EncryptedContent)
|
||||
.SetProperty(m => m.EditedAt, message.EditedAt)
|
||||
.SetProperty(m => m.IsEdited, message.IsEdited)
|
||||
.SetProperty(m => m.Reactions, message.Reactions)
|
||||
.SetProperty(m => m.ReplyToMessageId, message.ReplyToMessageId)
|
||||
.SetProperty(m => m.MessageViews, message.MessageViews)
|
||||
.SetProperty(m => m.MediaAttachments, message.MediaAttachments)
|
||||
|
||||
);
|
||||
|
||||
if (rowsAffected == 0)
|
||||
throw new NotFoundByKeyException<Guid>(message.Id);
|
||||
}
|
||||
catch (NotFoundByKeyException<Guid> ex)
|
||||
{
|
||||
throw new UpdateException($"Not found message by given id {message.Id}", ex);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new UpdateException($"Error when updating the message {message.Id}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void Update(Message message)
|
||||
public async Task RemoveAsync(Guid messageId)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
try
|
||||
{
|
||||
var result = await FindByIdAsync(messageId);
|
||||
|
||||
public void Delete(Guid messageId)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
_context.Messages.Remove(result);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (NotFoundByKeyException<Guid> ex)
|
||||
{
|
||||
throw new RemoveException($"Not found message by given id {messageId}", ex);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new RemoveException("Error when removing the message", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Exist(Message message)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
_validator.Validate(message);
|
||||
|
||||
return _context.Messages.Any(
|
||||
m => m.Id == message.Id &&
|
||||
m.SenderId == message.SenderId &&
|
||||
m.RecipientId == message.RecipientId &&
|
||||
m.EncryptedContent == message.EncryptedContent &&
|
||||
m.EditedAt == message.EditedAt &&
|
||||
m.IsEdited == message.IsEdited &&
|
||||
m.SentAt == message.SentAt &&
|
||||
m.ReplyToMessageId == message.ReplyToMessageId
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Exist(Guid messageId)
|
||||
{
|
||||
return _context.Messages.Any(m => m.Id == messageId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,11 +91,11 @@ public class UsersRepository : IUsersRepository
|
||||
}
|
||||
catch (InvalidObjectException<User> ex)
|
||||
{
|
||||
throw new AdditionUserException("User with given data invalid", ex);
|
||||
throw new AdditionException("User with given data invalid", ex);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new AdditionUserException("Cannot add user", ex);
|
||||
throw new AdditionException("Cannot add user", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,11 +122,11 @@ public class UsersRepository : IUsersRepository
|
||||
}
|
||||
catch (NotFoundByKeyException<Guid> ex)
|
||||
{
|
||||
throw new UserUpdateException($"Not found user by given id {user.Id}", ex);
|
||||
throw new UpdateException($"Not found user by given id {user.Id}", ex);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new UserUpdateException($"Error when updating the user {user.Id}", ex);
|
||||
throw new UpdateException($"Error when updating the user {user.Id}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,7 +139,7 @@ public class UsersRepository : IUsersRepository
|
||||
}
|
||||
catch (InvalidObjectException<User> ex)
|
||||
{
|
||||
throw new UserRemoveException("User with given data invalid", ex);
|
||||
throw new RemoveException("User with given data invalid", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,11 +154,11 @@ public class UsersRepository : IUsersRepository
|
||||
}
|
||||
catch (NotFoundByKeyException<Guid> ex)
|
||||
{
|
||||
throw new UserRemoveException($"Not found user by given id {userId}", ex);
|
||||
throw new RemoveException($"Not found user by given id {userId}", ex);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new UserRemoveException("Error when removing the user", ex);
|
||||
throw new RemoveException("Error when removing the user", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -183,14 +183,4 @@ public class UsersRepository : IUsersRepository
|
||||
return _context.Users.AnyAsync(u => u.Username == username);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class UserRemoveException(string s, Exception exception)
|
||||
: Exception(s, exception);
|
||||
|
||||
public class AdditionUserException(string s, Exception ex)
|
||||
: Exception(s, ex);
|
||||
|
||||
public class UserUpdateException(string s, Exception ex)
|
||||
: Exception(s, ex);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user