DD.WellWorkover.Cloud/AsbCloudWebApi.Tests/ServicesTests/DrillingProgramServiceTest.cs

263 lines
10 KiB
C#
Raw Normal View History

2022-02-21 12:04:26 +05:00
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Moq;
using Xunit;
using AsbCloudApp.Services;
using AsbCloudInfrastructure.Services.DrillingProgram;
using Microsoft.Extensions.Configuration;
using System.Threading;
using AsbCloudApp.Data;
using Mapster;
namespace AsbCloudWebApi.Tests.ServicesTests
{
public class DrillingProgramServiceTest
{
private const int idWell = 3001;
private static readonly SimpleTimezone baseTimezone = new () { Hours = 5d };
private readonly AsbCloudDbContext db;
private static readonly List<Well> wells = new()
{
new Well { Id = 3001, Caption = "well 1", Timezone = baseTimezone },
new Well { Id = 3002, Caption = "well 2", Timezone = baseTimezone },
};
private static readonly List<Company> companies = new() {
new Company { Id = 3001, Caption = "company name", IdCompanyType = 2, },
new Company { Id = 3002, Caption = "company name", IdCompanyType = 2, },
new Company { Id = 3003, Caption = "company name", IdCompanyType = 2, },
};
private static readonly User publisher1 = new () { Id = 3001, IdCompany = 3001, Login = "user 1", Email = "aa@aa.aa", IdState = 2 };
private static readonly User approver1 = new () { Id = 3002, IdCompany = 3001, Login = "user 2", Email = "aa@aa.aa", IdState = 2 };
private static readonly User approver2 = new () { Id = 3003, IdCompany = 3002, Login = "user 3", Email = "aa@aa.aa", IdState = 2 };
private static readonly List<User> users = new()
{
publisher1,
approver1,
approver2,
new User { Id = 3004, IdCompany = 3001, Login = "wrong 1", Email = "", IdState = 2 },
new User { Id = 3005, IdCompany = 3003, Login = "wrong 2", Email = "aa@aa.aa", IdState = 2 },
};
private static readonly List<RelationCompanyWell> relationsCompanyWell = new()
{
new RelationCompanyWell { IdCompany = 3001, IdWell = 3001, },
new RelationCompanyWell { IdCompany = 3002, IdWell = 3001, },
new RelationCompanyWell { IdCompany = 3002, IdWell = 3002, },
new RelationCompanyWell { IdCompany = 3003, IdWell = 3002, },
};
private readonly Mock<IFileService> fileServiceMock;
private readonly Mock<IUserService> userServiceMock;
private readonly Mock<IWellService> wellServiceMock;
private readonly Mock<IConfiguration> configurationMock;
private readonly Mock<IBackgroundWorkerService> backgroundWorkerMock;
public DrillingProgramServiceTest()
{
AsbCloudInfrastructure.DependencyInjection.MapsterSetup();
db = TestHelpter.MakeTestContext();
db.Wells.AddRange(wells);
db.Companies.AddRange(companies);
db.SaveChanges();
db.Users.AddRange(users);
db.RelationCompaniesWells.AddRange(relationsCompanyWell);
db.SaveChanges();
fileServiceMock = new Mock<IFileService>();
userServiceMock = new Mock<IUserService>();
wellServiceMock = new Mock<IWellService>();
configurationMock = new Mock<IConfiguration>();
backgroundWorkerMock = new Mock<IBackgroundWorkerService>();
}
[Fact]
public async Task GetAvailableUsers_returns_3_users()
{
var service = new DrillingProgramService(
db,
fileServiceMock.Object,
userServiceMock.Object,
wellServiceMock.Object,
configurationMock.Object,
backgroundWorkerMock.Object);
var users = await service.GetAvailableUsers(idWell, CancellationToken.None);
Assert.Equal(3, users.Count());
}
[Fact]
public async Task AddPartsAsync_returns_2()
{
var service = new DrillingProgramService(
db,
fileServiceMock.Object,
userServiceMock.Object,
wellServiceMock.Object,
configurationMock.Object,
backgroundWorkerMock.Object);
var result = await service.AddPartsAsync(idWell, new int[] { 1001, 1002 }, CancellationToken.None);
Assert.Equal(2, result);
}
[Fact]
public async Task RemovePartsAsync_returns_1()
{
db.DrillingProgramParts.Add(new DrillingProgramPart { IdFileCategory = 1005, IdWell = idWell});
db.SaveChanges();
var service = new DrillingProgramService(
db,
fileServiceMock.Object,
userServiceMock.Object,
wellServiceMock.Object,
configurationMock.Object,
backgroundWorkerMock.Object);
var result = await service.RemovePartsAsync(idWell, new int[] { 1005 }, CancellationToken.None);
Assert.Equal(1, result);
}
[Fact]
public async Task AddUserAsync_returns_1()
{
db.DrillingProgramParts.Add(new DrillingProgramPart { IdFileCategory = 1001, IdWell = idWell });
db.SaveChanges();
userServiceMock.Setup((s) => s.GetAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(publisher1.Adapt<UserExtendedDto>()));
var service = new DrillingProgramService(
db,
fileServiceMock.Object,
userServiceMock.Object,
wellServiceMock.Object,
configurationMock.Object,
backgroundWorkerMock.Object);
var result = await service.AddUserAsync(idWell, 1001, publisher1.Id, 1, CancellationToken.None);
Assert.Equal(1, result);
}
[Fact]
public async Task RemoveUserAsync_returns_1()
{
const int idUserRole = 1;
const int idFileCategory = 1001;
var entry = db.DrillingProgramParts.Add(new DrillingProgramPart {
IdFileCategory = idFileCategory,
IdWell = idWell
});
db.SaveChanges();
db.RelationDrillingProgramPartUsers.Add(new RelationUserDrillingProgramPart
{
IdUser = publisher1.Id,
IdDrillingProgramPart = entry.Entity.Id,
IdUserRole = idUserRole
});
db.SaveChanges();
userServiceMock.Setup((s) => s.GetAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(publisher1.Adapt<UserExtendedDto>()));
var service = new DrillingProgramService(
db,
fileServiceMock.Object,
userServiceMock.Object,
wellServiceMock.Object,
configurationMock.Object,
backgroundWorkerMock.Object);
var result = await service.RemoveUserAsync(idWell, idFileCategory, publisher1.Id, idUserRole, CancellationToken.None);
Assert.Equal(1, result);
}
[Fact]
public async Task GetStateAsync_returns_state_2()
{
ConfigureNotApproved();
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);
}
private void ConfigureNotApproved()
{
db.DrillingProgramParts.RemoveRange(db.DrillingProgramParts);
db.Files.RemoveRange(db.Files);
//db.RelationDrillingProgramPartUsers.RemoveRange(db.RelationDrillingProgramPartUsers);
db.SaveChanges();
var entry1 = db.DrillingProgramParts.Add(new DrillingProgramPart { IdWell = idWell, IdFileCategory = 1001 });
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,
IdUser = publisher1.Id,
IdUserRole = 1,
},
new RelationUserDrillingProgramPart{
IdDrillingProgramPart = entry2.Entity.Id,
IdUser = publisher1.Id,
IdUserRole = 1,
},
new RelationUserDrillingProgramPart{
IdDrillingProgramPart = entry2.Entity.Id,
IdUser = approver1.Id,
IdUserRole = 2,
},
new RelationUserDrillingProgramPart{
IdDrillingProgramPart = entry2.Entity.Id,
IdUser = approver2.Id,
IdUserRole = 2,
},
});
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,
}
});
db.SaveChanges();
}
}
}