This commit is contained in:
Фролов 2022-02-08 10:05:00 +05:00
commit 696dbee375
5 changed files with 134 additions and 57 deletions

View File

@ -10,6 +10,7 @@ namespace AsbCloudDb.Model
{ {
public virtual DbSet<Cluster> Clusters { get; set; } public virtual DbSet<Cluster> Clusters { get; set; }
public virtual DbSet<Company> Companies { get; set; } public virtual DbSet<Company> Companies { get; set; }
public virtual DbSet<CompanyType> CompaniesTypes { get; set; }
public virtual DbSet<Deposit> Deposits { get; set; } public virtual DbSet<Deposit> Deposits { get; set; }
public virtual DbSet<FileCategory> FileCategories { get; set; } public virtual DbSet<FileCategory> FileCategories { get; set; }
public virtual DbSet<FileInfo> Files { get; set; } public virtual DbSet<FileInfo> Files { get; set; }
@ -35,8 +36,9 @@ namespace AsbCloudDb.Model
public virtual DbSet<WellType> WellTypes { get; set; } public virtual DbSet<WellType> WellTypes { get; set; }
public virtual DbSet<DrillParams> DrillParams { get; set; } public virtual DbSet<DrillParams> DrillParams { get; set; }
public virtual DbSet<DrillFlowChart> DrillFlowChart { get; set; } public virtual DbSet<DrillFlowChart> DrillFlowChart { get; set; }
public virtual DbSet<RelationUserUserRole> RelationUserUserRoles { get; set; }
public virtual DbSet<Permission> Permissions { get; set; } public virtual DbSet<Permission> Permissions { get; set; }
public virtual DbSet<RelationCompanyWell> RelationCompaniesWells { get; set; }
public virtual DbSet<RelationUserUserRole> RelationUserUserRoles { get; set; }
public virtual DbSet<RelationUserRolePermission> RelationUserRolePermissions { get; set; } public virtual DbSet<RelationUserRolePermission> RelationUserRolePermissions { get; set; }
public virtual DbSet<RelationUserRoleUserRole> RelationUserRoleUserRoles { get; set; } public virtual DbSet<RelationUserRoleUserRole> RelationUserRoleUserRoles { get; set; }

View File

@ -9,22 +9,22 @@ namespace AsbCloudInfrastructure.Validators
{ {
public static IServiceCollection AddValidators(this IServiceCollection services) public static IServiceCollection AddValidators(this IServiceCollection services)
{ {
services.AddTransient<IValidator<AuthDto>, AuthDtoValidator>(); // services.AddTransient<IValidator<AuthDto>, AuthDtoValidator>();
services.AddTransient<IValidator<ClusterDto>, ClusterDtoValidator>(); // services.AddTransient<IValidator<ClusterDto>, ClusterDtoValidator>();
services.AddTransient<IValidator<CompanyDto>, CompanyDtoValidator>(); // services.AddTransient<IValidator<CompanyDto>, CompanyDtoValidator>();
services.AddTransient<IValidator<DepositDto>, DepositDtoValidator>(); // services.AddTransient<IValidator<DepositDto>, DepositDtoValidator>();
services.AddTransient<IValidator<DrillFlowChartDto>, DrillFlowChartDtoValidator>(); // services.AddTransient<IValidator<DrillFlowChartDto>, DrillFlowChartDtoValidator>();
services.AddTransient<IValidator<EventDto>, EventDtoValidator>(); // services.AddTransient<IValidator<EventDto>, EventDtoValidator>();
services.AddTransient<IValidator<FileInfoDto>, FileInfoDtoValidator>(); // services.AddTransient<IValidator<FileInfoDto>, FileInfoDtoValidator>();
services.AddTransient<IValidator<FileMarkDto>, FileMarkDtoValidator>(); // services.AddTransient<IValidator<FileMarkDto>, FileMarkDtoValidator>();
services.AddTransient<IValidator<MeasureDto>, MeasureDtoValidator>(); // services.AddTransient<IValidator<MeasureDto>, MeasureDtoValidator>();
services.AddTransient<IValidator<MessageDto>, MessageDtoValidator>(); // services.AddTransient<IValidator<MessageDto>, MessageDtoValidator>();
services.AddTransient<IValidator<PermissionDto>, PermissionDtoValidator>(); // services.AddTransient<IValidator<PermissionDto>, PermissionDtoValidator>();
services.AddTransient<IValidator<ReportPropertiesDto>, ReportPropertiesDtoValidator>(); // services.AddTransient<IValidator<ReportPropertiesDto>, ReportPropertiesDtoValidator>();
services.AddTransient<IValidator<UserRegistrationDto>, UserRegistrationDtoValidator>(); // services.AddTransient<IValidator<UserRegistrationDto>, UserRegistrationDtoValidator>();
services.AddTransient<IValidator<UserRoleDto>, UserRoleDtoValidator>(); // services.AddTransient<IValidator<UserRoleDto>, UserRoleDtoValidator>();
services.AddTransient<IValidator<WellDto>, WellDtoValidator>(); // services.AddTransient<IValidator<WellDto>, WellDtoValidator>();
services.AddTransient<IValidator<WellOperationDto>, WellOperationDtoValidator>(); // services.AddTransient<IValidator<WellOperationDto>, WellOperationDtoValidator>();
return services; return services;
} }

View File

@ -13,80 +13,96 @@ public class ClusterServiceTest
{ {
private readonly AsbCloudDbContext context; private readonly AsbCloudDbContext context;
private readonly Mock<IWellService> wellService; private readonly Mock<IWellService> wellService;
private readonly List<Deposit> deposits = new() private readonly List<Deposit> deposits = new()
{ {
new Deposit { Id = 1, Caption = "Test deposit 1" }, new Deposit { Id = 1, Caption = "Test deposit 1" },
new Deposit { Id = 2, Caption = "Test deposit 2" }, new Deposit { Id = 2, Caption = "Test deposit 2" },
new Deposit { Id = 3, Caption = "Test deposit 3" }, new Deposit { Id = 3, Caption = "Test deposit 3" },
new Deposit { Id = 4, Caption = "Test deposit 4" }, new Deposit { Id = 4, Caption = "Test deposit 4" }
}; };
private readonly List<Cluster> clusters = new() private readonly List<Cluster> clusters = new()
{ {
new Cluster { Id = 1, IdDeposit = 1 }, new Cluster { Id = 1, Caption = "Test cluster 1", IdDeposit = 1, Timezone = new SimpleTimezone()},
new Cluster { Id = 2, IdDeposit = 1 }, new Cluster { Id = 2, Caption = "Test cluster 2", IdDeposit = 1, Timezone = new SimpleTimezone() },
new Cluster { Id = 3, IdDeposit = 2 }, new Cluster { Id = 3, Caption = "Test cluster 3", IdDeposit = 2, Timezone = new SimpleTimezone() },
new Cluster { Id = 4, IdDeposit = 2 }, new Cluster { Id = 4, Caption = "Test cluster 4", IdDeposit = 2, Timezone = new SimpleTimezone() }
}; };
private readonly List<Well> wells = new() private readonly List<Well> wells = new()
{ {
new Well { Id = 1, IdCluster = 1 }, new Well { Id = 1, Caption = "Test well 1", IdCluster = 1 },
new Well { Id = 2, IdCluster = 1 }, new Well { Id = 2, Caption = "Test well 2", IdCluster = 2 },
new Well { Id = 3, IdCluster = 2 }, new Well { Id = 3, Caption = "Test well 3", IdCluster = 1 },
new Well { Id = 4, IdCluster = 2 }, new Well { Id = 4, Caption = "Test well 4", IdCluster = 2 }
};
private readonly List<CompanyType> companiesTypes = new()
{
new CompanyType { Id = 1, Caption = "test company type"}
}; };
private readonly List<Company> companies = new() private readonly List<Company> companies = new()
{ {
new Company { Id = 1 }, new Company { Id = 1, Caption = "Test company 1", IdCompanyType = 1},
new Company { Id = 2 } new Company { Id = 2, Caption = "Test company 2", IdCompanyType = 1}
}; };
private readonly List<RelationCompanyWell> relations = new() private readonly List<RelationCompanyWell> relations = new()
{ {
new RelationCompanyWell { IdCompany = 1, IdWell = 1 }, new RelationCompanyWell { IdCompany = 1, IdWell = 1 },
new RelationCompanyWell { IdCompany = 1, IdWell = 2 } new RelationCompanyWell { IdCompany = 1, IdWell = 2 },
new RelationCompanyWell { IdCompany = 2, IdWell = 2 }
};
private readonly List<WellSectionType> wellSectionTypes = new()
{
new WellSectionType { Id = 1, Caption = "Test well section type 1" }
};
private readonly List<DrillParams> drillParams = new()
{
new DrillParams {Id = 1, IdWell = 1, IdWellSectionType = 1},
new DrillParams {Id = 2, IdWell = 2, IdWellSectionType = 1}
}; };
public ClusterServiceTest() public ClusterServiceTest()
{ {
context = TestHelpter.MakeTestContext(); context = TestHelpter.MakeTestContext();
wellService = new Mock<IWellService>(); wellService = new Mock<IWellService>();
context.Deposits.RemoveRange(deposits); context.Deposits.RemoveRange(context.Deposits);
context.Clusters.RemoveRange(clusters); context.Clusters.RemoveRange(context.Clusters);
context.Wells.RemoveRange(wells); context.Wells.RemoveRange(context.Wells);
context.Companies.RemoveRange(companies); context.CompaniesTypes.RemoveRange(context.CompaniesTypes);
context.RemoveRange(relations); context.Companies.RemoveRange(context.Companies);
context.SaveChanges(); context.RelationCompaniesWells.RemoveRange(context.RelationCompaniesWells);
context.WellSectionTypes.RemoveRange(context.WellSectionTypes);
context.DrillParams.RemoveRange(context.DrillParams);
if(context.ChangeTracker.HasChanges())
context.SaveChanges();
context.Deposits.AddRange(deposits); context.Deposits.AddRange(deposits);
context.Clusters.AddRange(clusters); context.Clusters.AddRange(clusters);
context.Wells.AddRange(wells); context.Wells.AddRange(wells);
context.CompaniesTypes.AddRange(companiesTypes);
context.Companies.AddRange(companies); context.Companies.AddRange(companies);
context.AddRange(relations); context.RelationCompaniesWells.AddRange(relations);
context.WellSectionTypes.AddRange(wellSectionTypes);
context.DrillParams.AddRange(drillParams);
context.SaveChanges(); context.SaveChanges();
} }
~ClusterServiceTest() ~ClusterServiceTest()
{ {
context.Deposits.RemoveRange(context.Deposits.Where(u => u.Id > 1)); context.Deposits.RemoveRange(context.Deposits);
context.Clusters.RemoveRange(context.Clusters.Where(u => u.Id > 1)); context.Clusters.RemoveRange(context.Clusters);
context.Wells.RemoveRange(context.Wells.Where(u => u.Id > 1)); context.Wells.RemoveRange(context.Wells);
context.Companies.RemoveRange(context.Companies.Where(u => u.Id > 1)); context.CompaniesTypes.RemoveRange(context.CompaniesTypes);
context.RemoveRange(relations); context.Companies.RemoveRange(context.Companies);
context.RelationCompaniesWells.RemoveRange(context.RelationCompaniesWells);
context.SaveChanges(); context.SaveChanges();
} }
[Fact]
public async Task GetDepositsAsync_returns_depositDtos()
{
var service = new ClusterService(context, wellService.Object);
var dtos = await service.GetDepositsAsync(1);
Assert.True(dtos.Any());
}
[Fact] [Fact]
public async Task GetDepositsAsync_returns_one_deposit() public async Task GetDepositsAsync_returns_one_deposit()
{ {
@ -97,7 +113,7 @@ public class ClusterServiceTest
} }
[Fact] [Fact]
public async Task GetDepositsAsync_returns_one_deposit_with_two_companies() public async Task GetDepositsAsync_returns_one_deposit_with_two_clusters()
{ {
var service = new ClusterService(context, wellService.Object); var service = new ClusterService(context, wellService.Object);
var dtos = await service.GetDepositsAsync(1); var dtos = await service.GetDepositsAsync(1);
@ -123,7 +139,7 @@ public class ClusterServiceTest
} }
[Fact] [Fact]
public async Task GetDrillParamsAsync_returns_one_deposit_with_two_companies() public async Task GetDrillParamsAsync_returns_one_deposit_with_two_clusters()
{ {
var service = new ClusterService(context, wellService.Object); var service = new ClusterService(context, wellService.Object);
var dtos = await service.GetDepositsDrillParamsAsync(1); var dtos = await service.GetDepositsDrillParamsAsync(1);
@ -132,20 +148,31 @@ public class ClusterServiceTest
} }
[Fact] [Fact]
public async Task GetClustersAsync_returns_dtos() public async Task GetClustersAsync_returns_two_dtos()
{ {
var service = new ClusterService(context, wellService.Object); var service = new ClusterService(context, wellService.Object);
var dtos = await service.GetClustersAsync(1); var dtos = await service.GetClustersAsync(1);
Assert.True(dtos.Any()); Assert.Equal(2, dtos.Count());
} }
[Fact] [Fact]
public async Task GetClustersAsync_returns_one_cluster() public async Task GetClustersAsync_with_deposit_returns_two_clusters()
{ {
var service = new ClusterService(context, wellService.Object); var service = new ClusterService(context, wellService.Object);
var dtos = await service.GetClustersAsync(1); var dtos = await service.GetClustersAsync(1,1);
Assert.Equal(2, dtos.Count());
}
[Fact]
public async Task GetWellsAsync_returns_one_well_by_cluster_and_company()
{
var service = new ClusterService(context, wellService.Object);
var dtos = await service.GetWellsAsync(1,1);
Assert.Equal(1, dtos.Count()); Assert.Equal(1, dtos.Count());
} }
} }

View File

@ -0,0 +1,47 @@
using AsbCloudApp.Data;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
using AsbCloudInfrastructure.Services;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AsbCloudApp.Services;
using AsbCloudInfrastructure.Services;
using Moq;
using Xunit;
namespace AsbCloudWebApi.Tests.ServicesTests;
public class EventServiceTest
{
private readonly AsbCloudDbContext context;
private readonly CacheDb cacheDb;
private readonly Mock<ITelemetryService> telemetryService;
public EventServiceTest()
{
context = TestHelpter.MakeTestContext();
cacheDb = new CacheDb();
telemetryService = new Mock<ITelemetryService>();
telemetryService.Setup(s => s.GetOrCreateTelemetryIdByUid(It.IsAny<string>()))
.Returns(1);
context.TelemetryEvents.RemoveRange(context.TelemetryEvents);
context.SaveChanges();
}
[Fact]
public async Task It_should_save_two_telemetry_events()
{
var service = new EventService(context, cacheDb, telemetryService.Object);
var dtos = new List<EventDto>
{
new EventDto {Id = 1, IdCategory = 1, Message = "Test message 1"},
new EventDto {Id = 2, IdCategory = 1, Message = "Test message 2"}
};
await service.UpsertAsync("uid", dtos);
Assert.Equal(2, context.TelemetryEvents.Count());
}
}

View File

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