mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
Final Friends Service Tests
This commit is contained in:
@@ -271,4 +271,88 @@ public class FriendsServiceTests
|
|||||||
Assert.That(updatedFriendship.Status, Is.EqualTo(FriendshipStatus.Accepted));
|
Assert.That(updatedFriendship.Status, Is.EqualTo(FriendshipStatus.Accepted));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetFriendsAsync
|
||||||
|
[Test]
|
||||||
|
public async Task GetFriendsAsync_ReturnsUsers_IfUserExists()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var userId = Guid.NewGuid();
|
||||||
|
var friendships = _fixture.CreateMany<Friendship>().ToList();
|
||||||
|
|
||||||
|
friendships.ForEach(f =>
|
||||||
|
{
|
||||||
|
f.RequesterId = userId;
|
||||||
|
f.Status = FriendshipStatus.Accepted;
|
||||||
|
});
|
||||||
|
|
||||||
|
_friendshipsRepositoryMock.Setup(f => f.FindByUserIdAsync(userId))
|
||||||
|
.ReturnsAsync(friendships);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = await _service.GetFriendsAsync(userId);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(result.Count, Is.EqualTo(friendships.Count));
|
||||||
|
Assert.That(result, Is.EquivalentTo(friendships.Select(f => f.Addressee)));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetFriendsAsync_Throws_InvalidOperationException_IfUserNotExists()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_friendshipsRepositoryMock.Setup(f => f.FindByUserIdAsync(It.IsAny<Guid>()))
|
||||||
|
.ThrowsAsync(new NotFoundByKeyException<Guid>(Guid.NewGuid()));
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Assert.ThrowsAsync<InvalidOperationException>(async () => await _service.GetFriendsAsync(Guid.NewGuid()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetIncomingRequestsAsync
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task GetIncomingRequestsAsync_ReturnsFriendships_IfFriendshipsExists()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var userId = Guid.NewGuid();
|
||||||
|
|
||||||
|
var friendships = _fixture.CreateMany<Friendship>().ToList();
|
||||||
|
|
||||||
|
var user = _fixture.Build<User>()
|
||||||
|
.With(u => u.Id, userId)
|
||||||
|
.With(u => u.ReceivedFriendRequests, friendships)
|
||||||
|
.Create();
|
||||||
|
|
||||||
|
friendships.ForEach(f =>
|
||||||
|
{
|
||||||
|
f.AddresseeId = userId;
|
||||||
|
f.Addressee = user;
|
||||||
|
f.Status = FriendshipStatus.Pending;
|
||||||
|
});
|
||||||
|
|
||||||
|
_usersRepositoryMock.Setup(f => f.FindByIdAsync(userId))
|
||||||
|
.ReturnsAsync(user);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = await _service.GetIncomingRequestsAsync(userId);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(result.Count, Is.EqualTo(friendships.Count));
|
||||||
|
Assert.That(result.Select(u => u.Id), Is.EquivalentTo(friendships.Select(f => f.Id)));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetIncomingRequestsAsync_ThrowsInvalidOperationException_WhenUserNotFound()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var userId = Guid.NewGuid();
|
||||||
|
|
||||||
|
_usersRepositoryMock
|
||||||
|
.Setup(r => r.FindByIdAsync(userId))
|
||||||
|
.ThrowsAsync(new NotFoundByKeyException<Guid>(userId));
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Assert.ThrowsAsync<InvalidOperationException>(async () =>
|
||||||
|
await _service.GetIncomingRequestsAsync(userId));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
using Govor.Application.Exceptions;
|
|
||||||
using Govor.Application.Exceptions.FriendsService;
|
using Govor.Application.Exceptions.FriendsService;
|
||||||
using Govor.Application.Interfaces;
|
using Govor.Application.Interfaces;
|
||||||
using Govor.Core.Models;
|
using Govor.Core.Models;
|
||||||
@@ -110,7 +109,9 @@ public class FriendsService : IFriendsService
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var user = await _usersRepository.FindByIdAsync(userId);
|
var user = await _usersRepository.FindByIdAsync(userId);
|
||||||
return user.ReceivedFriendRequests;
|
return user.ReceivedFriendRequests
|
||||||
|
.Where(f => f.Status == FriendshipStatus.Pending)
|
||||||
|
.ToList();
|
||||||
}
|
}
|
||||||
catch (NotFoundByKeyException<Guid> ex)
|
catch (NotFoundByKeyException<Guid> ex)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user