public void Validate_WhenMultipleErrors_ReturnsAllErrors() { var validator = new Validator(); var chi = new ItemValidator(); chi.PrimaryConstraint = (PrimaryConstraint)Validator.CreateConstraint("chi", Consequence.Wrong); var prediction = new Prediction(new ChiSexPredictor(), "gender"); chi.AddSecondaryConstraint(prediction); validator.AddItemValidator(chi, "chi", typeof(string)); var age = new ItemValidator(); BoundDouble ageConstraint = (BoundDouble)Validator.CreateConstraint("bounddouble", Consequence.Wrong); ageConstraint.Lower = 0; ageConstraint.Upper = 30; age.AddSecondaryConstraint(ageConstraint); validator.AddItemValidator(age, "age", typeof(int)); var row = new Dictionary <string, object>(); row.Add("chi", TestConstants._INVALID_CHI_CHECKSUM); row.Add("age", 31); row.Add("gender", "F"); ValidationFailure result = validator.Validate(row); Assert.AreEqual(2, result.GetExceptionList().Count); }
public void AddItemValidator_DuplicateCalls_ThrowsException() { var validator = new Validator(); validator.AddItemValidator(new ItemValidator(), "foo", typeof(string)); Assert.Throws <ArgumentException>(() => validator.AddItemValidator(new ItemValidator(), "foo", typeof(string))); }
private Validator CreateChiAndAgeValidators() { var validator = new Validator(); var vChi = new ItemValidator { PrimaryConstraint = (PrimaryConstraint)Validator.CreateConstraint("chi", Consequence.Wrong) }; var vAge = new ItemValidator(); var age = (BoundDouble)Validator.CreateConstraint("bounddouble", Consequence.Wrong); age.Lower = 0; age.Upper = 120; vAge.AddSecondaryConstraint(age); validator.AddItemValidator(vChi, "chi", typeof(string)); validator.AddItemValidator(vAge, "age", typeof(int)); return(validator); }
private static Validator CreateOperationDateValidator(BoundDate b) { var v = new Validator(); var i = new ItemValidator(); i.AddSecondaryConstraint(b); v.AddItemValidator(i, "operation_date", typeof(DateTime)); return(v); }
private static Validator CreateParentDobValidator(BoundDate b) { var v = new Validator(); var i = new ItemValidator(); i.AddSecondaryConstraint(b); v.AddItemValidator(i, "parent_dob", typeof(DateTime)); return(v); }
private static Validator CreateValidatorForNonExistentProperty() { var validator = new Validator(); var itemValidator = new ItemValidator { PrimaryConstraint = (PrimaryConstraint)Validator.CreateConstraint("chi", Consequence.Wrong) }; validator.AddItemValidator(itemValidator, "non-existent", typeof(string)); return(validator); }
private static Validator CreateInitialisedValidatorWithNoPrimaryConstraint(SecondaryConstraint prediction) { var i = new ItemValidator(); i.SecondaryConstraints.Add(prediction); var v = new Validator(); v.AddItemValidator(i, "chi", typeof(string)); return(v); }
public void validation_scenario_CHI_and_age() { // 1. Create a new Validator - this is responsible for validating the entire target object (dictionary) var validator = new Validator(); // 2. Create new ItemValidator - this is respoonsible for validating an individual item in the target object var chi = new ItemValidator(); // 3. Set the ItemValidator's PrimaryConstraint (must be valid CHI) // (using Validator's CreateConstraint() method to create a Primary Constraint (CHI)) chi.PrimaryConstraint = (PrimaryConstraint)Validator.CreateConstraint("chi", Consequence.Wrong); // 4. Add the ItemValidator to our Validator, specifying the item it should validate against validator.AddItemValidator(chi, "chi", typeof(string)); // 5. Create a new ItemValidator (in this case, must be valid CHI and sensible age value) var age = new ItemValidator(); // 6. No PrimaryConstraint. In this case we ADD a SecondaryConstraint (age) var ageConstraint = (BoundDouble)Validator.CreateConstraint("bounddouble", Consequence.Wrong); ageConstraint.Lower = 0; ageConstraint.Upper = 30; age.AddSecondaryConstraint(ageConstraint); // 7. Add the ItemValidator to our Validator, specifying the item it should validate against validator.AddItemValidator(age, "age", typeof(int)); // 8. Create a target object (dictionary) against which to validate var domainObject = new Dictionary <string, object>(); domainObject.Add("chi", TestConstants._VALID_CHI); domainObject.Add("age", 12); // 9. Validate, passing in the target object to be validated against Assert.IsNull(validator.Validate(domainObject)); }
// This code is typically how a client of the API would set up validation for a domain object: // // A domain object contains a number of items, each of which we may wish to validate. // A Validator is responsible for validating a domain object. // Any useful Validator contains at least one ItemValidator. // An ItemValidator is created for each item in the domain object you wish to validate. // An ItemValidator contains a single PrimaryConstraint (e.g. must be valid CHI) and zero or more secondary constraints. private static Validator CreateSimpleChiValidator() { // 1. Create a new Validator, passing in the domain object to be validated (and its type) var validator = new Validator(); // 2. Create a new ItemValidator (in this case, must be valid CHI) var itemValidator = new ItemValidator { PrimaryConstraint = (PrimaryConstraint)Validator.CreateConstraint("chi", Consequence.Wrong) }; itemValidator.PrimaryConstraint.Consequence = Consequence.Wrong; // 3. Add the ItemValidator, specifying the value in the domain object it should validate against validator.AddItemValidator(itemValidator, "chi", typeof(string)); return(validator); }
public void PassValidatorArray_Passes() { Validator v = new Validator(); v.AddItemValidator(new ItemValidator(), "chi", null); v.ItemValidators[0].PrimaryConstraint = new Chi(); DataTable dt = new DataTable(); dt.Columns.Add("chi"); DataRow dr = dt.Rows.Add(); dr["chi"] = TestConstants._VALID_CHI; //validate the row Assert.IsNull(v.Validate(dr)); }
public void SetupValidationOnCatalogue() { Validator v = new Validator(); var iv = new ItemValidator("chi"); iv.PrimaryConstraint = new Chi(); iv.PrimaryConstraint.Consequence = Consequence.Wrong; v.AddItemValidator(iv, "chi", typeof(string)); catalogue.ValidatorXML = v.SaveToXml(); catalogue.TimeCoverage_ExtractionInformation_ID = catalogue.GetAllExtractionInformation(ExtractionCategory.Any) .Single(e => e.GetRuntimeName().Equals("dtCreated")).ID; catalogue.SaveToDatabase(); }
public void simple_integer_bounds() { var v = new Validator(); var b = (BoundDouble)Validator.CreateConstraint("bounddouble", Consequence.Wrong); b.Lower = 5; b.Upper = 120; var i = new ItemValidator(); i.AddSecondaryConstraint(b); v.AddItemValidator(i, "number", typeof(int)); var d = new Dictionary <string, object>(); d.Add("number", 119); Assert.IsNull(v.Validate(d)); }
public void RenameColumn_ThreeColumns_HasCorrectName() { Validator v = new Validator(); v.AddItemValidator(new ItemValidator(), "OldCol2", typeof(string)); //this constraint ensures that OldCol2 is between OldCol1 and OldcCol3 BoundDate boundDate = new BoundDate(); boundDate.LowerFieldName = "OldCol1"; boundDate.UpperFieldName = "OldCol3"; v.ItemValidators[0].SecondaryConstraints.Add(boundDate); Dictionary <string, string> dictionary = new Dictionary <string, string>(); dictionary.Add("OldCol2", "NewCol2"); //before and after rename of col2 Assert.AreEqual(v.ItemValidators[0].TargetProperty, "OldCol2"); v.RenameColumns(dictionary); Assert.AreEqual(v.ItemValidators[0].TargetProperty, "NewCol2"); Assert.AreEqual(((BoundDate)v.ItemValidators[0].SecondaryConstraints[0]).LowerFieldName, "OldCol1"); Assert.AreEqual(((BoundDate)v.ItemValidators[0].SecondaryConstraints[0]).UpperFieldName, "OldCol3"); //now rename col 1 dictionary.Add("OldCol1", "NewCol1"); v.RenameColumns(dictionary); Assert.AreEqual(v.ItemValidators[0].TargetProperty, "NewCol2"); Assert.AreEqual(((BoundDate)v.ItemValidators[0].SecondaryConstraints[0]).LowerFieldName, "NewCol1"); Assert.AreEqual(((BoundDate)v.ItemValidators[0].SecondaryConstraints[0]).UpperFieldName, "OldCol3"); //finally rename col 3 dictionary.Add("OldCol3", "NewCol3"); v.RenameColumns(dictionary); //not strict because we will get not found otherwise since we already renamed the first one Assert.AreEqual(v.ItemValidators[0].TargetProperty, "NewCol2"); Assert.AreEqual(((BoundDate)v.ItemValidators[0].SecondaryConstraints[0]).LowerFieldName, "NewCol1"); Assert.AreEqual(((BoundDate)v.ItemValidators[0].SecondaryConstraints[0]).UpperFieldName, "NewCol3"); }
public void validation_scenario_CHI() { // 1. Create a new Validator - this is responsible for validating the entire target object (dictionary) var validator = new Validator(); // 2. Create new ItemValidator - this is respoonsible for validating an individual item in the target object var chi = new ItemValidator(); // 3. Set the ItemValidator's PrimaryConstraint (must be valid CHI) // (using Validator's CreateConstraint() method to create a Primary Constraint (CHI)) chi.PrimaryConstraint = (PrimaryConstraint)Validator.CreateConstraint("chi", Consequence.Wrong); // 4. Add the ItemValidator to our Validator, specifying the item it should validate against validator.AddItemValidator(chi, "chi", typeof(string)); // 5. Create a target object (dictionary) against which to validate var domainObject = new Dictionary <string, object>(); domainObject.Add("chi", TestConstants._VALID_CHI); // 6. Validate, passing in the target object to be validated against - ValidationFailure or null is returned Assert.IsNull(validator.Validate(domainObject)); }