public void TestPerformValidation_Length() { var customer = new Customer { Name = "Maryika", IsFinraMember = true }; var validationController = new ValidationController(); validationController.PerformValidation(customer); Assert.IsFalse(customer.Errors.All.Any()); CollectionAssert.IsEmpty(customer.Errors.All); customer.Name = ""; validationController.PerformValidation(customer); Assert.IsTrue(customer.Errors.All.Any()); Assert.AreEqual(2, customer.Errors.Count); CollectionAssert.AreEquivalent(Customer.NameErrors, customer.Errors[() => customer.Name]); customer.Name = "Some long name exceeds max"; validationController.PerformValidation(customer); Assert.IsTrue(customer.Errors.All.Any()); Assert.AreEqual(1, customer.Errors.Count); Assert.AreEqual("Name is too long (maximum is 12).", customer.Errors[() => customer.Name].First()); var account = new Account { Number = "A0000000001" }; validationController.PerformValidation(account); Assert.IsTrue(account.Errors.All.Any()); Assert.AreEqual(1, account.Errors.Count); Assert.AreEqual("Number is too long (maximum is 10).", account.Errors[() => account.Number].First()); account.Number = null; validationController.PerformValidation(account); Assert.IsFalse(account.Errors.All.Any()); CollectionAssert.IsEmpty(account.Errors.All); var address = new Address(); validationController.PerformValidation(address); Assert.IsTrue(address.Errors.All.Any()); Assert.AreEqual(1, address.Errors.Count); Assert.AreEqual("StateCode should be 2 characters.", address.Errors[() => address.StateCode].First()); address.StateCode = "NY"; validationController.PerformValidation(address); Assert.IsFalse(address.Errors.All.Any()); CollectionAssert.IsEmpty(address.Errors.All); }
public void TestValidate_String_WrongLength() { const string WRONG_STATE_CODE_LENGTH = "StateCode should be 2 characters."; string error; var stateCodeProperty = typeof(Address).GetProperty("StateCode"); var address = new Address { StateCode = "NY1" }; var stateValidator = new LengthValidator(2); Assert.IsFalse(stateValidator.Validate(address, stateCodeProperty, out error)); Assert.AreEqual(WRONG_STATE_CODE_LENGTH, error); address.StateCode = null; Assert.IsFalse(stateValidator.Validate(address, stateCodeProperty, out error)); Assert.AreEqual(WRONG_STATE_CODE_LENGTH, error); address.StateCode = String.Empty; Assert.IsFalse(stateValidator.Validate(address, stateCodeProperty, out error)); Assert.AreEqual(WRONG_STATE_CODE_LENGTH, error); address.StateCode = " "; Assert.IsFalse(stateValidator.Validate(address, stateCodeProperty, out error)); Assert.AreEqual(WRONG_STATE_CODE_LENGTH, error); address.StateCode = "NY"; Assert.IsTrue(stateValidator.Validate(address, stateCodeProperty, out error)); Assert.IsNull(error); stateValidator.WrongLengthMessage = "Only {{count}} symbols length allowed"; address.StateCode = "N"; Assert.IsFalse(stateValidator.Validate(address, stateCodeProperty, out error)); Assert.AreEqual("Only 2 symbols length allowed", error); }
public void TestValidate_String() { string error; var accountNumberProperty = typeof(Account).GetProperty("Number"); var validator = new LengthValidator(6, 20); var account = new Account { Number = UnitTestHelper.ACCOUNT_NUMBER }; Assert.IsTrue(validator.Validate(account, accountNumberProperty, out error)); Assert.IsNull(error); account.Number = "A12345"; Assert.IsTrue(validator.Validate(account, accountNumberProperty, out error)); Assert.IsNull(error); account.Number = "A0000000000"; Assert.IsTrue(validator.Validate(account, accountNumberProperty, out error)); Assert.IsNull(error); account.Number = "12345678901234567890"; Assert.IsTrue(validator.Validate(account, accountNumberProperty, out error)); Assert.IsNull(error); var customerNameProperty = typeof(Customer).GetProperty("Name"); validator = new LengthValidator(4); var customer = new Customer { Name = "John" }; Assert.IsTrue(validator.Validate(customer, customerNameProperty, out error)); Assert.IsNull(error); customer.Name = " "; Assert.IsTrue(validator.Validate(customer, customerNameProperty, out error)); Assert.IsNull(error); var addressStateCodeProperty = typeof(Address).GetProperty("StateCode"); var stateCodeValidator = new LengthValidator(2); var address = new Address { StateCode = "NJ" }; Assert.IsTrue(stateCodeValidator.Validate(address, addressStateCodeProperty, out error)); Assert.IsNull(error); address.StateCode = " "; Assert.IsTrue(stateCodeValidator.Validate(address, addressStateCodeProperty, out error)); Assert.IsNull(error); }
public void TestValidate_NonStringType() { string error; // Value type property. var validator = new LengthValidator(4, 10); var valueTypeProperty = typeof(Customer).GetProperty("ActivationMode"); var customer = new Customer { ActivationMode = ActivationMode.Client }; Assert.Throws<NotSupportedException>(() => validator.Validate(customer, valueTypeProperty, out error)); // Nullable value type property. validator = new LengthValidator(2); var countryProperty = typeof(Address).GetProperty("Country"); var address = new Address { Country = Country.RussianFederation }; Assert.Throws<NotSupportedException>(() => validator.Validate(address, countryProperty, out error)); // Domain object ref property. var addressProperty = typeof(Customer).GetProperty("MailingAddress"); Assert.Throws<NotSupportedException>(() => validator.Validate(customer, addressProperty, out error)); // Collection property. var collectionProperty = typeof(Account).GetProperty("Owners"); var account = new Account { Number = UnitTestHelper.ACCOUNT_NUMBER }; Assert.Throws<NotSupportedException>(() => validator.Validate(account, collectionProperty, out error)); }