PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoDB.Bson.Tests/Serialization/Serializers/DateTimeOffsetSerializerTests.cs

https://github.com/mongodb/mongo-csharp-driver
C# | 143 lines | 114 code | 15 blank | 14 comment | 2 complexity | 4ca5d0873ef989875ebe33b09214a310 MD5 | raw file
  1. /* Copyright 2010-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.IO;
  17. using FluentAssertions;
  18. using MongoDB.Bson.IO;
  19. using MongoDB.Bson.Serialization;
  20. using MongoDB.Bson.Serialization.Serializers;
  21. using MongoDB.Bson.TestHelpers.XunitExtensions;
  22. using Xunit;
  23. namespace MongoDB.Bson.Tests.Serialization.Serializers
  24. {
  25. public class DateTimeOffsetSerializerTests
  26. {
  27. [Fact]
  28. public void constructor_with_no_arguments_should_return_expected_result()
  29. {
  30. var subject = new DateTimeOffsetSerializer();
  31. subject.Representation.Should().Be(BsonType.Array);
  32. }
  33. [Theory]
  34. [ParameterAttributeData]
  35. public void constructor_with_representation_should_return_expected_result(
  36. [Values(BsonType.Array, BsonType.DateTime, BsonType.Document, BsonType.String)] BsonType representation)
  37. {
  38. var subject = new DateTimeOffsetSerializer(representation);
  39. subject.Representation.Should().Be(representation);
  40. }
  41. [Fact]
  42. public void constructor_with_representation_should_throw_when_representation_is_invalid()
  43. {
  44. var exception = Record.Exception(() => new DateTimeOffsetSerializer(BsonType.Null));
  45. exception.Should().BeOfType<ArgumentException>();
  46. }
  47. [Theory]
  48. [InlineData("{ x : [{ $numberLong : '0' }, 0] }", "0001-01-01T00:00:00+00:00")]
  49. [InlineData("{ x : [{ $numberLong : '621355968000000000' }, 0] }", "1970-01-01T00:00:00+00:00")]
  50. [InlineData("{ x : [{ $numberLong : '621355968000000000' }, 60] }", "1970-01-01T00:00:00+01:00")]
  51. [InlineData("{ x : [{ $numberLong : '621355968000000000' }, -60] }", "1970-01-01T00:00:00-01:00")]
  52. [InlineData("{ x : { $date : { $numberLong : '0' } } }", "1970-01-01T00:00:00Z")]
  53. [InlineData("{ x : { $date : { $numberLong : '60000' } } }", "1970-01-01T00:01:00Z")]
  54. [InlineData("{ x : { $date : { $numberLong : '1640995200000' } } }", "2022-01-01T00:00:00Z")]
  55. [InlineData("{ x : { DateTime : 'ignored', Ticks : { $numberLong : 0 }, Offset : 0 } }", "0001-01-01T00:00:00Z")]
  56. [InlineData("{ x : { DateTime : 'ignored', Ticks : { $numberLong : '621355968000000000' }, Offset : 0 } }", "1970-01-01T00:00:00Z")]
  57. [InlineData("{ x : { DateTime : 'ignored', Ticks : { $numberLong : '621355968000000000' }, Offset : 60 } }", "1970-01-01T00:00:00+01:00")]
  58. [InlineData("{ x : { DateTime : 'ignored', Ticks : { $numberLong : '621355968000000000' }, Offset : -60 } }", "1970-01-01T00:00:00-01:00")]
  59. [InlineData("{ x : '0001-01-01T00:00:00+00:00' }", "0001-01-01T00:00:00+00:00")]
  60. [InlineData("{ x : '1970-01-01T00:00:00+00:00' }", "1970-01-01T00:00:00+00:00")]
  61. [InlineData("{ x : '1970-01-01T00:00:00+01:00' }", "1970-01-01T00:00:00+01:00")]
  62. [InlineData("{ x : '1970-01-01T00:00:00-01:00' }", "1970-01-01T00:00:00-01:00")]
  63. public void Deserialize_should_return_expected_result(string json, string expectedResult)
  64. {
  65. var x = DateTimeOffset.Parse(expectedResult);
  66. var m = BsonUtils.ToMillisecondsSinceEpoch(x.UtcDateTime);
  67. var subject = new DateTimeOffsetSerializer();
  68. DateTimeOffset result;
  69. using (var reader = new JsonReader(json))
  70. {
  71. reader.ReadStartDocument();
  72. reader.ReadName("x");
  73. var context = BsonDeserializationContext.CreateRoot(reader);
  74. result = subject.Deserialize(context);
  75. reader.ReadEndDocument();
  76. }
  77. result.Should().Be(DateTimeOffset.Parse(expectedResult));
  78. }
  79. [Theory]
  80. [InlineData(BsonType.Array, "0001-01-01T00:00:00Z", "{ \"x\" : [{ \"$numberLong\" : \"0\" }, { \"$numberInt\" : \"0\" }] }")]
  81. [InlineData(BsonType.Array, "1970-01-01T00:00:00Z", "{ \"x\" : [{ \"$numberLong\" : \"621355968000000000\" }, { \"$numberInt\" : \"0\" }] }")]
  82. [InlineData(BsonType.Array, "1970-01-01T00:00:00+01:00", "{ \"x\" : [{ \"$numberLong\" : \"621355968000000000\" }, { \"$numberInt\" : \"60\" }] }")]
  83. [InlineData(BsonType.Array, "1970-01-01T00:00:00-01:00", "{ \"x\" : [{ \"$numberLong\" : \"621355968000000000\" }, { \"$numberInt\" : \"-60\" }] }")]
  84. [InlineData(BsonType.DateTime, "0001-01-01T00:00:00Z", "{ \"x\" : { \"$date\" : { \"$numberLong\" : \"-62135596800000\" } } }")]
  85. [InlineData(BsonType.DateTime, "1970-01-01T00:00:00Z", "{ \"x\" : { \"$date\" : { \"$numberLong\" : \"0\" } } }")]
  86. [InlineData(BsonType.DateTime, "1970-01-01T00:00:00+01:00", "{ \"x\" : { \"$date\" : { \"$numberLong\" : \"-3600000\" } } }")]
  87. [InlineData(BsonType.DateTime, "1970-01-01T00:00:00-01:00", "{ \"x\" : { \"$date\" : { \"$numberLong\" : \"3600000\" } } }")]
  88. [InlineData(BsonType.Document, "0001-01-01T00:00:00Z", "{ \"x\" : { \"DateTime\" : { \"$date\" : { \"$numberLong\" : \"-62135596800000\" } }, \"Ticks\" : { \"$numberLong\" : \"0\" }, \"Offset\" : { \"$numberInt\" : \"0\" } } }")]
  89. [InlineData(BsonType.Document, "1970-01-01T00:00:00Z", "{ \"x\" : { \"DateTime\" : { \"$date\" : { \"$numberLong\" : \"0\" } }, \"Ticks\" : { \"$numberLong\" : \"621355968000000000\" }, \"Offset\" : { \"$numberInt\" : \"0\" } } }")]
  90. [InlineData(BsonType.Document, "1970-01-01T00:00:00+01:00", "{ \"x\" : { \"DateTime\" : { \"$date\" : { \"$numberLong\" : \"-3600000\" } }, \"Ticks\" : { \"$numberLong\" : \"621355968000000000\" }, \"Offset\" : { \"$numberInt\" : \"60\" } } }")]
  91. [InlineData(BsonType.Document, "1970-01-01T00:00:00-01:00", "{ \"x\" : { \"DateTime\" : { \"$date\" : { \"$numberLong\" : \"3600000\" } }, \"Ticks\" : { \"$numberLong\" : \"621355968000000000\" }, \"Offset\" : { \"$numberInt\" : \"-60\" } } }")]
  92. [InlineData(BsonType.String, "0001-01-01T00:00:00Z", "{ \"x\" : \"0001-01-01T00:00:00+00:00\" }")]
  93. [InlineData(BsonType.String, "1970-01-01T00:00:00Z", "{ \"x\" : \"1970-01-01T00:00:00+00:00\" }")]
  94. [InlineData(BsonType.String, "1970-01-01T00:00:00+01:00", "{ \"x\" : \"1970-01-01T00:00:00+01:00\" }")]
  95. [InlineData(BsonType.String, "1970-01-01T00:00:00-01:00", "{ \"x\" : \"1970-01-01T00:00:00-01:00\" }")]
  96. public void Serialize_should_have_expected_result(BsonType representation, string valueString, string expectedResult)
  97. {
  98. var subject = new DateTimeOffsetSerializer(representation);
  99. var value = DateTimeOffset.Parse(valueString);
  100. string result;
  101. using (var textWriter = new StringWriter())
  102. using (var writer = new JsonWriter(textWriter, new JsonWriterSettings { OutputMode = JsonOutputMode.CanonicalExtendedJson }))
  103. {
  104. var context = BsonSerializationContext.CreateRoot(writer);
  105. writer.WriteStartDocument();
  106. writer.WriteName("x");
  107. subject.Serialize(context, value);
  108. writer.WriteEndDocument();
  109. result = textWriter.ToString();
  110. }
  111. result.Should().Be(expectedResult);
  112. }
  113. [Theory]
  114. [ParameterAttributeData]
  115. public void WithRepresentation_should_return_expected_result(
  116. [Values(BsonType.Array, BsonType.DateTime, BsonType.Document, BsonType.String)] BsonType oldRepresentation,
  117. [Values(BsonType.Array, BsonType.DateTime, BsonType.Document, BsonType.String)] BsonType newRepresentation)
  118. {
  119. var subject = new DateTimeOffsetSerializer(oldRepresentation);
  120. var result = subject.WithRepresentation(newRepresentation);
  121. result.Representation.Should().Be(newRepresentation);
  122. if (newRepresentation == oldRepresentation)
  123. {
  124. result.Should().BeSameAs(subject);
  125. }
  126. }
  127. }
  128. }