Merge branch 'dev' into fix/24509912-wellOperations-date-difference

This commit is contained in:
Olga Nemt 2024-01-25 10:42:18 +05:00
commit eaa10e129a
12 changed files with 121 additions and 351 deletions

View File

@ -53,7 +53,7 @@
<PackageReference Include="AsbSaubReportLas" Version="3.24.116.510" />
<PackageReference Include="AsbSaubReportPdf" Version="3.24.116.510" />
<PackageReference Include="CliWrap" Version="3.6.6" />
<PackageReference Include="ClosedXML" Version="0.96.0" />
<PackageReference Include="ClosedXML" Version="0.97.0" />
<PackageReference Include="itext7" Version="8.0.2" />
<PackageReference Include="Mapster" Version="7.4.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Extensions" Version="2.1.21" />

View File

@ -8,7 +8,6 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Bogus" Version="35.4.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.26" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Refit" Version="7.0.0" />

View File

@ -1,23 +1,18 @@
using System.Net.Http.Headers;
using System.Text.Json;
using AsbCloudDb.Model;
using AsbCloudWebApi.IntegrationTests.Clients;
using Microsoft.Extensions.DependencyInjection;
using Refit;
using Xunit;
namespace AsbCloudWebApi.IntegrationTests;
public abstract class BaseIntegrationTest : IClassFixture<WebAppFactoryFixture>
{
protected readonly IServiceScope scope;
private readonly IServiceScope scope;
protected readonly IAsbCloudDbContext dbContext;
protected readonly WebAppFactoryFixture factory;
protected BaseIntegrationTest(WebAppFactoryFixture factory)
protected BaseIntegrationTest(WebAppFactoryFixture factory)
{
scope = factory.Services.CreateScope();
dbContext = scope.ServiceProvider.GetRequiredService<IAsbCloudDbContext>();
this.factory = factory;
}
}
}

View File

