diff --git a/SchengenVisaApi/VisaApiTests/Database/InMemoryContextProvider.cs b/SchengenVisaApi/VisaApiTests/Database/InMemoryContextProvider.cs new file mode 100644 index 0000000..4d33789 --- /dev/null +++ b/SchengenVisaApi/VisaApiTests/Database/InMemoryContextProvider.cs @@ -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 opts = new DbContextOptionsBuilder() + .UseInMemoryDatabase("VisaApiDB") + .ConfigureWarnings(b => b.Ignore(InMemoryEventId.TransactionIgnoredWarning)) + .Options; + + public static DbContext GetDbContext() => new(opts); + } +} diff --git a/SchengenVisaApi/VisaApiTests/Database/Repositories/Generic/GenericRepositoryTests.cs b/SchengenVisaApi/VisaApiTests/Database/Repositories/Generic/GenericRepositoryTests.cs new file mode 100644 index 0000000..7b4690e --- /dev/null +++ b/SchengenVisaApi/VisaApiTests/Database/Repositories/Generic/GenericRepositoryTests.cs @@ -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 GetRepository(DbContext context) => new TestGenericRepository(context, context); + + /// Test for method that should return empty collection if nothing added + [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(); + } + + /// Test for method that should return collection with added entities + [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)); + } + + /// Test for method that should return existing entity + [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); + } + + /// Test for method that should throw exception for not found entity + [Fact] + public async Task GetByIdForNotExistingShouldThrow() + { + await using var context = InMemoryContextProvider.GetDbContext(); + var repository = GetRepository(context); + + await context.SaveChangesAsync(); + + EntityNotFoundByIdException? result = null; + try + { + await repository.GetByIdAsync(Guid.NewGuid(), CancellationToken.None); + } + catch (Exception e) + { + result = e as EntityNotFoundByIdException; + } + + result.Should().NotBeNull(); + } +} diff --git a/SchengenVisaApi/VisaApiTests/Database/Repositories/Generic/TestGenericRepository.cs b/SchengenVisaApi/VisaApiTests/Database/Repositories/Generic/TestGenericRepository.cs new file mode 100644 index 0000000..c89a2e0 --- /dev/null +++ b/SchengenVisaApi/VisaApiTests/Database/Repositories/Generic/TestGenericRepository.cs @@ -0,0 +1,7 @@ +using Domains.Users; +using Infrastructure.Database.Generic; + +namespace VisaApi.Database.Repositories.Generic +{ + public class TestGenericRepository(IGenericReader reader, IGenericWriter writer) : GenericRepository(reader, writer); +} diff --git a/SchengenVisaApi/VisaApiTests/Database/Repositories/GenericRepositoryTests.cs b/SchengenVisaApi/VisaApiTests/Database/Repositories/GenericRepositoryTests.cs deleted file mode 100644 index 28e7ccf..0000000 --- a/SchengenVisaApi/VisaApiTests/Database/Repositories/GenericRepositoryTests.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace VisaApi.Database.Repositories; - -public class GenericRepositoryTests -{ - [Fact] - public void Test1() - { - } -} diff --git a/SchengenVisaApi/VisaApiTests/Services/TestDateTimeProvider.cs b/SchengenVisaApi/VisaApiTests/Services/TestDateTimeProvider.cs new file mode 100644 index 0000000..8241e0b --- /dev/null +++ b/SchengenVisaApi/VisaApiTests/Services/TestDateTimeProvider.cs @@ -0,0 +1,9 @@ +using ApplicationLayer.InfrastructureServicesInterfaces; + +namespace VisaApi.Services +{ + public class TestDateTimeProvider : IDateTimeProvider + { + public DateTime Now() => DateTime.Now; + } +} diff --git a/SchengenVisaApi/VisaApiTests/VisaApiTests.csproj b/SchengenVisaApi/VisaApiTests/VisaApiTests.csproj index 91e24fa..0be6f2d 100644 --- a/SchengenVisaApi/VisaApiTests/VisaApiTests.csproj +++ b/SchengenVisaApi/VisaApiTests/VisaApiTests.csproj @@ -11,6 +11,8 @@ + +