mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
Refactor EF tests
This commit is contained in:
@@ -27,4 +27,8 @@
|
||||
<ProjectReference Include="..\Govor.API\Govor.API.csproj" />
|
||||
<ProjectReference Include="..\Govor.Data\Govor.Data.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="IntegrationTests\EF\Repositories\" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -1,176 +0,0 @@
|
||||
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;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
|
||||
namespace Govor.API.Tests.IntegrationTests.EF.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)));
|
||||
}
|
||||
}
|
||||
@@ -1,191 +0,0 @@
|
||||
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 FriendshipsRepositoryTests
|
||||
{
|
||||
private Fixture _fixture;
|
||||
private DbContextOptions<GovorDbContext> _options;
|
||||
private IObjectValidator<Friendship> _validator = new FriendshipValidator();
|
||||
|
||||
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(FriendshipsRepositoryTests)}_{_testIteration}")
|
||||
.Options;
|
||||
_testIteration += 1;
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task Given_NotEmptyDbSet_When_GetAllAsync_Then_Friendships_Are_Returned()
|
||||
{
|
||||
// Arrange
|
||||
var random = new Random();
|
||||
var friendships = _fixture.CreateMany<Friendship>(random.Next(3, 10)).ToList();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new FriendshipsRepository(context, _validator);
|
||||
|
||||
context.AddRange(friendships);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result = await repository.GetAllAsync();
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Count(), Is.EqualTo(friendships.Count()));
|
||||
Assert.That(result.Select(r => r.Id), Is.EquivalentTo(friendships.Select(r => r.Id)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_EmptyDbSet_When_GetAllAsync_Should_Throw_NotFoundException()
|
||||
{
|
||||
// Arrange
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new FriendshipsRepository(context, _validator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<NotFoundException>(async () => await repository.GetAllAsync());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidFriendshipId_When_GetByIdAsync_Then_Friendships_Are_Returned()
|
||||
{
|
||||
// Arrange
|
||||
var random = new Random();
|
||||
var friendships = _fixture.CreateMany<Friendship>(random.Next(3, 10)).ToList();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new FriendshipsRepository(context, _validator);
|
||||
|
||||
context.AddRange(friendships);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result = await repository.GetByIdAsync(friendships.First().Id);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Id, Is.EqualTo(friendships.First().Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidFriendshipId_When_GetByIdAsync_Should_Throw_NotFoundByKeyException()
|
||||
{
|
||||
// Arrange
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new FriendshipsRepository(context, _validator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<NotFoundByKeyException<Guid>>(async () => await repository.GetByIdAsync(_fixture.Create<Guid>()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidUserId_When_GetByIdAsync_Then_Friendships_Are_Returned()
|
||||
{
|
||||
// Arrange
|
||||
var random = new Random();
|
||||
var friendships = _fixture.CreateMany<Friendship>(random.Next(3, 10)).ToList();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new FriendshipsRepository(context, _validator);
|
||||
|
||||
context.AddRange(friendships);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result = await repository.FindByUserIdAsync(friendships.First().RequesterId);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.First().Id, Is.EqualTo(friendships.First().Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidUserId_When_GetByIdAsync_Should_Throw_NotFoundByKeyException()
|
||||
{
|
||||
// Arrange
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new FriendshipsRepository(context, _validator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<NotFoundByKeyException<Guid>>(async () => await repository.FindByUserIdAsync(_fixture.Create<Guid>()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidFriendship_When_AddAsync_Then_Added()
|
||||
{
|
||||
// Arrange
|
||||
var friendship = _fixture.Create<Friendship>();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new FriendshipsRepository(context, _validator);
|
||||
|
||||
// Act
|
||||
await repository.AddAsync(friendship);
|
||||
|
||||
// Assert
|
||||
Assert.That(context.Friendships.Count, Is.EqualTo(1));
|
||||
Assert.That(context.Friendships.First().Id, Is.EqualTo(friendship.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidFriendship_When_AddAsync_Should_Throw_AdditionException()
|
||||
{
|
||||
// Arrange
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new FriendshipsRepository(context, _validator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<AdditionException>(async () => await repository.AddAsync(default));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ExistFriendship_When_Exist_Then_True()
|
||||
{
|
||||
// Arrange
|
||||
var friendship = _fixture.Create<Friendship>();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new FriendshipsRepository(context, _validator);
|
||||
|
||||
context.Friendships.Add(friendship);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result = repository.Exist(friendship.RequesterId, friendship.AddresseeId);
|
||||
// Assert
|
||||
Assert.That(result, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_NotExistFriendship_When_Exist_Then_False()
|
||||
{
|
||||
// Arrange
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new FriendshipsRepository(context, _validator);
|
||||
// Act & Assert
|
||||
Assert.That(repository.Exist(_fixture.Create<Guid>(), _fixture.Create<Guid>()), Is.False);
|
||||
}
|
||||
}
|
||||
@@ -1,308 +0,0 @@
|
||||
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 GroupRepositoryTests
|
||||
{
|
||||
private Fixture _fixture;
|
||||
private DbContextOptions<GovorDbContext> _options;
|
||||
private IObjectValidator<ChatGroup> _validator = new ChatGroupValidator();
|
||||
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(GroupRepositoryTests)}_{_testIteration}")
|
||||
.Options;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_NotEmptySetDb_When_GetAll_Then_ReturnAll()
|
||||
{
|
||||
// Arrange
|
||||
var random = new Random();
|
||||
var chats = _fixture.CreateMany<ChatGroup>(random.Next(2, 10)).ToList();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new GroupRepository(context, _validator);
|
||||
|
||||
context.ChatGroups.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 GroupRepository(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<ChatGroup>(10);
|
||||
var id = chats.First().Id;
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new GroupRepository(context, _validator);
|
||||
|
||||
context.ChatGroups.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 void Given_InvalidMessageId_When_FindById_Should_Throw_NotFoundByKeyException()
|
||||
{
|
||||
// Arrange
|
||||
using var context = new GovorDbContext(_options);
|
||||
var repository = new GroupRepository(context, _validator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<NotFoundByKeyException<Guid>>(async () =>
|
||||
await repository.GetByIdAsync(_fixture.Create<Guid>()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidQuery_When_SearchByNameAsync_Then_Returns_ChatGroups()
|
||||
{
|
||||
// Arrange
|
||||
var random = new Random();
|
||||
var chats = _fixture.CreateMany<ChatGroup>(random.Next(3, 10)).ToList();
|
||||
|
||||
using var context = new GovorDbContext(_options);
|
||||
var repository = new GroupRepository(context, _validator);
|
||||
|
||||
context.ChatGroups.AddRange(chats);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result = await repository.SearchByNameAsync(chats[0].Name[..14]);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Count, Is.EqualTo(1));
|
||||
Assert.That(result.First(), Is.EqualTo(chats.First()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Given_InvalidQuery_When_SearchPotentialFriendsAsync_Should_Throw_NotFoundByKeyException()
|
||||
{
|
||||
// Arrange
|
||||
using var context = new GovorDbContext(_options);
|
||||
var repository = new GroupRepository(context, _validator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<NotFoundByKeyException<string>>(async () => await
|
||||
repository.SearchByNameAsync(_fixture.Create<string>()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidAdminId_When_GetByAdminIdAsync_Returns_ChatGroups()
|
||||
{
|
||||
// Arrange
|
||||
var random = new Random();
|
||||
var chats = _fixture.CreateMany<ChatGroup>(random.Next(3, 10)).ToList();
|
||||
var adminId = chats.First().Admins.First().UserId;
|
||||
|
||||
using var context = new GovorDbContext(_options);
|
||||
var repository = new GroupRepository(context, _validator);
|
||||
|
||||
context.ChatGroups.AddRange(chats);
|
||||
await context.SaveChangesAsync();
|
||||
// Act
|
||||
var result = await repository.GetByAdminIdAsync(adminId);
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Count, Is.EqualTo(1));
|
||||
Assert.That(result.First(), Is.EqualTo(chats.First()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Given_ValidInvalidAdminId_When_GetByAdminIdAsync_Throws_NotFoundByKeyException()
|
||||
{
|
||||
// Arrange
|
||||
using var context = new GovorDbContext(_options);
|
||||
var repository = new GroupRepository(context, _validator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<NotFoundByKeyException<Guid>>(async () => await
|
||||
repository.GetByAdminIdAsync(_fixture.Create<Guid>()));
|
||||
}
|
||||
[Test]
|
||||
public async Task Given_ValidMemberId_When_GetByAdminIdAsync_Returns_ChatGroups()
|
||||
{
|
||||
// Arrange
|
||||
var random = new Random();
|
||||
var chats = _fixture.CreateMany<ChatGroup>(random.Next(3, 10)).ToList();
|
||||
var userId = chats.First().Members.First().UserId;
|
||||
|
||||
using var context = new GovorDbContext(_options);
|
||||
var repository = new GroupRepository(context, _validator);
|
||||
|
||||
context.ChatGroups.AddRange(chats);
|
||||
await context.SaveChangesAsync();
|
||||
// Act
|
||||
var result = await repository.GetByUserIdAsync(userId);
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Count, Is.EqualTo(1));
|
||||
Assert.That(result.First(), Is.EqualTo(chats.First()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Given_ValidInvalidMemberId_When_GetByAdminIdAsync_Throws_NotFoundByKeyException()
|
||||
{
|
||||
// Arrange
|
||||
using var context = new GovorDbContext(_options);
|
||||
var repository = new GroupRepository(context, _validator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<NotFoundByKeyException<Guid>>(async () => await
|
||||
repository.GetByUserIdAsync(_fixture.Create<Guid>()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidChatGroup_When_AddAsync_Then_PrivateChatAdded()
|
||||
{
|
||||
// Arrange
|
||||
var chat = _fixture.Create<ChatGroup>();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new GroupRepository(context, _validator);
|
||||
|
||||
// Act
|
||||
await repository.AddAsync(chat);
|
||||
|
||||
// Assert
|
||||
Assert.That(context.ChatGroups.Count, Is.EqualTo(1));
|
||||
Assert.That(context.ChatGroups.First(), Is.EqualTo(chat));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Given_InvalidChatGroup_When_AddAsync_Should_Throw_AdditionException()
|
||||
{
|
||||
// Arrange
|
||||
using var context = new GovorDbContext(_options);
|
||||
var repository = new GroupRepository(context, _validator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<AdditionException>(async () => await repository.AddAsync(default));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ExistChatGroup_When_Exist_Then_ReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
var chat = _fixture.Create<ChatGroup>();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new GroupRepository(context, _validator);
|
||||
|
||||
context.ChatGroups.Add(chat);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result1 = repository.Exist(chat.Id);
|
||||
var result2 = repository.Exist(chat);
|
||||
|
||||
|
||||
// Assert
|
||||
Assert.That(result1, Is.True);
|
||||
Assert.That(result2, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_NotExistChatGroup_When_Exist_Then_ReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
var chat = _fixture.Create<ChatGroup>();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new GroupRepository(context, _validator);
|
||||
|
||||
// Act
|
||||
var result1 = repository.Exist(chat.Id);
|
||||
var result2 = repository.Exist(chat);
|
||||
|
||||
// Assert
|
||||
Assert.That(result1, Is.False);
|
||||
Assert.That(result2, Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidUserIdAndChatGroupId_When_IsUserMemberOfGroupAsync_Then_ReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
var chat = _fixture.Create<ChatGroup>();
|
||||
var userId = chat.Members.First().UserId;
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new GroupRepository(context, _validator);
|
||||
|
||||
await context.ChatGroups.AddAsync(chat);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result = await repository.IsUserMemberOfGroupAsync(userId, chat.Id);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidValidUserIdAndChatGroupId_When_IsUserMemberOfGroupAsync_Then_ReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
var chat = _fixture.Create<ChatGroup>();
|
||||
var userId = chat.Members.First().UserId;
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new GroupRepository(context, _validator);
|
||||
|
||||
// Act
|
||||
var result = await repository.IsUserMemberOfGroupAsync(userId, chat.Id);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.False);
|
||||
}
|
||||
}
|
||||
@@ -1,249 +0,0 @@
|
||||
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 InvitesRepositoryTests
|
||||
{
|
||||
private Fixture _fixture;
|
||||
private DbContextOptions<GovorDbContext> _options;
|
||||
private IObjectValidator<Invitation> _validator = new InvitationValidator();
|
||||
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(InvitesRepositoryTests)}_{_testIteration}")
|
||||
.Options;
|
||||
_testIteration += 1;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_NotEmptyDbSet_When_GetAllAsync_Then_Invites_Are_Returned()
|
||||
{
|
||||
// Arrange
|
||||
var random = new Random();
|
||||
var invitations = _fixture.CreateMany<Invitation>(random.Next(3, 10)).ToList();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new InvitesRepository(context, _validator);
|
||||
|
||||
context.Invitations.AddRange(invitations);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result = await repository.GetAllAsync();
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Count(), Is.EqualTo(invitations.Count()));
|
||||
Assert.That(result, Is.EquivalentTo(invitations));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_EmptyDbSet_When_GetAllAsync_Should_Throw_NotFoundException()
|
||||
{
|
||||
// Arrange
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new InvitesRepository(context, _validator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<NotFoundException>(async () => await repository.GetAllAsync());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidInvitesId_When_FindByIdAsync_Then_Invites_Are_Returned()
|
||||
{
|
||||
// Arrange
|
||||
var invitation = _fixture.Create<Invitation>();
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new InvitesRepository(context, _validator);
|
||||
|
||||
context.Invitations.Add(invitation);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result = await repository.FindByIdAsync(invitation.Id);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Id, Is.EqualTo(invitation.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidInvitesId_When_FindByIdAsync_Should_Throw_NotFoundByKeyException()
|
||||
{
|
||||
// Arrange
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new InvitesRepository(context, _validator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<NotFoundByKeyException<Guid>>(async () => await repository.FindByIdAsync(Guid.Empty));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidInvitesCode_When_FindByCodeAsync_Then_Invites_Are_Returned()
|
||||
{
|
||||
// Arrange
|
||||
var invitation = _fixture.Create<Invitation>();
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new InvitesRepository(context, _validator);
|
||||
|
||||
context.Invitations.Add(invitation);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result = await repository.FindByCodeAsync(invitation.Code);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Id, Is.EqualTo(invitation.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidCode_When_FindByCodeAsync_Should_Throw_NotFoundByKeyException()
|
||||
{
|
||||
// Arrange
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new InvitesRepository(context, _validator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<NotFoundByKeyException<string>>(async () => await repository.FindByCodeAsync(_fixture.Create<string>()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidAdminInvites_When_FindAdminsInvitesAsync_Then_Invites_Are_Returned()
|
||||
{
|
||||
// Arrange
|
||||
var random = new Random();
|
||||
var invitations = _fixture.CreateMany<Invitation>(random.Next(3, 10)).ToList();
|
||||
|
||||
foreach (var inv in invitations)
|
||||
{
|
||||
inv.IsAdmin = true;
|
||||
}
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new InvitesRepository(context, _validator);
|
||||
|
||||
context.Invitations.AddRange(invitations);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result = await repository.FindAdminsInvitesAsync();
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Count(), Is.EqualTo(invitations.Count()));
|
||||
Assert.That(result, Is.EquivalentTo(invitations));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidAdminsInvites_When_FindAdminsInvitesAsync_Should_Throw_NotFoundByKeyException()
|
||||
{
|
||||
// Arrange
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new InvitesRepository(context, _validator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<NotFoundByKeyException<bool>>(async () => await repository.FindAdminsInvitesAsync());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidAdmin_When_AddAsync_Then_Added()
|
||||
{
|
||||
// Arrange
|
||||
var invitation = _fixture.Create<Invitation>();
|
||||
invitation.Code = string.Join("", _fixture.CreateMany<char>(12));
|
||||
invitation.EndDate = invitation.DateCreated.AddHours(1);
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new InvitesRepository(context, _validator);
|
||||
|
||||
// Act
|
||||
await repository.AddAsync(invitation);
|
||||
|
||||
// Assert
|
||||
Assert.That(context.Invitations.Count, Is.EqualTo(1));
|
||||
Assert.That(context.Invitations.First(), Is.EqualTo(invitation));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidAdmin_When_AddAsync_Should_Throw_AdditionException()
|
||||
{
|
||||
// Arrange
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new InvitesRepository(context, _validator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<AdditionException>(async () => await repository.AddAsync(default));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_NotExistInvitesCode_When_Exist_Then_Returns_False()
|
||||
{
|
||||
// Arrange
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new InvitesRepository(context, _validator);
|
||||
|
||||
// Act
|
||||
var result = repository.Exist(Guid.NewGuid());
|
||||
var result2 = repository.Exist(_fixture.Create<string>());
|
||||
|
||||
Assert.That(result, Is.False);
|
||||
Assert.That(result2, Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ExistInvites_When_Exist_Then_Returns_False()
|
||||
{
|
||||
// Arrange
|
||||
var invitation = _fixture.Create<Invitation>();
|
||||
|
||||
invitation.Code = string.Join("", _fixture.CreateMany<char>(12));
|
||||
invitation.EndDate = invitation.DateCreated.AddHours(1);
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new InvitesRepository(context, _validator);
|
||||
|
||||
context.Invitations.Add(invitation);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result = repository.Exist(invitation.Id);
|
||||
var result2 = repository.Exist(invitation);
|
||||
var result3 = repository.Exist(invitation.Code);
|
||||
|
||||
Assert.That(result, Is.True);
|
||||
Assert.That(result2, Is.True);
|
||||
Assert.That(result3, Is.True);
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidInvites_When_Exist_Then_Returns_False()
|
||||
{
|
||||
// Arrange
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var repository = new InvitesRepository(context, _validator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<InvalidObjectException<Invitation>>(() => repository.Exist(default(Invitation)));
|
||||
}
|
||||
}
|
||||
@@ -1,248 +0,0 @@
|
||||
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>()));
|
||||
}
|
||||
|
||||
[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)));
|
||||
}
|
||||
}
|
||||
@@ -1,397 +0,0 @@
|
||||
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 MessagesRepositoryTests
|
||||
{
|
||||
private Fixture _fixture;
|
||||
private DbContextOptions<GovorDbContext> _options;
|
||||
private readonly IObjectValidator<Message> _messageValidator = new MessageValidator();
|
||||
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(MessagesRepositoryTests)}_{_testIteration}")
|
||||
.Options;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_NotEmptySetDb_When_GetAllMessages_Then_ReturnAllMessages()
|
||||
{
|
||||
// Arrange
|
||||
var random = new Random();
|
||||
var messages = _fixture.CreateMany<Message>(random.Next(2, 10)).ToList();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var messagesRepository = new MessagesRepository(context, _messageValidator);
|
||||
|
||||
context.Messages.AddRange(messages);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
|
||||
var result = await messagesRepository.GetAllAsync();
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Count, Is.EqualTo(messages.Count));
|
||||
Assert.That(result, Is.EquivalentTo(messages));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Given_EmptySetDb_When_GetAllMessages_Should_Throw_NotFoundException()
|
||||
{
|
||||
// Arrange
|
||||
using var context = new GovorDbContext(_options);
|
||||
var messagesRepository = new MessagesRepository(context, _messageValidator);
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<NotFoundException>(async () => await messagesRepository.GetAllAsync());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidMessageId_When_FindMessageById_Then_ReturnMessage()
|
||||
{
|
||||
// Arrange
|
||||
var messages = _fixture.CreateMany<Message>(10);
|
||||
var id = messages.First().Id;
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var messagesRepository = new MessagesRepository(context, _messageValidator);
|
||||
|
||||
context.Messages.AddRange(messages);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result = await messagesRepository.FindByIdAsync(id);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Is.EqualTo(messages.First()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidMessageId_When_FindById_Should_Throw_NotFoundByKeyException()
|
||||
{
|
||||
// Arrange
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var messagesRepository = new MessagesRepository(context, _messageValidator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<NotFoundByKeyException<Guid>>(async () => await messagesRepository.FindByIdAsync(_fixture.Create<Guid>()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidSenderId_When_FindBySenderIdAsync_Then_ReturnMessages()
|
||||
{
|
||||
// Arrange
|
||||
var messages = _fixture.CreateMany<Message>(10).ToList();
|
||||
var senderId =_fixture.Create<Guid>();
|
||||
|
||||
foreach (var message in messages)
|
||||
{
|
||||
message.SenderId = senderId;
|
||||
}
|
||||
|
||||
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.FindBySenderIdAsync(senderId);
|
||||
|
||||
// Assert
|
||||
Assert.That(results, Is.Not.Null);
|
||||
Assert.That(results, Is.EquivalentTo(messages));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Given_InvalidSenderId_When_FindBySenderId_Should_Throw_NotFoundByKeyException()
|
||||
{
|
||||
// Arrange
|
||||
using var context = new GovorDbContext(_options);
|
||||
var messagesRepository = new MessagesRepository(context, _messageValidator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<NotFoundByKeyException<Guid>>(async () => await messagesRepository.FindBySenderIdAsync(_fixture.Create<Guid>()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidReceiverId_When_FindByReceiverIdAsync_Then_ReturnMessages()
|
||||
{
|
||||
// Arrange
|
||||
var messages = _fixture.CreateMany<Message>(10).ToList();
|
||||
var receiverId =_fixture.Create<Guid>();
|
||||
|
||||
foreach (var message in messages)
|
||||
{
|
||||
message.RecipientId = receiverId;
|
||||
}
|
||||
|
||||
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.FindByReceiverIdAsync(receiverId);
|
||||
|
||||
// Assert
|
||||
Assert.That(results, Is.Not.Null);
|
||||
Assert.That(results, Is.EquivalentTo(messages));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Given_InvalidReceiverId_When_FindByReceiverIdAsync_Should_Throw_NotFoundByKeyException()
|
||||
{
|
||||
// Arrange
|
||||
using var context = new GovorDbContext(_options);
|
||||
var messagesRepository = new MessagesRepository(context, _messageValidator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<NotFoundByKeyException<Guid>>(async () => await messagesRepository.FindByReceiverIdAsync(_fixture.Create<Guid>()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidSenderIdReceiverIdAndReceiverType_When_FindBySenderAndReceiverIdAsync_Then_ReturnMessages()
|
||||
{
|
||||
// Arrange
|
||||
var messages = _fixture.CreateMany<Message>(10).ToList();
|
||||
|
||||
var receiverId =_fixture.Create<Guid>();
|
||||
var senderId =_fixture.Create<Guid>();
|
||||
RecipientType recipientType =_fixture.Create<RecipientType>();
|
||||
|
||||
foreach (var message in messages)
|
||||
{
|
||||
message.RecipientId = receiverId;
|
||||
message.SenderId = senderId;
|
||||
message.RecipientType = recipientType;
|
||||
}
|
||||
|
||||
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.FindBySenderAndReceiverIdAsync(senderId, receiverId, recipientType);
|
||||
|
||||
Assert.That(results, Is.Not.Null);
|
||||
Assert.That(results, Is.EquivalentTo(messages));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidSenderId_When_FindBySenderAndReceiverIdAsync_Should_Throw_NotFoundByKeyException()
|
||||
{
|
||||
// Arrange
|
||||
var messages = _fixture.CreateMany<Message>(10).ToList();
|
||||
|
||||
var receiverId =_fixture.Create<Guid>();
|
||||
var senderId =_fixture.Create<Guid>();
|
||||
RecipientType recipientType =_fixture.Create<RecipientType>();
|
||||
|
||||
foreach (var message in messages)
|
||||
{
|
||||
message.RecipientId = receiverId;
|
||||
message.RecipientType = recipientType;
|
||||
}
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var messagesRepository = new MessagesRepository(context, _messageValidator);
|
||||
|
||||
context.Messages.AddRange(messages);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<NotFoundException>(async () => await messagesRepository.FindBySenderAndReceiverIdAsync(senderId, receiverId, recipientType));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidReceiverId_When_FindBySenderAndReceiverIdAsync_Should_Throw_NotFoundByKeyException()
|
||||
{
|
||||
// Arrange
|
||||
var messages = _fixture.CreateMany<Message>(10).ToList();
|
||||
|
||||
var receiverId =_fixture.Create<Guid>();
|
||||
var senderId =_fixture.Create<Guid>();
|
||||
RecipientType recipientType =_fixture.Create<RecipientType>();
|
||||
|
||||
foreach (var message in messages)
|
||||
{
|
||||
message.SenderId = senderId;
|
||||
message.RecipientType = recipientType;
|
||||
}
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var messagesRepository = new MessagesRepository(context, _messageValidator);
|
||||
|
||||
context.Messages.AddRange(messages);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// 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
|
||||
await 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)));
|
||||
}
|
||||
}
|
||||
@@ -1,204 +0,0 @@
|
||||
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 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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,424 +0,0 @@
|
||||
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 UsersRepositoryTests
|
||||
{
|
||||
private Fixture _fixture;
|
||||
private DbContextOptions<GovorDbContext> _options;
|
||||
private readonly IObjectValidator<User> _userValidator = new UserValidator();
|
||||
|
||||
[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")
|
||||
.Options;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetAll_Then_Returns_All_Users()
|
||||
{
|
||||
// Arrange
|
||||
var random = new Random();
|
||||
var users = _fixture.CreateMany<User>(random.Next(2, 10)).ToList();
|
||||
|
||||
_options = new DbContextOptionsBuilder<GovorDbContext>()
|
||||
.UseInMemoryDatabase(databaseName: "DbGovor_users_getall")
|
||||
.Options;
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var userRepository = new UsersRepository(context, _userValidator);
|
||||
|
||||
context.Users.AddRange(users);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
|
||||
var result = await userRepository.GetAllAsync();
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Count, Is.EqualTo(users.Count));
|
||||
Assert.That(result.Select(u => u.Id), Is.EquivalentTo(users.Select(u => u.Id)));
|
||||
Assert.That(result.Select(u => u.Username), Is.EquivalentTo(users.Select(u => u.Username)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidUserId_When_FindById_Then_Returns_User()
|
||||
{
|
||||
// Arrange
|
||||
var user = _fixture.Create<User>();
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var userRepository = new UsersRepository(context, _userValidator);
|
||||
|
||||
context.Users.Add(user);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result = await userRepository.FindByIdAsync(user.Id);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Username, Is.EqualTo(user.Username));
|
||||
Assert.That(result.Id, Is.EqualTo(user.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidUserId_When_FindById_Should_Throw_NotFoundException()
|
||||
{
|
||||
// Arrange
|
||||
var id = Guid.NewGuid();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var userRepository = new UsersRepository(context, _userValidator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<NotFoundByKeyException<Guid>>(async () => await userRepository.FindByIdAsync(id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_RangeValidUserId_When_FindByRangeId_Then_Returns_Users()
|
||||
{
|
||||
// Arrange
|
||||
var random = new Random();
|
||||
var users = _fixture.CreateMany<User>(random.Next(2, 10)).ToList();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var userRepository = new UsersRepository(context, _userValidator);
|
||||
|
||||
context.Users.AddRange(users);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result = await userRepository.FindByRangeIdAsync(users.Select(u => u.Id));
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Count, Is.EqualTo(users.Count));
|
||||
Assert.That(result.Select(r => r.Id), Is.EquivalentTo(users.Select(u => u.Id)));
|
||||
Assert.That(result.Select(u => u.Username), Is.EquivalentTo(users.Select(u => u.Username)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidRangeId_When_FindByRangeId_Should_Throw_NotFoundException()
|
||||
{
|
||||
// Arrange
|
||||
var random = new Random();
|
||||
var ids = _fixture.CreateMany<Guid>(random.Next(2, 10)).ToList();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var userRepository = new UsersRepository(context, _userValidator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<NotFoundByKeyException<IEnumerable<Guid>>>(async () =>
|
||||
await userRepository.FindByRangeIdAsync(ids));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidUsername_When_FindByUsername_Then_Returns_User()
|
||||
{
|
||||
// Arrange
|
||||
var user = _fixture.Create<User>();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var userRepository = new UsersRepository(context, _userValidator);
|
||||
|
||||
context.Users.Add(user);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result = await userRepository.FindByUsernameAsync(user.Username);
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Username, Is.EqualTo(user.Username));
|
||||
Assert.That(result.Id, Is.EqualTo(user.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidUsername_When_FindByUsername_Should_Throw_NotFoundByKeyException()
|
||||
{
|
||||
// Arrange
|
||||
string username = _fixture.Create<string>();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var userRepository = new UsersRepository(context, _userValidator);
|
||||
// Act & Assert
|
||||
|
||||
Assert.ThrowsAsync<NotFoundByKeyException<string>>(async () => await userRepository.FindByUsernameAsync(username));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidUsernames_When_FindByRangeUsernames_Then_Returns_Users()
|
||||
{
|
||||
// Arrange
|
||||
var random = new Random();
|
||||
var users = _fixture.CreateMany<User>(random.Next(3, 10)).ToList();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var userRepository = new UsersRepository(context, _userValidator);
|
||||
|
||||
context.Users.AddRange(users);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result = await userRepository.FindByRangeUsernamesAsync(users.Select(u => u.Username));
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Select(u => u.Username), Is.EquivalentTo(users.Select(u => u.Username)));
|
||||
Assert.That(result.Select(u => u.Id), Is.EquivalentTo(users.Select(u => u.Id)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidUsernames_When_FindByRangeUsernames_Should_Throw_NotFoundByKeyException()
|
||||
{
|
||||
// Arrange
|
||||
var random = new Random();
|
||||
var usernames = _fixture.CreateMany<string>(random.Next(3, 10)).ToList();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var userRepository = new UsersRepository(context, _userValidator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<NotFoundByKeyException<IEnumerable<string>>>(async () =>
|
||||
await userRepository.FindByRangeUsernamesAsync(usernames));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidQuery_When_SearchPotentialFriendsAsync_Then_Returns_Users()
|
||||
{
|
||||
// Arrange
|
||||
var random = new Random();
|
||||
var users = _fixture.CreateMany<User>(random.Next(3, 10)).ToList();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var userRepository = new UsersRepository(context, _userValidator);
|
||||
|
||||
context.Users.AddRange(users);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result = await userRepository.SearchPotentialFriendsAsync(users[1].Id, users[0].Username[..15]);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Count, Is.EqualTo(1));
|
||||
Assert.That(result.First().Id, Is.EqualTo(users[0].Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidQuery_When_SearchPotentialFriendsAsync_Should_Throw_NotFoundByKeyException()
|
||||
{
|
||||
// Arrange
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var userRepository = new UsersRepository(context, _userValidator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<NotFoundByKeyException<(string,Guid)>>(async () => await
|
||||
userRepository.SearchPotentialFriendsAsync(_fixture.Create<Guid>(), _fixture.Create<string>()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidDateOnly_When_FindByCreatedDate_Then_Returns_Users()
|
||||
{
|
||||
// Arrange
|
||||
var random = new Random();
|
||||
var users = _fixture.CreateMany<User>(random.Next(3, 10)).ToList();
|
||||
|
||||
var selectedDate = users[random.Next(users.Count)].CreatedOn;
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var userRepository = new UsersRepository(context, _userValidator);
|
||||
|
||||
context.Users.AddRange(users);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var result = await userRepository.FindUsersByCreatedDateAsync(selectedDate);
|
||||
|
||||
// Assert
|
||||
var expectedUsers = users
|
||||
.Where(u => u.CreatedOn == selectedDate)
|
||||
.ToList();
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Count, Is.EqualTo(expectedUsers.Count));
|
||||
Assert.That(result.Select(u => u.Id), Is.EquivalentTo(expectedUsers.Select(u => u.Id)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidDateOnly_When_FindByCreatedDate_Should_Throw_NotFoundByKeyException()
|
||||
{
|
||||
// Arrange
|
||||
var date = _fixture.Create<DateOnly>();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var userRepository = new UsersRepository(context, _userValidator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<NotFoundByKeyException<DateOnly>>(async () =>
|
||||
await userRepository.FindUsersByCreatedDateAsync(date));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidUser_When_AddUser_Then_CreateUser()
|
||||
{
|
||||
var user = _fixture.Create<User>();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var userRepository = new UsersRepository(context, _userValidator);
|
||||
|
||||
// Act
|
||||
userRepository.AddAsync(user);
|
||||
var res = context.Users.Find(user.Id);
|
||||
|
||||
// Assert
|
||||
|
||||
Assert.That(res, Is.Not.Null);
|
||||
Assert.That(res.Username, Is.EqualTo(user.Username));
|
||||
Assert.That(res.Id, Is.EqualTo(user.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidUser_When_AddUser_Should_Throw_AdditionUserException()
|
||||
{
|
||||
var user = _fixture.Create<User>();
|
||||
user.Username = string.Empty;
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var userRepository = new UsersRepository(context, _userValidator);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<AdditionException>(async () => await userRepository.AddAsync(user));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidUser_When_ExistUser_Then_True()
|
||||
{
|
||||
// Arrange
|
||||
var user = _fixture.Create<User>();
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var userRepository = new UsersRepository(context, _userValidator);
|
||||
|
||||
context.Users.Add(user);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var res = await userRepository.ExistsAsync(user);
|
||||
|
||||
// Assert
|
||||
Assert.That(res, Is.True);
|
||||
}
|
||||
[Test]
|
||||
public async Task Given_ValidUserButNotInDB_When_ExistUser_Then_False()
|
||||
{
|
||||
// Arrange
|
||||
var user = _fixture.Create<User>();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var userRepository = new UsersRepository(context, _userValidator);
|
||||
|
||||
|
||||
// Act
|
||||
var res = await userRepository.ExistsAsync(user);
|
||||
|
||||
// Assert
|
||||
Assert.That(res, Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidUser_When_ExistUser_Should_Throw_InvalidObjectException()
|
||||
{
|
||||
// Arrange
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var userRepository = new UsersRepository(context, _userValidator);
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<InvalidObjectException<User>>(async () => await userRepository.ExistsAsync(default));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidUserId_When_ExistUserById_Then_True()
|
||||
{
|
||||
// Arrange
|
||||
var user = _fixture.Create<User>();
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var userRepository = new UsersRepository(context, _userValidator);
|
||||
|
||||
context.Users.Add(user);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var res = await userRepository.ExistsByIdAsync(user.Id);
|
||||
|
||||
// Assert
|
||||
Assert.That(res, Is.True);
|
||||
}
|
||||
[Test]
|
||||
public async Task Given_ValidUserIdButNotInDB_When_ExistUser_Then_False()
|
||||
{
|
||||
// Arrange
|
||||
var user = _fixture.Create<User>();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var userRepository = new UsersRepository(context, _userValidator);
|
||||
|
||||
|
||||
// Act
|
||||
var res = await userRepository.ExistsByIdAsync(user.Id);
|
||||
|
||||
// Assert
|
||||
Assert.That(res, Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidUsername_When_ExistUserById_Then_True()
|
||||
{
|
||||
// Arrange
|
||||
var user = _fixture.Create<User>();
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var userRepository = new UsersRepository(context, _userValidator);
|
||||
|
||||
context.Users.Add(user);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
var res = await userRepository.ExistsUsernameAsync(user.Username);
|
||||
|
||||
// Assert
|
||||
Assert.That(res, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidUsernameButNotInDB_When_ExistUser_Then_False()
|
||||
{
|
||||
// Arrange
|
||||
var user = _fixture.Create<User>();
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var userRepository = new UsersRepository(context, _userValidator);
|
||||
|
||||
|
||||
// Act
|
||||
var res = await userRepository.ExistsUsernameAsync(user.Username);
|
||||
|
||||
// Assert
|
||||
Assert.That(res, Is.False);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user