Beispiel #1
0
        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);
            }
        }
Beispiel #2
0
        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);
        }
Beispiel #3
0
        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);
        }
Beispiel #4
0
        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);
        }
Beispiel #5
0
        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);
        }
Beispiel #6
0
        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);
        }
Beispiel #7
0
        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);
        }
Beispiel #8
0
        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);
        }
Beispiel #10
0
        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."));
        }
Beispiel #11
0
        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);
            }
        }
Beispiel #12
0
        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);
            }
        }