public void GivenInvalidArgumentsForNewValue_ThenExceptionIsThrown() { var protocol = ProtocolFactory.CreateTest(); var dataset = new DataSet(protocol); Assert.Throws <ArgumentException>(() => dataset.AddValue(new ValueDescriptor { Reference = "a" }, 1)); Assert.Throws <ArgumentNullException>(() => dataset.AddValue((ValueDescriptor)null, 1)); Assert.Throws <ArgumentNullException>(() => dataset.AddValue((string)null, 1)); }
public void WithCountAsAggregation_GivenOneNullValues_ThenItCanBeExcludedFromCount() { var protocol = new ProtocolDescriptor { Reference = "Aggregation test" }; var section = protocol.Sections.Add("Test"); section.Values.Add(new ValueDescriptor { Reference = "a", PreferredAggregation = AggregationMode.Count }); var dataset1 = new DataSet(protocol); dataset1.AddValue("a", 10); var dataset2 = new DataSet(protocol); dataset2.AddValue("a", null); // Null value is included in count var aggregator = new DataSetAggregator(protocol); var result = aggregator.Accumulate(dataset1, dataset2).Calculate(); Assert.Equal(2, result["a"].Value); // Null value is excluded from count aggregator.Clear(); aggregator.Options = AggregationOptions.Default | AggregationOptions.ExcludeNullValuesFromCount; result = aggregator.Accumulate(dataset1, dataset2).Calculate(); Assert.Equal(1, result["a"].Value); }
public void GivenValidationWithDependencies_ThenItIsCalculatedCorrectly(int a, int b) { var protocol = ProtocolFactory.CreateTest(); var section = protocol.Sections.First(); section.Values.Add(new ValueDescriptor { Reference = "a" }); section.Values.Add(new ValueDescriptor { Reference = "b", ValidIfExpression = "this < a" }); var dataset = new DataSet(protocol); dataset.AddValue("a", a); dataset.AddValue("b", b); dataset.Calculate(); Assert.Equal(b < a ? 0 : 1, dataset.Issues.Count); }
public void GivenExpressionWithThisKeyword_ThenItIsSynonimOfCurrentValueId() { var protocol = ProtocolFactory.CreateTest(); var section = protocol.Sections.First(); section.Values.Add(new ValueDescriptor { Reference = "a", ValidIfExpression = "this == 1 and this == a" }); section.Values.Add(new ValueDescriptor { Reference = "b", ValidIfExpression = "this == 2 and this == b" }); var dataset = new DataSet(protocol); dataset.AddValue("a", 1); dataset.AddValue("b", 2); dataset.Calculate(); Assert.Equal(0, dataset.Issues.Count); }
public void WhenStoreValidValue_ThenItCanBeRead() { var protocol = ProtocolFactory.CreateTest(); var a = new ValueDescriptor { Reference = "a" }; var b = new ValueDescriptor { Reference = "b" }; protocol.Sections.First().Values.Add(a); protocol.Sections.First().Values.Add(b); var dataset = new DataSet(protocol); dataset.AddValue(a, 1.0); dataset.AddValue("b", 2.0); dataset.Calculate(); Assert.Equal(1.0, Convert.ToDouble(dataset["a"].Value)); Assert.Equal(2.0, Convert.ToDouble(dataset["b"].Value)); }
public void GivenExternalValue_WhenInvalid_ThenAddError() { var protocol = ProtocolFactory.CreateTest(); var section = protocol.Sections.First(); section.Values.Add(new ValueDescriptor { Reference = "a", ValidIfExpression = "this > 5" }); var dataset = new DataSet(protocol); dataset.AddValue("a", 1); dataset.Calculate(); Assert.Equal(1, dataset.Issues.Count(x => x.Item.Reference == "a" && x.Severity == IssueSeverity.ValidationError)); Assert.Equal(1, dataset.Issues.Count); }
public void GivenValue_WhenAlertedAndInvalid_ThenThereAreIssuesForBoth() { var protocol = ProtocolFactory.CreateTest(); var section = protocol.Sections.First(); section.Values.Add(new ValueDescriptor { Reference = "a", WarningIfExpression = "this > 3", ValidIfExpression = "this < 5" }); var dataset = new DataSet(protocol); dataset.AddValue("a", 6); dataset.Calculate(); Assert.Equal(1, dataset.Issues.Count(x => x.Severity == IssueSeverity.ValidationError)); Assert.Equal(1, dataset.Issues.Count(x => x.Severity == IssueSeverity.Warning)); Assert.Equal(2, dataset.Issues.Count); }
public void GivenExpressionWithRequired_ThenItIsTrueIfValueExists() { var protocol = ProtocolFactory.CreateTest(); var section = protocol.Sections.First(); section.Values.Add(new ValueDescriptor { Reference = "a", ValidIfExpression = "required" }); var dataset = new DataSet(protocol); dataset.Calculate(); Assert.Equal(1, dataset.Issues.Count); dataset = new DataSet(protocol); dataset.AddValue("a", 1); dataset.Calculate(); Assert.Equal(0, dataset.Issues.Count); }
public void GivenInvalidExternalValuesAndDependantCalculatedValue_ThenDependantValueIsNotCalculated() { var protocol = ProtocolFactory.CreateTest(); var section = protocol.Sections.First(); section.Values.Add(new ValueDescriptor { Reference = "a", ValidIfExpression = "this < 5" }); section.Values.Add(new ValueDescriptor { Reference = "b", CalculatedValueExpression = "a * 2", ValidIfExpression = "this < 10" }); var dataset = new DataSet(protocol); dataset.AddValue("a", 5); dataset.Calculate(); Assert.Equal(1, dataset.Issues.Count(x => x.Item.Reference == "a" && x.Severity == IssueSeverity.ValidationError)); Assert.Equal(1, dataset.Issues.Count); }
public void GivenCalculatedValueWithValidation_ThenValidationIsApplied() { var protocol = ProtocolFactory.CreateTest(); var section = protocol.Sections.First(); section.Values.Add(new ValueDescriptor { Reference = "a", ValidIfExpression = "this < 5" }); section.Values.Add(new ValueDescriptor { Reference = "b", CalculatedValueExpression = "a * 2", ValidIfExpression = "this < 6" }); // 4 is valid for a but it will fail validation for b. Another test will ensure that validation for // b is not even evaluated if validation for a isn't passed. var dataset = new DataSet(protocol); dataset.AddValue("a", 4); dataset.Calculate(); Assert.Equal(1, dataset.Issues.Count(x => x.Item.Reference == "b" && x.Severity == IssueSeverity.ValidationError)); Assert.Equal(1, dataset.Issues.Count); }
public void WithNoAggregation_ThenCustomAggregationFunctionCanBeUsed() { var protocol = new ProtocolDescriptor { Reference = "Aggregation test" }; var section = protocol.Sections.Add("Test"); section.Values.Add(new ValueDescriptor { Reference = "a", PreferredAggregation = AggregationMode.None, AggregationExpression = @"sequence ( let('mean', average(values)), let('n', count(values) - 1), sqrt(sum(values, pow(value - mean, 2)) / n) )" }); var dataset1 = new DataSet(protocol); dataset1.AddValue("a", 10); var dataset2 = new DataSet(protocol); dataset2.AddValue("a", 20); var aggregator = new DataSetAggregator(protocol); var result = aggregator.Accumulate(dataset1, dataset2).Calculate(); if (result.Issues.HasErrors) { throw new InvalidOperationException(String.Join("\n", result.Issues.Select(x => x.Message))); } Assert.Equal(7.07, (double)result["a"].Value, 2); }
public void WithSumAsAggregation_ThenNullValuesEqualToZero() { var protocol = new ProtocolDescriptor { Reference = "Aggregation test" }; var section = protocol.Sections.Add("Test"); section.Values.Add(new ValueDescriptor { Reference = "a", PreferredAggregation = AggregationMode.Sum }); var dataset1 = new DataSet(protocol); dataset1.AddValue("a", 10); var dataset2 = new DataSet(protocol); dataset2.AddValue("a", null); var aggregator = new DataSetAggregator(protocol); var result = aggregator.Accumulate(dataset1, dataset2).Calculate(); Assert.Equal(10.0, result["a"].Value); }
public void WithCountAsAggregation_GivenDoubleValues() { var protocol = new ProtocolDescriptor { Reference = "Aggregation test" }; var section = protocol.Sections.Add("Test"); section.Values.Add(new ValueDescriptor { Reference = "a", PreferredAggregation = AggregationMode.Count }); var dataset1 = new DataSet(protocol); dataset1.AddValue("a", 10); var dataset2 = new DataSet(protocol); dataset2.AddValue("a", 5); var aggregator = new DataSetAggregator(protocol); var result = aggregator.Accumulate(dataset1, dataset2).Calculate(); Assert.Equal(2, result["a"].Value); }
public void WhenCalculatedValueDependsOnValuesWithDefaultAndExternalValues_ThenItIsCalculatedCorrectly() { var protocol = ProtocolFactory.CreateTest(); var section = protocol.Sections.First(); section.Values.Add(new ValueDescriptor { Reference = "a" }); section.Values.Add(new ValueDescriptor { Reference = "b", DefaultValueExpression = "2 + 2" }); section.Values.Add(new ValueDescriptor { Reference = "c", CalculatedValueExpression = "a + b" }); var dataset = new DataSet(protocol); dataset.AddValue("a", 2.0); dataset.Calculate(); Assert.Equal(2.0, Convert.ToDouble(dataset["a"].Value)); Assert.Equal(4.0, Convert.ToDouble(dataset["b"].Value)); Assert.Equal(6.0, Convert.ToDouble(dataset["c"].Value)); }