Files

266 lines
12 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 QviLineItemSavingsServiceTest
{
private Mock<ISnowflakeDatabaseContext> _snowflakeDatabaseContextMock;
private Mock<IConfigurationService> _configurationServiceMock;
private Mock<ILogger<QviLineItemSavingsService>> _loggerMock;
private QviLineItemSavingsService _service;
[SetUp]
public void SetUp()
{
_snowflakeDatabaseContextMock = new Mock<ISnowflakeDatabaseContext>();
_configurationServiceMock = new Mock<IConfigurationService>();
_loggerMock = new Mock<ILogger<QviLineItemSavingsService>>();
_service = new QviLineItemSavingsService(
_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<QviLineItemCaseTypeFamilyInfo>(
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<QviLineItemCaseTypeFamilyInfo>(
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 QviLineItemCaseTypeFamilyInfo { QualityVariationEventId = 42, CaseTypeFamilyId = 7 } };
_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<QviLineItemCaseTypeFamilyInfo>(
It.IsAny<string>(),
nameof(QviLineItemSavingsService.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<QviLineItemCaseTypeFamilyInfo>(
It.IsAny<string>(),
nameof(QviLineItemSavingsService.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<QviLineItemDrgTypeInfo>(
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<QviLineItemDrgTypeInfo>(
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 QviLineItemDrgTypeInfo { DrgType = "APR" } };
_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<QviLineItemDrgTypeInfo>(
It.IsAny<string>(),
nameof(QviLineItemSavingsService.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<QviLineItemDrgTypeInfo>(
It.IsAny<string>(),
nameof(QviLineItemSavingsService.GetDrgTypeInfoAsync),
It.IsAny<object>(),
It.IsAny<TimeSpan>()),
Times.Once);
}
[Test]
public async Task GetEncounterSavingInfoAsync_UsesConfigurationGuidParameter()
{
var configurationGuid = Guid.NewGuid();
var expected = new[] { new QviLineItemEncounterSavingInfo { EncounterRecordNumber = "enc-1" } };
_configurationServiceMock
.Setup(s => s.GetConfigurationAsync(configurationGuid, It.IsAny<CancellationToken>()))
.ReturnsAsync(new CIConfiguration());
_snowflakeDatabaseContextMock
.Setup(s => s.QueryAsync<QviLineItemEncounterSavingInfo>(
It.IsAny<string>(),
nameof(QviLineItemSavingsService.GetEncounterSavingInfoAsync),
It.IsAny<object>(),
It.IsAny<TimeSpan>()))
.ReturnsAsync(expected);
var result = await _service.GetEncounterSavingInfoAsync(configurationGuid, null, null, CancellationToken.None);
result.Should().BeEquivalentTo(expected);
_snowflakeDatabaseContextMock.Verify(
s => s.QueryAsync<QviLineItemEncounterSavingInfo>(
It.IsAny<string>(),
nameof(QviLineItemSavingsService.GetEncounterSavingInfoAsync),
It.Is<object>(o =>
o.GetType().GetProperty("ConfigurationGUID") != null &&
(Guid)o.GetType().GetProperty("ConfigurationGUID").GetValue(o)! == configurationGuid),
It.IsAny<TimeSpan>()),
Times.Once);
}
[Test]
public async Task GetEncounterSavingInfoAsync_WhenOpportunityGuidAndIsBaselineProvided_FiltersQuery()
{
var configurationGuid = Guid.NewGuid();
var opportunityGuid = Guid.NewGuid();
var expected = new[] { new QviLineItemEncounterSavingInfo { EncounterRecordNumber = "enc-1" } };
_configurationServiceMock
.Setup(s => s.GetConfigurationAsync(configurationGuid, It.IsAny<CancellationToken>()))
.ReturnsAsync(new CIConfiguration());
_snowflakeDatabaseContextMock
.Setup(s => s.QueryAsync<QviLineItemEncounterSavingInfo>(
It.Is<string>(q => q.Contains(":OpportunityGUID") && q.Contains(":IsBaseline")),
nameof(QviLineItemSavingsService.GetEncounterSavingInfoAsync),
It.IsAny<object>(),
It.IsAny<TimeSpan>()))
.ReturnsAsync(expected);
var result = await _service.GetEncounterSavingInfoAsync(configurationGuid, opportunityGuid, true, CancellationToken.None);
result.Should().BeEquivalentTo(expected);
}
[Test]
public async Task GetEncounterSavingInfoAsync_WhenSnowflakeThrows_Rethrows()
{
var configurationGuid = Guid.NewGuid();
_configurationServiceMock
.Setup(s => s.GetConfigurationAsync(configurationGuid, It.IsAny<CancellationToken>()))
.ReturnsAsync(new CIConfiguration());
_snowflakeDatabaseContextMock
.Setup(s => s.QueryAsync<QviLineItemEncounterSavingInfo>(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<object>(),
It.IsAny<TimeSpan>()))
.ThrowsAsync(new InvalidOperationException("boom"));
var action = async () => await _service.GetEncounterSavingInfoAsync(configurationGuid, null, null, CancellationToken.None);
await action.Should().ThrowAsync<InvalidOperationException>();
}
}
}