forked from ddrilling/AsbCloudServer
#6539681 test file service
This commit is contained in:
parent
9644f73090
commit
03e50a4297
17
AsbCloudWebApi.Tests/IRepositoryFactory.cs
Normal file
17
AsbCloudWebApi.Tests/IRepositoryFactory.cs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace AsbCloudWebApi.Tests
|
||||||
|
{
|
||||||
|
public interface IRepositoryFactory<TDto>
|
||||||
|
{
|
||||||
|
Task<int> DeleteAsync(int id, CancellationToken token);
|
||||||
|
Task<IEnumerable<TDto>> GetAllAsync(CancellationToken token);
|
||||||
|
TDto? GetOrDefault(int id);
|
||||||
|
Task<TDto?> GetOrDefaultAsync(int id, CancellationToken token);
|
||||||
|
Task<int> InsertAsync(TDto newItem, CancellationToken token);
|
||||||
|
Task<int> InsertRangeAsync(IEnumerable<TDto> newItems, CancellationToken token);
|
||||||
|
Task<int> UpdateAsync(TDto item, CancellationToken token);
|
||||||
|
}
|
||||||
|
}
|
44
AsbCloudWebApi.Tests/RepositoryFactory.cs
Normal file
44
AsbCloudWebApi.Tests/RepositoryFactory.cs
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
using AsbCloudApp.Services;
|
||||||
|
using Moq;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace AsbCloudWebApi.Tests
|
||||||
|
{
|
||||||
|
public class RepositoryFactory
|
||||||
|
{
|
||||||
|
public static Mock<TRepository> Make<TRepository, TDto>(IList<TDto> data)
|
||||||
|
where TDto : AsbCloudApp.Data.IId
|
||||||
|
where TRepository : class, ICrudService<TDto>
|
||||||
|
{
|
||||||
|
var repositoryMock = new Mock<TRepository>();
|
||||||
|
|
||||||
|
repositoryMock.Setup(x => x.InsertAsync(It.IsAny<TDto>(), It.IsAny<CancellationToken>()))
|
||||||
|
.Returns((TDto dto, CancellationToken token) => {
|
||||||
|
var id = data.Max(x => x.Id);
|
||||||
|
dto.Id = ++id;
|
||||||
|
data.Add(dto);
|
||||||
|
return Task.FromResult(dto.Id);
|
||||||
|
});
|
||||||
|
repositoryMock.Setup(x => x.DeleteAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
|
||||||
|
.Returns((int idFile, CancellationToken token) => {
|
||||||
|
var cnt = data.Count;
|
||||||
|
var dto = data.FirstOrDefault(x => x.Id == idFile);
|
||||||
|
data.Remove(dto);
|
||||||
|
return Task.FromResult(cnt - data.Count);
|
||||||
|
});
|
||||||
|
|
||||||
|
repositoryMock.Setup(x => x.GetAllAsync(It.IsAny<CancellationToken>())).ReturnsAsync(data);
|
||||||
|
repositoryMock.Setup(x => x.GetOrDefaultAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
|
||||||
|
.Returns((int idFile, CancellationToken token) => {
|
||||||
|
return Task.FromResult(data.FirstOrDefault(x => x.Id == idFile));
|
||||||
|
});
|
||||||
|
|
||||||
|
return repositoryMock;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
222
AsbCloudWebApi.Tests/ServicesTests/FileServiceTest.cs
Normal file
222
AsbCloudWebApi.Tests/ServicesTests/FileServiceTest.cs
Normal file
@ -0,0 +1,222 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
using AsbCloudDb.Model;
|
using AsbCloudApp.Services;
|
||||||
|
using AsbCloudDb.Model;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user