138 lines
4.1 KiB
C#
138 lines
4.1 KiB
C#
using CommunityToolkit.Mvvm.Messaging;
|
|
using Livia.Models;
|
|
using Livia.Models.Data;
|
|
using Livia.Utility;
|
|
using Livia.Utility.DependencyInjection;
|
|
using Livia.Views.Utility;
|
|
using Microsoft.Extensions.Logging;
|
|
using Moq;
|
|
|
|
namespace Livia.Tests.Models;
|
|
|
|
public class DataBlockLoaderTests
|
|
{
|
|
private readonly DataBlockLoader _dataBlockLoader;
|
|
|
|
private readonly Mock<IWarningSystem> _warningSystemMock = new();
|
|
private readonly Mock<IPatientInfoManager> _patientInfoManagerMock = new();
|
|
private readonly Mock<IServerHandler> _serverHandlerMock = new();
|
|
private readonly Mock<IRoiLegendManager> _roiLegendManagerMock = new();
|
|
|
|
public static IEnumerable<object[]> SharedDataTestInput => [["TestKey1", "TestValue1"], ["TestKey2", float.Pi], ["TestKey3", new LogoutMessage()]];
|
|
|
|
public DataBlockLoaderTests(ILogger logger)
|
|
{
|
|
_dataBlockLoader = new DataBlockLoader(_warningSystemMock.Object, _patientInfoManagerMock.Object, logger, _serverHandlerMock.Object, _roiLegendManagerMock.Object);
|
|
_serverHandlerMock.SetupAllProperties();
|
|
_warningSystemMock.Setup(o => o.ShowDialog(It.IsAny<WarningWindowKind>(), It.IsAny<bool>(), It.IsAny<string>())).Returns(true);
|
|
|
|
//TODO:: temp measure
|
|
ServiceProviderFactory.Init(_ => { });
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_ReceivesLogoutMessage_ShowPlaceHolder()
|
|
{
|
|
//arrange
|
|
_dataBlockLoader.PlaceholderVisible = false;
|
|
|
|
//act
|
|
WeakReferenceMessenger.Default.Send(new LogoutMessage());
|
|
|
|
//assert
|
|
Assert.True(_dataBlockLoader.PlaceholderVisible);
|
|
}
|
|
|
|
[Theory, MemberData(nameof(SharedDataTestInput))]
|
|
public void SaveSharedData_SaveAndGetData_ReturnsData(string key, object data)
|
|
{
|
|
//act
|
|
_dataBlockLoader.SaveSharedData(key, data);
|
|
object getResult = _dataBlockLoader.GetSharedData<object>(key);
|
|
|
|
//assert
|
|
Assert.Equal(data, getResult);
|
|
}
|
|
|
|
[Fact]
|
|
public void SaveSharedData_SaveDuplicateData_OverrideData()
|
|
{
|
|
//act
|
|
const string key = "DuplicateKey";
|
|
object correctValue = "override";
|
|
_dataBlockLoader.SaveSharedData(key, 0);
|
|
_dataBlockLoader.SaveSharedData(key, correctValue);
|
|
object getResult = _dataBlockLoader.GetSharedData<object>(key);
|
|
|
|
//assert
|
|
Assert.Equal(correctValue, getResult);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetSharedData_KeyDoesNotExists_ThrowsException()
|
|
{
|
|
//assert
|
|
Assert.Throws<KeyNotFoundException>(GetData);
|
|
return;
|
|
|
|
void GetData() => _dataBlockLoader.GetSharedData<object>("KeyThatDoesNotExists");
|
|
}
|
|
|
|
[Fact]
|
|
public void GetSharedData_TypeMismatch_ThrowsException()
|
|
{
|
|
//act
|
|
const string key = "TypeMismatchKey";
|
|
_dataBlockLoader.SaveSharedData(key, float.E);
|
|
|
|
//assert
|
|
Assert.Throws<KeyNotFoundException>(GetData);
|
|
return;
|
|
|
|
void GetData() => _dataBlockLoader.GetSharedData<double>(key);
|
|
}
|
|
|
|
[Fact]
|
|
public void SharedDataContainsKey_ContainsKey_ReturnsTrue()
|
|
{
|
|
//arrange
|
|
const string key = "KeyThatExists";
|
|
_dataBlockLoader.SaveSharedData(key, "Some Value");
|
|
|
|
//act
|
|
bool result = _dataBlockLoader.SharedDataContainsKey(key);
|
|
|
|
//assert
|
|
Assert.True(result);
|
|
}
|
|
|
|
[Fact]
|
|
public void SharedDataContainsKey_DoesNotContainsKey_ReturnsFalse()
|
|
{
|
|
//act
|
|
const string key = "KeyThatDoesNotExists";
|
|
bool result = _dataBlockLoader.SharedDataContainsKey(key);
|
|
|
|
//assert
|
|
Assert.False(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task LoadData_EmptyData_ShowsError()
|
|
{
|
|
//arrange
|
|
DataBlock dataBlock = new(string.Empty);
|
|
|
|
//act
|
|
await _dataBlockLoader.LoadData(dataBlock);
|
|
|
|
//assert
|
|
_warningSystemMock.Verify(o => o.ShowDialog(It.IsAny<WarningWindowKind>(), It.IsAny<bool>(),
|
|
It.Is<string>(s => s.Equals("InvalidDataError"))), Times.Once);
|
|
Assert.False(_serverHandlerMock.Object.Processing);
|
|
Assert.Null(_dataBlockLoader.CurrentDisplayingDataBlock);
|
|
Assert.True(_dataBlockLoader.PlaceholderVisible);
|
|
}
|
|
}
|
|
|