public void ActiveDate_TwoWindowsUsingGroups(int activeDateDaysFromToday, bool isValid) { var customer = new Customer() { ActiveDate = DateTime.Now.AddDays(activeDateDaysFromToday) }; var spec = new CustomerSpecification(); // Valid dates are in a five day window starting 10 days ago OR a five day window starting in 5 days from now. spec.Check(c => c.ActiveDate).Required() .Group(d => d.GreaterThan(DateTime.Now.AddDays(-10)) .And.LessThan(DateTime.Now.AddDays(-5))) .Or .Group(d => d.GreaterThan(DateTime.Now.AddDays(5)) .And.LessThan(DateTime.Now.AddDays(10))); var notification = spec.Validate(customer); if (isValid) { Assert.That(notification.Errors, Is.Empty); } else { Assert.That(notification.Errors, Is.Not.Empty); } }
public void CustomerContacts_Lambda_IsNotValid() { var contact1 = new Contact() { DateOfBirth = DateTime.Now.AddYears(-19) }; var contact2 = new Contact() { DateOfBirth = DateTime.Now.AddYears(-22) }; var customer = new Customer() { Contacts = new List <Contact> { contact1, contact2 } }; var spec = new CustomerSpecification(); spec.Check( c => from contact in c.Contacts where contact.DateOfBirth < DateTime.Now.AddYears(-20) select contact) .Optional() .ForEach(c => ((Contact)c).Active, "All contacts under age of 20 must be active."); var notification = spec.Validate(customer); Assert.IsNotEmpty(notification.Errors); Assert.AreEqual(1, notification.Errors.Count); }
public void CustomerName_Required_IsNotValid() { var customer = new Customer(); var spec = new CustomerSpecification(); spec.Check(cust => cust.Name).Required(); var notification = spec.Validate(customer); Assert.IsNotEmpty(notification.Errors); }
public void CustomerName_OptionalAndLength_IsValid() { var customer = new Customer(); var spec = new CustomerSpecification(); spec.Check(cust => cust.Name).Optional().LengthBetween(2, 100); var notification = spec.Validate(customer); Assert.IsEmpty(notification.Errors); }
public void CustomerName_RequiredAndLength_NullValue_IsNotValid() { var customer = new Customer(); var spec = new CustomerSpecification(); spec.Check(cust => cust.Name).Required().LengthBetween(2, 100); var notification = spec.Validate(customer); Assert.IsNotEmpty(notification.Errors); Assert.AreEqual(1, notification.Errors.Count); }
public void When_Customer_Contacts_IsInitializeButEmpty_And_DefinedRequired_IsInvalid() { var customer = new Customer { Contacts = new List <Contact>() }; var spec = new CustomerSpecification(); spec.Check(cust => cust.Contacts).Required(); var notifications = spec.Validate(customer); Assert.IsNotEmpty(notifications.Errors); }
public void PastCustomerPromotionDate_IsInFuture_IsNotValid() { var customer = new Customer() { PromotionDate = DateTime.Now.AddDays(-1) }; var spec = new CustomerSpecification(); spec.Check(c => c.PromotionDate).Optional().IsInFuture(); var notification = spec.Validate(customer); Assert.That(notification.Errors, Is.Not.Empty); }
public void CustomerName_RequiredAndNotMinLength_InvalidLength_IsNotValid() { var customer = new Customer { Name = string.Empty.PadLeft(105, 'X') }; var spec = new CustomerSpecification(); spec.Check(cust => cust.Name).Required().Not.MinLength(100); var notification = spec.Validate(customer); Assert.IsNotEmpty(notification.Errors); Assert.AreEqual(1, notification.Errors.Count); }
public void Specification_WithWarn_ReturnsValidationResultAsWarn() { var spec = new CustomerSpecification(); spec.Check(c => c.Name).Required(); spec.Warn(c => c.Address).Required().Specification <AddressSpecification>(); var customer = new Customer(); var validationResults = spec.Validate(customer); var addressValidationResult = validationResults.Errors.Where(vr => vr.Property.Name == "Address").First(); var nameValidationResult = validationResults.Errors.Where(vr => vr.Property.Name == "Name").First(); Assert.That(addressValidationResult.Level == ValidationLevelType.Warn); Assert.That(nameValidationResult.Level == ValidationLevelType.Error); }
public void CustomerAddressCountry_Required_IsValid() { var customer = new Customer() { Address = new Address() { Country = new Country() } }; var spec = new CustomerSpecification(); spec.Check(cust => cust.Address.Country.Name).Required(); var notification = spec.Validate(customer); Assert.That(notification.Errors, Is.Not.Empty); Assert.That(notification.Errors.Count, Is.EqualTo(1)); Assert.That(notification.Errors[0].Message, Is.EqualTo("Address Country Name is required.")); }
public void ActiveDate_GreaterThan_Or_LessThan(int activeDateDaysFromToday, bool isValid) { var customer = new Customer() { ActiveDate = DateTime.Now.AddDays(activeDateDaysFromToday) }; var spec = new CustomerSpecification(); spec.Check(c => c.ActiveDate).Required().GreaterThan(DateTime.Now).Or.LessThan(DateTime.Now.AddDays(-10)); var notification = spec.Validate(customer); if (isValid) { Assert.That(notification.Errors, Is.Empty); } else { Assert.That(notification.Errors, Is.Not.Empty); } }
public void ActiveDate_Or_And_RulePrecidence(int activeDateDaysFromToday, bool isValid) { var customer = new Customer() { ActiveDate = DateTime.Now.AddDays(activeDateDaysFromToday) }; var spec = new CustomerSpecification(); // Valid dates are greater than now or a window of 10 days starting 20 days ago. spec.Check(c => c.ActiveDate).Required().GreaterThan(DateTime.Now).Or.GreaterThan(DateTime.Now.AddDays(-20)).And.LessThan(DateTime.Now.AddDays(-10)); var notification = spec.Validate(customer); if (isValid) { Assert.That(notification.Errors, Is.Empty); } else { Assert.That(notification.Errors, Is.Not.Empty); } }