public void GetValidatorForProperty_returns_correct_validator_for_child_complex_property()
        {
            var entity = new DepartureArrivalInfoWithNestedComplexType
            {
                Airport = new AirportDetails(),
            };

            var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(entity);
            var childPropertyEntry      = mockInternalEntityEntry.Object.Property("Airport").Property("AirportCode");

            var childPropertyValidator   = new PropertyValidator("AirportCode", new IValidator[0]);
            var complexPropertyValidator = new ComplexPropertyValidator(
                "Airport", new IValidator[0],
                new ComplexTypeValidator(new[] { childPropertyValidator }, new IValidator[0]));
            var entityValidator = new EntityValidator(new[] { complexPropertyValidator }, new IValidator[0]);

            var mockValidationProvider = MockHelper.CreateMockValidationProvider();

            mockValidationProvider.Protected()
            .Setup <PropertyValidator>("GetValidatorForProperty", ItExpr.IsAny <EntityValidator>(), ItExpr.IsAny <InternalMemberEntry>())
            .Returns <EntityValidator, InternalMemberEntry>((ev, e) => complexPropertyValidator);

            var actualPropertyValidator = mockValidationProvider.Object.GetValidatorForPropertyBase(entityValidator, childPropertyEntry);

            Assert.Same(childPropertyValidator, actualPropertyValidator);
        }
        public void GetValidatorForProperty_returns_null_for_child_complex_property_if_no_property_validation()
        {
            var entity = new DepartureArrivalInfoWithNestedComplexType
            {
                Airport = new AirportDetails(),
            };

            var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(entity);
            var childPropertyEntry      = mockInternalEntityEntry.Object.Property("Airport").Property("AirportCode");

            var entityValidator = new EntityValidator(new PropertyValidator[0], new IValidator[0]);

            var actualPropertyValidator = MockHelper.CreateMockValidationProvider().Object.GetValidatorForPropertyBase(
                entityValidator, childPropertyEntry);

            Assert.Null(actualPropertyValidator);
        }
        public void ValidatableObjectValidator_wraps_exceptions()
        {
            var entity = new DepartureArrivalInfoWithNestedComplexType
            {
                Airport = new AirportDetails(),
            };
            var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(entity);

            var validator = new ValidatableObjectValidator(
                new DisplayAttribute
            {
                Name = "Airport information"
            });

            Assert.Equal(
                Strings.DbUnexpectedValidationException_IValidatableObject("Airport information", typeof(AirportDetails).FullName),
                Assert.Throws <DbUnexpectedValidationException>(
                    () => validator.Validate(
                        MockHelper.CreateEntityValidationContext(mockInternalEntityEntry.Object),
                        mockInternalEntityEntry.Object.Property("Airport"))).Message);
        }
        public void ValidatableObjectValidator_uses_localized_name()
        {
            var entity = new DepartureArrivalInfoWithNestedComplexType
            {
                Airport = new AirportDetails(),
            };
            var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(entity);

            var validator = new ValidatableObjectValidator(
                new DisplayAttribute
            {
                Name         = "ValidationContextDisplayAttributeName",
                ResourceType = typeof(System.Data.Entity.Properties.UnitTestResources)
            });

            Assert.Equal(
                Strings.DbUnexpectedValidationException_IValidatableObject("xyzzy", typeof(AirportDetails).FullName),
                Assert.Throws <DbUnexpectedValidationException>(
                    () => validator.Validate(
                        MockHelper.CreateEntityValidationContext(mockInternalEntityEntry.Object),
                        mockInternalEntityEntry.Object.Property("Airport"))).Message);
        }
        public void GetValidatorForProperty_returns_correct_validator_for_child_complex_property()
        {
            var entity = new DepartureArrivalInfoWithNestedComplexType
            {
                Airport = new AirportDetails(),
            };

            var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(entity);
            var childPropertyEntry = mockInternalEntityEntry.Object.Property("Airport").Property("AirportCode");

            var childPropertyValidator = new PropertyValidator("AirportCode", new IValidator[0]);
            var complexPropertyValidator = new ComplexPropertyValidator(
                "Airport", new IValidator[0],
                new ComplexTypeValidator(new[] { childPropertyValidator }, new IValidator[0]));
            var entityValidator = new EntityValidator(new[] { complexPropertyValidator }, new IValidator[0]);

            var mockValidationProvider = MockHelper.CreateMockValidationProvider();

            mockValidationProvider.Protected()
                .Setup<PropertyValidator>("GetValidatorForProperty", ItExpr.IsAny<EntityValidator>(), ItExpr.IsAny<InternalMemberEntry>())
                .Returns<EntityValidator, InternalMemberEntry>((ev, e) => complexPropertyValidator);

            var actualPropertyValidator = mockValidationProvider.Object.GetValidatorForPropertyBase(entityValidator, childPropertyEntry);

            Assert.Same(childPropertyValidator, actualPropertyValidator);
        }
        public void GetValidatorForProperty_returns_null_for_child_complex_property_if_no_property_validation()
        {
            var entity = new DepartureArrivalInfoWithNestedComplexType
            {
                Airport = new AirportDetails(),
            };

            var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(entity);
            var childPropertyEntry = mockInternalEntityEntry.Object.Property("Airport").Property("AirportCode");

            var entityValidator = new EntityValidator(new PropertyValidator[0], new IValidator[0]);

            var actualPropertyValidator = MockHelper.CreateMockValidationProvider().Object.GetValidatorForPropertyBase(
                entityValidator, childPropertyEntry);

            Assert.Null(actualPropertyValidator);
        }
        public void ValidatableObjectValidator_uses_localized_name()
        {
            var entity = new DepartureArrivalInfoWithNestedComplexType
            {
                Airport = new AirportDetails(),
            };
            var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(entity);

            var validator = new ValidatableObjectValidator(
                new DisplayAttribute
                {
                    Name = "ValidationContextDisplayAttributeName",
                    ResourceType = typeof(System.Data.Entity.Properties.UnitTestResources)
                });

            Assert.Equal(
                    Strings.DbUnexpectedValidationException_IValidatableObject("xyzzy", typeof(AirportDetails).FullName),
                    Assert.Throws<DbUnexpectedValidationException>(
                        () => validator.Validate(
                            MockHelper.CreateEntityValidationContext(mockInternalEntityEntry.Object),
                            mockInternalEntityEntry.Object.Property("Airport"))).Message);
        }
        public void ValidatableObjectValidator_wraps_exceptions()
        {
            var entity = new DepartureArrivalInfoWithNestedComplexType
            {
                Airport = new AirportDetails(),
            };
            var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(entity);

            var validator = new ValidatableObjectValidator(
                new DisplayAttribute
                {
                    Name = "Airport information"
                });

            Assert.Equal(
                    Strings.DbUnexpectedValidationException_IValidatableObject("Airport information", typeof(AirportDetails).FullName),
                    Assert.Throws<DbUnexpectedValidationException>(
                        () => validator.Validate(
                            MockHelper.CreateEntityValidationContext(mockInternalEntityEntry.Object),
                            mockInternalEntityEntry.Object.Property("Airport"))).Message);
        }