fix default EF data.

This commit is contained in:
Фролов 2022-02-21 12:04:26 +05:00
parent 4863b69cfe
commit 433bc30316
6 changed files with 271 additions and 6 deletions

View File

@ -548,7 +548,7 @@ namespace AsbCloudDb.Model
modelBuilder.Entity<RelationUserUserRole>(entity =>
{
entity.HasData(new List<RelationUserUserRole>{
new RelationUserUserRole { IdUser = 1, IdUserRole = 2, },
new RelationUserUserRole { IdUser = 1, IdUserRole = 1, },
});
});

View File

@ -325,6 +325,9 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
if (fileEntity is not null)
{
part.File = fileEntity.Adapt<FileInfoDto>();
if (part.File.FileMarks is not null)
part.File.FileMarks = part.File.FileMarks.Where(m => !m.IsDeleted);
part.IdState = idPartStateApproving;
var marks = fileEntity.FileMarks.Where(m => !m.IsDeleted);
var hasReject = marks.Any(m => m.IdMarkType == idMarkTypeReject);

View File

@ -109,7 +109,7 @@ public class ClusterServiceTest
var service = new ClusterService(context, wellService.Object);
var dtos = await service.GetDepositsAsync(1);
Assert.Equal(1, dtos.Count());
Assert.Single(dtos);
}
[Fact]
@ -135,7 +135,7 @@ public class ClusterServiceTest
var service = new ClusterService(context, wellService.Object);
var dtos = await service.GetDepositsDrillParamsAsync(1);
Assert.Equal(1, dtos.Count());
Assert.Single(dtos);
}
[Fact]
@ -171,7 +171,7 @@ public class ClusterServiceTest
var service = new ClusterService(context, wellService.Object);
var dtos = await service.GetWellsAsync(1,1);
Assert.Equal(1, dtos.Count());
Assert.Single(dtos);
}

View File

@ -0,0 +1,262 @@
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();
}
}
}

View File

@ -6,7 +6,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AsbCloudApp.Services;
using AsbCloudInfrastructure.Services;
using Moq;
using Xunit;

View File

@ -16,7 +16,8 @@ namespace AsbCloudWebApi.Tests
.UseNpgsql("Host=localhost;Database=tests;Username=postgres;Password=q;Persist Security Info=True;Include Error Detail=True")
.Options;
var context = new AsbCloudDbContext(options);
context.Database.Migrate();
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
return context;
}
}