diff --git a/Raccoon.Ninja.Domain.Core.Tests/Calculators/Handlers/TimeInRangeCalculatorHandlerTests.cs b/Raccoon.Ninja.Domain.Core.Tests/Calculators/Handlers/TimeInRangeCalculatorHandlerTests.cs new file mode 100644 index 0000000..92456bc --- /dev/null +++ b/Raccoon.Ninja.Domain.Core.Tests/Calculators/Handlers/TimeInRangeCalculatorHandlerTests.cs @@ -0,0 +1,123 @@ +using Raccoon.Ninja.Domain.Core.Calculators; +using Raccoon.Ninja.Domain.Core.Calculators.Handlers; + +namespace Raccoon.Ninja.Domain.Core.Tests.Calculators.Handlers; + +public class TimeInRangeCalculatorHandlerTests +{ + private readonly TimeInRangeCalculatorHandler _sut = new(); + + [Fact] + public void RunCalculation_ShouldReturnCorrectPercentages_WithValidData() + { + // Arrange + var data = new CalculationData + { + GlucoseValues = new List { 70, 90, 150, 200, 260 } + }; + + // Act + var result = _sut.Handle(data); + + // Assert + result.TimeInRange.Low.Should().Be(20); + result.TimeInRange.Normal.Should().Be(40); + result.TimeInRange.High.Should().Be(20); + result.TimeInRange.VeryHigh.Should().Be(20); + } + + [Fact] + public void RunCalculation_ShouldHandleSingleValue() + { + // Arrange + var data = new CalculationData + { + GlucoseValues = new List { 150 } + }; + + // Act + var result = _sut.Handle(data); + + // Assert + result.TimeInRange.Low.Should().Be(0); + result.TimeInRange.Normal.Should().Be(100); + result.TimeInRange.High.Should().Be(0); + result.TimeInRange.VeryHigh.Should().Be(0); + } + + [Fact] + public void RunCalculation_ShouldHandleAllLowValues() + { + // Arrange + var data = new CalculationData + { + GlucoseValues = new List { 70, 75, 80, 60, 85 } + }; + + // Act + var result = _sut.Handle(data); + + // Assert + result.TimeInRange.Low.Should().Be(80); + result.TimeInRange.Normal.Should().Be(20); + result.TimeInRange.High.Should().Be(0); + result.TimeInRange.VeryHigh.Should().Be(0); + } + + [Fact] + public void RunCalculation_ShouldHandleAllNormalValues() + { + // Arrange + var data = new CalculationData + { + GlucoseValues = new List { 90, 100, 150, 160, 170 } + }; + + // Act + var result = _sut.Handle(data); + + // Assert + result.TimeInRange.Low.Should().Be(0); + result.TimeInRange.Normal.Should().Be(100); + result.TimeInRange.High.Should().Be(0); + result.TimeInRange.VeryHigh.Should().Be(0); + } + + [Fact] + public void RunCalculation_ShouldHandleAllHighValues() + { + // Arrange + var data = new CalculationData + { + GlucoseValues = new List { 180, 185, 200, 240, 250 } + }; + + // Act + var result = _sut.Handle(data); + + // Assert + result.TimeInRange.Low.Should().Be(0); + result.TimeInRange.Normal.Should().Be(0); + result.TimeInRange.High.Should().Be(100); + result.TimeInRange.VeryHigh.Should().Be(0); + } + + [Fact] + public void RunCalculation_ShouldHandleAllVeryHighValues() + { + // Arrange + var data = new CalculationData + { + GlucoseValues = new List { 260, 270, 280, 290, 300 } + }; + + // Act + var result = _sut.Handle(data); + + // Assert + result.TimeInRange.Low.Should().Be(0); + result.TimeInRange.Normal.Should().Be(0); + result.TimeInRange.High.Should().Be(0); + result.TimeInRange.VeryHigh.Should().Be(100); + } +} \ No newline at end of file diff --git a/Raccoon.Ninja.Domain.Core/Calculators/Handlers/TimeInRangeCalculatorHandler.cs b/Raccoon.Ninja.Domain.Core/Calculators/Handlers/TimeInRangeCalculatorHandler.cs index df6a47f..5da5094 100644 --- a/Raccoon.Ninja.Domain.Core/Calculators/Handlers/TimeInRangeCalculatorHandler.cs +++ b/Raccoon.Ninja.Domain.Core/Calculators/Handlers/TimeInRangeCalculatorHandler.cs @@ -13,23 +13,23 @@ public class TimeInRangeCalculatorHandler: BaseCalculatorHandler protected override CalculationData RunCalculation(CalculationData data) { var low = data.GlucoseValues.Count(v => v < GlucoseConstants.LowGlucoseThreshold); - var normal = data.GlucoseValues.Count(v => v >= GlucoseConstants.LowGlucoseThreshold && v <= GlucoseConstants.HighGlucoseThreshold); + var normal = data.GlucoseValues.Count(v => v >= GlucoseConstants.LowGlucoseThreshold && v < GlucoseConstants.HighGlucoseThreshold); var high = data.GlucoseValues.Count(v => v >= GlucoseConstants.HighGlucoseThreshold && v <= GlucoseConstants.VeryHighGlucoseThreshold); var veryHigh = data.GlucoseValues.Count(v => v > GlucoseConstants.VeryHighGlucoseThreshold); - + return data with { TimeInRange = new CalculationDataTimeInRange { - Low = ToPercents(low, data.Count), - Normal = ToPercents(normal, data.Count), - High = ToPercents(high, data.Count), - VeryHigh = ToPercents(veryHigh, data.Count) + Low = ToPercent(low, data.Count), + Normal = ToPercent(normal, data.Count), + High = ToPercent(high, data.Count), + VeryHigh = ToPercent(veryHigh, data.Count) } }; } - private static float ToPercents(float value, float total) + private static float ToPercent(float value, float total) { return value / total * 100; }