mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
6d1c53beeb
Large refactor that renames/moves core types into a new Govor.Domain surface and reorganizes the Application layer. Models, configurations, migrations and many files moved from Govor.Core/Govor.Data to Govor.Domain; numerous Application services, interfaces and implementations were relocated or added (authentication, friends, messages, medias, push notifications, user sessions, storage, synching, private chats, etc.). Tests updated to use Govor.Domain namespaces and adjusted project references (removed Govor.Data reference from API tests). Also updated API, Hub and mapping code and project files to reflect the new structure and naming. This is primarily a codebase-wide namespace and module reorganization to establish a Domain project and restructure application services.
203 lines
6.5 KiB
C#
203 lines
6.5 KiB
C#
using AutoFixture;
|
|
using Govor.Domain.Infrastructure.Validators;
|
|
using Govor.Domain.Models;
|
|
using Govor.Domain;
|
|
using Govor.Domain.Repositories;
|
|
using Govor.Domain.Repositories.Exceptions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Govor.Domain.Tests.Repositories;
|
|
|
|
[TestFixture]
|
|
public class PrivateChatsRepositoryTests
|
|
{
|
|
private Fixture _fixture;
|
|
private DbContextOptions<GovorDbContext> _options;
|
|
private IObjectValidator<PrivateChat> _validator = new PrivateChatValidator();
|
|
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(PrivateChatsRepositoryTests)}_{_testIteration}")
|
|
.Options;
|
|
}
|
|
|
|
[Test]
|
|
public async Task Given_NotEmptySetDb_When_GetAll_Then_ReturnAll()
|
|
{
|
|
// Arrange
|
|
var random = new Random();
|
|
var chats = _fixture.CreateMany<PrivateChat>(random.Next(2, 10)).ToList();
|
|
|
|
await using var context = new GovorDbContext(_options);
|
|
var repository = new PrivateChatsRepository(context, _validator);
|
|
|
|
context.PrivateChats.AddRange(chats);
|
|
await context.SaveChangesAsync();
|
|
|
|
// Act
|
|
|
|
var result = await repository.GetAllAsync();
|
|
|
|
// Assert
|
|
Assert.That(result, Is.Not.Null);
|
|
Assert.That(result.Count, Is.EqualTo(chats.Count));
|
|
Assert.That(result, Is.EquivalentTo(chats));
|
|
}
|
|
|
|
[Test]
|
|
public void Given_EmptySetDb_When_GetAll_Should_Throw_NotFoundException()
|
|
{
|
|
// Arrange
|
|
using var context = new GovorDbContext(_options);
|
|
var repository = new PrivateChatsRepository(context, _validator);
|
|
// Act & Assert
|
|
Assert.ThrowsAsync<NotFoundException>(async () => await repository.GetAllAsync());
|
|
}
|
|
|
|
[Test]
|
|
public async Task Given_ValidChatId_When_GetById_Then_ReturnChat()
|
|
{
|
|
// Arrange
|
|
var chats = _fixture.CreateMany<PrivateChat>(10);
|
|
var id = chats.First().Id;
|
|
|
|
await using var context = new GovorDbContext(_options);
|
|
var repository = new PrivateChatsRepository(context, _validator);
|
|
|
|
context.PrivateChats.AddRange(chats);
|
|
await context.SaveChangesAsync();
|
|
|
|
// Act
|
|
var result = await repository.GetByIdAsync(id);
|
|
|
|
// Assert
|
|
Assert.That(result, Is.Not.Null);
|
|
Assert.That(result, Is.EqualTo(chats.First()));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Given_InvalidMessageId_When_FindById_Should_Throw_NotFoundByKeyException()
|
|
{
|
|
// Arrange
|
|
await using var context = new GovorDbContext(_options);
|
|
var repository = new PrivateChatsRepository(context, _validator);
|
|
|
|
// Act & Assert
|
|
Assert.ThrowsAsync<NotFoundByKeyException<Guid>>(async () => await repository.GetByIdAsync(_fixture.Create<Guid>()));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Given_MembersId_When_GetByMembersAsync_Then_ReturnChat()
|
|
{
|
|
// Arrange
|
|
var chats = _fixture.CreateMany<PrivateChat>(10);
|
|
var id1 = chats.First().UserAId;
|
|
var id2 = chats.First().UserBId;
|
|
|
|
await using var context = new GovorDbContext(_options);
|
|
var repository = new PrivateChatsRepository(context, _validator);
|
|
|
|
context.PrivateChats.AddRange(chats);
|
|
await context.SaveChangesAsync();
|
|
|
|
// Act
|
|
var result = await repository.GetByMembersAsync(id1, id2);
|
|
|
|
// Assert
|
|
Assert.That(result, Is.Not.Null);
|
|
Assert.That(result, Is.EqualTo(chats.First()));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Given_InvalidMembersId_When_GetByMembersAsync_Should_Throw_NotFoundByKeyException()
|
|
{
|
|
// Arrange
|
|
await using var context = new GovorDbContext(_options);
|
|
var repository = new PrivateChatsRepository(context, _validator);
|
|
|
|
// Act & Assert
|
|
Assert.ThrowsAsync<NotFoundByKeyException<(Guid, Guid)>>(async () => await repository.GetByMembersAsync(_fixture.Create<Guid>(), _fixture.Create<Guid>()));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Given_ValidPrivateChat_When_AddAsync_Then_PrivateChatAdded()
|
|
{
|
|
// Arrange
|
|
var chat = _fixture.Create<PrivateChat>();
|
|
|
|
await using var context = new GovorDbContext(_options);
|
|
var repository = new PrivateChatsRepository(context, _validator);
|
|
|
|
// Act
|
|
await repository.AddAsync(chat);
|
|
|
|
// Assert
|
|
Assert.That(context.PrivateChats.Count, Is.EqualTo(1));
|
|
Assert.That(context.PrivateChats.First(), Is.EqualTo(chat));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Given_InvalidPrivateChat_When_AddAsync_Should_Throw_AdditionException()
|
|
{
|
|
// Arrange
|
|
await using var context = new GovorDbContext(_options);
|
|
var repository = new PrivateChatsRepository(context, _validator);
|
|
|
|
// Act & Assert
|
|
Assert.ThrowsAsync<AdditionException>(async () => await repository.AddAsync(default));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Given_ExistPrivateChat_When_Exist_Then_ReturnTrue()
|
|
{
|
|
// Arrange
|
|
var chat = _fixture.Create<PrivateChat>();
|
|
|
|
await using var context = new GovorDbContext(_options);
|
|
var repository = new PrivateChatsRepository(context, _validator);
|
|
|
|
context.PrivateChats.Add(chat);
|
|
await context.SaveChangesAsync();
|
|
|
|
// Act
|
|
var result1 = repository.Exist(chat.UserAId, chat.UserBId);
|
|
var result2 = repository.Exist(chat.Id);
|
|
|
|
|
|
// Assert
|
|
Assert.That(result1, Is.True);
|
|
Assert.That(result2, Is.True);
|
|
}
|
|
|
|
[Test]
|
|
public async Task Given_NotExistPrivateChat_When_Exist_Then_ReturnFalse()
|
|
{
|
|
// Arrange
|
|
var chat = _fixture.Create<PrivateChat>();
|
|
|
|
await using var context = new GovorDbContext(_options);
|
|
var repository = new PrivateChatsRepository(context, _validator);
|
|
|
|
// Act
|
|
var result1 = repository.Exist(chat.UserAId, chat.UserBId);
|
|
var result2 = repository.Exist(chat.Id);
|
|
|
|
// Assert
|
|
Assert.That(result1, Is.False);
|
|
Assert.That(result2, Is.False);
|
|
}
|
|
} |