public void SetCollectionProperty_CanConvertDataTime_ByTimeZoneInfo() { // Arrange SampleClassWithDifferentCollectionProperties source = new SampleClassWithDifferentCollectionProperties(); IEdmProperty edmProperty = GetMockEdmProperty("DateTimeList", EdmPrimitiveTypeKind.DateTimeOffset); TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"); TimeZoneInfoHelper.TimeZone = tzi; DateTime dt = new DateTime(2014, 11, 15, 1, 2, 3); IList <DateTimeOffset> dtos = new List <DateTimeOffset> { new DateTimeOffset(dt, TimeSpan.Zero), new DateTimeOffset(dt, new TimeSpan(+7, 0, 0)), new DateTimeOffset(dt, new TimeSpan(-8, 0, 0)) }; // Act DeserializationHelpers.SetCollectionProperty(source, edmProperty, dtos, edmProperty.Name); // Assert Assert.Equal(new List <DateTime> { dt.AddHours(-8), dt.AddHours(-15), dt }, source.DateTimeList); }
public void SetCollectionProperty_CanConvertEnumCollection() { SampleClassWithDifferentCollectionProperties value = new SampleClassWithDifferentCollectionProperties(); DeserializationHelpers.SetCollectionProperty(value, "FlagsEnum", isDelta: false, value: new List <string> { "One", "Four, Two" }); Assert.Equal( new FlagsEnum[] { FlagsEnum.One, FlagsEnum.Four | FlagsEnum.Two }, value.FlagsEnum); }
public void SetCollectionProperty_CanConvertNonStandardEdmTypes() { SampleClassWithDifferentCollectionProperties value = new SampleClassWithDifferentCollectionProperties(); DeserializationHelpers.SetCollectionProperty(value, "UnsignedArray", isDelta: false, value: new List <int> { 1, 2, 3 }); Assert.Equal( new uint[] { 1, 2, 3 }, value.UnsignedArray); }
public void SetCollectionProperty_CanConvertEnumCollection() { SampleClassWithDifferentCollectionProperties value = new SampleClassWithDifferentCollectionProperties(); IEdmProperty edmProperty = GetMockEdmProperty("FlagsEnum", EdmPrimitiveTypeKind.String); DeserializationHelpers.SetCollectionProperty(value, edmProperty, value: new List <string> { "One", "Four, Two" }); Assert.Equal( new FlagsEnum[] { FlagsEnum.One, FlagsEnum.Four | FlagsEnum.Two }, value.FlagsEnum); }
public void SetCollectionProperty_CanConvertNonStandardEdmTypes() { SampleClassWithDifferentCollectionProperties value = new SampleClassWithDifferentCollectionProperties(); IEdmProperty edmProperty = GetMockEdmProperty("UnsignedArray", EdmPrimitiveTypeKind.Int32); DeserializationHelpers.SetCollectionProperty(value, edmProperty, value: new List <int> { 1, 2, 3 }); Assert.Equal( new uint[] { 1, 2, 3 }, value.UnsignedArray); }
public void SetCollectionProperty_OnNonCollection_ThrowsSerialization(string propertyName) { object value = new SampleClassWithDifferentCollectionProperties(); Type propertyType = typeof(SampleClassWithDifferentCollectionProperties).GetProperty(propertyName).PropertyType; Assert.Throws <SerializationException>( () => DeserializationHelpers.SetCollectionProperty(value, propertyName, isDelta: false, value: new List <int> { 1, 2, 3 }), Error.Format( "The type '{0}' of the property '{1}' on type 'System.Web.Http.OData.Formatter.Deserialization.DeserializationHelpersTest+SampleClassWithDifferentCollectionProperties' must be a collection.", propertyType.FullName, propertyName)); }
public void SetCollectionProperty_OnNonCollection_ThrowsSerialization(string propertyName) { object value = new SampleClassWithDifferentCollectionProperties(); Type propertyType = typeof(SampleClassWithDifferentCollectionProperties).GetProperty(propertyName).PropertyType; IEdmProperty edmProperty = GetMockEdmProperty(propertyName, EdmPrimitiveTypeKind.Int32); ExceptionAssert.Throws <SerializationException>( () => DeserializationHelpers.SetCollectionProperty(value, edmProperty, value: new List <int> { 1, 2, 3 }, propertyName: edmProperty.Name), Error.Format( "The type '{0}' of the property '{1}' on type 'Microsoft.AspNet.OData.Test.Formatter.Deserialization.DeserializationHelpersTest+SampleClassWithDifferentCollectionProperties' must be a collection.", propertyType.FullName, propertyName)); }
public void SetCollectionProperty_CanConvertEnumCollection() { SampleClassWithDifferentCollectionProperties value = new SampleClassWithDifferentCollectionProperties(); IEdmProperty edmProperty = GetMockEdmProperty("FlagsEnum", EdmPrimitiveTypeKind.String); DeserializationHelpers.SetCollectionProperty( value, edmProperty, value: new List <FlagsEnum> { FlagsEnum.One, FlagsEnum.Four | FlagsEnum.Two | (FlagsEnum)123 }, propertyName: edmProperty.Name, timeZoneInfo: null); Assert.Equal( new FlagsEnum[] { FlagsEnum.One, FlagsEnum.Four | FlagsEnum.Two | (FlagsEnum)123 }, value.FlagsEnum); }
public void SetCollectionProperty_CanConvertDataTime_ByDefault() { // Arrange SampleClassWithDifferentCollectionProperties source = new SampleClassWithDifferentCollectionProperties(); IEdmProperty edmProperty = GetMockEdmProperty("DateTimeList", EdmPrimitiveTypeKind.DateTimeOffset); DateTime dt = new DateTime(2014, 11, 15, 1, 2, 3); IList <DateTimeOffset> dtos = new List <DateTimeOffset> { new DateTimeOffset(dt, TimeSpan.Zero), new DateTimeOffset(dt, new TimeSpan(+7, 0, 0)), new DateTimeOffset(dt, new TimeSpan(-8, 0, 0)) }; IEnumerable <DateTime> expects = dtos.Select(e => e.LocalDateTime); // Act DeserializationHelpers.SetCollectionProperty(source, edmProperty, dtos, edmProperty.Name, timeZoneInfo: null); // Assert Assert.Equal(expects, source.DateTimeList); }