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); }
private static string SerializeProtocolAsJsonString(ProtocolDescriptor descriptor) { using (var writer = new StringWriter()) { ProtocolDescriptorJsonStorage.Save(writer, descriptor); return(writer.ToString()); } }
public static ProtocolDescriptor CreateTest() { // This simple protocol consists of one default section // with all requried fields. Note that NCalc expression syntax // isn't enforced by ProtocolDescriptor itself. var section = new SectionDescriptor { Name = PhysicalDataSectionName }; // ...with a numeric weight field section.Values.Add(new ValueDescriptor { Reference = WeightFieldId, Name = WeightFieldName, }); // ...with a numeri height field section.Values.Add(new ValueDescriptor { Reference = HeightFieldId, Name = HeightFieldName, }); // ...and a gender at birth list field var genderField = new ValueDescriptor { Reference = GenderFieldId, Name = GenderFieldName, DefaultValueExpression = "0", }; genderField.AvailableValues.Add(new ListItem { Name = "Male", Value = "0" }); genderField.AvailableValues.Add(new ListItem { Name = "Female", Value = "1" }); section.Values.Add(genderField); // Just create and return this simple protocol, caller will decorate those fields // according to what must be tested. var protocol = new ProtocolDescriptor { Reference = BmiProtocolReference, Name = BmiProtocolName }; protocol.Sections.Add(section); return(protocol); }
public void WithAveragesAggregation_ThenEmptyDatasetsAreZero() { var protocol = new ProtocolDescriptor { Reference = "Aggregation test" }; var section = protocol.Sections.Add("Test"); section.Values.Add(new ValueDescriptor { Reference = "a", PreferredAggregation = AggregationMode.Average }); var aggregator = new DataSetAggregator(protocol); var result = aggregator.Calculate(); Assert.Equal(null, result["a"].Value); }
public static void CheckConsistency(ProtocolDescriptor protocol) { Assert.NotNull(protocol); Assert.Equal(protocol.Sections.Count, 1); Assert.Equal(protocol.Sections.VisitAllValues().Count(), 3); Assert.Empty(protocol.ValidateModel()); Assert.Equal(BmiProtocolReference, protocol.Reference); Assert.Equal(BmiProtocolName, protocol.Name); Assert.Equal(PhysicalDataSectionName, protocol.Sections.Single().Name); Assert.Equal(WeightFieldName, protocol[WeightFieldId].Name); Assert.Equal(HeightFieldName, protocol[HeightFieldId].Name); Assert.Equal(GenderFieldName, protocol[GenderFieldId].Name); }
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); }