コード例 #1
0
ファイル: ValidationTests.cs プロジェクト: tmont/portoa
        public void Should_stop_validating_after_first_error()
        {
            var validator = new EntityValidator(new ActivatorServiceProvider(), null);
            var entity = new Validatable();
            var results = validator.Validate(entity, stopOnFirstError: true);

            Assert.That(results.Count(), Is.EqualTo(1));

            var result = results.First();
            Assert.That(result.ErrorMessage, Is.EqualTo("The NotNullProperty field is required."));
            Assert.That(result.MemberNames.Count(), Is.EqualTo(1));
            Assert.That(result.MemberNames.First(), Is.EqualTo("NotNullProperty"));
        }
コード例 #2
0
ファイル: ValidationTests.cs プロジェクト: tmont/portoa
        public void Should_validate_all_properties_and_fields()
        {
            var validator = new EntityValidator(new ActivatorServiceProvider(), null);
            var entity = new Validatable();
            var results = validator.Validate(entity);

            Assert.That(results.Count(), Is.EqualTo(2));

            var result = results.First();
            Assert.That(result.ErrorMessage, Is.EqualTo("The NotNullProperty field is required."));
            Assert.That(result.MemberNames.Count(), Is.EqualTo(1));
            Assert.That(result.MemberNames.First(), Is.EqualTo("NotNullProperty"));

            result = results.Last();
            Assert.That(result.ErrorMessage, Is.EqualTo("The notNullField field is required."));
            Assert.That(result.MemberNames.Count(), Is.EqualTo(1));
            Assert.That(result.MemberNames.First(), Is.EqualTo("notNullField"));
        }
コード例 #3
0
 public void IsValidatedFail()
 {
     try
     {
         var validatable = new Validatable();
         FulcrumAssert.IsValidated(validatable, CodeLocation.AsString());
         UT.Assert.Fail("An exception should have been thrown");
     }
     catch (FulcrumAssertionFailedException fulcrumException)
     {
         UT.Assert.IsNotNull(fulcrumException.TechnicalMessage);
         UT.Assert.IsTrue(fulcrumException.TechnicalMessage.StartsWith("Expected validation to pass (Property Validatable.Name"),
                          $"TechnicalMessage: '{fulcrumException.TechnicalMessage}'");
     }
     catch (Exception e)
     {
         UT.Assert.Fail($"Expected a specific FulcrumException but got {e.GetType().FullName}.");
     }
 }
 public void IsValidatedFail()
 {
     try
     {
         var validatable = new Validatable();
         InternalContract.RequireValidated(validatable, nameof(validatable));
         Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Fail("An exception should have been thrown");
     }
     catch (FulcrumContractException fulcrumException)
     {
         Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsNotNull(fulcrumException?.TechnicalMessage);
         Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsTrue(fulcrumException.TechnicalMessage.StartsWith("Validation failed"));
         Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsTrue(fulcrumException.TechnicalMessage.Contains("Property Name"));
     }
     catch (Exception e)
     {
         Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Fail($"Expected a specific FulcrumException but got {e.GetType().FullName}.");
     }
 }
コード例 #5
0
        public void MainTest()
        {
            using (var session = Domain.OpenSession()) {
                using (var transactionScope = session.OpenTransaction()) {
                    Validatable first = null;
                    Validatable second;

                    AssertEx.Throws <ValidationFailedException>(() => {
                        first = new Validatable();
                        session.Validate();
                    });

                    first.IsValid = true;
                    session.Validate();

                    transactionScope.Complete();
                }
            }
        }
コード例 #6
0
        public async Task When_Value_Is_Greater_Than_PropertyToCheck_Value_Then_Valid()
        {
            // Arrange
            var validatable = new Validatable
            {
                PropertyToCheckAgainst    = 1,
                MustBeGreaterThanProperty = 2
            };

            var validator = new BlueprintValidator(new IValidationSource[] { new DataAnnotationsValidationSource() });

            // Act
            var failures = await validator.GetValidationResultsAsync(validatable, null);

            var failedProperties = failures.AsDictionary().Keys;

            // Assert
            CollectionAssert.DoesNotContain(failedProperties, "MustBeGreaterThanProperty");
        }
コード例 #7
0
        public void InvalidCommitTest()
        {
            using (var session = Domain.OpenSession()) {
                var transactionScope = session.OpenTransaction();

                var first = new Validatable {
                    IsValid = true
                };
                var second = new Validatable {
                    IsValid = false
                };
                var third = new Validatable {
                    IsValid = true
                };

                transactionScope.Complete();

                AssertEx.Throws <ValidationFailedException>(transactionScope.Dispose);
            }
        }
