public void CreateODataValue_ReturnsDateTimeOffset_ForDateTime_WithDifferentTimeZone(DateTime value, DateTimeOffset expect)
        {
            // Arrange
            IEdmPrimitiveTypeReference edmPrimitiveType =
                EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(DateTime));
            ODataPrimitiveSerializer serializer = new ODataPrimitiveSerializer();

            TimeZoneInfo      tzi           = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
            HttpConfiguration configuration = new HttpConfiguration();

            configuration.SetTimeZoneInfo(tzi);

            HttpRequestMessage request = new HttpRequestMessage();

            request.SetConfiguration(configuration);

            ODataSerializerContext context = new ODataSerializerContext {
                Request = request
            };

            // Act
            ODataValue odataValue = serializer.CreateODataValue(value, edmPrimitiveType, context);

            // Assert
            ODataPrimitiveValue primitiveValue = Assert.IsType <ODataPrimitiveValue>(odataValue);

            Assert.Equal(expect, primitiveValue.Value);
        }
        [InlineData("China Standard Time")]   // +8:00
        public void CreateODataValue_ReturnsDateTimeOffsetMaxValue_ForDateTimeMaxValue(string timeZoneId)
        {
            // Arrange
            IEdmPrimitiveTypeReference edmPrimitiveType =
                EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(DateTime));
            ODataPrimitiveSerializer serializer = new ODataPrimitiveSerializer();
            DateTime dt = DateTime.MaxValue;

            TimeZoneInfoHelper.TimeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);

            // Act
            ODataValue odataValue = serializer.CreateODataValue(dt, edmPrimitiveType, new ODataSerializerContext());

            // Assert
            ODataPrimitiveValue primitiveValue = Assert.IsType <ODataPrimitiveValue>(odataValue);

            if (TimeZoneInfoHelper.TimeZone.BaseUtcOffset.Hours > 0)
            {
                Assert.Equal(new DateTimeOffset(dt, TimeZoneInfoHelper.TimeZone.GetUtcOffset(dt)), primitiveValue.Value);
            }
            else
            {
                Assert.Equal(DateTimeOffset.MaxValue, primitiveValue.Value);
            }
        }
        public void CreateODataValue_ReturnsODataNullValue_ForNullValue()
        {
            IEdmPrimitiveTypeReference edmPrimitiveType = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(string));
            var serializer = new ODataPrimitiveSerializer();
            var odataValue = serializer.CreateODataValue(null, edmPrimitiveType, new ODataSerializerContext());

            Assert.IsType <ODataNullValue>(odataValue);
        }
        public void CreateODataValue_PrimitiveValue()
        {
            IEdmPrimitiveTypeReference edmPrimitiveType = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(int));
            var serializer = new ODataPrimitiveSerializer();

            var odataValue = serializer.CreateODataValue(20, edmPrimitiveType, writeContext: null);

            Assert.NotNull(odataValue);
            ODataPrimitiveValue primitiveValue = Assert.IsType <ODataPrimitiveValue>(odataValue);

            Assert.Equal(primitiveValue.Value, 20);
        }
        public void CreateODataValue_ReturnsTimeOfDay_ForTimeSpan()
        {
            // Arrange
            IEdmPrimitiveTypeReference edmPrimitiveType = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(TimeOfDay));
            ODataPrimitiveSerializer   serializer       = new ODataPrimitiveSerializer();
            TimeSpan ts = new TimeSpan(0, 10, 11, 12, 13);

            // Act
            ODataValue odataValue = serializer.CreateODataValue(ts, edmPrimitiveType, new ODataSerializerContext());

            // Assert
            ODataPrimitiveValue primitiveValue = Assert.IsType <ODataPrimitiveValue>(odataValue);

            Assert.IsType <TimeOfDay>(primitiveValue.Value);
            Assert.Equal(new TimeOfDay(ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds), primitiveValue.Value);
        }
        public void CreateODataValue_ReturnsDate_ForDateTime()
        {
            // Arrange
            IEdmPrimitiveTypeReference edmPrimitiveType = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(Date));
            ODataPrimitiveSerializer   serializer       = new ODataPrimitiveSerializer();
            DateTime dt = new DateTime(2014, 10, 27);

            // Act
            ODataValue odataValue = serializer.CreateODataValue(dt, edmPrimitiveType, new ODataSerializerContext());

            // Assert
            ODataPrimitiveValue primitiveValue = Assert.IsType <ODataPrimitiveValue>(odataValue);

            Assert.IsType <Date>(primitiveValue.Value);
            Assert.Equal(new Date(dt.Year, dt.Month, dt.Day), primitiveValue.Value);
        }
        public void CreateODataValue_ReturnsDateTimeOffset_ForDateTime_ByDefault()
        {
            // Arrange
            IEdmPrimitiveTypeReference edmPrimitiveType =
                EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(DateTime));
            ODataPrimitiveSerializer serializer = new ODataPrimitiveSerializer();
            DateTime dt = new DateTime(2014, 10, 27);

            TimeZoneInfoHelper.TimeZone = null;

            // Act
            ODataValue odataValue = serializer.CreateODataValue(dt, edmPrimitiveType, new ODataSerializerContext());

            // Assert
            ODataPrimitiveValue primitiveValue = Assert.IsType <ODataPrimitiveValue>(odataValue);

            Assert.Equal(new DateTimeOffset(dt), primitiveValue.Value);
        }
        public void CreateODataValue_ReturnsODataNullValue_ForNullValue()
        {
            IEdmPrimitiveTypeReference edmPrimitiveType = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(string));
            var serializer = new ODataPrimitiveSerializer();
            var odataValue = serializer.CreateODataValue(null, edmPrimitiveType, new ODataSerializerContext());

            Assert.IsType<ODataNullValue>(odataValue);
        }
        public void CreateODataValue_PrimitiveValue()
        {
            IEdmPrimitiveTypeReference edmPrimitiveType = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(int));
            var serializer = new ODataPrimitiveSerializer();

            var odataValue = serializer.CreateODataValue(20, edmPrimitiveType, writeContext: null);
            Assert.NotNull(odataValue);
            ODataPrimitiveValue primitiveValue = Assert.IsType<ODataPrimitiveValue>(odataValue);
            Assert.Equal(primitiveValue.Value, 20);
        }
        public void CreateODataValue_ReturnsTimeOfDay_ForTimeSpan()
        {
            // Arrange
            IEdmPrimitiveTypeReference edmPrimitiveType = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(TimeOfDay));
            ODataPrimitiveSerializer serializer = new ODataPrimitiveSerializer();
            TimeSpan ts = new TimeSpan(0, 10, 11, 12, 13);

            // Act
            ODataValue odataValue = serializer.CreateODataValue(ts, edmPrimitiveType, new ODataSerializerContext());

            // Assert
            ODataPrimitiveValue primitiveValue = Assert.IsType<ODataPrimitiveValue>(odataValue);
            Assert.IsType<TimeOfDay>(primitiveValue.Value);
            Assert.Equal(new TimeOfDay(ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds), primitiveValue.Value);
        }
        public void CreateODataValue_ReturnsDate_ForDateTime()
        {
            // Arrange
            IEdmPrimitiveTypeReference edmPrimitiveType = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(Date));
            ODataPrimitiveSerializer serializer = new ODataPrimitiveSerializer();
            DateTime dt = new DateTime(2014, 10, 27);

            // Act
            ODataValue odataValue = serializer.CreateODataValue(dt, edmPrimitiveType, new ODataSerializerContext());

            // Assert
            ODataPrimitiveValue primitiveValue = Assert.IsType<ODataPrimitiveValue>(odataValue);
            Assert.IsType<Date>(primitiveValue.Value);
            Assert.Equal(new Date(dt.Year, dt.Month, dt.Day), primitiveValue.Value);
        }
        public void CreateODataValue_ReturnsDateTimeOffset_ForDateTime_WithDifferentTimeZone(DateTime value, DateTimeOffset expect)
        {
            // Arrange
            IEdmPrimitiveTypeReference edmPrimitiveType =
                EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(DateTime));
            ODataPrimitiveSerializer serializer = new ODataPrimitiveSerializer();

            TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
            HttpConfiguration configuration = new HttpConfiguration();
            configuration.SetTimeZoneInfo(tzi);

            HttpRequestMessage request = new HttpRequestMessage();
            request.SetConfiguration(configuration);

            ODataSerializerContext context = new ODataSerializerContext{ Request = request };

            // Act
            ODataValue odataValue = serializer.CreateODataValue(value, edmPrimitiveType, context);

            // Assert
            ODataPrimitiveValue primitiveValue = Assert.IsType<ODataPrimitiveValue>(odataValue);
            Assert.Equal(expect, primitiveValue.Value);
        }
        [InlineData("China Standard Time")] // +8:00
        public void CreateODataValue_ReturnsDateTimeOffsetMaxValue_ForDateTimeMaxValue(string timeZoneId)
        {
            // Arrange
            IEdmPrimitiveTypeReference edmPrimitiveType =
                EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(DateTime));
            ODataPrimitiveSerializer serializer = new ODataPrimitiveSerializer();
            DateTime dt = DateTime.MaxValue;
            TimeZoneInfoHelper.TimeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);

            // Act
            ODataValue odataValue = serializer.CreateODataValue(dt, edmPrimitiveType, new ODataSerializerContext());

            // Assert
            ODataPrimitiveValue primitiveValue = Assert.IsType<ODataPrimitiveValue>(odataValue);

            if (TimeZoneInfoHelper.TimeZone.BaseUtcOffset.Hours > 0)
            {
                Assert.Equal(new DateTimeOffset(dt, TimeZoneInfoHelper.TimeZone.GetUtcOffset(dt)), primitiveValue.Value);
            }
            else
            {
                Assert.Equal(DateTimeOffset.MaxValue, primitiveValue.Value);
            }
        }
        public void CreateODataValue_ReturnsDateTimeOffset_ForDateTime_ByDefault()
        {
            // Arrange
            IEdmPrimitiveTypeReference edmPrimitiveType =
                EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(DateTime));
            ODataPrimitiveSerializer serializer = new ODataPrimitiveSerializer();
            DateTime dt = new DateTime(2014, 10, 27);
            TimeZoneInfoHelper.TimeZone = null;

            // Act
            ODataValue odataValue = serializer.CreateODataValue(dt, edmPrimitiveType, new ODataSerializerContext());

            // Assert
            ODataPrimitiveValue primitiveValue = Assert.IsType<ODataPrimitiveValue>(odataValue);
            Assert.Equal(new DateTimeOffset(dt), primitiveValue.Value);
        }