using FluentAssertions; using Microsoft.Extensions.Logging; using Moq; using NUnit.Framework; using Strata.ContinuousImprovement.Biz.Configuration; using Strata.ContinuousImprovement.Biz.DbContexts; using Strata.ContinuousImprovement.Biz.DbContexts.Snowflake; using Strata.ContinuousImprovement.Biz.QualityVariation.QVI; using Strata.SqlTools.Configuration.Common.AsyncFactory; 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 QviEncounterServiceTest { private Mock _snowflakeDatabaseContextMock; private Mock _configurationServiceMock; private Mock> _dbContextFactoryMock; private Mock> _loggerMock; private QviEncounterService _service; [SetUp] public void SetUp() { _snowflakeDatabaseContextMock = new Mock(); _configurationServiceMock = new Mock(); _dbContextFactoryMock = new Mock>(); _loggerMock = new Mock>(); _service = new QviEncounterService( _snowflakeDatabaseContextMock.Object, _configurationServiceMock.Object, _dbContextFactoryMock.Object, _loggerMock.Object); } [Test] public async Task GetEncounterInfoAsync_WhenNoEntityIds_ReturnsEmptyAndSkipsDatabase() { var configurationGuid = Guid.NewGuid(); _configurationServiceMock .Setup(s => s.GetConfigurationAsync(configurationGuid, It.IsAny())) .ReturnsAsync(new CIConfiguration { EntityIdsCSV = string.Empty, SourceSystemIdsCSV = "1" }); var result = await _service.GetEncounterInfoAsync(configurationGuid, DateTime.UtcNow, DateTime.UtcNow, CancellationToken.None); result.Should().BeEmpty(); _dbContextFactoryMock.Verify(f => f.CreateDbContextAsync(It.IsAny()), Times.Never); _snowflakeDatabaseContextMock.Verify( s => s.QueryAsync( It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task GetEncounterInfoAsync_WhenNoSourceSystemIds_ReturnsEmptyAndSkipsDatabase() { var configurationGuid = Guid.NewGuid(); _configurationServiceMock .Setup(s => s.GetConfigurationAsync(configurationGuid, It.IsAny())) .ReturnsAsync(new CIConfiguration { EntityIdsCSV = "1", SourceSystemIdsCSV = string.Empty }); var result = await _service.GetEncounterInfoAsync(configurationGuid, DateTime.UtcNow, DateTime.UtcNow, CancellationToken.None); result.Should().BeEmpty(); _dbContextFactoryMock.Verify(f => f.CreateDbContextAsync(It.IsAny()), Times.Never); _snowflakeDatabaseContextMock.Verify( s => s.QueryAsync( It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task GetEncounterInfoAsync_WhenConfigurationThrows_Rethrows() { var configurationGuid = Guid.NewGuid(); _configurationServiceMock .Setup(s => s.GetConfigurationAsync(configurationGuid, It.IsAny())) .ThrowsAsync(new InvalidOperationException("boom")); var action = async () => await _service.GetEncounterInfoAsync(configurationGuid, DateTime.UtcNow, DateTime.UtcNow, CancellationToken.None); await action.Should().ThrowAsync(); } } }