MediaAttachmentsTests init

This commit is contained in:
Artemy
2025-06-22 15:37:20 +07:00
parent 64a06c0d1c
commit 4956a175c5
4 changed files with 163 additions and 2 deletions
@@ -0,0 +1,103 @@
using AutoFixture;
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;
[TestFixture]
public class MediaAttachmentsTests
{
private Fixture _fixture;
private DbContextOptions<GovorDbContext> _options;
private readonly IObjectValidator<MediaAttachments> _validator = new MediaAttachmentsValidator();
private int _testIteration = 0;
[SetUp]
public void SetUp()
{
_testIteration += 1;
_fixture = new Fixture();
_fixture.Behaviors
.OfType<ThrowingRecursionBehavior>()
.ToList()
.ForEach(b => _fixture.Behaviors.Remove(b));
_fixture.Behaviors.Add(new OmitOnRecursionBehavior());
_options = new DbContextOptionsBuilder<GovorDbContext>()
.UseInMemoryDatabase(databaseName: $"DbGovor_{nameof(MediaAttachmentsTests)}_{_testIteration}")
.Options;
}
[Test]
public async Task Given_NotEmptySetDb_When_GetAll_Then_ReturnAllAttachments()
{
// Arrange
var random = new Random();
var attachments = _fixture.CreateMany<MediaAttachments>(random.Next(2, 10)).ToList();
await using var context = new GovorDbContext(_options);
var attachmentsRepository = new MediaAttachmentsRepository(context, _validator);
context.MediaAttachments.AddRange(attachments);
await context.SaveChangesAsync();
// Act
var result = await attachmentsRepository.GetAllAsync();
// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result.Count, Is.EqualTo(attachments.Count));
Assert.That(result, Is.EquivalentTo(attachments));
}
[Test]
public void Given_EmptySetDb_When_GetAll_Should_Throw_NotFoundException()
{
// Arrange
using var context = new GovorDbContext(_options);
var mediaAttachments = new MediaAttachmentsRepository(context, _validator);
// Act & Assert
Assert.ThrowsAsync<NotFoundException>(async () => await mediaAttachments.GetAllAsync());
}
[Test]
public async Task Given_ValidMessageId_When_GetAllByMessageId_Then_ReturnAllAttachments()
{
// Arrange
var random = new Random();
var attachments = _fixture.CreateMany<MediaAttachments>(random.Next(2, 10)).ToList();
await using var context = new GovorDbContext(_options);
var attachmentsRepository = new MediaAttachmentsRepository(context, _validator);
context.MediaAttachments.AddRange(attachments);
await context.SaveChangesAsync();
// Act
var result = await attachmentsRepository.GetAllByMessageId(attachments.First().MessageId);
// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result.Count, Is.EqualTo(1));
Assert.That(result.First(), Is.EqualTo(attachments.First()));
}
[Test]
public async Task Given_InvalidMessageId_When_GetAllByMessageId_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.GetAllByMessageId(_fixture.Create<Guid>()));
}
}
@@ -0,0 +1,42 @@
using Govor.Core.Models;
using Exception = System.Exception;
namespace Govor.Core.Infrastructure.Validators;
public class MediaAttachmentsValidator : IObjectValidator<MediaAttachments>
{
public void Validate(MediaAttachments attachments)
{
try
{
if (attachments is null)
throw new ArgumentNullException(nameof(attachments));
if(attachments.Id == Guid.Empty)
throw new ArgumentException("Id cannot be empty", nameof(attachments));
if(attachments.MessageId == Guid.Empty)
throw new ArgumentException("MessageId cannot be empty", nameof(attachments));
if(string.IsNullOrWhiteSpace(attachments.FilePath))
throw new ArgumentException("File path cannot be empty", nameof(attachments));
if(string.IsNullOrWhiteSpace(attachments.EncryptedKey))
throw new ArgumentException("Encrypted key cannot be empty", nameof(attachments));
}
catch (Exception ex)
{
throw new InvalidObjectException<MediaAttachments>(ex);
}
}
public bool TryValidate(MediaAttachments attachments)
{
try
{
Validate(attachments);
return true;
}
catch (Exception)
{
return false;
}
}
}
+12
View File
@@ -11,6 +11,18 @@ public class MediaAttachments
public string? EncryptedKey { get; set; }
public Message Message { get; set; } = null!;
public override bool Equals(object? obj)
{
if (obj is not MediaAttachments other) return false;
return Id == other.Id &&
MessageId == other.MessageId &&
EncryptedKey == other.EncryptedKey &&
Type == other.Type &&
FilePath == other.FilePath &&
MimeType == other.MimeType;
}
}
public enum MediaType
@@ -26,9 +26,13 @@ public class MediaAttachmentsRepository : IMediaAttachmentsRepository
.ToListOrThrowIfEmpty(new NotFoundException("No media attachments found."));
}
public Task<List<MediaAttachments>> GetAllByMessageId(Guid messageId)
public async Task<List<MediaAttachments>> GetAllByMessageId(Guid messageId)
{
throw new NotImplementedException();
return await _context.MediaAttachments
.AsNoTracking()
.Include(ma => ma.Message)
.Where(m => m.MessageId == messageId)
.ToListOrThrowIfEmpty(new NotFoundByKeyException<Guid>(messageId, "No media attachments found by given message Id"));
}
public Task AddAsync(MediaAttachments mediaAttachments)