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

69 lines
2.1 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 Bogus;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using Xunit;
namespace AsbCloudWebApi.Tests.ServicesTests.SAUB;
public class TelemetryDataSaubCacheTests
{
private readonly IEnumerable<TelemetryDataSaubDto> fakeTelemetries = new Faker<TelemetryDataSaubDto>()
.RuleFor(t => t.DateTime, DateTime.UtcNow)
.Generate(5)
.OrderBy(t => t.DateTime);
private readonly IServiceProvider serviceProvider = Substitute.For<IServiceProvider>();
private readonly TelemetryDataCache<TelemetryDataSaubDto> telemetryDataCache;
public TelemetryDataSaubCacheTests()
{
serviceProvider.GetService<BackgroundWorker>().Returns(new BackgroundWorker(serviceProvider));
telemetryDataCache = TelemetryDataCache<TelemetryDataSaubDto>.GetInstance<TelemetryDataSaub>(serviceProvider);
}
[Fact]
public void AddRange_ShouldReturn_OneAddedElementToCache()
{
//arrange
const int expectedCacheItemCount = 1;
var idTelemetry = new Random().Next(1, 100);
var telemetryDataCacheType = telemetryDataCache.GetType();
telemetryDataCacheType.GetField("isLoading", BindingFlags.NonPublic | BindingFlags.Instance)?.SetValue(telemetryDataCache, false);
//act
telemetryDataCache.AddRange(idTelemetry, fakeTelemetries);
//assert
Assert.True(telemetryDataCache.CacheItemCount == expectedCacheItemCount);
}
[Fact]
public void AddRange_ShouldReturn_ZeroAddedElementToCache()
{
//arrange
const int expectedCacheItemCount = 0;
var idTelemetry = new Random().Next(1, 100);
var telemetryDataCacheType = telemetryDataCache.GetType();
telemetryDataCacheType.GetField("isLoading", BindingFlags.NonPublic | BindingFlags.Instance)?.SetValue(telemetryDataCache, true);
//act
telemetryDataCache.AddRange(idTelemetry, fakeTelemetries);
//assert
Assert.True(telemetryDataCache.CacheItemCount == expectedCacheItemCount);
}
}