diff --git a/AsbCloudInfrastructure/AsbCloudInfrastructure.csproj b/AsbCloudInfrastructure/AsbCloudInfrastructure.csproj
index dbaa1e65..c607d523 100644
--- a/AsbCloudInfrastructure/AsbCloudInfrastructure.csproj
+++ b/AsbCloudInfrastructure/AsbCloudInfrastructure.csproj
@@ -53,7 +53,7 @@
-
+
diff --git a/AsbCloudWebApi.IntegrationTests/AsbCloudWebApi.IntegrationTests.csproj b/AsbCloudWebApi.IntegrationTests/AsbCloudWebApi.IntegrationTests.csproj
index 5946859b..f20fc316 100644
--- a/AsbCloudWebApi.IntegrationTests/AsbCloudWebApi.IntegrationTests.csproj
+++ b/AsbCloudWebApi.IntegrationTests/AsbCloudWebApi.IntegrationTests.csproj
@@ -8,7 +8,6 @@
-
diff --git a/AsbCloudWebApi.IntegrationTests/BaseIntegrationTest.cs b/AsbCloudWebApi.IntegrationTests/BaseIntegrationTest.cs
index a31e8500..d18c54bf 100644
--- a/AsbCloudWebApi.IntegrationTests/BaseIntegrationTest.cs
+++ b/AsbCloudWebApi.IntegrationTests/BaseIntegrationTest.cs
@@ -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
{
- 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();
- this.factory = factory;
- }
+ }
}
\ No newline at end of file
diff --git a/AsbCloudWebApi.IntegrationTests/Controllers/AdminDepositControllerTests.cs b/AsbCloudWebApi.IntegrationTests/Controllers/AdminDepositControllerTest.cs
similarity index 51%
rename from AsbCloudWebApi.IntegrationTests/Controllers/AdminDepositControllerTests.cs
rename to AsbCloudWebApi.IntegrationTests/Controllers/AdminDepositControllerTest.cs
index 8fd089bf..1d2a2c3c 100644
--- a/AsbCloudWebApi.IntegrationTests/Controllers/AdminDepositControllerTests.cs
+++ b/AsbCloudWebApi.IntegrationTests/Controllers/AdminDepositControllerTest.cs
@@ -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();
- }
+ private readonly IAdminDepositClient client;
+
+ public AdminDepositControllerTest(WebAppFactoryFixture factory)
+ : base(factory)
+ {
+ client = factory.GetAuthorizedHttpClient();
+
+ dbContext.CleanupDbSet();
+ }
[Fact]
- public async Task InsertAsync_ReturnsSuccess_WhenNewItemIsValid()
+ public async Task Insert_returns_success()
{
- //arrange
- var dbset = dbContext.Set();
- dbset.RemoveRange(dbset);
- dbContext.SaveChanges();
-
- var expectedDto = EntitiesFaker.Deposit.Generate().Adapt();
-
//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();
+ var deposit = entity?.Adapt();
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();
- dbset.RemoveRange(dbset);
- dbContext.SaveChanges();
-
- var dto = EntitiesFaker.Deposit.Generate().Adapt();
-
- //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();
+ var dtoBad = dto.Adapt();
//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();
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();
+ 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();
-
- 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();
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);
diff --git a/AsbCloudWebApi.IntegrationTests/Controllers/ProcessMapPlanDrillingControllerTest.cs b/AsbCloudWebApi.IntegrationTests/Controllers/ProcessMapPlanDrillingControllerTest.cs
index 7c53db1a..822b582a 100644
--- a/AsbCloudWebApi.IntegrationTests/Controllers/ProcessMapPlanDrillingControllerTest.cs
+++ b/AsbCloudWebApi.IntegrationTests/Controllers/ProcessMapPlanDrillingControllerTest.cs
@@ -77,6 +77,7 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
public ProcessMapPlanDrillingControllerTest(WebAppFactoryFixture factory) : base(factory)
{
+ dbContext.CleanupDbSet();
client = factory.GetAuthorizedHttpClient();
}
@@ -84,9 +85,6 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
public async Task InsertRange_returns_success()
{
//arrange
- var dbset = dbContext.Set();
- dbset.RemoveRange(dbset);
- dbContext.SaveChanges();
var expected = dto.Adapt();
//act
@@ -96,12 +94,12 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(1, response.Content);
- var entity = dbContext.Set()
+ var entity = dbContext
+ .Set()
.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();
- dbset.RemoveRange(dbset);
- dbContext.SaveChanges();
var badDto = dto.Adapt();
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();
- dbset.RemoveRange(dbset);
- dbContext.SaveChanges();
var badDto = dto.Adapt();
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();
- dbset.RemoveRange(dbset);
- dbContext.SaveChanges();
var badDto = dto.Adapt();
badDto.IdWell = int.MaxValue;
@@ -173,13 +162,13 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
{
// arrange
var dbset = dbContext.Set();
- 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();
var startTime = DateTimeOffset.UtcNow;
- dbset.RemoveRange(dbset);
- dbContext.SaveChanges();
+
+ var dbset = dbContext.Set();
+
var entry = dbset.Add(entity);
dbContext.SaveChanges();
entry.State = EntityState.Detached;
@@ -274,13 +263,13 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
{
//arrange
var dbset = dbContext.Set();
- 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()
- .Where(p => p.Id == entry.Entity.Id)
- .FirstOrDefault();
+ var actual = dbContext
+ .Set()
+ .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();
- dbset.RemoveRange(dbset);
- dbContext.SaveChanges();
+
var entity2 = entity.Adapt();
entity2.Creation = entity.Creation.AddDays(1);
dbset.Add(entity);
@@ -333,8 +321,6 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
{
//arrange
var dbset = dbContext.Set();
- 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();
- dbset.RemoveRange(dbset);
- dbContext.SaveChanges();
-
+
dbset.Add(entity);
var entityDeleted = entity.Adapt();
@@ -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();
- dbset.RemoveRange(dbset);
- dbContext.SaveChanges();
+
var now = DateTimeOffset.UtcNow;
var entityDeleted = entity.Adapt();
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();
- dbset.RemoveRange(dbset);
- dbContext.SaveChanges();
-
+
dbset.Add(entity);
var entity2 = entity.Adapt();
@@ -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();
- dbset.RemoveRange(dbset);
- dbContext.SaveChanges();
-
+
dbset.Add(entity);
var entity2 = entity.Adapt();
@@ -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();
var excludeProps = new[] {
diff --git a/AsbCloudWebApi.IntegrationTests/Controllers/SlipsStatControllerTest.cs b/AsbCloudWebApi.IntegrationTests/Controllers/SlipsStatControllerTest.cs
index 4c48fdc3..a31c5f19 100644
--- a/AsbCloudWebApi.IntegrationTests/Controllers/SlipsStatControllerTest.cs
+++ b/AsbCloudWebApi.IntegrationTests/Controllers/SlipsStatControllerTest.cs
@@ -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();
+ dbContext.Schedule.Add(schedule);
+ dbContext.DetectedOperations.Add(detectedOperation);
+ dbContext.WellOperations.Add(factWellOperation);
+ dbContext.SaveChanges();
+
+ client = factory.GetAuthorizedHttpClient();
}
[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);
}
}
\ No newline at end of file
diff --git a/AsbCloudWebApi.IntegrationTests/Data/Defaults.cs b/AsbCloudWebApi.IntegrationTests/Data/Defaults.cs
index b4c6cafa..ddb7dbe7 100644
--- a/AsbCloudWebApi.IntegrationTests/Data/Defaults.cs
+++ b/AsbCloudWebApi.IntegrationTests/Data/Defaults.cs
@@ -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
+ };
}
}
diff --git a/AsbCloudWebApi.IntegrationTests/EFCoreExtensions.cs b/AsbCloudWebApi.IntegrationTests/EFCoreExtensions.cs
new file mode 100644
index 00000000..422c9362
--- /dev/null
+++ b/AsbCloudWebApi.IntegrationTests/EFCoreExtensions.cs
@@ -0,0 +1,14 @@
+using AsbCloudDb.Model;
+
+namespace AsbCloudWebApi.IntegrationTests;
+
+public static class EFCoreExtensions
+{
+ public static void CleanupDbSet(this IAsbCloudDbContext dbContext)
+ where T : class
+ {
+ var dbset = dbContext.Set();
+ dbset.RemoveRange(dbset);
+ dbContext.SaveChanges();
+ }
+}
\ No newline at end of file
diff --git a/AsbCloudWebApi.IntegrationTests/Middlware/UserConnectionsLimitMiddlwareTest.cs b/AsbCloudWebApi.IntegrationTests/Middlware/UserConnectionsLimitMiddlwareTest.cs
deleted file mode 100644
index ee1ebe1c..00000000
--- a/AsbCloudWebApi.IntegrationTests/Middlware/UserConnectionsLimitMiddlwareTest.cs
+++ /dev/null
@@ -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> GetAsync(int idWell, DateTime dateBegin = default, double intervalSec = 600, int approxPointsCount = 1024, CancellationToken token = default)
- {
- await Task.Delay(1000, token);
- return Enumerable.Empty();
- }
-
- public Task> 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 GetRangeAsync(int idWell, DateTimeOffset start, DateTimeOffset end, CancellationToken token)
- {
- throw new NotImplementedException();
- }
-
- public Task GetRangeAsync(int idWell, DateTimeOffset geDate, DateTimeOffset? leDate, CancellationToken token)
- {
- throw new NotImplementedException();
- }
-
- public Task> GetTelemetryDataStatAsync(int idTelemetry, CancellationToken token) => throw new NotImplementedException();
-
- public Task GetZippedCsv(int idWell, DateTime beginDate, DateTime endDate, CancellationToken token)
- {
- throw new NotImplementedException();
- }
-
- public Task UpdateDataAsync(string uid, IEnumerable dtos, CancellationToken token) => throw new NotImplementedException();
- }
-
- public UserConnectionsLimitMiddlwareTest()
- {
- var host = Host.CreateDefaultBuilder(Array.Empty())
- .ConfigureWebHostDefaults(webBuilder =>
- {
- webBuilder.UseStartup();
- webBuilder.ConfigureServices(serviceCollection =>
- {
- object value = ReplaceService(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(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;
- }
- }
-
-}
diff --git a/AsbCloudWebApi.IntegrationTests/TestFakers/EntitiesFaker.cs b/AsbCloudWebApi.IntegrationTests/TestFakers/EntitiesFaker.cs
deleted file mode 100644
index 8b39356b..00000000
--- a/AsbCloudWebApi.IntegrationTests/TestFakers/EntitiesFaker.cs
+++ /dev/null
@@ -1,41 +0,0 @@
-using AsbCloudDb.Model;
-using Bogus;
-
-namespace AsbCloudWebApi.IntegrationTests.TestFakers;
-
-//TODO: выпилить
-public static class EntitiesFaker
-{
- public static Faker Deposit { get; } = new Faker()
- .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 { get; } = new Faker()
- .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 { get; } = new Faker()
- .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()
- });
-}
\ No newline at end of file
diff --git a/AsbCloudWebApi.IntegrationTests/WebAppFactoryFixture.cs b/AsbCloudWebApi.IntegrationTests/WebAppFactoryFixture.cs
index 0bb7e26b..a0111303 100644
--- a/AsbCloudWebApi.IntegrationTests/WebAppFactoryFixture.cs
+++ b/AsbCloudWebApi.IntegrationTests/WebAppFactoryFixture.cs
@@ -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;
diff --git a/AsbCloudWebApi/AsbCloudWebApi.csproj b/AsbCloudWebApi/AsbCloudWebApi.csproj
index 05ca9173..d0485b9a 100644
--- a/AsbCloudWebApi/AsbCloudWebApi.csproj
+++ b/AsbCloudWebApi/AsbCloudWebApi.csproj
@@ -32,4 +32,8 @@
PreserveNewest
+
+
+
+