tests for generic repository
This commit is contained in:
@@ -0,0 +1,16 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||||
|
using DbContext = Infrastructure.Database.DbContext;
|
||||||
|
|
||||||
|
namespace VisaApi.Database
|
||||||
|
{
|
||||||
|
public static class InMemoryContextProvider
|
||||||
|
{
|
||||||
|
private static DbContextOptions<DbContext> opts = new DbContextOptionsBuilder<DbContext>()
|
||||||
|
.UseInMemoryDatabase("VisaApiDB")
|
||||||
|
.ConfigureWarnings(b => b.Ignore(InMemoryEventId.TransactionIgnoredWarning))
|
||||||
|
.Options;
|
||||||
|
|
||||||
|
public static DbContext GetDbContext() => new(opts);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
using ApplicationLayer.GeneralExceptions;
|
||||||
|
using Domains.Users;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Infrastructure.Database;
|
||||||
|
using Infrastructure.Database.Generic;
|
||||||
|
|
||||||
|
namespace VisaApi.Database.Repositories.Generic;
|
||||||
|
|
||||||
|
public class GenericRepositoryTests
|
||||||
|
{
|
||||||
|
private static GenericRepository<User> GetRepository(DbContext context) => new TestGenericRepository(context, context);
|
||||||
|
|
||||||
|
/// <summary> Test for <see cref="GenericRepository{T}.GetAllAsync"/> method that should return empty collection if nothing added </summary>
|
||||||
|
[Fact]
|
||||||
|
public async Task GetAllForEmptyShouldReturnEmpty()
|
||||||
|
{
|
||||||
|
await using var context = InMemoryContextProvider.GetDbContext();
|
||||||
|
var repository = GetRepository(context);
|
||||||
|
|
||||||
|
var result = await repository.GetAllAsync(CancellationToken.None);
|
||||||
|
|
||||||
|
result.Should().BeEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary> Test for <see cref="GenericRepository{T}.GetAllAsync"/> method that should return collection with added entities </summary>
|
||||||
|
[Fact]
|
||||||
|
public async Task GetAllForNotEmptyShouldReturnEntities()
|
||||||
|
{
|
||||||
|
await using var context = InMemoryContextProvider.GetDbContext();
|
||||||
|
var repository = GetRepository(context);
|
||||||
|
User[] users =
|
||||||
|
[
|
||||||
|
new() { Email = "nasrudin@mail.ru", Password = "12345", Role = Role.Admin },
|
||||||
|
new() { Email = "bruh@mail.ru", Password = "123", Role = Role.Applicant }
|
||||||
|
];
|
||||||
|
foreach (var user in users)
|
||||||
|
{
|
||||||
|
await repository.AddAsync(user, CancellationToken.None);
|
||||||
|
}
|
||||||
|
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
|
||||||
|
var result = await repository.GetAllAsync(CancellationToken.None);
|
||||||
|
|
||||||
|
result.Should().OnlyContain(user => users.Contains(user));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary> Test for <see cref="GenericRepository{T}.GetByIdAsync"/> method that should return existing entity </summary>
|
||||||
|
[Fact]
|
||||||
|
public async Task GetByIdForExistingShouldReturnEntity()
|
||||||
|
{
|
||||||
|
await using var context = InMemoryContextProvider.GetDbContext();
|
||||||
|
var repository = GetRepository(context);
|
||||||
|
var user = new User { Email = "nasrudin@mail.ru", Password = "12345", Role = Role.Admin };
|
||||||
|
await repository.AddAsync(user, CancellationToken.None);
|
||||||
|
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
|
||||||
|
var result = await repository.GetByIdAsync(user.Id, CancellationToken.None);
|
||||||
|
|
||||||
|
result.Should().Be(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary> Test for <see cref="GenericRepository{T}.GetByIdAsync"/> method that should throw exception for not found entity </summary>
|
||||||
|
[Fact]
|
||||||
|
public async Task GetByIdForNotExistingShouldThrow()
|
||||||
|
{
|
||||||
|
await using var context = InMemoryContextProvider.GetDbContext();
|
||||||
|
var repository = GetRepository(context);
|
||||||
|
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
|
||||||
|
EntityNotFoundByIdException<User>? result = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await repository.GetByIdAsync(Guid.NewGuid(), CancellationToken.None);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
result = e as EntityNotFoundByIdException<User>;
|
||||||
|
}
|
||||||
|
|
||||||
|
result.Should().NotBeNull();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
using Domains.Users;
|
||||||
|
using Infrastructure.Database.Generic;
|
||||||
|
|
||||||
|
namespace VisaApi.Database.Repositories.Generic
|
||||||
|
{
|
||||||
|
public class TestGenericRepository(IGenericReader reader, IGenericWriter writer) : GenericRepository<User>(reader, writer);
|
||||||
|
}
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
namespace VisaApi.Database.Repositories;
|
|
||||||
|
|
||||||
public class GenericRepositoryTests
|
|
||||||
{
|
|
||||||
[Fact]
|
|
||||||
public void Test1()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
using ApplicationLayer.InfrastructureServicesInterfaces;
|
||||||
|
|
||||||
|
namespace VisaApi.Services
|
||||||
|
{
|
||||||
|
public class TestDateTimeProvider : IDateTimeProvider
|
||||||
|
{
|
||||||
|
public DateTime Now() => DateTime.Now;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,6 +11,8 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="FluentAssertions" Version="6.12.1" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.8" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/>
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/>
|
||||||
<PackageReference Include="xunit" Version="2.4.2"/>
|
<PackageReference Include="xunit" Version="2.4.2"/>
|
||||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
|
||||||
|
|||||||
Reference in New Issue
Block a user