Files
continuousimprovement/tests/Strata.ContinuousImprovement.Biz.Test.Unit/QualityVariation/QVI/NonQviSavingsServiceTest.cs
T

202 lines
8.4 KiB
C#

using FluentAssertions;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
using Strata.ContinuousImprovement.Biz.Configuration;
using Strata.ContinuousImprovement.Biz.DbContexts.Snowflake;
using Strata.ContinuousImprovement.Biz.QualityVariation.QVI;
using CIConfiguration = Strata.ContinuousImprovement.Biz.Configuration.Configuration;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Strata.ContinuousImprovement.Biz.Test.Unit.QualityVariation.QVI
{
[TestFixture, Category("Unit")]
public class NonQviSavingsServiceTest
{
private Mock<ISnowflakeDatabaseContext> _snowflakeDatabaseContextMock;
private Mock<IConfigurationService> _configurationServiceMock;
private Mock<ILogger<NonQviSavingsService>> _loggerMock;
private NonQviSavingsService _service;
[SetUp]
public void SetUp()
{
_snowflakeDatabaseContextMock = new Mock<ISnowflakeDatabaseContext>();
_configurationServiceMock = new Mock<IConfigurationService>();
_loggerMock = new Mock<ILogger<NonQviSavingsService>>();
_service = new NonQviSavingsService(
_snowflakeDatabaseContextMock.Object,
_configurationServiceMock.Object,
_loggerMock.Object);
}
[Test]
public async Task GetCaseTypeFamilyInfoAsync_WhenNoEntityIds_ReturnsEmpty()
{
var configurationGuid = Guid.NewGuid();
_configurationServiceMock
.Setup(s => s.GetConfigurationAsync(configurationGuid, It.IsAny<CancellationToken>()))
.ReturnsAsync(new CIConfiguration { EntityIdsCSV = string.Empty, SourceSystemIdsCSV = "1" });
var result = await _service.GetCaseTypeFamilyInfoAsync(configurationGuid, true, CancellationToken.None);
result.Should().BeEmpty();
_snowflakeDatabaseContextMock.Verify(
s => s.QueryAsync<NonQviCaseTypeFamilyInfo>(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<object>(),
It.IsAny<TimeSpan>()),
Times.Never);
}
[Test]
public async Task GetCaseTypeFamilyInfoAsync_WhenNoSourceSystemIds_ReturnsEmpty()
{
var configurationGuid = Guid.NewGuid();
_configurationServiceMock
.Setup(s => s.GetConfigurationAsync(configurationGuid, It.IsAny<CancellationToken>()))
.ReturnsAsync(new CIConfiguration { EntityIdsCSV = "1", SourceSystemIdsCSV = string.Empty });
var result = await _service.GetCaseTypeFamilyInfoAsync(configurationGuid, true, CancellationToken.None);
result.Should().BeEmpty();
_snowflakeDatabaseContextMock.Verify(
s => s.QueryAsync<NonQviCaseTypeFamilyInfo>(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<object>(),
It.IsAny<TimeSpan>()),
Times.Never);
}
[Test]
public async Task GetCaseTypeFamilyInfoAsync_WhenEntitiesExist_QueriesSnowflake()
{
var configurationGuid = Guid.NewGuid();
var expected = new[] { new NonQviCaseTypeFamilyInfo { CaseTypeFamilyId = 10, CaseCount = 3 } };
_configurationServiceMock
.Setup(s => s.GetConfigurationAsync(configurationGuid, It.IsAny<CancellationToken>()))
.ReturnsAsync(new CIConfiguration
{
EntityIdsCSV = "1",
SourceSystemIdsCSV = "1",
DepartmentRollupGlobalId = string.Empty,
QualityVariationEndDate = new DateTime(2024, 12, 31)
});
_snowflakeDatabaseContextMock
.Setup(s => s.QueryAsync<NonQviCaseTypeFamilyInfo>(
It.IsAny<string>(),
nameof(NonQviSavingsService.GetCaseTypeFamilyInfoAsync),
It.IsAny<object>(),
It.IsAny<TimeSpan>()))
.ReturnsAsync(expected);
var result = await _service.GetCaseTypeFamilyInfoAsync(configurationGuid, true, CancellationToken.None);
result.Should().BeEquivalentTo(expected);
_snowflakeDatabaseContextMock.Verify(
s => s.QueryAsync<NonQviCaseTypeFamilyInfo>(
It.IsAny<string>(),
nameof(NonQviSavingsService.GetCaseTypeFamilyInfoAsync),
It.IsAny<object>(),
It.IsAny<TimeSpan>()),
Times.Once);
}
[Test]
public async Task GetDrgTypeInfoAsync_WhenNoEntityIds_ReturnsEmpty()
{
var configurationGuid = Guid.NewGuid();
_configurationServiceMock
.Setup(s => s.GetConfigurationAsync(configurationGuid, It.IsAny<CancellationToken>()))
.ReturnsAsync(new CIConfiguration { EntityIdsCSV = string.Empty, SourceSystemIdsCSV = "1" });
var result = await _service.GetDrgTypeInfoAsync(configurationGuid, true, CancellationToken.None);
result.Should().BeEmpty();
_snowflakeDatabaseContextMock.Verify(
s => s.QueryAsync<NonQviDrgTypeInfo>(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<object>(),
It.IsAny<TimeSpan>()),
Times.Never);
}
[Test]
public async Task GetDrgTypeInfoAsync_WhenNoSourceSystemIds_ReturnsEmpty()
{
var configurationGuid = Guid.NewGuid();
_configurationServiceMock
.Setup(s => s.GetConfigurationAsync(configurationGuid, It.IsAny<CancellationToken>()))
.ReturnsAsync(new CIConfiguration { EntityIdsCSV = "1", SourceSystemIdsCSV = string.Empty });
var result = await _service.GetDrgTypeInfoAsync(configurationGuid, true, CancellationToken.None);
result.Should().BeEmpty();
_snowflakeDatabaseContextMock.Verify(
s => s.QueryAsync<NonQviDrgTypeInfo>(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<object>(),
It.IsAny<TimeSpan>()),
Times.Never);
}
[Test]
public async Task GetDrgTypeInfoAsync_WhenEntitiesExist_QueriesSnowflake()
{
var configurationGuid = Guid.NewGuid();
var expected = new[] { new NonQviDrgTypeInfo { DrgType = "MS" } };
_configurationServiceMock
.Setup(s => s.GetConfigurationAsync(configurationGuid, It.IsAny<CancellationToken>()))
.ReturnsAsync(new CIConfiguration
{
EntityIdsCSV = "1",
SourceSystemIdsCSV = "1",
DepartmentRollupGlobalId = string.Empty,
QualityVariationEndDate = new DateTime(2024, 12, 31)
});
_snowflakeDatabaseContextMock
.Setup(s => s.QueryAsync<NonQviDrgTypeInfo>(
It.IsAny<string>(),
nameof(NonQviSavingsService.GetDrgTypeInfoAsync),
It.IsAny<object>(),
It.IsAny<TimeSpan>()))
.ReturnsAsync(expected);
var result = await _service.GetDrgTypeInfoAsync(configurationGuid, true, CancellationToken.None);
result.Should().BeEquivalentTo(expected);
_snowflakeDatabaseContextMock.Verify(
s => s.QueryAsync<NonQviDrgTypeInfo>(
It.IsAny<string>(),
nameof(NonQviSavingsService.GetDrgTypeInfoAsync),
It.IsAny<object>(),
It.IsAny<TimeSpan>()),
Times.Once);
}
[Test]
public async Task GetDrgTypeInfoAsync_WhenConfigurationThrows_Rethrows()
{
var configurationGuid = Guid.NewGuid();
_configurationServiceMock
.Setup(s => s.GetConfigurationAsync(configurationGuid, It.IsAny<CancellationToken>()))
.ThrowsAsync(new InvalidOperationException("boom"));
var action = async () => await _service.GetDrgTypeInfoAsync(configurationGuid, true, CancellationToken.None);
await action.Should().ThrowAsync<InvalidOperationException>();
}
}
}