From 2b84cd5b8f6eb41920f1f4085460a7a06c966d73 Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Wed, 22 Feb 2023 15:00:43 +0500 Subject: [PATCH] Tests Add extension method to replace services in webhost --- AsbCloudWebApi.Tests/AspExtentions.cs | 19 ++++++++ .../UserConnectionsLimitMiddlwareTest.cs | 47 ++++++++++--------- 2 files changed, 43 insertions(+), 23 deletions(-) create mode 100644 AsbCloudWebApi.Tests/AspExtentions.cs diff --git a/AsbCloudWebApi.Tests/AspExtentions.cs b/AsbCloudWebApi.Tests/AspExtentions.cs new file mode 100644 index 00000000..62f0cbf7 --- /dev/null +++ b/AsbCloudWebApi.Tests/AspExtentions.cs @@ -0,0 +1,19 @@ +using Microsoft.Extensions.DependencyInjection; +using System.Linq; + +namespace AsbCloudWebApi.Tests +{ + public static class AspExtentions + { + public static IServiceCollection ReplaceService(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; + } + } +} diff --git a/AsbCloudWebApi.Tests/Middlware/UserConnectionsLimitMiddlwareTest.cs b/AsbCloudWebApi.Tests/Middlware/UserConnectionsLimitMiddlwareTest.cs index 9b2c99a7..7c2166be 100644 --- a/AsbCloudWebApi.Tests/Middlware/UserConnectionsLimitMiddlwareTest.cs +++ b/AsbCloudWebApi.Tests/Middlware/UserConnectionsLimitMiddlwareTest.cs @@ -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?> GetOrDefaultAsync(int idWell, DateTime dateBegin = default, double intervalSec = 600, int approxPointsCount = 1024, CancellationToken token = default) + { + await Task.Delay(1000, token); + return Enumerable.Empty(); + } + + public Task> GetTelemetryDataStatAsync(int idTelemetry, 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 => { + serviceCollection.ReplaceService(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); });