DD.WellWorkover.Cloud/AsbCloudWebApi.Tests/Services/SAUB/TelemetryDataSaubCacheTests.cs

65 lines
1.9 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using AsbCloudApp.Data.SAUB;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Background;
using AsbCloudInfrastructure.Services.SAUB;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using Xunit;
namespace AsbCloudWebApi.Tests.ServicesTests.SAUB;
public class TelemetryDataSaubCacheTests
{
2023-10-24 16:44:24 +05:00
private const int idTelemetry = 1;
2023-10-24 16:44:24 +05:00
private readonly IEnumerable<TelemetryDataSaubDto> fakeTelemetries = new[]
{
new TelemetryDataSaubDto()
};
2023-10-24 16:44:24 +05:00
private readonly IServiceProvider serviceProviderMock = Substitute.For<IServiceProvider>();
private readonly TelemetryDataCache<TelemetryDataSaubDto> telemetryDataCache;
private readonly Type telemetryDataCacheType;
public TelemetryDataSaubCacheTests()
{
2023-10-24 16:44:24 +05:00
serviceProviderMock.GetService<BackgroundWorker>().Returns(new BackgroundWorker(serviceProviderMock));
2023-10-24 16:44:24 +05:00
telemetryDataCache = TelemetryDataCache<TelemetryDataSaubDto>.GetInstance<TelemetryDataSaub>(serviceProviderMock);
telemetryDataCacheType = telemetryDataCache.GetType();
}
[Fact]
2023-10-24 16:44:24 +05:00
public void AddRange_ShouldReturn_AddedElementToCache()
{
//arrange
telemetryDataCacheType.GetField("isLoading", BindingFlags.NonPublic | BindingFlags.Instance)?.SetValue(telemetryDataCache, false);
//act
telemetryDataCache.AddRange(idTelemetry, fakeTelemetries);
2023-10-24 16:44:24 +05:00
var lastTelemetry = telemetryDataCache.GetLastOrDefault(idTelemetry);
//assert
2023-10-24 16:44:24 +05:00
Assert.Equal(lastTelemetry, fakeTelemetries.Last());
}
[Fact]
2023-10-24 16:44:24 +05:00
public void AddRange_ShouldReturn_NotAddedToCache()
{
//arrange
telemetryDataCacheType.GetField("isLoading", BindingFlags.NonPublic | BindingFlags.Instance)?.SetValue(telemetryDataCache, true);
2023-10-24 16:44:24 +05:00
//act
telemetryDataCache.AddRange(idTelemetry, fakeTelemetries);
2023-10-24 16:44:24 +05:00
var lastTelemetry = telemetryDataCache.GetLastOrDefault(idTelemetry);
//assert
2023-10-24 16:44:24 +05:00
Assert.NotEqual(lastTelemetry, fakeTelemetries.Last());
}
}