public void GetCustomAttributes_RangeDataTypeIsProvided_ZeroAttributesAreCreated() { var fieldProvider = MockRepository.GenerateMock <IDynamicRecordFieldProvider>(); var dynamicRecordFactory = new DynamicRecordFactory(fieldProvider); var dynamicRecordField = new DataAnnotatedDynamicRecordField() { Name = "Test", DotNetDataType = typeof(int), RangeDataType = typeof(int) }; var fields = new List <IDynamicRecordField>() { dynamicRecordField }; fieldProvider.Stub(x => x.GetFields()).Return(fields); var record = dynamicRecordFactory.Create(null); var property = record.GetType().GetProperty(dynamicRecordField.Name); var customAttributes = property.GetCustomAttributes(true); Assert.AreEqual(0, customAttributes.Count()); }
public void GetCustomAttributes_CustomValidatorTypeAndMethodIsProvided_CustomValidatorAttributeIsCreated() { var fieldProvider = MockRepository.GenerateMock <IDynamicRecordFieldProvider>(); var dynamicRecordFactory = new DynamicRecordFactory(fieldProvider); var dynamicRecordField = new DataAnnotatedDynamicRecordField() { Name = "Test", DotNetDataType = typeof(string), CustomValidatorType = typeof(object), CustomValidatorMethod = "CustomValidatorMethod" }; var fields = new List <IDynamicRecordField>() { dynamicRecordField }; fieldProvider.Stub(x => x.GetFields()).Return(fields); var record = dynamicRecordFactory.Create(null); var property = record.GetType().GetProperty(dynamicRecordField.Name); var customValidationAttribute = (CustomValidationAttribute)property .GetCustomAttributes(true) .Single(x => x is CustomValidationAttribute); Assert.AreEqual("CustomValidatorMethod", customValidationAttribute.Method); }
public void GetCustomAttributes_EnumDataTypeIsProvided_EnumDataTypeAttributeIsCreated() { var fieldProvider = MockRepository.GenerateMock <IDynamicRecordFieldProvider>(); var dynamicRecordFactory = new DynamicRecordFactory(fieldProvider); var dynamicRecordField = new DataAnnotatedDynamicRecordField() { Name = "Test", DotNetDataType = typeof(MockEnum), EnumDataType = typeof(MockEnum) }; var fields = new List <IDynamicRecordField>() { dynamicRecordField }; fieldProvider.Stub(x => x.GetFields()).Return(fields); var record = dynamicRecordFactory.Create(null); var property = record.GetType().GetProperty(dynamicRecordField.Name); var enumDataTypeAttribute = (EnumDataTypeAttribute)property .GetCustomAttributes(true) .Single(x => x is EnumDataTypeAttribute); Assert.AreEqual(typeof(MockEnum), enumDataTypeAttribute.EnumType); }
public void GetCustomAttributes_RequiredIsProvided_RequiredAttributeIsCreated() { var fieldProvider = MockRepository.GenerateMock <IDynamicRecordFieldProvider>(); var dynamicRecordFactory = new DynamicRecordFactory(fieldProvider); var dynamicRecordField = new DataAnnotatedDynamicRecordField() { Name = "Test", DotNetDataType = typeof(string), Required = true }; var fields = new List <IDynamicRecordField>() { dynamicRecordField }; fieldProvider.Stub(x => x.GetFields()).Return(fields); var record = dynamicRecordFactory.Create(null); var property = record.GetType().GetProperty(dynamicRecordField.Name); var requiredAttribute = (RequiredAttribute)property .GetCustomAttributes(true) .Single(x => x is RequiredAttribute); }
public void GetCustomAttributes_MaxLengthIsProvided_MaxLengthAttributeIsCreated() { var fieldProvider = MockRepository.GenerateMock <IDynamicRecordFieldProvider>(); var dynamicRecordFactory = new DynamicRecordFactory(fieldProvider); var dynamicRecordField = new DataAnnotatedDynamicRecordField() { Name = "Test", DotNetDataType = typeof(string), MaxLength = 10 }; var fields = new List <IDynamicRecordField>() { dynamicRecordField }; fieldProvider.Stub(x => x.GetFields()).Return(fields); var record = dynamicRecordFactory.Create(null); var property = record.GetType().GetProperty(dynamicRecordField.Name); var maxLengthAttribute = (MaxLengthAttribute)property .GetCustomAttributes(true) .Single(x => x is MaxLengthAttribute); Assert.AreEqual(10, maxLengthAttribute.Length); }
public void Create_OneFieldWithNoAttributes_RecordIsCreatedWithExpectedField() { var source = (object)null; var sourceFieldOrdinalAttributeConstructor = typeof(SourceFieldOrdinalAttribute).GetConstructor(new Type[] { typeof(int) }); var stringFieldAttributes = new List <CustomAttributeBuilder>(); var stringField = MockRepository.GenerateMock <IDynamicRecordField>(); stringField.Expect(x => x.Name).Return("StringField"); stringField.Expect(x => x.DotNetDataType).Return(typeof(string)); stringField.Expect(x => x.GetCustomAttributes()).Return(stringFieldAttributes); var fields = new List <IDynamicRecordField>() { stringField }; var fieldProvider = MockRepository.GenerateMock <IDynamicRecordFieldProvider>(); fieldProvider.Expect(x => x.GetFields()).Return(fields).Repeat.Once(); var dynamicRecordFactory = new DynamicRecordFactory(fieldProvider); var record = dynamicRecordFactory.Create(source); var stringFieldProperty = record.GetType().GetProperty("StringField"); Assert.IsNotNull(stringFieldProperty); fieldProvider.VerifyAllExpectations(); stringField.VerifyAllExpectations(); }
public void GetCustomAttributes_RangeDataTypeAndMinMaxValuesAreProvided_RangeAttributeIsCreated() { var fieldProvider = MockRepository.GenerateMock <IDynamicRecordFieldProvider>(); var dynamicRecordFactory = new DynamicRecordFactory(fieldProvider); var dynamicRecordField = new DataAnnotatedDynamicRecordField() { Name = "Test", DotNetDataType = typeof(int), RangeDataType = typeof(int), RangeMinimum = "1", RangeMaximum = "10" }; var fields = new List <IDynamicRecordField>() { dynamicRecordField }; fieldProvider.Stub(x => x.GetFields()).Return(fields); var record = dynamicRecordFactory.Create(null); var property = record.GetType().GetProperty(dynamicRecordField.Name); var rangeAttribute = (RangeAttribute)property .GetCustomAttributes(true) .Single(x => x is RangeAttribute); Assert.AreEqual(typeof(int), rangeAttribute.OperandType); Assert.AreEqual("1", rangeAttribute.Minimum); Assert.AreEqual("10", rangeAttribute.Maximum); }
public void DynamicRecordField_SourceFieldLayoutIsProvided_SourceFieldLayoutAttributeIsCreated() { var fieldProvider = MockRepository.GenerateMock <IDynamicRecordFieldProvider>(); var dynamicRecordFactory = new DynamicRecordFactory(fieldProvider); var dynamicRecordField = new DynamicRecordField() { Name = "Field0", DotNetDataType = typeof(string), SourceFieldStartIndex = 1, SourceFieldLength = 10 }; var fields = new List <IDynamicRecordField>() { dynamicRecordField }; fieldProvider.Stub(x => x.GetFields()).Return(fields); var record = dynamicRecordFactory.Create(null); var property = record.GetType().GetProperty(dynamicRecordField.Name); var sourceFieldLayoutAttribute = (SourceFieldLayoutAttribute)property .GetCustomAttributes(true) .Single(x => x is SourceFieldLayoutAttribute); Assert.AreEqual(1, sourceFieldLayoutAttribute.StartIndex); Assert.AreEqual(10, sourceFieldLayoutAttribute.Length); }
public void DynamicRecordField_DestinationFieldNameIsProvided_DestinationFieldNameAttributeIsCreated() { var fieldProvider = MockRepository.GenerateMock <IDynamicRecordFieldProvider>(); var dynamicRecordFactory = new DynamicRecordFactory(fieldProvider); var dynamicRecordField = new DynamicRecordField() { Name = "Field0", DotNetDataType = typeof(string), DestinationFieldName = "Field0Name" }; var fields = new List <IDynamicRecordField>() { dynamicRecordField }; fieldProvider.Stub(x => x.GetFields()).Return(fields); var record = dynamicRecordFactory.Create(null); var property = record.GetType().GetProperty(dynamicRecordField.Name); var DestinationFieldNameAttribute = (DestinationFieldNameAttribute)property .GetCustomAttributes(true) .Single(x => x is DestinationFieldNameAttribute); Assert.AreEqual("Field0Name", DestinationFieldNameAttribute.FieldName); }
public void Create_MultipleFieldsWithAttributes_RecordIsCreatedWithExpectedFieldsAndAttributes() { var source = (object)null; var sourceFieldOrdinalAttributeConstructor = typeof(SourceFieldOrdinalAttribute).GetConstructor(new Type[] { typeof(int) }); var stringFieldAttributes = new List <CustomAttributeBuilder>() { new CustomAttributeBuilder(sourceFieldOrdinalAttributeConstructor, new object[] { 1 }) }; var stringField = MockRepository.GenerateMock <IDynamicRecordField>(); stringField.Expect(x => x.Name).Return("StringField"); stringField.Expect(x => x.DotNetDataType).Return(typeof(string)); stringField.Expect(x => x.GetCustomAttributes()).Return(stringFieldAttributes); var intFieldAttributes = new List <CustomAttributeBuilder>() { new CustomAttributeBuilder(sourceFieldOrdinalAttributeConstructor, new object[] { 2 }) }; var intField = MockRepository.GenerateMock <IDynamicRecordField>(); intField.Expect(x => x.Name).Return("IntField"); intField.Expect(x => x.DotNetDataType).Return(typeof(int)); intField.Expect(x => x.GetCustomAttributes()).Return(intFieldAttributes); var fields = new List <IDynamicRecordField>() { stringField, intField }; var fieldProvider = MockRepository.GenerateMock <IDynamicRecordFieldProvider>(); fieldProvider.Expect(x => x.GetFields()).Return(fields).Repeat.Once(); var dynamicRecordFactory = new DynamicRecordFactory(fieldProvider); var record = dynamicRecordFactory.Create(source); var stringFieldProperty = record.GetType().GetProperty("StringField"); var intFieldProperty = record.GetType().GetProperty("IntField"); var stringFieldSourceFieldOrdinalAttribute = (SourceFieldOrdinalAttribute)stringFieldProperty .GetCustomAttributes(true) .Single(x => x is SourceFieldOrdinalAttribute); var intFieldSourceFieldOrdinalAttribute = (SourceFieldOrdinalAttribute)intFieldProperty .GetCustomAttributes(true) .Single(x => x is SourceFieldOrdinalAttribute); Assert.AreEqual(1, stringFieldSourceFieldOrdinalAttribute.Ordinal); Assert.AreEqual(2, intFieldSourceFieldOrdinalAttribute.Ordinal); fieldProvider.VerifyAllExpectations(); stringField.VerifyAllExpectations(); }
public void Create_DynamicRecordFieldProviderThrowsException_ExceptionIsPropogated() { var source = (object)null; var fieldProvider = MockRepository.GenerateMock <IDynamicRecordFieldProvider>(); fieldProvider.Stub(x => x.GetFields()).Throw(new InternalTestFailureException()); var dynamicRecordFactory = new DynamicRecordFactory(fieldProvider); dynamicRecordFactory.Create(source); }
public void Create_RecordTypeProviderThrowsException_ExceptionIsPropogated() { var source = new object(); var fieldProvider = MockRepository.GenerateMock <IDynamicRecordFieldProvider>(); var recordTypeProvider = MockRepository.GenerateMock <IRecordTypeProvider>(); recordTypeProvider.Stub(x => x.GetRecordType(Arg <object> .Is.Anything)).Throw(new InternalTestFailureException()); var dynamicRecordFactory = new DynamicRecordFactory(fieldProvider, recordTypeProvider); dynamicRecordFactory.Create(source); }
public void DynamicRecordField_DotNetDataTypeIsNotProvided_ExceptionIsThrown() { var fieldProvider = MockRepository.GenerateMock <IDynamicRecordFieldProvider>(); var dynamicRecordFactory = new DynamicRecordFactory(fieldProvider); var dynamicRecordField = new DynamicRecordField() { Name = "Field0", DotNetDataType = null, SourceFieldOrdinal = 1 }; var fields = new List <IDynamicRecordField>() { dynamicRecordField }; fieldProvider.Stub(x => x.GetFields()).Return(fields); dynamicRecordFactory.Create(null); }
public void Create_FieldProviderReturnsEmptyList_ExceptionIsThrown() { var source = (object)null; var fields = new List <IDynamicRecordField>(); var fieldProvider = MockRepository.GenerateMock <IDynamicRecordFieldProvider>(); fieldProvider.Expect(x => x.GetFields()).Return(fields).Repeat.Once(); var dynamicRecordFactory = new DynamicRecordFactory(fieldProvider); try { dynamicRecordFactory.Create(source); } catch { fieldProvider.VerifyAllExpectations(); throw; } Assert.Fail(); }