2022-10-05 15:47:02 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2022-10-06 14:37:03 +05:00
|
|
|
|
using AsbCloudApp.Repositories;
|
2022-10-11 08:28:37 +05:00
|
|
|
|
using AsbCloudApp.Requests;
|
2022-10-05 15:47:02 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using Moq;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudWebApi.Tests.ServicesTests
|
|
|
|
|
{
|
|
|
|
|
public class FileServiceTest
|
|
|
|
|
{
|
2022-10-06 14:37:03 +05:00
|
|
|
|
private FileService fileService;
|
2022-10-05 15:47:02 +05:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
|
2022-10-11 08:28:37 +05:00
|
|
|
|
repositoryMock.Setup(x => x.GetInfosByWellIdAsync(It.IsAny<FileServiceRequest>(), It.IsAny<CancellationToken>()))
|
|
|
|
|
.Returns((FileServiceRequest request, CancellationToken token) => {
|
|
|
|
|
var data = Files.Where(x => x.IdWell == request.IdWell);
|
2022-10-05 15:47:02 +05:00
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
|
2022-10-06 13:41:46 +05:00
|
|
|
|
var storageRepositoryMock = new Mock<IFileStorageRepository>();
|
|
|
|
|
|
|
|
|
|
fileService = new FileService(repositoryMock.Object, storageRepositoryMock.Object);
|
2022-10-05 15:47:02 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetByMarkId_returns_FileInfo_by_idMark()
|
|
|
|
|
{
|
|
|
|
|
var data = await fileService.GetByMarkId(133, CancellationToken.None);
|
|
|
|
|
Assert.NotNull(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2022-10-06 13:41:46 +05:00
|
|
|
|
public async Task GetInfoAsync_returns_FileInfo()
|
2022-10-05 15:47:02 +05:00
|
|
|
|
{
|
2022-10-06 13:41:46 +05:00
|
|
|
|
var data = await fileService.GetInfoAsync(1742, CancellationToken.None);
|
|
|
|
|
Assert.NotNull(data);
|
2022-10-05 15:47:02 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2022-10-06 13:41:46 +05:00
|
|
|
|
public async Task GetInfoByIdsAsync_returns_FileInfo()
|
2022-10-05 15:47:02 +05:00
|
|
|
|
{
|
2022-10-06 13:41:46 +05:00
|
|
|
|
var data = await fileService.GetInfoByIdsAsync(new int[] { 1742, 1743 }, CancellationToken.None);
|
|
|
|
|
Assert.NotNull(data);
|
2022-10-05 15:47:02 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2022-10-06 14:37:03 +05:00
|
|
|
|
public async Task SaveAsync_returns_FileInfo()
|
2022-10-05 15:47:02 +05:00
|
|
|
|
{
|
2022-10-06 14:37:03 +05:00
|
|
|
|
using var stream = new MemoryStream(Array.Empty<byte>());
|
2022-10-05 15:47:02 +05:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|