コード例 #8
0
        public void EnforceValidationTest()
        {
            using (var session = Domain.OpenSession()) {
                var transactionScope = session.OpenTransaction();

                var entity = new Validatable {
                    IsValid = false
                };
                AssertEx.Throws <ValidationFailedException>(session.Validate);
                AssertEx.Throws <ValidationFailedException>(session.Validate);

                entity.IsValid = true;
                session.Validate();

                entity.IsValid = false;
                AssertEx.Throws <ValidationFailedException>(session.Validate);

                transactionScope.Complete();
                AssertEx.Throws <ValidationFailedException>(transactionScope.Dispose);
            }
        }
コード例 #9
0
        public async Task When_Value_Is_Equal_To_PropertyToCheck_Value_Then_InValid()
        {
            var date = DateTime.Now;

            // Arrange
            var validatable = new Validatable
            {
                PropertyToCheckAgainst    = date,
                MustBeGreaterThanProperty = date
            };

            var validator = new BlueprintValidator(new IValidationSource[] { new DataAnnotationsValidationSource() });

            // Act
            var failures = await validator.GetValidationResultsAsync(validatable, null);

            var failedProperties = failures.AsDictionary().Keys;

            // Assert
            CollectionAssert.Contains(failedProperties, "MustBeGreaterThanProperty");
        }
コード例 #10
0
 public void IsValidatedFail()
 {
     try
     {
         var validatable = new Validatable();
         InternalContract.RequireValidated(validatable, nameof(validatable));
         UT.Assert.Fail("An exception should have been thrown");
     }
     catch (FulcrumContractException fulcrumException)
     {
         UT.Assert.IsNotNull(fulcrumException.TechnicalMessage);
         var validationFailed = "ContractViolation: Validation failed";
         UT.Assert.IsTrue(fulcrumException.TechnicalMessage.StartsWith(validationFailed), $"Expected {nameof(fulcrumException.TechnicalMessage)}  to start with \"{validationFailed}\", but the message was \"{fulcrumException.TechnicalMessage}\".");
         UT.Assert.IsTrue(fulcrumException.TechnicalMessage.Contains("Property Validatable.Name"),
                          $"TechnicalMessage: '{fulcrumException.TechnicalMessage}'");
     }
     catch (Exception e)
     {
         UT.Assert.Fail($"Expected a specific FulcrumException but got {e.GetType().FullName}.");
     }
 }
コード例 #11
0
        public UserValidator()
        {
            LastName = new Validatable <string>();
            Name     = new Validatable <string>();
            Email    = new Validatable <string>();

            _unit1 = new ValidationUnit(Name, LastName, Email);

            // Name validations
            Name.Validations.Add(new IsNotNullOrEmptyRule <string> {
                ValidationMessage = "A name is required."
            });

            //Lastname validations
            LastName.Validations.Add(new IsNotNullOrEmptyRule <string> {
                ValidationMessage = "A lastname is required."
            });

            //Email validations
            Email.Validations.Add(new IsNotNullOrEmptyRule <string> {
                ValidationMessage = "A email is required."
            });
            Email.Validations.Add(new EmailRule());
        }
コード例 #12
0
        /// <summary>
        /// Validate the data annotations
        /// </summary>
        /// <param name="validationResults">out parameter for the result of the validation</param>
        public static bool Validate(this Validatable obj, ICollection <ValidationResult> validationResults)
        {
            var validationContext = new ValidationContext(obj);

            return(Validator.TryValidateObject(obj, validationContext, validationResults, true));
        }
コード例 #13
0
 public static Validatable <TModel> Must <TModel>(this Validatable <TModel> validatable, Func <TModel, bool> predicate, string errorMessage = "")
 {
     validatable.Validations.Add(new FunctionRule <TModel>(predicate).WithMessage(errorMessage));
     return(validatable);
 }
コード例 #14
0
 public static Validatable <TModel> WithRule <TModel>(this Validatable <TModel> validatable, params IValidationRule <TModel>[] validations)
 {
     validatable.Validations.AddRange(validations);
     return(validatable);
 }
コード例 #15
0
 public WhenRule(ref Validatable <T> context, Func <T, bool> ruleFunc)
 {
     _context  = context;
     _ruleFunc = ruleFunc;
 }
コード例 #16
0
 public static Validatable <TModel> When <TModel>(this Validatable <TModel> validatable, Func <TModel, bool> predicate)
 {
     validatable.HasWhenCondition(true);
     validatable.Validations.Insert(0, new WhenRule <TModel>(ref validatable, predicate));
     return(validatable);
 }
コード例 #17
0
 /// <summary>
 /// Validate the data annotations
 /// </summary>
 public static bool Validate(this Validatable obj)
 {
     return(obj.Validate(new List <ValidationResult>()));
 }