This commit is contained in:
Artemy
2025-06-17 18:42:31 +07:00
parent 7810cafb4e
commit e860abce40
2 changed files with 114 additions and 61 deletions
@@ -123,7 +123,8 @@ public class UsersRepositoryTests
var userRepository = new UsersRepository(context, _userValidator); var userRepository = new UsersRepository(context, _userValidator);
// Act & Assert // Act & Assert
Assert.ThrowsAsync<NotFoundByKeyException<IEnumerable<Guid>>>(async () => await userRepository.FindByRangeId(ids)); Assert.ThrowsAsync<NotFoundByKeyException<IEnumerable<Guid>>>(async () =>
await userRepository.FindByRangeId(ids));
} }
[Test] [Test]
@@ -192,7 +193,8 @@ public class UsersRepositoryTests
var userRepository = new UsersRepository(context, _userValidator); var userRepository = new UsersRepository(context, _userValidator);
// Act & Assert // Act & Assert
Assert.ThrowsAsync<NotFoundByKeyException<IEnumerable<string>>>(async () => await userRepository.FindByRangeUsernames(usernames)); Assert.ThrowsAsync<NotFoundByKeyException<IEnumerable<string>>>(async () =>
await userRepository.FindByRangeUsernames(usernames));
} }
[Test] [Test]
@@ -233,7 +235,8 @@ public class UsersRepositoryTests
var userRepository = new UsersRepository(context, _userValidator); var userRepository = new UsersRepository(context, _userValidator);
// Act & Assert // Act & Assert
Assert.ThrowsAsync<NotFoundByKeyException<DateOnly>>(async () => await userRepository.FindUsersByCreatedDate(date)); Assert.ThrowsAsync<NotFoundByKeyException<DateOnly>>(async () =>
await userRepository.FindUsersByCreatedDate(date));
} }
[Test] [Test]
@@ -268,4 +271,24 @@ public class UsersRepositoryTests
Assert.ThrowsAsync<AdditionUserException>(async () => await userRepository.Add(user)); Assert.ThrowsAsync<AdditionUserException>(async () => await userRepository.Add(user));
} }
[Test]
public async Task Given_ValidUser_When_RemoveUser_Then_RemoveUser()
{
// 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
await userRepository.Remove(user);
var res = context.Users.AsNoTracking().FirstOrDefault(u => u.Id == user.Id);
// Assert
Assert.That(res, Is.Null);
}
} }
+38 -8
View File
@@ -87,7 +87,6 @@ public class UsersRepository : IUsersRepository
_validator.Validate(user); _validator.Validate(user);
_context.Users.Add(user); _context.Users.Add(user);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
} }
catch (InvalidObjectException<User> ex) catch (InvalidObjectException<User> ex)
@@ -100,6 +99,7 @@ public class UsersRepository : IUsersRepository
} }
} }
// TODO: Test this block
public async Task Update(User user) public async Task Update(User user)
{ {
try try
@@ -130,34 +130,64 @@ public class UsersRepository : IUsersRepository
} }
} }
public Task Remove(User user) public async Task Remove(User user)
{
try
{ {
_validator.Validate(user); _validator.Validate(user);
throw new NotImplementedException(); await Remove(user.Id);
}
catch (InvalidObjectException<User> ex)
{
throw new UserRemoveException("User with given data invalid", ex);
}
} }
public Task Remove(Guid userId) public async Task Remove(Guid userId)
{ {
throw new NotImplementedException(); try
{
var result = await FindById(userId);
_context.Users.Remove(result);
await _context.SaveChangesAsync();
}
catch (NotFoundByKeyException<Guid> ex)
{
throw new UserRemoveException($"Not found user by given id {userId}", ex);
}
catch (Exception ex)
{
throw new UserRemoveException("Error when removing the user", ex);
}
} }
public Task<bool> Exists(User user) public Task<bool> Exists(User user)
{ {
_validator.Validate(user); _validator.Validate(user);
throw new NotImplementedException();
return _context.Users.AnyAsync(u =>
u.Id == user.Id &&
u.Username == user.Username &&
u.HashPassword == user.HashPassword
);
} }
public Task<bool> ExistsById(Guid id) public Task<bool> ExistsById(Guid id)
{ {
throw new NotImplementedException(); return _context.Users.AnyAsync(u => u.Id == id);
} }
public Task<bool> ExistsUsername(string username) public Task<bool> ExistsUsername(string username)
{ {
throw new NotImplementedException(); return _context.Users.AnyAsync(u => u.Username == username);
} }
} }
public class UserRemoveException(string s, Exception exception)
: Exception(s, exception);
public class AdditionUserException(string s, Exception ex) public class AdditionUserException(string s, Exception ex)
: Exception(s, ex); : Exception(s, ex);