public void PropertyValidator_returns_validation_errors_if_primitive_property_value_is_not_valid()
        {
            var mockValidator = new Mock <IValidator>();

            mockValidator
            .Setup(v => v.Validate(It.IsAny <EntityValidationContext>(), It.IsAny <InternalMemberEntry>()))
            .Returns(() => new[] { new DbValidationError("Name", "error") });

            var propertyValidator = new PropertyValidator(
                "Name",
                new[] { mockValidator.Object });

            var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(
                new Dictionary <string, object>
            {
                { "Name", "" }
            });

            var results = propertyValidator.Validate(
                MockHelper.CreateEntityValidationContext(mockInternalEntityEntry.Object),
                mockInternalEntityEntry.Object.Member("Name"));

            ValidationErrorHelper.VerifyResults(
                new[] { new Tuple <string, string>("Name", "error") },
                results);
        }
        public void ValidationAttributeValidator_returns_errors_if_complex_property_type_level_validation_fails()
        {
            var entity = new FlightSegmentWithNestedComplexTypes
            {
                Departure = new DepartureArrivalInfoWithNestedComplexType
                {
                    Airport = new AirportDetails
                    {
                        AirportCode         = "YVR",
                        CityCode            = "YVR",
                        CountryOrRegionCode = "ZZ"
                    }
                }
            };

            var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(entity);

            var validator = new ValidationAttributeValidator(new CustomValidationAttribute(typeof(AirportDetails), "ValidateCountryOrRegion"), null);

            var results = validator.Validate(
                MockHelper.CreateEntityValidationContext(mockInternalEntityEntry.Object),
                mockInternalEntityEntry.Object.Property("Departure").Property("Airport"));

            ValidationErrorHelper.VerifyResults(
                new[]
            {
                new Tuple <string, string>("Departure.Airport", "City 'YVR' is not located in country or region 'ZZ'.")
            }, results);
        }
        public void ValidationAttributeValidator_returns_errors_for_invalid_complex_property_child_property_values()
        {
            var entity = new FlightSegmentWithNestedComplexTypes
            {
                Departure = new DepartureArrivalInfoWithNestedComplexType
                {
                    Airport = new AirportDetails
                    {
                        AirportCode = "???",
                    }
                }
            };

            var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(entity);

            var validator = new ValidationAttributeValidator(new RegularExpressionAttribute("^[A-Z]{3}$"), null);

            var results = validator.Validate(
                MockHelper.CreateEntityValidationContext(mockInternalEntityEntry.Object),
                mockInternalEntityEntry.Object.Property("Departure").Property("Airport").Property("AirportCode"));

            ValidationErrorHelper.VerifyResults(
                new[]
            {
                new Tuple <string, string>(
                    "Departure.Airport.AirportCode",
                    string.Format(RegexAttribute_ValidationError, "Departure.Airport.AirportCode", "^[A-Z]{3}$"))
            }, results);
        }
        public void EntityValidator_does_not_run_other_validation_if_property_validation_failed()
        {
            var mockValidator = new Mock <IValidator>();

            mockValidator
            .Setup(v => v.Validate(It.IsAny <EntityValidationContext>(), It.IsAny <InternalMemberEntry>()))
            .Returns(() => new[] { new DbValidationError("ID", "error") });

            var mockUncalledValidator = new Mock <IValidator>(MockBehavior.Strict);

            var entityValidator = new EntityValidator(
                new[]
            {
                new PropertyValidator(
                    "ID",
                    new[] { mockValidator.Object })
            },
                new[] { mockUncalledValidator.Object });

            var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(
                new Dictionary <string, object>
            {
                { "ID", -1 }
            });

            var entityValidationResult = entityValidator.Validate(MockHelper.CreateEntityValidationContext(mockInternalEntityEntry.Object));

            Assert.NotNull(entityValidationResult);

            ValidationErrorHelper.VerifyResults(
                new[] { new Tuple <string, string>("ID", "error") }, entityValidationResult.ValidationErrors);
        }
        public void ComplexTypeValidator_returns_an_error_if_IValidatableObject_validation_failed()
        {
            var entity = new FlightSegmentWithNestedComplexTypes
            {
                Departure = new DepartureArrivalInfoWithNestedComplexType
                {
                    Airport = new AirportDetails
                    {
                        AirportCode = "???",
                    }
                }
            };

            var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(entity);

            var entityValidator = new ComplexTypeValidator(
                new PropertyValidator[0],
                new[] { MockHelper.CreateValidatableObjectValidator("object", "IValidatableObject is invalid") });

            var entityValidationResult = entityValidator.Validate(
                MockHelper.CreateEntityValidationContext(mockInternalEntityEntry.Object),
                mockInternalEntityEntry.Object.Property("Departure").Property("Airport").Property("AirportCode"));

            Assert.NotNull(entityValidationResult);

            ValidationErrorHelper.VerifyResults(
                new[] { new Tuple <string, string>("object", "IValidatableObject is invalid") }, entityValidationResult);
        }
        public void ComplexPropertyValidator_does_not_run_complex_type_validation_if_property_validation_failed()
        {
            var entity = new FlightSegmentWithNestedComplexTypes
            {
                Departure = new DepartureArrivalInfoWithNestedComplexType
                {
                    Airport = new AirportDetails
                    {
                        AirportCode = null,
                    },
                }
            };

            var mockValidator = new Mock <IValidator>();

            mockValidator
            .Setup(v => v.Validate(It.IsAny <EntityValidationContext>(), It.IsAny <InternalMemberEntry>()))
            .Returns(() => new[] { new DbValidationError("Airport", "error") });

            var mockComplexValidator = new Mock <IValidator>(MockBehavior.Strict);

            var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(entity);
            var propertyValidator       = new ComplexPropertyValidator(
                "Airport",
                new[]
            {
                mockValidator.Object
            },
                new ComplexTypeValidator(
                    new[]
            {
                new PropertyValidator(
                    "AirportCode",
                    new[] { mockComplexValidator.Object })
            }, new ValidationAttributeValidator[0]));

            var results = propertyValidator.Validate(
                MockHelper.CreateEntityValidationContext(mockInternalEntityEntry.Object),
                mockInternalEntityEntry.Object.Property("Departure").Property("Airport"));

            Assert.Equal(1, results.Count());

            ValidationErrorHelper.VerifyResults(
                new[]
            {
                new Tuple <string, string>("Airport", "error")
            }, results);
        }
        public void EntityValidator_returns_an_error_if_IValidatableObject_validation_failed()
        {
            var entity = new FlightSegmentWithNestedComplexTypes
            {
            };

            var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(entity);

            var entityValidator = new EntityValidator(
                new PropertyValidator[0],
                new[] { MockHelper.CreateValidatableObjectValidator("object", "IValidatableObject is invalid") });

            var entityValidationResult = entityValidator.Validate(MockHelper.CreateEntityValidationContext(mockInternalEntityEntry.Object));

            Assert.NotNull(entityValidationResult);

            ValidationErrorHelper.VerifyResults(
                new[] { new Tuple <string, string>("object", "IValidatableObject is invalid") }, entityValidationResult.ValidationErrors);
        }
        public void ValidatableObjectValidator_returns_empty_enumerator_if_complex_property_IValidatableObject_validation_returns_null()
        {
            var entity = new FlightSegmentWithNestedComplexTypes
            {
                Departure = new DepartureArrivalInfoWithNestedComplexType
                {
                    ValidationResults = new[] { ValidationResult.Success, null }
                }
            };

            var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(entity);

            var validator = new ValidatableObjectValidator(null);

            var results = validator.Validate(
                MockHelper.CreateEntityValidationContext(mockInternalEntityEntry.Object),
                mockInternalEntityEntry.Object.Property("Departure"));

            ValidationErrorHelper.VerifyResults(new Tuple <string, string> [0], results);
        }
        public void ValidatableObjectValidator_returns_errors_if_complex_property_IValidatableObject_validation_fails()
        {
            var entity = new FlightSegmentWithNestedComplexTypes
            {
                Departure = new DepartureArrivalInfoWithNestedComplexType
                {
                    Time = DateTime.MinValue
                }
            };

            var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(entity);

            var validator = new ValidatableObjectValidator(null);

            var results = validator.Validate(
                MockHelper.CreateEntityValidationContext(mockInternalEntityEntry.Object),
                mockInternalEntityEntry.Object.Property("Departure"));

            ValidationErrorHelper.VerifyResults(new[] { new Tuple <string, string>("Departure", "Date cannot be in the past.") }, results);
        }
        public void ValidatableObjectValidator_returns_errors_if_entity_IValidatableObject_validation_fails()
        {
            var entity = new FlightSegmentWithNestedComplexTypesWithTypeLevelValidation
            {
                Aircraft = new AircraftInfo
                {
                    Code = "A380"
                },
                FlightNumber = "QF0006"
            };

            var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(entity);

            var validator = new ValidatableObjectValidator(null);

            var results = validator.Validate(MockHelper.CreateEntityValidationContext(mockInternalEntityEntry.Object), null);

            ValidationErrorHelper.VerifyResults(
                new[]
            {
                new Tuple <string, string>("Aircraft.Code", "Your trip may end in Singapore."),
                new Tuple <string, string>("FlightNumber", "Your trip may end in Singapore.")
            }, results);
        }