Tests Add extension method to replace services in webhost

This commit is contained in:
ngfrolov 2023-02-22 15:00:43 +05:00
parent 496f8d18fb
commit 2b84cd5b8f
Signed by untrusted user who does not match committer: ng.frolov
GPG Key ID: E99907A0357B29A7
2 changed files with 43 additions and 23 deletions

View File

@ -0,0 +1,19 @@
using Microsoft.Extensions.DependencyInjection;
using System.Linq;
namespace AsbCloudWebApi.Tests
{
public static class AspExtentions
{
public static IServiceCollection ReplaceService<T>(this 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,7 +1,14 @@
using Microsoft.AspNetCore.Hosting;
using AsbCloudApp.Data.SAUB;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
@ -11,27 +18,6 @@ namespace AsbCloudWebApi.Tests.Middlware
{
const int iterations2Block = 8;
//Данные в тестовой БД
//select
// tw.id,
// t_stat.minDate,
// t_stat.maxDate
//from(
// select
// id_telemetry,
// count(1) as count,
// min("date") as minDate,
// max("date") as maxDate
// from t_telemetry_data_saub
// group by id_telemetry
//) as t_stat
//join t_well tw on tw.id_telemetry = t_stat.id_telemetry
//where tw is not null
//order by t_stat.count
//limit 10;
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)),
@ -46,12 +32,28 @@ namespace AsbCloudWebApi.Tests.Middlware
(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>?> GetOrDefaultAsync(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<TelemetryDataSaubStatDto>> GetTelemetryDataStatAsync(int idTelemetry, 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 => {
serviceCollection.ReplaceService<ITelemetryDataSaubService>(new TelemetryDataSaubService());
});
})
.Build();
host.Start();
@ -67,7 +69,6 @@ namespace AsbCloudWebApi.Tests.Middlware
var well = wells[i];
var url = MakeUrl(well.Item1, well.Item2, well.Item3);
var response = await MakeHttpClient().GetAsync(url);
//await response.Content.ReadAsStringAsync();
await Task.Delay(1000);
});