public void GivenModelWithMultipleErrors_ThenTheyAreAllDetectedWhenCalculating() { var protocol = ProtocolFactory.CreateTest(); // Here we're trying to detect an error on EnabledIfExpression. CalculatedValueExpression is invalid but it won't be evaluated protocol.Sections.First().Values.Add(new ValueDescriptor { Reference = "Value1", CalculatedValueExpression = "invalid", EnabledIfExpression = "invalid" }); // This will add an error for an invalid DefaultValueExpression protocol.Sections.First().Values.Add(new ValueDescriptor { Reference = "Value2", DefaultValueExpression = "invalid" }); // This will add an error for an invalid CalculatedValueExpression protocol.Sections.First().Values.Add(new ValueDescriptor { Reference = "Value3", CalculatedValueExpression = "invalid", EnabledIfExpression = "true" }); var dataset = new DataSet(protocol); dataset.Calculate(); // Note that the same expression may cause more than one issue if it is evaluated // multiple times (especially EnabledIfExpression because it's used to filter out the list // of descriptors). Errors in model are a development thing that won't happen in production // then it's easier to leave this as-is than changing design to handle this corner case. Assert.True(dataset.Issues.Count > 3); }
public void WhenWriteJson_ThenOutputContainsExceptedText() { var json = SerializeProtocolAsJsonString(ProtocolFactory.CreateTest()); // Raw validation using known substrings Assert.True(!String.IsNullOrWhiteSpace(json)); Assert.Contains(ProtocolFactory.HeightFieldName, json); Assert.Contains(ProtocolFactory.WeightFieldName, json); Assert.Contains(ProtocolFactory.GenderFieldName, json); }
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 WhenReadingFromKnownJson_ThenProtocolIsWhatExpected() { var json = SerializeProtocolAsJsonString(ProtocolFactory.CreateTest()); using (var reader = new StringReader(json)) { var protocol = ProtocolDescriptorJsonStorage.Load(reader); ProtocolFactory.CheckConsistency(protocol); Assert.Equal(json, SerializeProtocolAsJsonString(protocol)); } }
public void GivenAnExpressionWithMultipleArgumentsForIsNull_ThenItReturnsTrueIfAnyArgumentIsNull() { var protocol = ProtocolFactory.CreateTest(); protocol.Sections.First().Values.Add(new ValueDescriptor { Reference = "a", ValidIfExpression = "isnull(1, 'text', null)" }); var dataset = new DataSet(protocol); dataset.Calculate(); Assert.Equal(0, dataset.Issues.Count); }
public void GivenValidationMessageWithoutExpression_ThenAddIssue() { var protocol = ProtocolFactory.CreateTest(); Assert.Empty(protocol.ValidateModel()); var value = protocol[ProtocolFactory.WeightFieldId]; value.WarningMessage = "test"; // This will result in a warning because of missing WarningIfExpression value.ValidationMessage = "test"; // This will result in a warning because of missing ValidIfExpression Assert.Equal(2, protocol.ValidateModel().Count(x => x.Severity == IssueSeverity.Warning)); }
public void GivenPreferredAggregationWithAggregationExpression_ThenAddIssue() { var protocol = ProtocolFactory.CreateTest(); Assert.Empty(protocol.ValidateModel()); var value = protocol[ProtocolFactory.WeightFieldId]; value.PreferredAggregation = AggregationMode.Sum; value.AggregationExpression = "sum(values)"; Assert.Equal(1, protocol.ValidateModel().Count(x => x.Severity == IssueSeverity.Warning)); }
public void GivenExpressionWithNullCoalescingForUnknownValue_ThenItIsNullInsteadOfThrowing() { var protocol = ProtocolFactory.CreateTest(); var section = protocol.Sections.First(); section.Values.Add(new ValueDescriptor { Reference = "a", ValidIfExpression = "isnull([invalid?])" }); var dataset = new DataSet(protocol); dataset.Calculate(); Assert.Equal(0, dataset.Issues.Count); }
public void WhenSavingAndLoadingWithUtf16Encoding_ThenRetrievedObjectIsWhatExpected() { var tempFilePath = Path.GetTempFileName(); try { var json = SerializeProtocolAsJsonString(ProtocolFactory.CreateTest()); File.WriteAllText(tempFilePath, json, Encoding.Unicode); ProtocolFactory.CheckConsistency(ProtocolDescriptorJsonStorage.Load(tempFilePath, Encoding.Unicode)); } finally { File.Delete(tempFilePath); } }
public void GivenEditableValueWithoutReferenceId_ThenAddIssue() { var protocol = ProtocolFactory.CreateTest(); Assert.Empty(protocol.ValidateModel()); // Calculated values cannot have a default value var value = new ValueDescriptor { Reference = "", Name = "Test" }; protocol.Sections.First().Values.Add(value); Assert.Equal(1, protocol.ValidateModel().Count()); }
public void GivenValuesWithDuplicatedReferenceId_ThenAddIssue() { var protocol = ProtocolFactory.CreateTest(); Assert.Empty(protocol.ValidateModel()); protocol.Sections.First().Values.Add(new ValueDescriptor { Reference = ProtocolFactory.WeightFieldId, Name = "Test 1" }); protocol.Sections.First().Values.Add(new ValueDescriptor { Reference = ProtocolFactory.HeightFieldId, Name = "Test 2" }); Assert.Equal(2, protocol.ValidateModel().Count()); }
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 GivenCalculatedValueWithDefault_ThenAddIssue() { var protocol = ProtocolFactory.CreateTest(); Assert.Empty(protocol.ValidateModel()); // Calculated values cannot have a default value var value = new ValueDescriptor { Reference = "Test", Name = "Test" }; value.CalculatedValueExpression = "[Weight] / [Height]"; value.DefaultValueExpression = "10"; protocol.Sections.First().Values.Add(value); Assert.Equal(1, protocol.ValidateModel().Count()); }
public void WhenSavingWithDefaultSettings_ThenOutputFileIsUtf8Encoded() { var tempFilePath = Path.GetTempFileName(); try { var protocol = ProtocolFactory.CreateTest(); ProtocolDescriptorJsonStorage.Save(tempFilePath, protocol); Assert.True((new FileInfo(tempFilePath)).Length > 0); Assert.Equal(SerializeProtocolAsJsonString(protocol), File.ReadAllText(tempFilePath, Encoding.UTF8)); } finally { File.Delete(tempFilePath); } }
public void GivenModelWithErrors_ThenTheyAreDetectedWhenBuildingDataSet() { var protocol = ProtocolFactory.CreateTest(); // Duplicated IDs are not allowed... protocol.Sections.First().Values.Add(new ValueDescriptor { Reference = "Value" }); protocol.Sections.First().Values.Add(new ValueDescriptor { Reference = "Value" }); // We expect the dataset to validate its input model, create an issue and be inusable... var dataset = new DataSet(protocol); 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 GivenDataSetWithMissingData_ThenTheyAreDetectedWhenCalculating() { var protocol = ProtocolFactory.CreateTest(); protocol.Sections.First().Values.Add(new ValueDescriptor { Reference = "a" }); protocol.Sections.First().Values.Add(new ValueDescriptor { Reference = "b", CalculatedValueExpression = "a * 2" }); var dataset = new DataSet(protocol); dataset.Calculate(); // Value for "a" is required to calculate "b" but it has not been provided Assert.Equal(1, dataset.Issues.Count); }
public void WhenValuesHaveCalculatedOrLiteralDefaults_ThenTheyAreCalculatedAndCanBeRead() { var protocol = ProtocolFactory.CreateTest(); protocol.Sections.First().Values.Add(new ValueDescriptor { Reference = "Literal", DefaultValueExpression = "2" }); protocol.Sections.First().Values.Add(new ValueDescriptor { Reference = "LiteralExpression", DefaultValueExpression = "2 + 2" }); var dataset = new DataSet(protocol); dataset.Calculate(); Assert.Equal(2.0, Convert.ToDouble(dataset["Literal"].Value)); Assert.Equal(4.0, Convert.ToDouble(dataset["LiteralExpression"].Value)); }
public void GivenListOfAvailableValuesForCalculatedField_ThenAddIssue() { var protocol = ProtocolFactory.CreateTest(); Assert.Empty(protocol.ValidateModel()); // Calculated values cannot have a list of available values var value = new ValueDescriptor { Reference = "Test", Name = "Test" }; value.CalculatedValueExpression = "[Weight] / [Height]"; value.AvailableValues.Add(new ListItem { Name = "1", Value = "1" }); protocol.Sections.First().Values.Add(value); Assert.Equal(1, protocol.ValidateModel().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 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 GivenStringFieldAggregatedWithAverage_ThenAddIssue() { var protocol = ProtocolFactory.CreateTest(); Assert.Empty(protocol.ValidateModel()); var value = new ValueDescriptor { Reference = "Name", Name = "Name" }; value.Type = TypeOfValue.String; value.PreferredAggregation = AggregationMode.Average; protocol.Sections.First().Values.Add(value); // Strings cannot be averaged Assert.Equal(1, protocol.ValidateModel().Count()); // ...unless we supply a custom transformation value.TransformationForAggregationExpression = "length(this)"; // Code is not really parsed during model validation... Assert.Equal(0, protocol.ValidateModel().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 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 WhenCalculatedValueDependsOnValuesWithDefault_ThenItIsCalculatedCorrectly() { var protocol = ProtocolFactory.CreateTest(); var section = protocol.Sections.First(); section.Values.Add(new ValueDescriptor { Reference = "a", DefaultValueExpression = "2" }); 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.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)); }
public void GivenValidProtocol_ThenItCanBeCreated() { ProtocolFactory.CheckConsistency(ProtocolFactory.CreateTest()); }