Add tests for DrillingProgramService

This commit is contained in:
Фролов 2022-02-21 17:55:50 +05:00
parent 433bc30316
commit 29845aa36a
2 changed files with 186 additions and 21 deletions

View File

@ -8,7 +8,7 @@ namespace AsbCloudApp.Data
public int Id { get; set; }
public int IdWell { get; set; }
public int IdCategory { get; set; }
public int IdAuthor { get; set; }
public int? IdAuthor { get; set; }
public string Name { get; set; }
public DateTime UploadDate { get; set; }
public long Size { get; set; }

View File

@ -11,6 +11,7 @@ using Microsoft.Extensions.Configuration;
using System.Threading;
using AsbCloudApp.Data;
using Mapster;
using System;
namespace AsbCloudWebApi.Tests.ServicesTests
{
@ -45,6 +46,30 @@ namespace AsbCloudWebApi.Tests.ServicesTests
new User { Id = 3005, IdCompany = 3003, Login = "wrong 2", Email = "aa@aa.aa", IdState = 2 },
};
private static readonly FileInfo file1001 = new FileInfo
{
Id = 3001,
IdWell = idWell,
IdCategory = 1001,
IdAuthor = publisher1.Id,
IsDeleted = false,
Name = "file1.xlsx",
Size = 1024,
UploadDate = System.DateTimeOffset.UtcNow,
};
private static readonly FileInfo file1002 = new FileInfo
{
Id = 3002,
IdWell = idWell,
IdCategory = 1002,
IdAuthor = publisher1.Id,
IsDeleted = false,
Name = "file2.xlsx",
Size = 1024,
UploadDate = System.DateTimeOffset.UtcNow,
};
private static readonly List<RelationCompanyWell> relationsCompanyWell = new()
{
new RelationCompanyWell { IdCompany = 3001, IdWell = 3001, },
@ -184,7 +209,108 @@ namespace AsbCloudWebApi.Tests.ServicesTests
}
[Fact]
public async Task GetStateAsync_returns_state_2()
public async Task AddOrReplaceFileMarkAsync_returns_1()
{
ConfigureNotApproved();
fileServiceMock
.Setup(s => s.GetInfoAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(file1002.Adapt<FileInfoDto>()));
fileServiceMock
.Setup(s => s.CreateFileMarkAsync(It.IsAny<FileMarkDto>(), It.IsAny<int>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(1));
var service = new DrillingProgramService(
db,
fileServiceMock.Object,
userServiceMock.Object,
wellServiceMock.Object,
configurationMock.Object,
backgroundWorkerMock.Object);
var fileMark = new FileMarkDto
{
IdFile = file1002.Id,
IdMarkType = 1,
DateCreated = DateTime.Now,
};
var affected = await service.AddOrReplaceFileMarkAsync(fileMark, approver1.Id, CancellationToken.None);
Assert.Equal(1, affected);
}
[Fact]
public async Task AddOrReplaceFileMarkAsync_as_replace_returns_1()
{
ConfigureNotApproved();
fileServiceMock
.Setup(s => s.GetInfoAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(file1002.Adapt<FileInfoDto>()));
fileServiceMock
.Setup(s => s.CreateFileMarkAsync(It.IsAny<FileMarkDto>(), It.IsAny<int>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(1));
var service = new DrillingProgramService(
db,
fileServiceMock.Object,
userServiceMock.Object,
wellServiceMock.Object,
configurationMock.Object,
backgroundWorkerMock.Object);
var fileMark = new FileMarkDto
{
IdFile = file1001.Id,
IdMarkType = 0,
DateCreated = DateTime.Now,
};
await service.AddOrReplaceFileMarkAsync(fileMark, approver1.Id, CancellationToken.None);
var fileMark2 = new FileMarkDto
{
IdFile = file1001.Id,
IdMarkType = 1,
DateCreated = DateTime.Now.AddHours(1),
};
var affected = await service.AddOrReplaceFileMarkAsync(fileMark2, approver1.Id, CancellationToken.None);
Assert.Equal(1, affected);
}
[Fact]
public async Task MarkAsDeletedFileMarkAsync_returns_1()
{
ConfigureNotApproved();
fileServiceMock
.Setup(s => s.GetByMarkId(It.IsAny<int>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(file1002.Adapt<FileInfoDto>()));
fileServiceMock
.Setup(s => s.MarkFileMarkAsDeletedAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(1));
var service = new DrillingProgramService(
db,
fileServiceMock.Object,
userServiceMock.Object,
wellServiceMock.Object,
configurationMock.Object,
backgroundWorkerMock.Object);
var fileMark = new FileMarkDto
{
IdFile = file1001.Id,
IdMarkType = 0,
DateCreated = DateTime.Now,
};
int idMark = 0;
var affected = await service.MarkAsDeletedFileMarkAsync(idMark, CancellationToken.None);
Assert.Equal(1, affected);
}
[Fact]
public async Task GetStateAsync_returns_state_1()
{
ConfigureNotApproved();
var service = new DrillingProgramService(
@ -197,7 +323,63 @@ namespace AsbCloudWebApi.Tests.ServicesTests
var state = await service.GetStateAsync(idWell, publisher1.Id, CancellationToken.None);
Assert.Equal(1, state.IdState);
}
[Fact]
public async Task GetStateAsync_returns_state_2()
{
ConfigureNotApproved();
db.FileMarks.AddRange(
new FileMark { IdFile = file1002.Id, IdUser = approver1.Id, IdMarkType = 1, DateCreated = System.DateTimeOffset.UtcNow },
new FileMark { IdFile = file1002.Id, IdUser = approver2.Id, IdMarkType = 1, DateCreated = System.DateTimeOffset.UtcNow }
);
await db.SaveChangesAsync();
wellServiceMock.Setup(s => s.GetAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(new WellDto { Caption = "test well", Cluster = "test cluster" }));
var service = new DrillingProgramService(
db,
fileServiceMock.Object,
userServiceMock.Object,
wellServiceMock.Object,
configurationMock.Object,
backgroundWorkerMock.Object);
var state = await service.GetStateAsync(idWell, publisher1.Id, CancellationToken.None);
Assert.Equal(2, state.IdState);
backgroundWorkerMock.Verify(s => s.Enqueue(It.IsAny<Func<string, CancellationToken, Task>>()));
}
[Fact]
public async Task GetStateAsync_returns_state_3()
{
ConfigureNotApproved();
db.FileMarks.AddRange(
new FileMark { IdFile = file1002.Id, IdUser = approver1.Id, IdMarkType = 1, DateCreated = DateTimeOffset.UtcNow },
new FileMark { IdFile = file1002.Id, IdUser = approver2.Id, IdMarkType = 1, DateCreated = DateTimeOffset.UtcNow }
);
db.Files.AddRange(new FileInfo { IdCategory = 1000, IdWell = idWell, Name = "DrillingProgram.xalsx", Size = 1024*1024, UploadDate = DateTimeOffset.UtcNow });
await db.SaveChangesAsync();
wellServiceMock.Setup(s => s.GetAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(new WellDto { Caption = "test well", Cluster = "test cluster" }));
var service = new DrillingProgramService(
db,
fileServiceMock.Object,
userServiceMock.Object,
wellServiceMock.Object,
configurationMock.Object,
backgroundWorkerMock.Object);
var state = await service.GetStateAsync(idWell, publisher1.Id, CancellationToken.None);
Assert.Equal(3, state.IdState);
backgroundWorkerMock.VerifyNoOtherCalls();
}
private void ConfigureNotApproved()
@ -211,7 +393,6 @@ namespace AsbCloudWebApi.Tests.ServicesTests
var entry2 = db.DrillingProgramParts.Add(new DrillingProgramPart { IdWell = idWell, IdFileCategory = 1002 });
db.SaveChanges();
db.RelationDrillingProgramPartUsers.AddRange(new List<RelationUserDrillingProgramPart>{
new RelationUserDrillingProgramPart{
IdDrillingProgramPart = entry1.Entity.Id,
@ -236,24 +417,8 @@ namespace AsbCloudWebApi.Tests.ServicesTests
});
db.Files.AddRange(new List<FileInfo>{
new FileInfo{
IdWell = idWell,
IdCategory = 1001,
IdAuthor = publisher1.Id,
IsDeleted = false,
Name = "file1.xlsx",
Size = 1024,
UploadDate = System.DateTimeOffset.UtcNow,
},
new FileInfo{
IdWell = idWell,
IdCategory = 1002,
IdAuthor = publisher1.Id,
IsDeleted = false,
Name = "file2.xlsx",
Size = 1024,
UploadDate = System.DateTimeOffset.UtcNow,
}
file1001,
file1002
});
db.SaveChanges();