Автотест для проверки расчета статистики сообщений

This commit is contained in:
Olga Nemt 2024-09-10 12:33:24 +05:00
parent cdb6ed7390
commit a6496a1af2
2 changed files with 328 additions and 0 deletions

View File

@ -31,6 +31,14 @@ public class MessageRequestBase : RequestBase
/// </summary>
public string? SearchString { get; set; }
/// <summary>
///
/// </summary>
public MessageRequestBase()
{
}
/// <summary>
/// копирующий конструктор
/// </summary>

View File

@ -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<TelemetryBaseDto> telemetries = new List<TelemetryBaseDto>()
{
new TelemetryBaseDto()
{
Id = 1,
TimeZone = new SimpleTimezoneDto()
{
Hours = 5
}
},
new TelemetryBaseDto()
{
Id = 2,
TimeZone = new SimpleTimezoneDto()
{
Hours = 5
}
}
};
private static readonly List<TelemetryEventDto> events = new List<TelemetryEventDto>()
{
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"
}
};
/// <summary>
/// Ñòðóêòóðà:
/// Òåëåìåòðèÿ 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
///
/// </summary>
private static readonly List<TelemetryMessageDto> messages = new List<TelemetryMessageDto>()
{
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<IMessageService>();
private readonly IMessageRepository messageRepositoryMock = Substitute.For<IMessageRepository>();
private readonly IEventRepository eventRepositoryMock = Substitute.For<IEventRepository>();
private readonly ITelemetryService telemetryServiceMock = Substitute.For<ITelemetryService>();
private readonly IAsbCloudDbContext db = Substitute.For<IAsbCloudDbContext>();
public MessageServiceTest()
{
messageServiceMock = new MessageService(messageRepositoryMock, telemetryServiceMock, eventRepositoryMock);
telemetryServiceMock
.GetOrDefaultTelemetriesByIdsWells(Arg.Any<IEnumerable<int>>())
.Returns(telemetries);
eventRepositoryMock
.Get(Arg.Any<TelemetryEventRequest>(), Arg.Any<CancellationToken>())
.Returns(events);
messageRepositoryMock
.GetMessagesAsync(Arg.Any<MessageTelemetryRequest>(), Arg.Any<CancellationToken>())
.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);
}
}