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

71 lines
2.2 KiB
C#
Raw Normal View History

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