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 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 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 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 GetPropertyValidator_returns_null_if_entity_validator_does_not_exist() { var entity = new FlightSegmentWithNestedComplexTypes(); var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(entity); var propertyEntry = mockInternalEntityEntry.Object.Property("Departure"); var propertyValidator = MockHelper.CreateMockValidationProvider().Object.GetPropertyValidatorBase( mockInternalEntityEntry.Object, propertyEntry); Assert.Null(propertyValidator); }
public void GetValidatorForProperty_returns_correct_validator_for_scalar_property() { var entity = new FlightSegmentWithNestedComplexTypes(); var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(entity); var propertyEntry = mockInternalEntityEntry.Object.Property("FlightNumber"); var propertyValidator = new PropertyValidator("FlightNumber", new IValidator[0]); var entityValidator = new EntityValidator(new[] { propertyValidator }, new IValidator[0]); var actualPropertyValidator = MockHelper.CreateMockValidationProvider().Object.GetValidatorForPropertyBase(entityValidator, propertyEntry); Assert.Same(propertyValidator, actualPropertyValidator); }
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 ValidatableObjectValidator_does_not_return_errors_for_null_complex_property_with_IValidatableObject_validation() { var entity = new FlightSegmentWithNestedComplexTypes { }; var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(entity); var validator = new ValidatableObjectValidator(null); var results = validator.Validate( MockHelper.CreateEntityValidationContext(mockInternalEntityEntry.Object), mockInternalEntityEntry.Object.Property("Departure")); Assert.False(results.Any()); }
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 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_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_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_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 GetPropertyValidator_returns_correct_validator() { var entity = new FlightSegmentWithNestedComplexTypes(); var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(entity); var propertyEntry = mockInternalEntityEntry.Object.Property("Departure"); var propertyValidator = new PropertyValidator("Departure", new IValidator[0]); var entityValidator = new EntityValidator(new[] { propertyValidator }, new IValidator[0]); var mockValidationProvider = MockHelper.CreateMockValidationProvider(); mockValidationProvider.Setup(p => p.GetEntityValidator(It.IsAny<InternalEntityEntry>())) .Returns<InternalEntityEntry>(e => entityValidator); mockValidationProvider.Protected() .Setup<PropertyValidator>("GetValidatorForProperty", ItExpr.IsAny<EntityValidator>(), ItExpr.IsAny<InternalMemberEntry>()) .Returns<EntityValidator, InternalMemberEntry>((ev, e) => propertyValidator); var actualPropertyValidator = mockValidationProvider.Object.GetPropertyValidatorBase( mockInternalEntityEntry.Object, propertyEntry); Assert.Same(propertyValidator, actualPropertyValidator); }
public void GetPropertyValidator_returns_correct_validator() { var entity = new FlightSegmentWithNestedComplexTypes(); var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(entity); var propertyEntry = mockInternalEntityEntry.Object.Property("Departure"); var propertyValidator = new PropertyValidator("Departure", new IValidator[0]); var entityValidator = new EntityValidator(new[] { propertyValidator }, new IValidator[0]); var mockValidationProvider = MockHelper.CreateMockValidationProvider(); mockValidationProvider.Setup(p => p.GetEntityValidator(It.IsAny <InternalEntityEntry>())) .Returns <InternalEntityEntry>(e => entityValidator); mockValidationProvider.Protected() .Setup <PropertyValidator>("GetValidatorForProperty", ItExpr.IsAny <EntityValidator>(), ItExpr.IsAny <InternalMemberEntry>()) .Returns <EntityValidator, InternalMemberEntry>((ev, e) => propertyValidator); var actualPropertyValidator = mockValidationProvider.Object.GetPropertyValidatorBase( mockInternalEntityEntry.Object, propertyEntry); Assert.Same(propertyValidator, actualPropertyValidator); }
public void ComplexPropertyValidator_does_not_return_errors_if_complex_property_value_is_valid() { var entity = new FlightSegmentWithNestedComplexTypes { Departure = new DepartureArrivalInfoWithNestedComplexType { Airport = new AirportDetails { }, } }; var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(entity); var propertyValidator = new ComplexPropertyValidator( "Departure", new ValidationAttributeValidator[0], new ComplexTypeValidator(new PropertyValidator[0], new ValidationAttributeValidator[0])); var results = propertyValidator.Validate( MockHelper.CreateEntityValidationContext(mockInternalEntityEntry.Object), mockInternalEntityEntry.Object.Property("Departure")); Assert.False(results.Any()); }
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", CountryCode = "ZZ" } } }; var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(entity); var validator = new ValidationAttributeValidator(new CustomValidationAttribute(typeof(AirportDetails), "ValidateCountry"), 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 'ZZ'.") }, results); }