mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
I'm working on the UsersRepository
userValidotor has been added and tests are being developed
This commit is contained in:
@@ -9,8 +9,12 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoFixture" Version="5.0.0-preview0012" />
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.2" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.6" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.6" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
|
||||
<PackageReference Include="Moq" Version="4.20.72" />
|
||||
<PackageReference Include="NUnit" Version="4.2.2" />
|
||||
<PackageReference Include="NUnit.Analyzers" Version="4.3.0" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
|
||||
@@ -20,4 +24,14 @@
|
||||
<Using Include="NUnit.Framework" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="IntegrationTests\Controllers\" />
|
||||
<Folder Include="UnitTests\Infrastructure\" />
|
||||
<Folder Include="UnitTests\Services\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Govor.Data\Govor.Data.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
using AutoFixture;
|
||||
using Govor.Core.Infrastructure.Validators;
|
||||
using Govor.Core.Models;
|
||||
using Govor.Data;
|
||||
using Govor.Data.Repositories;
|
||||
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();
|
||||
|
||||
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.GetAll();
|
||||
|
||||
// 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)));
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
namespace Govor.API.Tests;
|
||||
|
||||
public class Tests
|
||||
{
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test1()
|
||||
{
|
||||
Assert.Pass();
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,6 @@
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Controllers\" />
|
||||
<Folder Include="Hubs\" />
|
||||
<Folder Include="Services\" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace Govor.API.Hubs;
|
||||
|
||||
public class ChatsHub : Hub
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace Govor.API.Hubs;
|
||||
|
||||
public class UsersHub : Hub
|
||||
{
|
||||
|
||||
}
|
||||
@@ -8,7 +8,6 @@
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="DTOs\" />
|
||||
<Folder Include="Infrastructure\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace Govor.Core;
|
||||
|
||||
/// <summary>
|
||||
/// Base exception class for Govor solutions
|
||||
/// </summary>
|
||||
public class GovorCoreException : Exception
|
||||
{
|
||||
public GovorCoreException() { }
|
||||
|
||||
public GovorCoreException(string message)
|
||||
: base(message) { }
|
||||
|
||||
public GovorCoreException(string message, Exception innerException)
|
||||
: base(message, innerException) { }
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Govor.Core.Infrastructure.Validators;
|
||||
|
||||
public interface IObjectValidator<T>
|
||||
{
|
||||
void Validate(T objectToValidate);
|
||||
bool TryValidate(T objectToValidate);
|
||||
}
|
||||
|
||||
class InvalidObjectException<T>(Exception ex) : GovorCoreException($"The object {typeof(T).FullName} is invalid.", ex);
|
||||
@@ -0,0 +1,39 @@
|
||||
using Govor.Core.Models;
|
||||
using ArgumentNullException = System.ArgumentNullException;
|
||||
|
||||
namespace Govor.Core.Infrastructure.Validators;
|
||||
|
||||
public class UserValidator : IObjectValidator<User>
|
||||
{
|
||||
public void Validate(User user)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (user is null)
|
||||
throw new ArgumentNullException(nameof(user));
|
||||
if(user.Id == Guid.Empty)
|
||||
throw new ArgumentException("User ID cannot be empty", nameof(user.Id));
|
||||
if(user.HashPassword is null || user.HashPassword == string.Empty)
|
||||
throw new ArgumentException("Password cannot be empty", nameof(user.HashPassword));
|
||||
if(user.CreatedOn == DateTime.MinValue)
|
||||
throw new ArgumentException("Time of creation account cannot be empty", nameof(user.CreatedOn));
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
throw new InvalidObjectException<User>(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryValidate(User objectToValidate)
|
||||
{
|
||||
try
|
||||
{
|
||||
Validate(objectToValidate);
|
||||
return true;
|
||||
}
|
||||
catch (InvalidObjectException<User> ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,26 @@
|
||||
using Govor.Core.Infrastructure.Validators;
|
||||
using Govor.Core.Models;
|
||||
using Govor.Core.Repositories;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Govor.Data.Repositories;
|
||||
|
||||
public class UsersRepository : IUsersRepository
|
||||
{
|
||||
public Task<IEnumerable<User>> GetAll()
|
||||
private GovorDbContext _context;
|
||||
private IObjectValidator<User> _validator;
|
||||
public UsersRepository(GovorDbContext context, IObjectValidator<User> validator)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
_context = context;
|
||||
_validator = validator;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<User>> GetAll()
|
||||
{
|
||||
return await _context.Users
|
||||
.AsNoTracking()
|
||||
.Where(x => true)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public Task<User> FindById(Guid id)
|
||||
|
||||
@@ -10,6 +10,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Govor.Data", "Govor.Data\Go
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Govor.Console", "Govor.Console\Govor.Console.csproj", "{F4535DC3-BDFB-4EB2-B259-F92B6BBB535B}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{4ED5259A-6FB4-4D89-8E6B-4778DC68F7D4}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{114F53C1-B0AB-4BA0-9E36-0E811D1B3776}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -38,5 +42,10 @@ Global
|
||||
{F4535DC3-BDFB-4EB2-B259-F92B6BBB535B}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(NestedProjects) = preSolution
|
||||
{15031CBD-F319-4755-BA91-B86F20BD8E37} = {4ED5259A-6FB4-4D89-8E6B-4778DC68F7D4}
|
||||
{EA8F272F-4276-438A-9DEA-C58860A440AE} = {114F53C1-B0AB-4BA0-9E36-0E811D1B3776}
|
||||
{F4535DC3-BDFB-4EB2-B259-F92B6BBB535B} = {114F53C1-B0AB-4BA0-9E36-0E811D1B3776}
|
||||
{F7BB1EC7-63D6-4525-ADE4-E4AC937E219D} = {114F53C1-B0AB-4BA0-9E36-0E811D1B3776}
|
||||
{E4EDB179-7EB5-468D-9C1F-0CBE2E5E459E} = {114F53C1-B0AB-4BA0-9E36-0E811D1B3776}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
||||
Reference in New Issue
Block a user