From 568b09ba818bcb84566e64781e0ec2b0bfd1aaa5 Mon Sep 17 00:00:00 2001
From: Artemy <109195690+stalcker2288969@users.noreply.github.com>
Date: Tue, 17 Jun 2025 11:57:57 +0700
Subject: [PATCH] I'm working on the UsersRepository
userValidotor has been added and tests are being developed
---
Govor.API.Tests/Govor.API.Tests.csproj | 26 +++++++--
.../EF/Repositories/UsersRepositoryTests.cs | 57 +++++++++++++++++++
Govor.API.Tests/UnitTest1.cs | 15 -----
Govor.API/Govor.API.csproj | 1 -
Govor.API/Hubs/ChatsHub.cs | 8 +++
Govor.API/Hubs/UsersHub.cs | 8 +++
Govor.Core/Govor.Core.csproj | 1 -
Govor.Core/GovorCoreException.cs | 15 +++++
.../Validators/IObjectValidator.cs | 9 +++
.../Validators/UserValidator.cs | 39 +++++++++++++
Govor.Data/Repositories/UsersRepository.cs | 17 +++++-
Govor.sln | 9 +++
12 files changed, 180 insertions(+), 25 deletions(-)
create mode 100644 Govor.API.Tests/IntegrationTests/EF/Repositories/UsersRepositoryTests.cs
delete mode 100644 Govor.API.Tests/UnitTest1.cs
create mode 100644 Govor.API/Hubs/ChatsHub.cs
create mode 100644 Govor.API/Hubs/UsersHub.cs
create mode 100644 Govor.Core/GovorCoreException.cs
create mode 100644 Govor.Core/Infrastructure/Validators/IObjectValidator.cs
create mode 100644 Govor.Core/Infrastructure/Validators/UserValidator.cs
diff --git a/Govor.API.Tests/Govor.API.Tests.csproj b/Govor.API.Tests/Govor.API.Tests.csproj
index d7f90e2..1f1527b 100644
--- a/Govor.API.Tests/Govor.API.Tests.csproj
+++ b/Govor.API.Tests/Govor.API.Tests.csproj
@@ -9,15 +9,29 @@
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
diff --git a/Govor.API.Tests/IntegrationTests/EF/Repositories/UsersRepositoryTests.cs b/Govor.API.Tests/IntegrationTests/EF/Repositories/UsersRepositoryTests.cs
new file mode 100644
index 0000000..be69968
--- /dev/null
+++ b/Govor.API.Tests/IntegrationTests/EF/Repositories/UsersRepositoryTests.cs
@@ -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 _options;
+ private readonly IObjectValidator _userValidator = new UserValidator();
+
+ [SetUp]
+ public void SetUp()
+ {
+ _fixture = new Fixture();
+
+ _fixture.Behaviors
+ .OfType()
+ .ToList()
+ .ForEach(b => _fixture.Behaviors.Remove(b));
+
+ _fixture.Behaviors.Add(new OmitOnRecursionBehavior());
+
+ _options = new DbContextOptionsBuilder()
+ .UseInMemoryDatabase(databaseName: "DbGovor")
+ .Options;
+ }
+
+ [Test]
+ public async Task GetAll_Then_Returns_All_Users()
+ {
+ // Arrange
+ var random = new Random();
+ var users = _fixture.CreateMany(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)));
+ }
+}
\ No newline at end of file
diff --git a/Govor.API.Tests/UnitTest1.cs b/Govor.API.Tests/UnitTest1.cs
deleted file mode 100644
index 7b376d8..0000000
--- a/Govor.API.Tests/UnitTest1.cs
+++ /dev/null
@@ -1,15 +0,0 @@
-namespace Govor.API.Tests;
-
-public class Tests
-{
- [SetUp]
- public void Setup()
- {
- }
-
- [Test]
- public void Test1()
- {
- Assert.Pass();
- }
-}
\ No newline at end of file
diff --git a/Govor.API/Govor.API.csproj b/Govor.API/Govor.API.csproj
index 5003fdd..9c0771d 100644
--- a/Govor.API/Govor.API.csproj
+++ b/Govor.API/Govor.API.csproj
@@ -23,7 +23,6 @@
-
diff --git a/Govor.API/Hubs/ChatsHub.cs b/Govor.API/Hubs/ChatsHub.cs
new file mode 100644
index 0000000..40cc036
--- /dev/null
+++ b/Govor.API/Hubs/ChatsHub.cs
@@ -0,0 +1,8 @@
+using Microsoft.AspNetCore.SignalR;
+
+namespace Govor.API.Hubs;
+
+public class ChatsHub : Hub
+{
+
+}
\ No newline at end of file
diff --git a/Govor.API/Hubs/UsersHub.cs b/Govor.API/Hubs/UsersHub.cs
new file mode 100644
index 0000000..880aba6
--- /dev/null
+++ b/Govor.API/Hubs/UsersHub.cs
@@ -0,0 +1,8 @@
+using Microsoft.AspNetCore.SignalR;
+
+namespace Govor.API.Hubs;
+
+public class UsersHub : Hub
+{
+
+}
\ No newline at end of file
diff --git a/Govor.Core/Govor.Core.csproj b/Govor.Core/Govor.Core.csproj
index f7b0c18..b9fad79 100644
--- a/Govor.Core/Govor.Core.csproj
+++ b/Govor.Core/Govor.Core.csproj
@@ -8,7 +8,6 @@
-
diff --git a/Govor.Core/GovorCoreException.cs b/Govor.Core/GovorCoreException.cs
new file mode 100644
index 0000000..e109d3d
--- /dev/null
+++ b/Govor.Core/GovorCoreException.cs
@@ -0,0 +1,15 @@
+namespace Govor.Core;
+
+///
+/// Base exception class for Govor solutions
+///
+public class GovorCoreException : Exception
+{
+ public GovorCoreException() { }
+
+ public GovorCoreException(string message)
+ : base(message) { }
+
+ public GovorCoreException(string message, Exception innerException)
+ : base(message, innerException) { }
+}
\ No newline at end of file
diff --git a/Govor.Core/Infrastructure/Validators/IObjectValidator.cs b/Govor.Core/Infrastructure/Validators/IObjectValidator.cs
new file mode 100644
index 0000000..ca05fef
--- /dev/null
+++ b/Govor.Core/Infrastructure/Validators/IObjectValidator.cs
@@ -0,0 +1,9 @@
+namespace Govor.Core.Infrastructure.Validators;
+
+public interface IObjectValidator
+{
+ void Validate(T objectToValidate);
+ bool TryValidate(T objectToValidate);
+}
+
+class InvalidObjectException(Exception ex) : GovorCoreException($"The object {typeof(T).FullName} is invalid.", ex);
\ No newline at end of file
diff --git a/Govor.Core/Infrastructure/Validators/UserValidator.cs b/Govor.Core/Infrastructure/Validators/UserValidator.cs
new file mode 100644
index 0000000..3b0b9b9
--- /dev/null
+++ b/Govor.Core/Infrastructure/Validators/UserValidator.cs
@@ -0,0 +1,39 @@
+using Govor.Core.Models;
+using ArgumentNullException = System.ArgumentNullException;
+
+namespace Govor.Core.Infrastructure.Validators;
+
+public class UserValidator : IObjectValidator
+{
+ 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(ex);
+ }
+ }
+
+ public bool TryValidate(User objectToValidate)
+ {
+ try
+ {
+ Validate(objectToValidate);
+ return true;
+ }
+ catch (InvalidObjectException ex)
+ {
+ return false;
+ }
+ }
+}
\ No newline at end of file
diff --git a/Govor.Data/Repositories/UsersRepository.cs b/Govor.Data/Repositories/UsersRepository.cs
index 3a43c4f..303c368 100644
--- a/Govor.Data/Repositories/UsersRepository.cs
+++ b/Govor.Data/Repositories/UsersRepository.cs
@@ -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> GetAll()
+ private GovorDbContext _context;
+ private IObjectValidator _validator;
+ public UsersRepository(GovorDbContext context, IObjectValidator validator)
{
- throw new NotImplementedException();
+ _context = context;
+ _validator = validator;
+ }
+
+ public async Task> GetAll()
+ {
+ return await _context.Users
+ .AsNoTracking()
+ .Where(x => true)
+ .ToListAsync();
}
public Task FindById(Guid id)
diff --git a/Govor.sln b/Govor.sln
index 5d24e59..20e5835 100644
--- a/Govor.sln
+++ b/Govor.sln
@@ -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