forked from ddrilling/AsbCloudServer
223 lines
8.0 KiB
C#
223 lines
8.0 KiB
C#
|
using AsbCloudApp.Data;
|
|||
|
using AsbCloudApp.Services;
|
|||
|
using AsbCloudDb.Model;
|
|||
|
using AsbCloudInfrastructure.Services;
|
|||
|
using DocumentFormat.OpenXml.Spreadsheet;
|
|||
|
using DocumentFormat.OpenXml.Wordprocessing;
|
|||
|
using Moq;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.IO;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using Xunit;
|
|||
|
using static AsbCloudWebApi.Tests.TestHelpter;
|
|||
|
|
|||
|
namespace AsbCloudWebApi.Tests.ServicesTests
|
|||
|
{
|
|||
|
public class FileServiceTest
|
|||
|
{
|
|||
|
private IFileService fileService;
|
|||
|
|
|||
|
private static UserDto Author = new UserDto {
|
|||
|
Id = 1,
|
|||
|
IdCompany = 1
|
|||
|
};
|
|||
|
|
|||
|
private static List<FileMarkDto> FileMarks = new List<FileMarkDto> {
|
|||
|
new FileMarkDto
|
|||
|
{
|
|||
|
Id = 132,
|
|||
|
IdFile = 1742,
|
|||
|
User = Author,
|
|||
|
Comment = "qqq",
|
|||
|
IdMarkType = 1,
|
|||
|
DateCreated = DateTime.Now,
|
|||
|
IsDeleted = false
|
|||
|
},
|
|||
|
new FileMarkDto
|
|||
|
{
|
|||
|
Id = 133,
|
|||
|
IdFile = 1742,
|
|||
|
User = Author,
|
|||
|
Comment = "qqq3",
|
|||
|
IdMarkType = 1,
|
|||
|
DateCreated = DateTime.Now,
|
|||
|
IsDeleted = false
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
private static List<FileInfoDto> Files = new List<FileInfoDto> {
|
|||
|
new FileInfoDto {
|
|||
|
Id = 1742,
|
|||
|
IdAuthor = 1,
|
|||
|
Author = Author,
|
|||
|
IdWell = 90,
|
|||
|
IdCategory = 10040,
|
|||
|
Name = "test.txt",
|
|||
|
Size = 0,
|
|||
|
UploadDate = DateTime.Now,
|
|||
|
FileMarks = FileMarks
|
|||
|
},
|
|||
|
new FileInfoDto
|
|||
|
{
|
|||
|
Id = 1743,
|
|||
|
IdAuthor = 1,
|
|||
|
Author = Author,
|
|||
|
IdWell = 90,
|
|||
|
IdCategory = 10021,
|
|||
|
Name = "test1.txt",
|
|||
|
Size = 0,
|
|||
|
UploadDate = DateTime.Now
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
public FileServiceTest()
|
|||
|
{
|
|||
|
var repositoryMock = RepositoryFactory.Make<IFileRepository, FileInfoDto>(Files);
|
|||
|
|
|||
|
repositoryMock.Setup(x => x.GetByMarkId(It.IsAny<int>(), It.IsAny<CancellationToken>()))
|
|||
|
.Returns((int idMark, CancellationToken token) => {
|
|||
|
var data = Files.FirstOrDefault(x => x.FileMarks.Any(m => m.Id == idMark));
|
|||
|
return Task.FromResult(data);
|
|||
|
});
|
|||
|
repositoryMock.Setup(x => x.GetInfoByIdsAsync(It.IsAny<IEnumerable<int>>(), It.IsAny<CancellationToken>()))
|
|||
|
.Returns((int[] idsFile, CancellationToken token) => {
|
|||
|
var data = Files.Where(x => idsFile.Contains(x.Id));
|
|||
|
return Task.FromResult(data);
|
|||
|
});
|
|||
|
|
|||
|
repositoryMock.Setup(x => x.DeleteAsync(It.IsAny<IEnumerable<int>>(), It.IsAny<CancellationToken>()))
|
|||
|
.Returns((int[] idsFile, CancellationToken token) => {
|
|||
|
var dtos = Files.Where(x => idsFile.Contains(x.Id)).ToArray();
|
|||
|
Files.RemoveAll(x => dtos.Select(d => d.Id).Contains(x.Id));
|
|||
|
return Task.FromResult(dtos.AsEnumerable());
|
|||
|
});
|
|||
|
|
|||
|
repositoryMock.Setup(x => x.MarkFileMarkAsDeletedAsync(It.IsAny<IEnumerable<int>>(), It.IsAny<CancellationToken>()))
|
|||
|
.Returns((int[] idsMarks, CancellationToken token) => {
|
|||
|
var data = FileMarks.Where(m => idsMarks.Contains(m.Id));
|
|||
|
|
|||
|
foreach (var fileMark in data)
|
|||
|
fileMark.IsDeleted = true;
|
|||
|
|
|||
|
var result = data.All(x => x.IsDeleted) ? 1 : 0;
|
|||
|
return Task.FromResult(result);
|
|||
|
});
|
|||
|
|
|||
|
repositoryMock.Setup(x => x.MarkAsDeletedAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
|
|||
|
.Returns((int idFile, CancellationToken token) => {
|
|||
|
var result = Files.Where(x => x.Id == idFile).Any() ? 1 : 0;
|
|||
|
return Task.FromResult(result);
|
|||
|
});
|
|||
|
|
|||
|
repositoryMock.Setup(x => x.GetInfosByWellIdAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
|
|||
|
.Returns((int idWell, CancellationToken token) => {
|
|||
|
var data = Files.Where(x => x.IdWell == idWell);
|
|||
|
return Task.FromResult(data);
|
|||
|
});
|
|||
|
|
|||
|
repositoryMock.Setup(x => x.GetInfosByCategoryAsync(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()))
|
|||
|
.Returns((int idWell, int idCategory, CancellationToken token) => {
|
|||
|
var data = Files.Where(x => x.IdWell == idWell && x.IdCategory == idCategory);
|
|||
|
return Task.FromResult(data);
|
|||
|
});
|
|||
|
|
|||
|
repositoryMock.Setup(x => x.CreateFileMarkAsync(It.IsAny<FileMarkDto>(), It.IsAny<int>(), It.IsAny<CancellationToken>()))
|
|||
|
.Returns((FileMarkDto dto, int idUser, CancellationToken token) => {
|
|||
|
dto.Id = FileMarks.Max(x => x.Id) + 1;
|
|||
|
dto.DateCreated = DateTime.UtcNow;
|
|||
|
dto.User = null;
|
|||
|
FileMarks.Add(dto);
|
|||
|
|
|||
|
var result = FileMarks.Any(x => x.Id == dto.Id) ? 1 : 0;
|
|||
|
return Task.FromResult(result);
|
|||
|
});
|
|||
|
|
|||
|
fileService = new FileService(repositoryMock.Object);
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public async Task GetByMarkId_returns_FileInfo_by_idMark()
|
|||
|
{
|
|||
|
var data = await fileService.GetByMarkId(133, CancellationToken.None);
|
|||
|
Assert.NotNull(data);
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public async Task GetInfoAsync_throws_FileNotFoundException()
|
|||
|
{
|
|||
|
await Assert.ThrowsAsync<FileNotFoundException>(async () =>
|
|||
|
{
|
|||
|
await fileService.GetInfoAsync(1743, CancellationToken.None);
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public async Task GetInfoByIdsAsync_throws_FileNotFoundException()
|
|||
|
{
|
|||
|
await Assert.ThrowsAsync<FileNotFoundException>(async () =>
|
|||
|
{
|
|||
|
await fileService.GetInfoByIdsAsync(new int[] { 1742, 1743 }, CancellationToken.None);
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public async Task SaveAsync_returns_FileInfoa()
|
|||
|
{
|
|||
|
var stream = new FileStream("D:\\test\\test.txt", FileMode.Open);
|
|||
|
var data = await fileService.SaveAsync(90, 1, 10040, "test.txt", stream, CancellationToken.None);
|
|||
|
Assert.NotNull(data);
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public async Task DeleteAsync()
|
|||
|
{
|
|||
|
var result = await fileService.DeleteAsync(new int[] { 1743 }, CancellationToken.None);
|
|||
|
Assert.True(result > 0);
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public async Task MarkFileMarkAsDeletedAsync()
|
|||
|
{
|
|||
|
var result = await fileService.MarkFileMarkAsDeletedAsync(new int[] { 132, 133 }, CancellationToken.None);
|
|||
|
Assert.True(result > 0);
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public async Task MarkAsDeletedAsync()
|
|||
|
{
|
|||
|
var result = await fileService.MarkAsDeletedAsync(1742, CancellationToken.None);
|
|||
|
Assert.True(result > 0);
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public async Task GetInfosByWellIdAsync_returns_FileInfo()
|
|||
|
{
|
|||
|
var data = await fileService.GetInfosByWellIdAsync(90, CancellationToken.None);
|
|||
|
Assert.NotNull(data);
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public async Task GetInfosByCategoryAsync_returns_FileInfo()
|
|||
|
{
|
|||
|
var data = await fileService.GetInfosByCategoryAsync(90, 10040, CancellationToken.None);
|
|||
|
Assert.NotNull(data);
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public async Task CreateFileMarkAsync()
|
|||
|
{
|
|||
|
var dto = new FileMarkDto {
|
|||
|
Comment = "test",
|
|||
|
IdFile = 1742,
|
|||
|
IdMarkType = 1
|
|||
|
};
|
|||
|
var result = await fileService.CreateFileMarkAsync(dto, 1, CancellationToken.None);
|
|||
|
Assert.True(result > 0);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|