diff --git a/src/PKSim.Assets/PKSimConstants.cs b/src/PKSim.Assets/PKSimConstants.cs index 4eca66f03..079d47882 100644 --- a/src/PKSim.Assets/PKSimConstants.cs +++ b/src/PKSim.Assets/PKSimConstants.cs @@ -1449,24 +1449,24 @@ public static class Parameter public static string ValueShouldBeGreaterThanOrEqualToZero(string parameterName) => $"{parameterName} value should be greater than or equal to 0."; - public static string MinGreaterThanDbMinValue(string parameterName, double? dbMinValue, string unit) + public static string MinGreaterThanDbMinValue(string parameterName, string displayMinValue, string unit) { - return $"Minimum value for {parameterName} should be greater than or equal to {dbMinValue} {unit}."; + return $"Minimum value for {parameterName} should be greater than or equal to {displayMinValue} {unit}."; } - public static string MaxGreaterThanDbMinValue(string parameterName, double? dbMinValue, string unit) + public static string MaxGreaterThanDbMinValue(string parameterName, string displayMinValue, string unit) { - return $"Maximum value for {parameterName} should be greater than or equal to {dbMinValue} {unit}."; + return $"Maximum value for {parameterName} should be greater than or equal to {displayMinValue} {unit}."; } - public static string MaxLessThanDbMaxValue(string parameterName, double? dbMaxValue, string unit) + public static string MaxLessThanDbMaxValue(string parameterName, string displayMaxValue, string unit) { - return $"Maximum value for {parameterName} should be less than or equal to {dbMaxValue} {unit}."; + return $"Maximum value for {parameterName} should be less than or equal to {displayMaxValue} {unit}."; } - public static string MinLessThanDbMaxValue(string parameterName, double? dbMaxValue, string unit) + public static string MinLessThanDbMaxValue(string parameterName, string displayMaxValue, string unit) { - return $"Minimum value for {parameterName} should be less than or equal to {dbMaxValue} {unit}."; + return $"Minimum value for {parameterName} should be less than or equal to {displayMaxValue} {unit}."; } public static string ValueSmallerThanMax(string parameterName, string value, string unit) diff --git a/src/PKSim.Core/Model/ParameterRange.cs b/src/PKSim.Core/Model/ParameterRange.cs index b73aa4926..d69d31108 100644 --- a/src/PKSim.Core/Model/ParameterRange.cs +++ b/src/PKSim.Core/Model/ParameterRange.cs @@ -2,6 +2,7 @@ using OSPSuite.Core.Domain; using OSPSuite.Core.Domain.Services; using OSPSuite.Core.Domain.UnitSystem; +using OSPSuite.Utility.Format; using OSPSuite.Utility.Reflection; using OSPSuite.Utility.Validation; using PKSim.Assets; @@ -18,6 +19,7 @@ public class ParameterRange : Notifier, IValidatable, IUpdatable public double? MaxValue { get; set; } public IDimension Dimension { get; set; } public Unit Unit { get; set; } + private static readonly NumericFormatter _numericFormatter = new NumericFormatter(NumericFormatterOptions.Instance); public ParameterRange() { @@ -57,6 +59,10 @@ public double? MaxValueInDisplayUnit set => MaxValue = baseValueFrom(value); } + public double? DbMinValueInDisplayUnit => displayValueFrom(DbMinValue); + + public double? DbMaxValueInDisplayUnit => displayValueFrom(DbMaxValue); + private double? baseValueFrom(double? displayValue) { if (!displayValue.HasValue) @@ -86,71 +92,35 @@ public override string ToString() private static class AllRules { - public static IBusinessRule MinLessThanMax - { - get - { - return CreateRule.For() - .Property(item => item.MinValueInDisplayUnit) - .WithRule(minLessThanMax) - .WithError((param, value) => PKSimConstants.Rules.Parameter.MinLessThanMax(param.ParameterName)); - } - } - - public static IBusinessRule MaxGreaterThanMin - { - get - { - return CreateRule.For() - .Property(item => item.MaxValueInDisplayUnit) - .WithRule(maxGreaterThanMin) - .WithError((param, value) => PKSimConstants.Rules.Parameter.MaxGreaterThanMin(param.ParameterName)); - } - } - - public static IBusinessRule MinGreaterThanDbMin - { - get - { - return CreateRule.For() - .Property(item => item.MinValueInDisplayUnit) - .WithRule(minGreaterThanDbMin) - .WithError((param, value) => PKSimConstants.Rules.Parameter.MinGreaterThanDbMinValue(param.ParameterName, param.DbMinValue, param.Unit.ToString())); - } - } - - public static IBusinessRule MaxLessThanDbMax - { - get - { - return CreateRule.For() - .Property(item => item.MaxValueInDisplayUnit) - .WithRule(maxLessThanDbMax) - .WithError((param, value) => PKSimConstants.Rules.Parameter.MaxLessThanDbMaxValue(param.ParameterName, param.DbMaxValue, param.Unit.ToString())); - } - } - - public static IBusinessRule MinLessThanDbMax - { - get - { - return CreateRule.For() - .Property(item => item.MinValueInDisplayUnit) - .WithRule(minLessThanDbMax) - .WithError((param, value) => PKSimConstants.Rules.Parameter.MinLessThanDbMaxValue(param.ParameterName, param.DbMaxValue, param.Unit.ToString())); - } - } - - public static IBusinessRule MaxGreaterThanDbMin - { - get - { - return CreateRule.For() - .Property(item => item.MaxValueInDisplayUnit) - .WithRule(maxGreaterThanDbMin) - .WithError((param, value) => PKSimConstants.Rules.Parameter.MaxGreaterThanDbMinValue(param.ParameterName, param.DbMinValue, param.Unit.ToString())); - } - } + public static IBusinessRule MinLessThanMax { get; } = CreateRule.For() + .Property(item => item.MinValueInDisplayUnit) + .WithRule(minLessThanMax) + .WithError((param, value) => PKSimConstants.Rules.Parameter.MinLessThanMax(param.ParameterName)); + + public static IBusinessRule MaxGreaterThanMin { get; } = CreateRule.For() + .Property(item => item.MaxValueInDisplayUnit) + .WithRule(maxGreaterThanMin) + .WithError((param, value) => PKSimConstants.Rules.Parameter.MaxGreaterThanMin(param.ParameterName)); + + public static IBusinessRule MinGreaterThanDbMin { get; } = CreateRule.For() + .Property(item => item.MinValueInDisplayUnit) + .WithRule(minGreaterThanDbMin) + .WithError((param, value) => PKSimConstants.Rules.Parameter.MinGreaterThanDbMinValue(param.ParameterName, displayFor(param.DbMinValueInDisplayUnit), param.Unit.ToString())); + + public static IBusinessRule MaxLessThanDbMax { get; } = CreateRule.For() + .Property(item => item.MaxValueInDisplayUnit) + .WithRule(maxLessThanDbMax) + .WithError((param, value) => PKSimConstants.Rules.Parameter.MaxLessThanDbMaxValue(param.ParameterName, displayFor(param.DbMaxValueInDisplayUnit), param.Unit.ToString())); + + public static IBusinessRule MinLessThanDbMax { get; } = CreateRule.For() + .Property(item => item.MinValueInDisplayUnit) + .WithRule(minLessThanDbMax) + .WithError((param, value) => PKSimConstants.Rules.Parameter.MinLessThanDbMaxValue(param.ParameterName, displayFor(param.DbMaxValueInDisplayUnit), param.Unit.ToString())); + + public static IBusinessRule MaxGreaterThanDbMin { get; } = CreateRule.For() + .Property(item => item.MaxValueInDisplayUnit) + .WithRule(maxGreaterThanDbMin) + .WithError((param, value) => PKSimConstants.Rules.Parameter.MaxGreaterThanDbMinValue(param.ParameterName, displayFor(param.DbMinValueInDisplayUnit), param.Unit.ToString())); private static bool minLessThanMax(ParameterRange param, double? minValueInDisplayUnit) { @@ -212,6 +182,8 @@ private static bool doesNotNeedCheck(double? referenceValue, double? valueToChec return false; } + + private static string displayFor(double? value) => _numericFormatter.Format(value ?? double.NaN); } public virtual void UpdatePropertiesFrom(IUpdatable source, ICloneManager cloneManager) diff --git a/src/PKSim.Presentation/DTO/Populations/ParameterRangeDTO.cs b/src/PKSim.Presentation/DTO/Populations/ParameterRangeDTO.cs index 4a1949e94..dcbd9f219 100644 --- a/src/PKSim.Presentation/DTO/Populations/ParameterRangeDTO.cs +++ b/src/PKSim.Presentation/DTO/Populations/ParameterRangeDTO.cs @@ -9,7 +9,7 @@ namespace PKSim.Presentation.DTO.Populations { public class ParameterRangeDTO : DxValidatableDTO { - public ParameterRange ParameterRange { get; private set; } + public ParameterRange ParameterRange { get; } public ParameterRangeDTO(ParameterRange parameterRange) : base(parameterRange) @@ -17,31 +17,25 @@ public ParameterRangeDTO(ParameterRange parameterRange) ParameterRange = parameterRange; } - public string ParameterName - { - get { return ParameterRange.ParameterName; } - } + public string ParameterName => ParameterRange.ParameterName; - public string ParameterDisplayName - { - get { return ParameterRange.ParameterDisplayName; } - } + public string ParameterDisplayName => ParameterRange.ParameterDisplayName; public double? MinValueInDisplayUnit { - get { return ParameterRange.MinValueInDisplayUnit; } - set { ParameterRange.MinValueInDisplayUnit = value; } + get => ParameterRange.MinValueInDisplayUnit; + set => ParameterRange.MinValueInDisplayUnit = value; } public double? MaxValueInDisplayUnit { - get { return ParameterRange.MaxValueInDisplayUnit; } - set { ParameterRange.MaxValueInDisplayUnit = value; } + get => ParameterRange.MaxValueInDisplayUnit; + set => ParameterRange.MaxValueInDisplayUnit = value; } public Unit Unit { - get { return ParameterRange.Unit; } + get => ParameterRange.Unit; set { var minDisplayValue = MinValueInDisplayUnit; @@ -52,10 +46,7 @@ public Unit Unit } } - public bool IsDiscrete - { - get { return ParameterRange.IsAnImplementationOf(); } - } + public bool IsDiscrete => ParameterRange.IsAnImplementationOf(); public IEnumerable ListOfValues { diff --git a/tests/PKSim.Tests/Core/ParameterRangeSpecs.cs b/tests/PKSim.Tests/Core/ParameterRangeSpecs.cs index a002f10be..a7505be0b 100644 --- a/tests/PKSim.Tests/Core/ParameterRangeSpecs.cs +++ b/tests/PKSim.Tests/Core/ParameterRangeSpecs.cs @@ -1,8 +1,9 @@ -using FakeItEasy; +using NUnit.Framework; using OSPSuite.BDDHelper; using OSPSuite.BDDHelper.Extensions; +using OSPSuite.Utility.Format; using OSPSuite.Utility.Validation; -using NUnit.Framework; +using PKSim.Assets; using PKSim.Core.Model; namespace PKSim.Core @@ -11,7 +12,11 @@ public abstract class concern_for_ParameterRange : ContextSpecification x.MinValueInDisplayUnit, valueInHours).IsEmpty.ShouldBeEqualTo(valid); + InitRange(null, max, dbMin, dbMax); + sut.Validate(x => x.MinValueInDisplayUnit, valueInHours).IsEmpty.ShouldBeEqualTo(valid); + } + + [TestCase(90, 1)] + public void should_return_a_message_when_invalid_using_the_display_database_values(double? dbMin, double? valueInHours) + { + InitRange(null, null, dbMin); + var formatter = new NumericFormatter(NumericFormatterOptions.Instance); + var validation = sut.Validate(x => x.MinValueInDisplayUnit, valueInHours); + validation.IsEmpty.ShouldBeEqualTo(false); + validation.Message.ShouldBeEqualTo(PKSimConstants.Rules.Parameter.MinGreaterThanDbMinValue(sut.ParameterName, formatter.Format(sut.DbMinValueInDisplayUnit.Value), sut.Unit.Name)); } } @@ -45,10 +60,20 @@ public class When_checking_if_a_value_in_display_unit_for_maximum_is_valid : con [TestCase(null, 60.0, 120.0, 3.0, false)] [TestCase(80.0, 60.0, 120.0, 1.0, false)] [TestCase(60.0, null, null, 2.0, true)] - public void should_return_the_validation_according_to_acceptable_range(double? min, double? dbMin, double? dbMax, double? valueInHours, bool valid) + public void should_return_the_validation_according_to_acceptable_range(double? min, double? dbMin, double? dbMax, double? valueInHours, bool valid) { InitRange(min, null, dbMin, dbMax); sut.Validate(x => x.MaxValueInDisplayUnit, valueInHours).IsEmpty.ShouldBeEqualTo(valid); } + + [TestCase(90, 2)] + public void should_return_a_message_when_invalid_using_the_display_database_values(double? dbMax, double? valueInHours) + { + InitRange(null, null, null, dbMax); + var formatter = new NumericFormatter(NumericFormatterOptions.Instance); + var validation = sut.Validate(x => x.MaxValueInDisplayUnit, valueInHours); + validation.IsEmpty.ShouldBeEqualTo(false); + validation.Message.ShouldBeEqualTo(PKSimConstants.Rules.Parameter.MaxLessThanDbMaxValue(sut.ParameterName, formatter.Format(sut.DbMaxValueInDisplayUnit.Value), sut.Unit.Name)); + } } -} \ No newline at end of file +} \ No newline at end of file