mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
Finished MediaAttachments
the functionality and tests are completed
This commit is contained in:
@@ -100,4 +100,149 @@ public class MediaAttachmentsTests
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<NotFoundByKeyException<Guid>>(async () => await attachmentsRepository.GetAllByMessageId(_fixture.Create<Guid>()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidAttachmentsId_When_FindByIdAsync_Then_Returns_Attachment()
|
||||
{
|
||||
// Arrange
|
||||
var attachment = _fixture.Create<MediaAttachments>();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var attachmentsRepository = new MediaAttachmentsRepository(context, _validator);
|
||||
|
||||
context.MediaAttachments.Add(attachment);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result = await attachmentsRepository.FindByIdAsync(attachment.Id);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Is.EqualTo(attachment));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidAttachmentsId_When_FindByIdAsync_Should_Throw_NotFoundByKeyException()
|
||||
{
|
||||
// Arrange
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var attachmentsRepository = new MediaAttachmentsRepository(context, _validator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<NotFoundByKeyException<Guid>>(async () => await attachmentsRepository.FindByIdAsync(_fixture.Create<Guid>()));
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidAttachments_When_AddAsync_Then_AttachmentsAdded()
|
||||
{
|
||||
// Arrange
|
||||
var attachments = _fixture.Create<MediaAttachments>();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var messagesRepository = new MediaAttachmentsRepository(context, _validator);
|
||||
|
||||
// Act
|
||||
await messagesRepository.AddAsync(attachments);
|
||||
|
||||
// Assert
|
||||
Assert.That(context.MediaAttachments.Count, Is.EqualTo(1));
|
||||
Assert.That(context.MediaAttachments.First(), Is.EqualTo(attachments));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidAttachments_When_AddAsync_Should_Throw_AdditionException()
|
||||
{
|
||||
// Arrange
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new MediaAttachmentsRepository(context, _validator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<AdditionException>(async () => await repository.AddAsync(default));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ExistAttachment_When_Exist_Then_ReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
var attachments = _fixture.Create<MediaAttachments>();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new MediaAttachmentsRepository(context, _validator);
|
||||
|
||||
context.MediaAttachments.Add(attachments);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result = repository.Exist(attachments);
|
||||
var result2 = repository.Exist(attachments.Id);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.True);
|
||||
Assert.That(result2, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_NotExistMessage_When_Exist_Then_ReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
var attachments = _fixture.Create<MediaAttachments>();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new MediaAttachmentsRepository(context, _validator);
|
||||
|
||||
|
||||
// Act
|
||||
var result = repository.Exist(attachments);
|
||||
var result2 = repository.Exist(attachments.Id);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.False);
|
||||
Assert.That(result2, Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_NotEqualMessage_When_Exist_Then_ReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
var attachments = _fixture.Create<MediaAttachments>();
|
||||
var attachments2 = _fixture.Create<MediaAttachments>();
|
||||
attachments2.Id = attachments.Id;
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new MediaAttachmentsRepository(context, _validator);
|
||||
|
||||
context.MediaAttachments.Add(attachments);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result = repository.Exist(attachments2);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidMessage_When_Exist_Should_Throw_InvalidObjectException()
|
||||
{
|
||||
// Arrange
|
||||
var attachments = _fixture.Create<MediaAttachments>();
|
||||
attachments.MessageId = Guid.Empty;
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new MediaAttachmentsRepository(context, _validator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<InvalidObjectException<MediaAttachments>>(() => repository.Exist(attachments));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Given_NullMessage_When_Exist_Should_Throw_InvalidObjectException()
|
||||
{
|
||||
using var context = new GovorDbContext(_options);
|
||||
var repository = new MediaAttachmentsRepository(context, _validator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<InvalidObjectException<MediaAttachments>>(() => repository.Exist(default(MediaAttachments)));
|
||||
}
|
||||
}
|
||||
@@ -292,7 +292,7 @@ public class MessagesRepositoryTests
|
||||
var messagesRepository = new MessagesRepository(context, _messageValidator);
|
||||
|
||||
// Act
|
||||
messagesRepository.AddAsync(message);
|
||||
await messagesRepository.AddAsync(message);
|
||||
|
||||
// Assert
|
||||
Assert.That(context.Messages.Count, Is.EqualTo(1));
|
||||
|
||||
@@ -4,6 +4,6 @@ namespace Govor.Core.Repositories.MediasAttachments;
|
||||
|
||||
public interface IMediaAttachmentsExist
|
||||
{
|
||||
bool Exists(Guid id);
|
||||
bool Exists(MediaAttachments attachments);
|
||||
bool Exist(Guid id);
|
||||
bool Exist(MediaAttachments attachments);
|
||||
}
|
||||
@@ -5,5 +5,6 @@ namespace Govor.Core.Repositories.MediasAttachments;
|
||||
public interface IMediaAttachmentsReader
|
||||
{
|
||||
Task<List<MediaAttachments>> GetAllAsync();
|
||||
Task<MediaAttachments> FindByIdAsync(Guid id);
|
||||
Task<List<MediaAttachments>> GetAllByMessageId(Guid messageId);
|
||||
}
|
||||
@@ -5,6 +5,6 @@ namespace Govor.Core.Repositories.MediasAttachments;
|
||||
public interface IMediaAttachmentsWriter
|
||||
{
|
||||
Task AddAsync(MediaAttachments mediaAttachments);
|
||||
Task UpdateAsync(MediaAttachments mediaAttachments);
|
||||
Task UpdateAsync(MediaAttachments attachments);
|
||||
Task RemoveAsync(Guid Id);
|
||||
}
|
||||
@@ -35,28 +35,98 @@ public class MediaAttachmentsRepository : IMediaAttachmentsRepository
|
||||
.ToListOrThrowIfEmpty(new NotFoundByKeyException<Guid>(messageId, "No media attachments found by given message Id"));
|
||||
}
|
||||
|
||||
public Task AddAsync(MediaAttachments mediaAttachments)
|
||||
public async Task<MediaAttachments> FindByIdAsync(Guid id)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
return await _context.MediaAttachments
|
||||
.AsNoTracking()
|
||||
.Include(ma => ma.Message)
|
||||
.FirstOrDefaultAsync(m => m.Id == id)
|
||||
?? throw new NotFoundByKeyException<Guid>(id, "No media attachments found by given Id");
|
||||
}
|
||||
|
||||
public Task UpdateAsync(MediaAttachments mediaAttachments)
|
||||
public async Task AddAsync(MediaAttachments mediaAttachments)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
try
|
||||
{
|
||||
_validator.Validate(mediaAttachments);
|
||||
|
||||
_context.MediaAttachments.Add(mediaAttachments);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (InvalidObjectException<Message> ex)
|
||||
{
|
||||
throw new AdditionException("Attachments with given data invalid", ex);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new AdditionException("Cannot add Attachments", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public Task RemoveAsync(Guid Id)
|
||||
public async Task UpdateAsync(MediaAttachments attachments)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
try
|
||||
{
|
||||
_validator.Validate(attachments);
|
||||
|
||||
var rowsAffected = await _context.MediaAttachments
|
||||
.Where(m => m.Id == attachments.Id)
|
||||
.ExecuteUpdateAsync(u => u
|
||||
.SetProperty(m => m.MessageId, attachments.MessageId)
|
||||
.SetProperty(m => m.Message, attachments.Message)
|
||||
.SetProperty(m => m.FilePath, attachments.FilePath)
|
||||
.SetProperty(m => m.MimeType, attachments.MimeType)
|
||||
.SetProperty(m => m.EncryptedKey, attachments.EncryptedKey)
|
||||
);
|
||||
|
||||
if (rowsAffected == 0)
|
||||
throw new NotFoundByKeyException<Guid>(attachments.Id);
|
||||
}
|
||||
catch (NotFoundByKeyException<Guid> ex)
|
||||
{
|
||||
throw new UpdateException($"Not found attachments by given id {attachments.Id}", ex);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new UpdateException($"Error when updating the attachments {attachments.Id}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Exists(Guid id)
|
||||
public async Task RemoveAsync(Guid Id)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
try
|
||||
{
|
||||
var result = await FindByIdAsync(Id);
|
||||
|
||||
_context.MediaAttachments.Remove(result);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (NotFoundByKeyException<Guid> ex)
|
||||
{
|
||||
throw new RemoveException($"Not found attachments by given id {Id}", ex);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new RemoveException("Error when removing the attachments", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Exists(MediaAttachments attachments)
|
||||
public bool Exist(Guid id)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
return _context.MediaAttachments.Any(e => e.Id == id);
|
||||
}
|
||||
|
||||
public bool Exist(MediaAttachments attachments)
|
||||
{
|
||||
_validator.Validate(attachments);
|
||||
|
||||
return _context.MediaAttachments.Any(
|
||||
e => e.Id == attachments.Id &&
|
||||
e.EncryptedKey == attachments.EncryptedKey &&
|
||||
e.MimeType == attachments.MimeType &&
|
||||
e.FilePath == attachments.FilePath &&
|
||||
e.MessageId == attachments.MessageId &&
|
||||
e.Type == attachments.Type
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user