@ -1,61 +1,58 @@
using System.Net;
using AsbCloudApp.Data;
using AsbCloudDb.Model;
using AsbCloudDb.Model.ProcessMaps;
using AsbCloudWebApi.IntegrationTests.Clients;
using AsbCloudWebApi.IntegrationTests.TestFakers;
using Mapster;
using Microsoft.EntityFrameworkCore;
using Xunit;
namespace AsbCloudWebApi.IntegrationTests.Controllers;
public class AdminDepositControllerTests : BaseIntegrationTest
public class AdminDepositControllerTest : BaseIntegrationTest
{
protected IAdminDepositClient client;
private static readonly DepositBaseDto dto = new()
{
Caption = "test",
Latitude = 90,
Longitude = 100,
Timezone = new SimpleTimezoneDto
{
Hours = 1
}
};
public AdminDepositControllerTests(WebAppFactoryFixture factory)
: base(factory)
{
client = factory.GetAuthorizedHttpClient<IAdminDepositClient>();
}
private readonly IAdminDepositClient client;
public AdminDepositControllerTest(WebAppFactoryFixture factory)
: base(factory)
{
client = factory.GetAuthorizedHttpClient<IAdminDepositClient>();
dbContext.CleanupDbSet<Deposit>();
}
[Fact]
public async Task InsertAsync_ReturnsSuccess_WhenNewItemIsValid()
public async Task Insert_returns_success()
{
//arrange
var dbset = dbContext.Set<Deposit>();
dbset.RemoveRange(dbset);
dbContext.SaveChanges();
var expectedDto = EntitiesFaker.Deposit.Generate().Adapt<DepositBaseDto>();
//act
var response = await client.InsertAsync(expectedDto);
var response = await client.InsertAsync(dto);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.True(response.Content > 0);
var entity = await dbContext.Deposits.FirstOrDefaultAsync(d => d.Id == response.Content);
var actualDto = entity?.Adapt<DepositBaseDto>();
var deposit = entity?.Adapt<DepositBaseDto>();
var excludeProps = new[] { nameof(DepositBaseDto.Id) };
MatchHelper.Match(expectedDto, actualDto, excludeProps);
MatchHelper.Match(dto, deposit, excludeProps);
}
[Fact]
public async Task InsertRangeAsync_ReturnsSuccess_WhenAllNewItemsIsValid()
public async Task InsertRange_returns_success()
{
//arrange
var dbset = dbContext.Set<Deposit>();
dbset.RemoveRange(dbset);
dbContext.SaveChanges();
var dto = EntitiesFaker.Deposit.Generate().Adapt<DepositBaseDto>();
//act
var responce = await client.InsertRangeAsync(new[] { dto });
//act
var responce = await client.InsertRangeAsync(new[] { dto });
//assert
Assert.Equal(HttpStatusCode.OK, responce.StatusCode);
@ -69,23 +66,22 @@ public class AdminDepositControllerTests : BaseIntegrationTest
}
[Fact]
public async Task UpdateAsync_ReturnsBadRequest_WhenUpdatedItemHasInvalidId()
public async Task Update_returns_BadRequest_for_IdDeposit()
{
//arrange
var dto = EntitiesFaker.Deposit.Generate().Adapt<DepositBaseDto>();
var dtoBad = dto.Adapt<DepositBaseDto>();
//act
var response = await client.UpdateAsync(dto);
var response = await client.UpdateAsync(dtoBad);
//assert
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
}
[Fact]
public async Task UpdateAsync_ReturnsSuccess_WhenUpdatedItemIsValid()
public async Task Update_returns_success()
{
//arrange
var dto = EntitiesFaker.Deposit.Generate().Adapt<DepositBaseDto>();
var insertResponse = await client.InsertAsync(dto);
dto.Id = insertResponse.Content;
@ -112,38 +108,44 @@ public class AdminDepositControllerTests : BaseIntegrationTest
}
[Fact]
public async Task GetOrDefaultAsync_ReturnsSuccess_WhenIdIsValid()
public async Task GetOrDefault_returns_success()
{
//arrange
var entity = await dbContext.Deposits.FirstAsync();
var expected = entity.Adapt<DepositBaseDto>();
var insertResponse = await client.InsertAsync(dto);
var id = insertResponse.Content;
//act
var response = await client.GetOrDefaultAsync(entity.Id);
//act
var response = await client.GetOrDefaultAsync(id);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
MatchHelper.Match(expected, response.Content);
var deposit = response.Content;
var excludeProps = new[] { nameof(DepositBaseDto.Id) };
MatchHelper.Match(dto, deposit, excludeProps);
}
[Fact]
public async Task GetOrDefaultAsync_ReturnsNoContent_WhenIdIsInvalid()
public async Task GetOrDefault_returns_NoContent_for_IdDeposit()
{
//arrange
const int id = 0;
const int idInvalid = 0;
//act
var responce = await client.GetOrDefaultAsync(id);
var responce = await client.GetOrDefaultAsync(idInvalid);
//assert
Assert.Equal(HttpStatusCode.NoContent, responce.StatusCode);
}
[Fact]
public async Task GetAllAsync_ReturnsSuccess()
public async Task GetAll_returns_success()
{
//arrange
await client.InsertAsync(dto);
//act
var response = await client.GetAllAsync();
@ -154,18 +156,15 @@ public class AdminDepositControllerTests : BaseIntegrationTest
var expectedCount = await dbContext.Deposits.CountAsync();
Assert.Equal(expectedCount, response.Content.Count());
var entity = await dbContext.Deposits.FirstOrDefaultAsync();
var dto = entity?.Adapt<DepositBaseDto>();
MatchHelper.Match(dto, response.Content.FirstOrDefault());
var deposit = response.Content.First();
var excludeProps = new[] { nameof(DepositBaseDto.Id) };
MatchHelper.Match(dto, deposit, excludeProps);
}
[Fact]
public async Task DeleteAsync_ReturnsSuccess_WhenIdIsValid()
public async Task Delete_returns_success()
{
//arrange
var dto = EntitiesFaker.Deposit.Generate().Adapt<DepositBaseDto>();
var insertResponse = await client.InsertAsync(dto);
var id = insertResponse.Content;
@ -181,13 +180,13 @@ public class AdminDepositControllerTests : BaseIntegrationTest
}
[Fact]
public async Task DeleteAsync_ReturnsNoContent_WhenIdIsInvalid()
public async Task Delete_returns_NoContent_IdDeposit()
{
//arrange
const int id = 0;
const int idInvalid = 0;
//act
var response = await client.DeleteAsync(id);
var response = await client.DeleteAsync(idInvalid);
//assert
Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);

View File

@ -77,6 +77,7 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
public ProcessMapPlanDrillingControllerTest(WebAppFactoryFixture factory) : base(factory)
{
dbContext.CleanupDbSet<ProcessMapPlanDrilling>();
client = factory.GetAuthorizedHttpClient<IProcessMapPlanDrillingClient>();
}
@ -84,9 +85,6 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
public async Task InsertRange_returns_success()
{
//arrange
var dbset = dbContext.Set<ProcessMapPlanDrilling>();
dbset.RemoveRange(dbset);
dbContext.SaveChanges();
var expected = dto.Adapt<ProcessMapPlanDrillingDto>();
//act
@ -96,12 +94,12 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(1, response.Content);
var entity = dbContext.Set<ProcessMapPlanDrilling>()
var entity = dbContext
.Set<ProcessMapPlanDrilling>()
.Where(p => p.AxialLoadPlan == dto.AxialLoadPlan)
.Where(p => p.AxialLoadLimitMax == dto.AxialLoadLimitMax)
.Where(p => p.Comment == dto.Comment)
.Where(p => p.IdWell == dto.IdWell)
.FirstOrDefault();
.FirstOrDefault(p => p.IdWell == dto.IdWell);
Assert.NotNull(entity);
@ -121,9 +119,6 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
public async Task InsertRange_returns_BadRequest_for_IdWellSectionType()
{
//arrange
var dbset = dbContext.Set<ProcessMapPlanDrilling>();
dbset.RemoveRange(dbset);
dbContext.SaveChanges();
var badDto = dto.Adapt<ProcessMapPlanDrillingDto>();
badDto.IdWellSectionType = int.MaxValue;
@ -138,9 +133,6 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
public async Task InsertRange_returns_BadRequest_for_IdMode()
{
//arrange
var dbset = dbContext.Set<ProcessMapPlanDrilling>();
dbset.RemoveRange(dbset);
dbContext.SaveChanges();
var badDto = dto.Adapt<ProcessMapPlanDrillingDto>();
badDto.IdMode = int.MaxValue;
@ -155,9 +147,6 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
public async Task InsertRange_returns_BadRequest_for_IdWell()
{
//arrange
var dbset = dbContext.Set<ProcessMapPlanDrilling>();
dbset.RemoveRange(dbset);
dbContext.SaveChanges();
var badDto = dto.Adapt<ProcessMapPlanDrillingDto>();
badDto.IdWell = int.MaxValue;
@ -173,13 +162,13 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
{
// arrange
var dbset = dbContext.Set<ProcessMapPlanDrilling>();
var startTime = DateTimeOffset.UtcNow;
dbset.RemoveRange(dbset);
dbContext.SaveChanges();
var entry = dbset.Add(entity);
dbContext.SaveChanges();
entry.State = EntityState.Detached;
var startTime = DateTimeOffset.UtcNow;
// act
var result = await client.ClearAndInsertRange(entity.IdWell, new ProcessMapPlanDrillingDto[] { dto });
@ -210,10 +199,10 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
public async Task UpdateRange_returns_success()
{
// arrange
var dbset = dbContext.Set<ProcessMapPlanDrilling>();
var startTime = DateTimeOffset.UtcNow;
dbset.RemoveRange(dbset);
dbContext.SaveChanges();
var dbset = dbContext.Set<ProcessMapPlanDrilling>();
var entry = dbset.Add(entity);
dbContext.SaveChanges();
entry.State = EntityState.Detached;
@ -274,13 +263,13 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
{
//arrange
var dbset = dbContext.Set<ProcessMapPlanDrilling>();
var startTime = DateTimeOffset.UtcNow;
dbset.RemoveRange(dbset);
dbContext.SaveChanges();
var entry = dbset.Add(entity);
dbContext.SaveChanges();
entry.State = EntityState.Detached;
var startTime = DateTimeOffset.UtcNow;
//act
var response = await client.DeleteRange(dto.IdWell, new[] { entry.Entity.Id });
@ -289,9 +278,9 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(1, response.Content);
var actual = dbContext.Set<ProcessMapPlanDrilling>()
.Where(p => p.Id == entry.Entity.Id)
.FirstOrDefault();
var actual = dbContext
.Set<ProcessMapPlanDrilling>()
.FirstOrDefault(p => p.Id == entry.Entity.Id);
Assert.NotNull(actual);
Assert.Equal(ProcessMapPlanBase.IdStateDeleted, actual.IdState);
@ -305,8 +294,7 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
{
//arrange
var dbset = dbContext.Set<ProcessMapPlanDrilling>();
dbset.RemoveRange(dbset);
dbContext.SaveChanges();
var entity2 = entity.Adapt<ProcessMapPlanDrilling>();
entity2.Creation = entity.Creation.AddDays(1);
dbset.Add(entity);
@ -333,8 +321,6 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
{
//arrange
var dbset = dbContext.Set<ProcessMapPlanDrilling>();
dbset.RemoveRange(dbset);
dbContext.SaveChanges();
dbset.Add(entity);
@ -346,9 +332,6 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
dbset.Add(entityDeleted);
dbContext.SaveChanges();
var timezoneHours = Data.Defaults.Wells[0].Timezone.Hours;
var offset = TimeSpan.FromHours(timezoneHours);
//act
var request = new ProcessMapPlanBaseRequest();
@ -365,9 +348,7 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
{
//arrange
var dbset = dbContext.Set<ProcessMapPlanDrilling>();
dbset.RemoveRange(dbset);
dbContext.SaveChanges();
dbset.Add(entity);
var entityDeleted = entity.Adapt<ProcessMapPlanDrilling>();
@ -380,9 +361,6 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
dbContext.SaveChanges();
var timezoneHours = Data.Defaults.Wells[0].Timezone.Hours;
var offset = TimeSpan.FromHours(timezoneHours);
//act
var request = new ProcessMapPlanBaseRequest {
Moment = new DateTimeOffset(3000, 1, 1, 0, 0, 0, 0, TimeSpan.Zero)
@ -410,8 +388,7 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
{
//arrange
var dbset = dbContext.Set<ProcessMapPlanDrilling>();
dbset.RemoveRange(dbset);
dbContext.SaveChanges();
var now = DateTimeOffset.UtcNow;
var entityDeleted = entity.Adapt<ProcessMapPlanDrilling>();
entityDeleted.Creation = now;
@ -431,9 +408,6 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
dbContext.SaveChanges();
var timezoneHours = Data.Defaults.Wells[0].Timezone.Hours;
var offset = TimeSpan.FromHours(timezoneHours);
//act
var request = new ProcessMapPlanBaseRequest
{
@ -452,9 +426,7 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
{
//arrange
var dbset = dbContext.Set<ProcessMapPlanDrilling>();
dbset.RemoveRange(dbset);
dbContext.SaveChanges();
dbset.Add(entity);
var entity2 = entity.Adapt<ProcessMapPlanDrilling>();
@ -464,9 +436,6 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
dbContext.SaveChanges();
var timezoneHours = Data.Defaults.Wells[0].Timezone.Hours;
var offset = TimeSpan.FromHours(timezoneHours);
//act
var request = new ProcessMapPlanBaseRequest
{
@ -495,9 +464,7 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
{
//arrange
var dbset = dbContext.Set<ProcessMapPlanDrilling>();
dbset.RemoveRange(dbset);
dbContext.SaveChanges();
dbset.Add(entity);
var entity2 = entity.Adapt<ProcessMapPlanDrilling>();
@ -529,8 +496,7 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
Assert.Equal(2, response.Content.Count());
var actual = response.Content
.Where(p=>p.Comment == entity2.Comment)
.First();
.First(p => p.Comment == entity2.Comment);
var expected = entity2.Adapt<ProcessMapPlanDrillingDto>();
var excludeProps = new[] {

View File

@ -46,23 +46,23 @@ public class SlipsStatControllerTest : BaseIntegrationTest
DurationHours = 1
};
private readonly ISlipsTimeClient slipsTimeClient;
private readonly ISlipsTimeClient client;
public SlipsStatControllerTest(WebAppFactoryFixture factory)
: base(factory)
{
slipsTimeClient = factory.GetAuthorizedHttpClient<ISlipsTimeClient>();
dbContext.Schedule.Add(schedule);
dbContext.DetectedOperations.Add(detectedOperation);
dbContext.WellOperations.Add(factWellOperation);
dbContext.SaveChanges();
client = factory.GetAuthorizedHttpClient<ISlipsTimeClient>();
}
[Fact]
public async Task GetAll_returns_success()
{
//arrange
dbContext.Schedule.Add(schedule);
dbContext.DetectedOperations.Add(detectedOperation);
dbContext.WellOperations.Add(factWellOperation);
dbContext.SaveChanges();
var request = new OperationStatRequest
{
DateStartUTC = schedule.DrillStart.DateTime,
@ -82,13 +82,13 @@ public class SlipsStatControllerTest : BaseIntegrationTest
};
//act
var response = await slipsTimeClient.GetAll(request);
var response = await client.GetAll(request);
//assert
Assert.NotNull(response.Content);
Assert.Single(response.Content);
var dtoActual = response.Content.First();
MatchHelper.Match(dtoExpected, dtoActual);
var slipsStat = response.Content.First();
MatchHelper.Match(dtoExpected, slipsStat);
}
}

View File

@ -22,9 +22,7 @@ namespace AsbCloudWebApi.IntegrationTests.Data
Caption = "Deposit1",
Latitude = 10,
Longitude = 20,
Timezone = new SimpleTimezone{
Hours = 1
}
Timezone = GetTimezone()
}
};
@ -36,9 +34,7 @@ namespace AsbCloudWebApi.IntegrationTests.Data
Caption = "Cluster1",
Latitude = 10,
Longitude = 20,
Timezone = new SimpleTimezone{
Hours = 1
}
Timezone = GetTimezone()
}
};
@ -48,10 +44,7 @@ namespace AsbCloudWebApi.IntegrationTests.Data
{
Id = 1,
RemoteUid = "555-555-555",
TimeZone = new SimpleTimezone
{
Hours = 1
}
TimeZone = GetTimezone()
}
};
@ -66,15 +59,13 @@ namespace AsbCloudWebApi.IntegrationTests.Data
Latitude = 10,
Longitude = 20,
IdTelemetry = Telemetries[0].Id,
Timezone = new SimpleTimezone{
Hours = 1
}
Timezone = GetTimezone()
}
};
public static RelationCompanyWell[] RelationsCompanyWell = new RelationCompanyWell[]
{
new(){IdCompany= 1, IdWell = Wells[0].Id},
new() { IdCompany = 1, IdWell = Wells[0].Id },
};
public static RelationUserUserRole[] RelationsUserUserRole = new RelationUserUserRole[]
@ -82,5 +73,10 @@ namespace AsbCloudWebApi.IntegrationTests.Data
new(){ IdUserRole= 1, IdUser = 1}
};
private static SimpleTimezone GetTimezone() =>
new ()
{
Hours = 1
};
}
}

View File

@ -0,0 +1,14 @@
using AsbCloudDb.Model;
namespace AsbCloudWebApi.IntegrationTests;
public static class EFCoreExtensions
{
public static void CleanupDbSet<T>(this IAsbCloudDbContext dbContext)
where T : class
{
var dbset = dbContext.Set<T>();
dbset.RemoveRange(dbset);
dbContext.SaveChanges();
}
}

View File

@ -1,160 +0,0 @@
using AsbCloudApp.Data;
using AsbCloudApp.Data.SAUB;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Xunit;
namespace AsbCloudWebApi.IntegrationTests.Middlware
{
//TODO: переписать как интеграционный тест. Использовать WebApplicationFactory.
public class UserConnectionsLimitMiddlwareTest
{
const int iterations2Block = 8;
private readonly (int, DateTime, DateTime)[] wells = new[]
{
(191, new DateTime(2022, 09, 01, 21, 43, 00, DateTimeKind.Utc), new DateTime(2022, 09, 04, 07, 37, 31, DateTimeKind.Utc)),
(3 , new DateTime(2021, 09, 16, 06, 13, 33, DateTimeKind.Utc), new DateTime(2021, 09, 20, 00, 29, 28, DateTimeKind.Utc)),
(199, new DateTime(2022, 09, 15, 11, 27, 18, DateTimeKind.Utc), new DateTime(2022, 09, 20, 14, 00, 23, DateTimeKind.Utc)),
(6 , new DateTime(2021, 09, 20, 00, 35, 03, DateTimeKind.Utc), new DateTime(2021, 09, 25, 06, 46, 17, DateTimeKind.Utc)),
(41 , new DateTime(2021, 12, 10, 00, 59, 52, DateTimeKind.Utc), new DateTime(2022, 10, 31, 15, 29, 24, DateTimeKind.Utc)),
(100, new DateTime(2022, 04, 24, 03, 04, 05, DateTimeKind.Utc), new DateTime(2022, 04, 29, 11, 38, 36, DateTimeKind.Utc)),
(154, new DateTime(2022, 03, 28, 10, 09, 14, DateTimeKind.Utc), new DateTime(2022, 06, 14, 15, 01, 12, DateTimeKind.Utc)),
(5 , new DateTime(2021, 09, 25, 08, 09, 37, DateTimeKind.Utc), new DateTime(2021, 10, 01, 14, 39, 51, DateTimeKind.Utc)),
(1 , new DateTime(2021, 09, 10, 01, 32, 42, DateTimeKind.Utc), new DateTime(2021, 09, 18, 00, 35, 22, DateTimeKind.Utc)),
(112, new DateTime(2022, 04, 20, 16, 47, 51, DateTimeKind.Utc), new DateTime(2022, 04, 28, 15, 04, 33, DateTimeKind.Utc)),
};
public class TelemetryDataSaubService : ITelemetryDataSaubService
{
public async Task<IEnumerable<TelemetryDataSaubDto>> GetAsync(int idWell, DateTime dateBegin = default, double intervalSec = 600, int approxPointsCount = 1024, CancellationToken token = default)
{
await Task.Delay(1000, token);
return Enumerable.Empty<TelemetryDataSaubDto>();
}
public Task<IEnumerable<TelemetryDataSaubDto>> GetAsync(int idWell, TelemetryDataRequest request, CancellationToken token)
{
throw new NotImplementedException();
}
public DatesRangeDto? GetRange(int idWell, DateTimeOffset start, DateTimeOffset end)
{
throw new NotImplementedException();
}
public DatesRangeDto? GetRange(int idWell)
{
throw new NotImplementedException();
}
public Task<DatesRangeDto?> GetRangeAsync(int idWell, DateTimeOffset start, DateTimeOffset end, CancellationToken token)
{
throw new NotImplementedException();
}
public Task<DatesRangeDto?> GetRangeAsync(int idWell, DateTimeOffset geDate, DateTimeOffset? leDate, CancellationToken token)
{
throw new NotImplementedException();
}
public Task<IEnumerable<TelemetryDataSaubStatDto>> GetTelemetryDataStatAsync(int idTelemetry, CancellationToken token) => throw new NotImplementedException();
public Task<Stream> GetZippedCsv(int idWell, DateTime beginDate, DateTime endDate, CancellationToken token)
{
throw new NotImplementedException();
}
public Task<int> UpdateDataAsync(string uid, IEnumerable<TelemetryDataSaubDto> dtos, CancellationToken token) => throw new NotImplementedException();
}
public UserConnectionsLimitMiddlwareTest()
{
var host = Host.CreateDefaultBuilder(Array.Empty<string>())
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.ConfigureServices(serviceCollection =>
{
object value = ReplaceService<ITelemetryDataSaubService>(serviceCollection, new TelemetryDataSaubService());
});
})
.Build();
host.Start();
}
[Fact]
public async Task Send_n_requests_and_get_blocked()
{
var i = 0;
for (; i < iterations2Block; i++)
_ = Task.Run(async () =>
{
var well = wells[i];
var url = MakeUrl(well.Item1, well.Item2, well.Item3);
var response = await MakeHttpClient().GetAsync(url);
await Task.Delay(1000);
});
var well = wells[i];
var url = MakeUrl(well.Item1, well.Item2, well.Item3);
var response = await MakeHttpClient().GetAsync(url);
Assert.Equal(System.Net.HttpStatusCode.TooManyRequests, response.StatusCode);
}
[Fact]
public async Task Send_n_requests_and_get_blocked_then_restored()
{
var i = 0;
var tasks = new Task[iterations2Block];
for (; i < iterations2Block; i++)
tasks[i] = Task.Run(async () =>
{
var well = wells[i];
var url = MakeUrl(well.Item1, well.Item2, well.Item3);
var response = await MakeHttpClient().GetAsync(url);
await Task.Delay(1000);
});
var well = wells[i];
var url = MakeUrl(well.Item1, well.Item2, well.Item3);
var response = await MakeHttpClient().GetAsync(url);
Assert.Equal(System.Net.HttpStatusCode.TooManyRequests, response.StatusCode);
Task.WaitAll(tasks);
response = await MakeHttpClient().GetAsync(url);
Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);
}
private static string MakeUrl(int idWell, DateTime dateBegin, DateTime dateEnd)
{
var interval = (dateEnd - dateBegin).TotalSeconds;
var dateBeginString = dateBegin.ToString("yyyy-MM-ddZ");
var url = $"http://127.0.0.1:5000/api/TelemetryDataSaub/{idWell}?begin={dateBeginString}&intervalSec={interval}&approxPointsCount={interval}";
return url;
}
private static HttpClient MakeHttpClient()
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new("Bearer", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjEiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiZGV2IiwiaWRDb21wYW55IjoiMSIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6InJvb3QiLCJuYmYiOjE2NjY1ODY2MjAsImV4cCI6MTY5ODE0NDIyMCwiaXNzIjoiYSIsImF1ZCI6ImEifQ.zqBdR4nYB87-Xyzv025waasN47i43c9FJ23RfzIvUsM");
return httpClient;
}
private static IServiceCollection ReplaceService<T>(IServiceCollection services, T instance)
where T : notnull
{
var typeofT = typeof(T);
var originalDecriptor = services.Last(s => s.ServiceType == typeofT);
var newDecriptor = new ServiceDescriptor(typeofT, instance);
services.Remove(originalDecriptor);
services.Add(newDecriptor);
return services;
}
}
}

View File

@ -1,41 +0,0 @@
using AsbCloudDb.Model;
using Bogus;
namespace AsbCloudWebApi.IntegrationTests.TestFakers;
//TODO: выпилить
public static class EntitiesFaker
{
public static Faker<Deposit> Deposit { get; } = new Faker<Deposit>()
.RuleFor(d => d.Id, 0)
.RuleFor(d => d.Caption, f => f.Random.String2(1, 50))
.RuleFor(d => d.Latitude, f => f.Random.Double(-90, 90))
.RuleFor(d => d.Longitude, f => f.Random.Double(-180, 180))
.RuleFor(d => d.Timezone, f => new SimpleTimezone
{
Hours = f.Random.Int(1, 12),
IsOverride = f.Random.Bool()
});
public static Faker<Cluster> Cluster { get; } = new Faker<Cluster>()
.RuleFor(d => d.Id, 0)
.RuleFor(d => d.Caption, f => f.Random.String2(1, 50))
.RuleFor(d => d.Latitude, f => f.Random.Double(-90, 90))
.RuleFor(d => d.Longitude, f => f.Random.Double(-180, 180))
.RuleFor(d => d.Timezone, f => new SimpleTimezone
{
Hours = f.Random.Int(1, 12),
IsOverride = f.Random.Bool()
});
public static Faker<Well> Well { get; } = new Faker<Well>()
.RuleFor(d => d.Id, 0)
.RuleFor(d => d.Caption, f => f.Random.String2(1, 50))
.RuleFor(d => d.Latitude, f => f.Random.Double(-90, 90))
.RuleFor(d => d.Longitude, f => f.Random.Double(-180, 180))
.RuleFor(d => d.Timezone, f => new SimpleTimezone
{
Hours = f.Random.Int(1, 12),
IsOverride = f.Random.Bool()
});
}

View File

@ -1,12 +1,10 @@
using AsbCloudDb;
using AsbCloudDb.Model;
using AsbCloudWebApi.IntegrationTests.TestFakers;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ProtoBuf.Serializers;
using Refit;
using System.Net.Http.Headers;
using System.Text.Json;

View File

@ -32,4 +32,8 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<RuntimeHostConfigurationOption Include="System.Drawing.EnableUnixSupport" Value="true" />
</ItemGroup>
</Project>