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.
177 lines
5.6 KiB
C#
177 lines
5.6 KiB
C#
using AutoFixture;
|
|
using Govor.Domain.Infrastructure.Validators;
|
|
using Govor.Domain.Models;
|
|
using Govor.Domain.Models.Users;
|
|
using Govor.Domain;
|
|
using Govor.Domain.Repositories;
|
|
using Govor.Domain.Repositories.Exceptions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Govor.Domain.Tests.Repositories;
|
|
|
|
[TestFixture]
|
|
public class AdminsRepositoryTests
|
|
{
|
|
private Fixture _fixture;
|
|
private DbContextOptions<GovorDbContext> _options;
|
|
private IObjectValidator<Admin> _validator = new AdminValidator();
|
|
|
|
private int _testIteration = 0;
|
|
|
|
[SetUp]
|
|
public void Setup()
|
|
{
|
|
_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(AdminsRepositoryTests)}_{_testIteration}")
|
|
.Options;
|
|
_testIteration += 1;
|
|
}
|
|
|
|
[Test]
|
|
public async Task Given_NotEmptyDbSet_When_GetAllAsync_Then_Admins_Are_Returned()
|
|
{
|
|
// Arrange
|
|
var random = new Random();
|
|
var admins = _fixture.CreateMany<Admin>(random.Next(3, 10)).ToList();
|
|
|
|
await using var context = new GovorDbContext(_options);
|
|
var repository = new AdminsRepository(context, _validator);
|
|
|
|
context.AddRange(admins);
|
|
await context.SaveChangesAsync();
|
|
|
|
// Act
|
|
var result = await repository.GetAllAsync();
|
|
|
|
// Assert
|
|
Assert.That(result, Is.Not.Null);
|
|
Assert.That(result.Count(), Is.EqualTo(admins.Count()));
|
|
Assert.That(result.Select(r => r.UserId), Is.EquivalentTo(admins.Select(r => r.UserId)));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Given_EmptyDbSet_When_GetAllAsync_Should_Throw_NotFoundException()
|
|
{
|
|
// Arrange
|
|
await using var context = new GovorDbContext(_options);
|
|
var repository = new AdminsRepository(context, _validator);
|
|
|
|
// Act & Assert
|
|
Assert.ThrowsAsync<NotFoundException>(async () => await repository.GetAllAsync());
|
|
}
|
|
|
|
[Test]
|
|
public async Task Given_ValidUserId_When_GetByIdAsync_Then_Admin_Should_Return_Admin()
|
|
{
|
|
// Arrange
|
|
var admin = _fixture.Create<Admin>();
|
|
await using var context = new GovorDbContext(_options);
|
|
var repository = new AdminsRepository(context, _validator);
|
|
|
|
context.Add(admin);
|
|
await context.SaveChangesAsync();
|
|
|
|
// Act
|
|
var result = await repository.GetByIdAsync(admin.UserId);
|
|
|
|
// Assert
|
|
Assert.That(result, Is.Not.Null);
|
|
Assert.That(result.UserId, Is.EqualTo(admin.UserId));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Given_InvalidUserId_When_GetByIdAsync_Should_Throw_NotFoundByKeyException()
|
|
{
|
|
// Arrange
|
|
await using var context = new GovorDbContext(_options);
|
|
var repository = new AdminsRepository(context, _validator);
|
|
|
|
// Act & Assert
|
|
Assert.ThrowsAsync<NotFoundByKeyException<Guid>>(async () => await repository.GetByIdAsync(_fixture.Create<Guid>()));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Given_ValidAdmin_When_AddAsync_Then_Added()
|
|
{
|
|
// Arrange
|
|
var admin = _fixture.Create<Admin>();
|
|
|
|
await using var context = new GovorDbContext(_options);
|
|
var repository = new AdminsRepository(context, _validator);
|
|
|
|
// Act
|
|
await repository.AddAsync(admin);
|
|
|
|
// Assert
|
|
Assert.That(context.Admins.Count, Is.EqualTo(1));
|
|
Assert.That(context.Admins.First(), Is.EqualTo(admin));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Given_InvalidAdmin_When_AddAsync_Should_Throw_AdditionException()
|
|
{
|
|
// Arrange
|
|
await using var context = new GovorDbContext(_options);
|
|
var repository = new AdminsRepository(context, _validator);
|
|
|
|
// Act & Assert
|
|
Assert.ThrowsAsync<AdditionException>(async () => await repository.AddAsync(default));
|
|
}
|
|
|
|
[Test]
|
|
public void Given_ValidAdmin_When_Exist_Then_Returns_True()
|
|
{
|
|
// Arrange
|
|
var admin = _fixture.Create<Admin>();
|
|
using var context = new GovorDbContext(_options);
|
|
var repository = new AdminsRepository(context, _validator);
|
|
|
|
context.Add(admin);
|
|
context.SaveChanges();
|
|
// Act
|
|
var result = repository.Exist(admin.UserId);
|
|
var result2 = repository.Exist(admin);
|
|
|
|
// Assert
|
|
Assert.That(result, Is.True);
|
|
Assert.That(result2, Is.True);
|
|
}
|
|
|
|
[Test]
|
|
public void Given_ExistAdmin_When_Exist_Then_Returns_False()
|
|
{
|
|
// Arrange
|
|
var admin = _fixture.Create<Admin>();
|
|
using var context = new GovorDbContext(_options);
|
|
var repository = new AdminsRepository(context, _validator);
|
|
|
|
// Act
|
|
var result = repository.Exist(admin.UserId);
|
|
var result2 = repository.Exist(admin);
|
|
|
|
// Assert
|
|
Assert.That(result, Is.False);
|
|
Assert.That(result2, Is.False);
|
|
}
|
|
|
|
[Test]
|
|
public void Given_NullAdmin_When_Exist_Should_Throw_InvalidObjectException()
|
|
{
|
|
// Arrange
|
|
using var context = new GovorDbContext(_options);
|
|
var repository = new AdminsRepository(context, _validator);
|
|
|
|
// Act & Assert
|
|
Assert.Throws<InvalidObjectException<Admin>>(() => repository.Exist(default(Admin)));
|
|
}
|
|
}
|