From a6496a1af29ba013ed24a83fc1417cc765d38e93 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Tue, 10 Sep 2024 12:33:24 +0500 Subject: [PATCH] =?UTF-8?q?=D0=90=D0=B2=D1=82=D0=BE=D1=82=D0=B5=D1=81?= =?UTF-8?q?=D1=82=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5?= =?UTF-8?q?=D1=80=D0=BA=D0=B8=20=D1=80=D0=B0=D1=81=D1=87=D0=B5=D1=82=D0=B0?= =?UTF-8?q?=20=D1=81=D1=82=D0=B0=D1=82=D0=B8=D1=81=D1=82=D0=B8=D0=BA=D0=B8?= =?UTF-8?q?=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Requests/MessageRequest.cs | 8 + .../Services/MessageServiceTest.cs | 320 ++++++++++++++++++ 2 files changed, 328 insertions(+) create mode 100644 AsbCloudInfrastructure.Tests/Services/MessageServiceTest.cs diff --git a/AsbCloudApp/Requests/MessageRequest.cs b/AsbCloudApp/Requests/MessageRequest.cs index 0d05138a..861d091a 100644 --- a/AsbCloudApp/Requests/MessageRequest.cs +++ b/AsbCloudApp/Requests/MessageRequest.cs @@ -31,6 +31,14 @@ public class MessageRequestBase : RequestBase /// public string? SearchString { get; set; } + /// + /// + /// + public MessageRequestBase() + { + + } + /// /// копирующий конструктор /// diff --git a/AsbCloudInfrastructure.Tests/Services/MessageServiceTest.cs b/AsbCloudInfrastructure.Tests/Services/MessageServiceTest.cs new file mode 100644 index 00000000..955c2101 --- /dev/null +++ b/AsbCloudInfrastructure.Tests/Services/MessageServiceTest.cs @@ -0,0 +1,320 @@ +using AsbCloudApp.Data; +using AsbCloudApp.Data.SAUB; +using AsbCloudApp.Repositories; +using AsbCloudApp.Requests; +using AsbCloudApp.Services; +using AsbCloudDb.Model; +using AsbCloudInfrastructure.Repository; +using AsbCloudInfrastructure.Services; +using AsbCloudInfrastructure.Services.SAUB; +using DocumentFormat.OpenXml.Office2010.ExcelAc; +using NSubstitute; +using Org.BouncyCastle.Asn1.Ocsp; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Xunit; + +namespace AsbCloudInfrastructure.Tests.Services; + +public class MessageServiceTest +{ + private static readonly List telemetries = new List() + { + new TelemetryBaseDto() + { + Id = 1, + TimeZone = new SimpleTimezoneDto() + { + Hours = 5 + } + }, + new TelemetryBaseDto() + { + Id = 2, + TimeZone = new SimpleTimezoneDto() + { + Hours = 5 + } + } + }; + + private static readonly List events = new List() + { + new TelemetryEventDto() + { + Id = 1, + IdCategory = 1, + MessageTemplate = " 1 1" + }, + new TelemetryEventDto() + { + Id = 2, + IdCategory = 1, + MessageTemplate = " 2 1" + }, + new TelemetryEventDto() + { + Id = 3, + IdCategory = 1, + MessageTemplate = " 3 1" + }, + new TelemetryEventDto() + { + Id = 4, + IdCategory = 2, + MessageTemplate = " 1 2" + }, + new TelemetryEventDto() + { + Id = 5, + IdCategory = 2, + MessageTemplate = " 2 2" + }, + new TelemetryEventDto() + { + Id = 6, + IdCategory = 3, + MessageTemplate = " 1 3" + } + }; + + /// + /// : + /// 1: + /// 1: + /// 1: + /// 1 + /// 2 + /// 3 + /// 2: + /// 1 + /// 2 + /// 3 + /// 3: + /// 1 + /// 2 + /// 3 + /// 2: + /// 2: + /// 1 + /// 2 + /// 3: + /// 1 + /// 2 + /// 2 + /// 3: + /// 3: + /// 1 + /// 2 + /// 3 + /// + /// + private static readonly List messages = new List() + { + new TelemetryMessageDto() + { + Id = 1, + IdEvent = 1, + IdTelemetry = 1 + }, + new TelemetryMessageDto() + { + Id = 2, + IdEvent = 1, + IdTelemetry = 1 + }, + new TelemetryMessageDto() + { + Id = 3, + IdEvent = 1, + IdTelemetry = 1 + }, + new TelemetryMessageDto() + { + Id = 4, + IdEvent = 2, + IdTelemetry = 1 + }, + new TelemetryMessageDto() + { + Id = 5, + IdEvent = 2, + IdTelemetry = 1 + }, + new TelemetryMessageDto() + { + Id = 6, + IdEvent = 2, + IdTelemetry = 1 + }, + new TelemetryMessageDto() + { + Id = 7, + IdEvent = 3, + IdTelemetry = 1 + }, + new TelemetryMessageDto() + { + Id = 8, + IdEvent = 3, + IdTelemetry = 1 + }, + new TelemetryMessageDto() + { + Id = 9, + IdEvent = 3, + IdTelemetry = 1 + }, + new TelemetryMessageDto() + { + Id = 10, + IdEvent = 4, + IdTelemetry = 1 + }, + new TelemetryMessageDto() + { + Id = 11, + IdEvent = 4, + IdTelemetry = 1 + }, + new TelemetryMessageDto() + { + Id = 12, + IdEvent = 5, + IdTelemetry = 1 + }, + new TelemetryMessageDto() + { + Id = 13, + IdEvent = 5, + IdTelemetry = 1 + }, + new TelemetryMessageDto() + { + Id = 14, + IdEvent = 6, + IdTelemetry = 2 + }, + new TelemetryMessageDto() + { + Id = 15, + IdEvent = 6, + IdTelemetry = 2 + }, + new TelemetryMessageDto() + { + Id = 16, + IdEvent = 6, + IdTelemetry = 2 + }, + }; + + + + private readonly IMessageService messageServiceMock = Substitute.For(); + private readonly IMessageRepository messageRepositoryMock = Substitute.For(); + private readonly IEventRepository eventRepositoryMock = Substitute.For(); + private readonly ITelemetryService telemetryServiceMock = Substitute.For(); + private readonly IAsbCloudDbContext db = Substitute.For(); + + public MessageServiceTest() + { + messageServiceMock = new MessageService(messageRepositoryMock, telemetryServiceMock, eventRepositoryMock); + + telemetryServiceMock + .GetOrDefaultTelemetriesByIdsWells(Arg.Any>()) + .Returns(telemetries); + + eventRepositoryMock + .Get(Arg.Any(), Arg.Any()) + .Returns(events); + + messageRepositoryMock + .GetMessagesAsync(Arg.Any(), Arg.Any()) + .Returns(messages); + } + + + [Fact] + public async Task GetStatMessages_ShouldReturn_Success() + { + //act + var baseRequest = new MessageRequestBase() { }; + var messageRequest = new MessageRequest(baseRequest, [1]); + var result = await messageServiceMock.GetStat(messageRequest, CancellationToken.None); + + Assert.Equal(6, result.Count()); + + var mCount_Telemetry_1_Category_1_Template_1_Expected = messages + .Where(x => x.IdTelemetry == 1) + .Where(x => x.IdEvent == 1) + .Count(); + var mCount_Telemetry_1_Category_1_Template_1_Actual = result + .Where(x => x.IdTelemetry == 1) + .Where(x => x.IdCategory == 1) + .Where(x => x.MessageTemplate == " 1 1") + .Sum(x => x.MessagesCount); + Assert.Equal(mCount_Telemetry_1_Category_1_Template_1_Expected, mCount_Telemetry_1_Category_1_Template_1_Actual); + + var mCount_Telemetry_1_Category_1_Template_2_Expected = messages + .Where(x => x.IdTelemetry == 1) + .Where(x => x.IdEvent == 2) + .Count(); + var mCount_Telemetry_1_Category_1_Template_2_Actual = result + .Where(x => x.IdTelemetry == 1) + .Where(x => x.IdCategory == 1) + .Where(x => x.MessageTemplate == " 2 1") + .Sum(x => x.MessagesCount); + Assert.Equal(mCount_Telemetry_1_Category_1_Template_2_Expected, mCount_Telemetry_1_Category_1_Template_2_Actual); + + var mCount_Telemetry_1_Category_1_Template_3_Expected = messages + .Where(x => x.IdTelemetry == 1) + .Where(x => x.IdEvent == 3) + .Count(); + var mCount_Telemetry_1_Category_1_Template_3_Actual = result + .Where(x => x.IdTelemetry == 1) + .Where(x => x.IdCategory == 1) + .Where(x => x.MessageTemplate == " 3 1") + .Sum(x => x.MessagesCount); + Assert.Equal(mCount_Telemetry_1_Category_1_Template_3_Expected, mCount_Telemetry_1_Category_1_Template_3_Actual); + + var mCount_Telemetry_1_Category_2_Template_2_Expected = messages + .Where(x => x.IdTelemetry == 1) + .Where(x => x.IdEvent == 5) + .Count(); + var mCount_Telemetry_1_Category_2_Template_2_Actual = result + .Where(x => x.IdTelemetry == 1) + .Where(x => x.IdCategory == 2) + .Where(x => x.MessageTemplate == " 2 2") + .Sum(x => x.MessagesCount); + Assert.Equal(mCount_Telemetry_1_Category_2_Template_2_Expected, mCount_Telemetry_1_Category_2_Template_2_Actual); + + var mCount_Telemetry_1_Category_2_Template_3_Expected = messages + .Where(x => x.IdTelemetry == 1) + .Where(x => x.IdEvent == 6) + .Count(); + var mCount_Telemetry_1_Category_2_Template_3_Actual = result + .Where(x => x.IdTelemetry == 1) + .Where(x => x.IdCategory == 2) + .Where(x => x.MessageTemplate == " 3 2") + .Sum(x => x.MessagesCount); + Assert.Equal(mCount_Telemetry_1_Category_2_Template_3_Expected, mCount_Telemetry_1_Category_2_Template_3_Actual); + + var mCount_Telemetry_2_Category_3_Template_1_Expected = messages + .Where(x => x.IdTelemetry == 2) + .Where(x => x.IdEvent == 6) + .Count(); + var mCount_Telemetry_2_Category_3_Template_1_Actual = result + .Where(x => x.IdTelemetry == 2) + .Where(x => x.IdCategory == 3) + .Where(x => x.MessageTemplate == " 1 3") + .Sum(x => x.MessagesCount); + Assert.Equal(mCount_Telemetry_2_Category_3_Template_1_Expected, mCount_Telemetry_2_Category_3_Template_1_Actual); + } + + +} \ No newline at end of file