fix unit test

This commit is contained in:
ngfrolov 2024-08-19 13:54:29 +05:00
parent e0d199380f
commit d416d77573
Signed by untrusted user who does not match committer: ng.frolov
GPG Key ID: E99907A0357B29A7
2 changed files with 15 additions and 6 deletions

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using AsbCloudApp.Data.SAUB;
using AsbCloudApp.Repositories;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Background;
using AsbCloudInfrastructure.Services.SAUB;
@ -14,6 +15,7 @@ namespace AsbCloudInfrastructure.Tests.Services.SAUB;
public class TelemetryDataSaubCacheTests
{
private static object _lock = new object();
private const int idTelemetry = 1;
private readonly IEnumerable<TelemetryDataSaubDto> fakeTelemetries = new[]
@ -29,20 +31,22 @@ public class TelemetryDataSaubCacheTests
public TelemetryDataSaubCacheTests()
{
serviceProviderMock.GetService<BackgroundWorker>().Returns(new BackgroundWorker(serviceProviderMock));
telemetryDataCache = TelemetryDataCache<TelemetryDataSaubDto>.GetInstance<TelemetryDataSaub>(serviceProviderMock);
telemetryDataCacheType = telemetryDataCache.GetType();
}
[Fact]
public void AddRange_ShouldReturn_AddedElementToCache()
{
//arrange
telemetryDataCacheType.GetField("isLoading", BindingFlags.NonPublic | BindingFlags.Instance)?.SetValue(telemetryDataCache, false);
telemetryDataCache.AddRange(idTelemetry, fakeTelemetries);
telemetryDataCacheType
.GetField("isLoading", BindingFlags.NonPublic | BindingFlags.Instance)!
.SetValue(telemetryDataCache, false);
//act
telemetryDataCache.AddRange(idTelemetry, fakeTelemetries);
var lastTelemetry = telemetryDataCache.GetLastOrDefault(idTelemetry);
//assert
@ -53,10 +57,12 @@ public class TelemetryDataSaubCacheTests
public void AddRange_ShouldReturn_NotAddedToCache()
{
//arrange
telemetryDataCacheType.GetField("isLoading", BindingFlags.NonPublic | BindingFlags.Instance)?.SetValue(telemetryDataCache, true);
telemetryDataCache.AddRange(idTelemetry, fakeTelemetries);
telemetryDataCacheType
.GetField("isLoading", BindingFlags.NonPublic | BindingFlags.Instance)!
.SetValue(telemetryDataCache, true);
//act
telemetryDataCache.AddRange(idTelemetry, fakeTelemetries);
var lastTelemetry = telemetryDataCache.GetLastOrDefault(idTelemetry);
//assert

View File

@ -139,6 +139,9 @@ public class TelemetryDataCache<TDto> : ITelemetryDataCache<TDto> where TDto : A
public virtual TDto? GetLastOrDefault(int idTelemetry)
{
if (isLoading)
return default;
if (!caches.TryGetValue(idTelemetry, out TelemetryDataCacheItem? cacheItem))
return default;