171 lines
5.0 KiB
C#
171 lines
5.0 KiB
C#
namespace WebApi.Test.Application;
|
|
|
|
/// <summary>
|
|
/// ImportApplication 单元测试
|
|
/// </summary>
|
|
public class ImportApplicationTest : BaseApplicationTest
|
|
{
|
|
private readonly IImportService _importService;
|
|
private readonly ILogger<ImportApplication> _logger;
|
|
private readonly ImportApplication _application;
|
|
|
|
public ImportApplicationTest()
|
|
{
|
|
_importService = Substitute.For<IImportService>();
|
|
_logger = CreateMockLogger<ImportApplication>();
|
|
_application = new ImportApplication(_importService, _logger);
|
|
}
|
|
|
|
#region ImportAlipayAsync Tests
|
|
|
|
[Fact]
|
|
public async Task ImportAlipayAsync_有效文件_应返回成功消息()
|
|
{
|
|
// Arrange
|
|
var stream = new MemoryStream([1, 2, 3]);
|
|
var request = new ImportRequest
|
|
{
|
|
FileStream = stream,
|
|
FileExtension = ".csv",
|
|
FileName = "test.csv",
|
|
FileSize = 100
|
|
};
|
|
_importService.ImportAlipayAsync(Arg.Any<MemoryStream>(), ".csv")
|
|
.Returns((true, "成功导入1条记录"));
|
|
|
|
// Act
|
|
var response = await _application.ImportAlipayAsync(request);
|
|
|
|
// Assert
|
|
response.Should().NotBeNull();
|
|
response.Message.Should().Contain("成功");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ImportAlipayAsync_空文件_应抛出ValidationException()
|
|
{
|
|
// Arrange
|
|
var stream = new MemoryStream();
|
|
var request = new ImportRequest
|
|
{
|
|
FileStream = stream,
|
|
FileExtension = ".csv",
|
|
FileName = "test.csv",
|
|
FileSize = 0
|
|
};
|
|
|
|
// Act & Assert
|
|
await Assert.ThrowsAsync<ValidationException>(() =>
|
|
_application.ImportAlipayAsync(request));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ImportAlipayAsync_不支持的文件格式_应抛出ValidationException()
|
|
{
|
|
// Arrange
|
|
var stream = new MemoryStream([1, 2, 3]);
|
|
var request = new ImportRequest
|
|
{
|
|
FileStream = stream,
|
|
FileExtension = ".txt",
|
|
FileName = "test.txt",
|
|
FileSize = 100
|
|
};
|
|
|
|
// Act & Assert
|
|
var exception = await Assert.ThrowsAsync<ValidationException>(() =>
|
|
_application.ImportAlipayAsync(request));
|
|
exception.Message.Should().Contain("CSV 或 Excel");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ImportAlipayAsync_文件过大_应抛出ValidationException()
|
|
{
|
|
// Arrange
|
|
var stream = new MemoryStream([1, 2, 3]);
|
|
var request = new ImportRequest
|
|
{
|
|
FileStream = stream,
|
|
FileExtension = ".csv",
|
|
FileName = "test.csv",
|
|
FileSize = 11 * 1024 * 1024 // 11MB
|
|
};
|
|
|
|
// Act & Assert
|
|
var exception = await Assert.ThrowsAsync<ValidationException>(() =>
|
|
_application.ImportAlipayAsync(request));
|
|
exception.Message.Should().Contain("10MB");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ImportAlipayAsync_导入失败_应抛出BusinessException()
|
|
{
|
|
// Arrange
|
|
var stream = new MemoryStream([1, 2, 3]);
|
|
var request = new ImportRequest
|
|
{
|
|
FileStream = stream,
|
|
FileExtension = ".csv",
|
|
FileName = "test.csv",
|
|
FileSize = 100
|
|
};
|
|
_importService.ImportAlipayAsync(Arg.Any<MemoryStream>(), ".csv")
|
|
.Returns((false, "解析失败"));
|
|
|
|
// Act & Assert
|
|
var exception = await Assert.ThrowsAsync<BusinessException>(() =>
|
|
_application.ImportAlipayAsync(request));
|
|
exception.Message.Should().Contain("解析失败");
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region ImportWeChatAsync Tests
|
|
|
|
[Fact]
|
|
public async Task ImportWeChatAsync_有效文件_应返回成功消息()
|
|
{
|
|
// Arrange
|
|
var stream = new MemoryStream([1, 2, 3]);
|
|
var request = new ImportRequest
|
|
{
|
|
FileStream = stream,
|
|
FileExtension = ".xlsx",
|
|
FileName = "test.xlsx",
|
|
FileSize = 100
|
|
};
|
|
_importService.ImportWeChatAsync(Arg.Any<MemoryStream>(), ".xlsx")
|
|
.Returns((true, "成功导入2条记录"));
|
|
|
|
// Act
|
|
var response = await _application.ImportWeChatAsync(request);
|
|
|
|
// Assert
|
|
response.Should().NotBeNull();
|
|
response.Message.Should().Contain("成功");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ImportWeChatAsync_导入失败_应抛出BusinessException()
|
|
{
|
|
// Arrange
|
|
var stream = new MemoryStream([1, 2, 3]);
|
|
var request = new ImportRequest
|
|
{
|
|
FileStream = stream,
|
|
FileExtension = ".xlsx",
|
|
FileName = "test.xlsx",
|
|
FileSize = 100
|
|
};
|
|
_importService.ImportWeChatAsync(Arg.Any<MemoryStream>(), ".xlsx")
|
|
.Returns((false, "数据格式错误"));
|
|
|
|
// Act & Assert
|
|
var exception = await Assert.ThrowsAsync<BusinessException>(() =>
|
|
_application.ImportWeChatAsync(request));
|
|
exception.Message.Should().Contain("数据格式错误");
|
|
}
|
|
|
|
#endregion
|
|
}
|