forked from ddrilling/AsbCloudServer
Merge branch 'dev' into feature/7887519
This commit is contained in:
commit
454566dc2e
@ -1,10 +1,11 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace AsbCloudDb.Model.DefaultData
|
||||
{
|
||||
internal static class DefaultContextData
|
||||
{
|
||||
public static void Fill(ModelBuilder modelBuilder)
|
||||
public static IEnumerable<IEntityFiller> GetFillers()
|
||||
{
|
||||
var fillers = new IEntityFiller[]
|
||||
{
|
||||
@ -24,6 +25,12 @@ namespace AsbCloudDb.Model.DefaultData
|
||||
new EntityFillerCompanyType(),
|
||||
new EntityFillerSubsystem(),
|
||||
};
|
||||
return fillers;
|
||||
}
|
||||
|
||||
public static void Fill(ModelBuilder modelBuilder)
|
||||
{
|
||||
var fillers = GetFillers();
|
||||
|
||||
foreach (var filler in fillers)
|
||||
filler.FillData(modelBuilder);
|
||||
|
@ -1,11 +1,12 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace AsbCloudDb.Model.DefaultData
|
||||
{
|
||||
internal abstract class EntityFiller<TEntity> : IEntityFiller
|
||||
where TEntity : class
|
||||
{
|
||||
protected abstract TEntity[] GetData();
|
||||
public abstract TEntity[] GetData();
|
||||
|
||||
public void FillData(ModelBuilder modelBuilder)
|
||||
{
|
||||
|
@ -2,7 +2,7 @@
|
||||
{
|
||||
internal class EntityFillerCompany : EntityFiller<Company>
|
||||
{
|
||||
protected override Company[] GetData() => new Company[]
|
||||
public override Company[] GetData() => new Company[]
|
||||
{
|
||||
new (){ Id = 1, Caption = "ООО \"АСБ\"", IdCompanyType = 3},
|
||||
};
|
||||
|
@ -2,7 +2,7 @@
|
||||
{
|
||||
internal class EntityFillerCompanyType : EntityFiller<CompanyType>
|
||||
{
|
||||
protected override CompanyType[] GetData() => new CompanyType[] {
|
||||
public override CompanyType[] GetData() => new CompanyType[] {
|
||||
new (){ Id = 1, Caption = "Недрапользователь", },
|
||||
new (){ Id = 2, Caption = "Буровой подрядчик", },
|
||||
new (){ Id = 3, Caption = "Сервис автоматизации бурения", },
|
||||
|
@ -2,7 +2,7 @@
|
||||
{
|
||||
internal class EntityFillerFileCategory: EntityFiller<FileCategory>
|
||||
{
|
||||
protected override FileCategory[] GetData() => new FileCategory[]{
|
||||
public override FileCategory[] GetData() => new FileCategory[]{
|
||||
new () {Id = 1, Name = "Растворный сервис", ShortName = "fluidService"},
|
||||
new () {Id = 2, Name = "Цементирование", ShortName = "cement"},
|
||||
new () {Id = 3, Name = "ННБ", ShortName = "nnb"},
|
||||
|
@ -2,7 +2,7 @@
|
||||
{
|
||||
internal class EntityFillerMeasureCategory : EntityFiller<MeasureCategory>
|
||||
{
|
||||
protected override MeasureCategory[] GetData() => new MeasureCategory[] {
|
||||
public override MeasureCategory[] GetData() => new MeasureCategory[] {
|
||||
new (){ Id = 1, Name = "Показатели бурового раствора", ShortName = "Раствор"},
|
||||
new (){ Id = 2, Name = "Шламограмма", ShortName = "Шламограмма"},
|
||||
new (){ Id = 3, Name = "ННБ", ShortName = "ННБ"},
|
||||
|
@ -2,7 +2,7 @@
|
||||
{
|
||||
internal class EntityFillerPermission : EntityFiller<Permission>
|
||||
{
|
||||
protected override Permission[] GetData() => new Permission[]{
|
||||
public override Permission[] GetData() => new Permission[]{
|
||||
new (){ Id = 100, Name="AdminCluster.delete", Description="Разрешение удалять админ. Кусты"},
|
||||
new (){ Id = 101, Name="AdminCluster.edit", Description="Разрешение редактировать админ. Кусты"},
|
||||
new (){ Id = 102, Name="AdminCluster.get", Description="Разрешение просматривать админ. Кусты"},
|
||||
|
@ -2,7 +2,7 @@
|
||||
{
|
||||
internal class EntityFillerRelationUserRolePermission: EntityFiller<RelationUserRolePermission>
|
||||
{
|
||||
protected override RelationUserRolePermission[] GetData() => new RelationUserRolePermission[]{
|
||||
public override RelationUserRolePermission[] GetData() => new RelationUserRolePermission[]{
|
||||
new (){ IdUserRole = 1100, IdPermission = 102}, new (){ IdUserRole = 1100, IdPermission = 111},
|
||||
new (){ IdUserRole = 1101, IdPermission = 101}, new (){ IdUserRole = 1101, IdPermission = 100},
|
||||
new (){ IdUserRole = 1102, IdPermission = 105}, new (){ IdUserRole = 1102, IdPermission = 108},
|
||||
|
@ -2,7 +2,7 @@
|
||||
{
|
||||
internal class EntityFillerRelationUserRoleUserRole: EntityFiller< RelationUserRoleUserRole>
|
||||
{
|
||||
protected override RelationUserRoleUserRole[] GetData() => new RelationUserRoleUserRole[]{
|
||||
public override RelationUserRoleUserRole[] GetData() => new RelationUserRoleUserRole[]{
|
||||
new (){ Id = 1101, IdInclude = 1100 },
|
||||
new (){ Id = 1103, IdInclude = 1102 },
|
||||
new (){ Id = 1105, IdInclude = 1104 },
|
||||
|
@ -2,7 +2,7 @@
|
||||
{
|
||||
internal class EntityFillerRelationUserUserRole : EntityFiller<RelationUserUserRole>
|
||||
{
|
||||
protected override RelationUserUserRole[] GetData() => new RelationUserUserRole[]
|
||||
public override RelationUserUserRole[] GetData() => new RelationUserUserRole[]
|
||||
{
|
||||
new () { IdUser = 1, IdUserRole = 1, },
|
||||
};
|
||||
|
@ -3,7 +3,7 @@ namespace AsbCloudDb.Model.DefaultData
|
||||
{
|
||||
internal class EntityFillerSubsystem : EntityFiller<Subsystem>
|
||||
{
|
||||
protected override Subsystem[] GetData() => new Subsystem[]{
|
||||
public override Subsystem[] GetData() => new Subsystem[]{
|
||||
// САУБ - ид подсистем с 1 до 65_535
|
||||
new () {Id = 1, Name = "АКБ", Description = "Совместная работа режимов \"Бурение в роторе\" и \"Бурение в слайде\""},
|
||||
new () {Id = 2, Name = "MSE", Description = "Алгоритм поиска оптимальных параметров бурения САУБ"},
|
||||
|
@ -2,7 +2,7 @@
|
||||
{
|
||||
internal class EntityFillerUser : EntityFiller<User>
|
||||
{
|
||||
protected override User[] GetData() => new User[]{
|
||||
public override User[] GetData() => new User[]{
|
||||
new (){
|
||||
Id = 1,
|
||||
IdCompany = 1,
|
||||
|
@ -2,7 +2,7 @@
|
||||
{
|
||||
internal class EntityFillerUserRole : EntityFiller<UserRole>
|
||||
{
|
||||
protected override UserRole[] GetData() => new UserRole[]{
|
||||
public override UserRole[] GetData() => new UserRole[]{
|
||||
new (){ Id = 1, Caption = "root", IdType = 1},
|
||||
new (){ Id = 1100, Caption = "admin_cluster.view", IdType = 1},
|
||||
new (){ Id = 1101, Caption = "admin_cluster.edit", IdType = 1},
|
||||
|
@ -2,7 +2,7 @@
|
||||
{
|
||||
internal class EntityFillerWellOperationCategory : EntityFiller<WellOperationCategory>
|
||||
{
|
||||
protected override WellOperationCategory[] GetData() => new WellOperationCategory[]{
|
||||
public override WellOperationCategory[] GetData() => new WellOperationCategory[]{
|
||||
// Автоматически определяемые операции
|
||||
new () {Id = 1, Name = "Невозможно определить операцию", Code = 0, KeyValueName = "dT", KeyValueUnits = "мин"},
|
||||
new () {Id = 2, Name = "Роторное бурение", Code = 0, KeyValueName = "МСП", KeyValueUnits = "м/ч" },
|
||||
|
@ -2,7 +2,7 @@
|
||||
{
|
||||
internal class EntityFillerWellSectionType : EntityFiller<WellSectionType>
|
||||
{
|
||||
protected override WellSectionType[] GetData() => new WellSectionType[]
|
||||
public override WellSectionType[] GetData() => new WellSectionType[]
|
||||
{
|
||||
new (){ Id = 1, Caption = "Пилотный ствол"},
|
||||
new (){ Id = 2, Caption = "Направление"},
|
||||
|
@ -2,7 +2,7 @@
|
||||
{
|
||||
internal class EntityFillerWellType : EntityFiller<WellType>
|
||||
{
|
||||
protected override WellType[] GetData() => new WellType[]
|
||||
public override WellType[] GetData() => new WellType[]
|
||||
{
|
||||
new (){ Id = 1, Caption = "Наклонно-направленная"},
|
||||
new (){ Id = 2, Caption = "Горизонтальная"},
|
||||
|
@ -13,6 +13,7 @@
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.8" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.8" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
|
||||
<PackageReference Include="MockQueryable.Moq" Version="6.0.1" />
|
||||
<PackageReference Include="Moq" Version="4.18.2" />
|
||||
<PackageReference Include="xunit" Version="2.4.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
|
||||
|
@ -1,14 +1,12 @@
|
||||
using AsbCloudDb.Model;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using MockQueryable.Moq;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AsbCloudWebApi.Tests
|
||||
{
|
||||
@ -29,9 +27,13 @@ namespace AsbCloudWebApi.Tests
|
||||
public static Mock<IAsbCloudDbContext> AddDbSetMock<T>(this Mock<IAsbCloudDbContext> contextMock, IEnumerable<T> dbSetData)
|
||||
where T : class
|
||||
{
|
||||
var dbSetMock = MakeDbSetMock(dbSetData);
|
||||
var mockDbSet = dbSetData
|
||||
.ToList()
|
||||
.AsQueryable()
|
||||
.BuildMockDbSet();
|
||||
|
||||
contextMock.Setup(o => o.Set<T>())
|
||||
.Returns(() => dbSetMock.Object);
|
||||
.Returns(mockDbSet.Object);
|
||||
|
||||
var dbSetProperty = typeof(IAsbCloudDbContext)
|
||||
.GetProperties()
|
||||
@ -43,84 +45,10 @@ namespace AsbCloudWebApi.Tests
|
||||
var objParameterExpr = Expression.Parameter(typeof(IAsbCloudDbContext), "instance");
|
||||
var propertyExpr = Expression.Property(objParameterExpr, dbSetProperty);
|
||||
var expression = Expression.Lambda<Func<IAsbCloudDbContext, DbSet<T>>>(propertyExpr, objParameterExpr);
|
||||
contextMock.SetupGet(expression).Returns(dbSetMock.Object);
|
||||
contextMock.SetupGet(expression).Returns(mockDbSet.Object);
|
||||
}
|
||||
|
||||
return contextMock;
|
||||
}
|
||||
|
||||
public static Mock<DbSet<T>> MakeDbSetMock<T>()
|
||||
where T : class
|
||||
=> MakeDbSetMock(new List<T>());
|
||||
|
||||
class DummyAsyncQueriable<T> : IQueryable<T>, IAsyncEnumerable<T>
|
||||
{
|
||||
private readonly IQueryable<T> queriable;
|
||||
|
||||
public Type ElementType => queriable.ElementType;
|
||||
|
||||
public Expression Expression => queriable.Expression;
|
||||
|
||||
public IQueryProvider Provider => queriable.Provider;
|
||||
|
||||
class QueriableAsyncEminaretor<T2> : IAsyncEnumerator<T2>
|
||||
{
|
||||
private readonly IEnumerator<T2> syncEnumerator;
|
||||
|
||||
public QueriableAsyncEminaretor(IEnumerator<T2> syncEnumerator)
|
||||
{
|
||||
this.syncEnumerator = syncEnumerator;
|
||||
}
|
||||
|
||||
public T2 Current => syncEnumerator.Current;
|
||||
|
||||
public ValueTask DisposeAsync()
|
||||
{
|
||||
syncEnumerator.Dispose();
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
public ValueTask<bool> MoveNextAsync()
|
||||
{
|
||||
var result = syncEnumerator.MoveNext();
|
||||
return ValueTask.FromResult(result);
|
||||
}
|
||||
}
|
||||
|
||||
public DummyAsyncQueriable(IEnumerable<T> dbSetData)
|
||||
{
|
||||
queriable = dbSetData.ToList().AsQueryable();
|
||||
}
|
||||
|
||||
public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
var enumerator = this.AsEnumerable().GetEnumerator();
|
||||
return new QueriableAsyncEminaretor<T>(enumerator);
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
=> queriable.GetEnumerator();
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
=> queriable.GetEnumerator();
|
||||
}
|
||||
|
||||
public static Mock<DbSet<T>> MakeDbSetMock<T>(IEnumerable<T> dbSetData)
|
||||
where T : class
|
||||
{
|
||||
var dbSetDataQueriable = new DummyAsyncQueriable<T>(dbSetData);
|
||||
|
||||
Mock<DbSet<T>> dbSetMock = new();
|
||||
dbSetMock.As<IQueryable<T>>().Setup(o => o.Provider).Returns(() => dbSetDataQueriable.Provider);
|
||||
dbSetMock.As<IQueryable<T>>().Setup(o => o.Expression).Returns(() => dbSetDataQueriable.Expression);
|
||||
dbSetMock.As<IQueryable<T>>().Setup(o => o.ElementType).Returns(() => dbSetDataQueriable.ElementType);
|
||||
dbSetMock.As<IQueryable<T>>().Setup(o => o.GetEnumerator()).Returns(() => dbSetDataQueriable.GetEnumerator());
|
||||
|
||||
dbSetMock.As<IAsyncEnumerable<T>>()
|
||||
.Setup(o => o.GetAsyncEnumerator(It.IsAny<CancellationToken>()))
|
||||
.Returns(() => dbSetDataQueriable.GetAsyncEnumerator());
|
||||
|
||||
return dbSetMock;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user