159 lines
4.8 KiB
C#
159 lines
4.8 KiB
C#
using Xunit;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Moq;
|
|
using AutoMapper;
|
|
using Microsoft.Extensions.Logging;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using System;
|
|
using IM_API.Services;
|
|
using IM_API.Models;
|
|
using IM_API.Dtos;
|
|
using IM_API.Exceptions;
|
|
using IM_API.Tools;
|
|
|
|
public class FriendServiceTests
|
|
{
|
|
private ImContext CreateDbContext()
|
|
{
|
|
var options = new DbContextOptionsBuilder<ImContext>()
|
|
.UseInMemoryDatabase(Guid.NewGuid().ToString())
|
|
.Options;
|
|
|
|
return new ImContext(options);
|
|
}
|
|
|
|
private IMapper CreateMapper()
|
|
{
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.CreateMap<Friend, FriendInfoDto>();
|
|
cfg.CreateMap<FriendRequestDto, FriendRequest>();
|
|
cfg.CreateMap<HandleFriendRequestDto, FriendRequest>();
|
|
cfg.CreateMap<FriendRequest, Friend>()
|
|
.ForMember(dest => dest.UserId, opt => opt.MapFrom(src => src.ResponseUser))
|
|
.ForMember(dest => dest.FriendId, opt => opt.MapFrom(src => src.RequestUser))
|
|
.ForMember(dest => dest.RemarkName, opt => opt.MapFrom(src => "AutoAdded"));
|
|
});
|
|
|
|
return config.CreateMapper();
|
|
}
|
|
|
|
private FriendService CreateService(ImContext context)
|
|
{
|
|
var logger = new Mock<ILogger<FriendService>>();
|
|
return new FriendService(context, logger.Object, CreateMapper());
|
|
}
|
|
|
|
// --------------------------- 测试 BlockFriendAsync ---------------------------
|
|
[Fact]
|
|
public async Task BlockFriendAsync_Should_Set_Status_To_Blocked()
|
|
{
|
|
var context = CreateDbContext();
|
|
context.Friends.Add(new Friend
|
|
{
|
|
Id = 1,
|
|
UserId = 10,
|
|
FriendId = 20,
|
|
StatusEnum = FriendStatus.Added,
|
|
RemarkName = "test remark"
|
|
});
|
|
await context.SaveChangesAsync();
|
|
|
|
var service = CreateService(context);
|
|
|
|
var result = await service.BlockeFriendAsync(1);
|
|
|
|
Assert.True(result);
|
|
Assert.Equal(FriendStatus.Blocked, context.Friends.Find(1).StatusEnum);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task BlockFriendAsync_Should_Throw_When_NotFound()
|
|
{
|
|
var service = CreateService(CreateDbContext());
|
|
|
|
await Assert.ThrowsAsync<BaseException>(() => service.BlockeFriendAsync(99));
|
|
}
|
|
|
|
// --------------------------- 删除好友关系 ---------------------------
|
|
[Fact]
|
|
public async Task DeleteFriendAsync_Should_Remove_Friend()
|
|
{
|
|
var context = CreateDbContext();
|
|
context.Friends.Add(new Friend
|
|
{
|
|
Id = 2,
|
|
UserId = 1,
|
|
FriendId = 3,
|
|
RemarkName = "remark",
|
|
StatusEnum = FriendStatus.Added
|
|
});
|
|
await context.SaveChangesAsync();
|
|
|
|
var service = CreateService(context);
|
|
|
|
var result = await service.DeleteFriendAsync(2);
|
|
|
|
Assert.True(result);
|
|
Assert.Empty(context.Friends);
|
|
}
|
|
|
|
// --------------------------- 获取好友列表 ---------------------------
|
|
[Fact]
|
|
public async Task GetFriendListAsync_Should_Return_Only_Added_Friends()
|
|
{
|
|
var context = CreateDbContext();
|
|
context.Friends.AddRange(new List<Friend>
|
|
{
|
|
new Friend{ UserId = 1, FriendId = 2, RemarkName ="a1", StatusEnum = FriendStatus.Added },
|
|
new Friend{ UserId = 1, FriendId = 3, RemarkName ="a2", StatusEnum = FriendStatus.Blocked }
|
|
});
|
|
await context.SaveChangesAsync();
|
|
|
|
var service = CreateService(context);
|
|
|
|
var result = await service.GetFriendListAsync(1, 1, 10, false);
|
|
|
|
Assert.Single(result);
|
|
}
|
|
|
|
// --------------------------- 发起好友请求 ---------------------------
|
|
[Fact]
|
|
public async Task SendFriendRequestAsync_Should_Succeed()
|
|
{
|
|
var context = CreateDbContext();
|
|
context.Users.Add(new User { Id = 10, Username = "A", Password = "123" });
|
|
context.Users.Add(new User { Id = 20, Username = "B", Password = "123" });
|
|
await context.SaveChangesAsync();
|
|
|
|
var service = CreateService(context);
|
|
|
|
var result = await service.SendFriendRequestAsync(new FriendRequestDto
|
|
{
|
|
FromUserId = 10,
|
|
ToUserId = 20
|
|
});
|
|
|
|
Assert.True(result);
|
|
Assert.Single(context.FriendRequests);
|
|
Assert.Single(context.Friends);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SendFriendRequestAsync_Should_Throw_When_User_NotFound()
|
|
{
|
|
var context = CreateDbContext();
|
|
context.Users.Add(new User { Id = 10, Username = "A", Password = "123" });
|
|
await context.SaveChangesAsync();
|
|
|
|
var service = CreateService(context);
|
|
|
|
await Assert.ThrowsAsync<BaseException>(() => service.SendFriendRequestAsync(new FriendRequestDto
|
|
{
|
|
FromUserId = 10,
|
|
ToUserId = 99
|
|
}));
|
|
}
|
|
}
|