PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 111 lines | 82 code | 14 blank | 15 comment | 0 complexity | f4f01c2d8296365adcb48e5d238f5812 MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2015-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 Xunit;
  22. namespace MongoDB.Bson.Tests.Serialization.Serializers
  23. {
  24. public class PartiallyRawBsonDocumentSerializerTests
  25. {
  26. [Fact]
  27. public void constructor_should_throw_when_name_is_null()
  28. {
  29. Action action = () => new PartiallyRawBsonDocumentSerializer(null, BsonDocumentSerializer.Instance);
  30. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("name");
  31. }
  32. [Fact]
  33. public void constructor_should_throw_when_rawSerializer_is_null()
  34. {
  35. Action action = () => new PartiallyRawBsonDocumentSerializer("name", null);
  36. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("rawSerializer");
  37. }
  38. [Fact]
  39. public void constructor_should_throw_when_rawSerializer_is_not_a_BsonValue_serializer()
  40. {
  41. Action action = () => new PartiallyRawBsonDocumentSerializer("name", new Int32Serializer());
  42. action.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("rawSerializer");
  43. }
  44. [Fact]
  45. public void Deserialize_should_return_partially_raw_BsonDocument()
  46. {
  47. var document = new BsonDocument
  48. {
  49. { "a", new BsonDocument("x", 1) },
  50. { "b", new BsonDocument("x", 2) },
  51. { "c", new BsonDocument("x", 3) }
  52. };
  53. var bson = document.ToBson();
  54. var subject = new PartiallyRawBsonDocumentSerializer("b", RawBsonDocumentSerializer.Instance);
  55. var result = Deserialize(bson, subject);
  56. result["a"].Should().BeOfType<BsonDocument>();
  57. result["b"].Should().BeOfType<RawBsonDocument>();
  58. result["c"].Should().BeOfType<BsonDocument>();
  59. }
  60. [Fact]
  61. public void Deserialize_should_return_nested_partially_raw_BsonDocument()
  62. {
  63. var document = new BsonDocument
  64. {
  65. { "a", new BsonDocument("x", 1) },
  66. { "b", new BsonDocument
  67. {
  68. { "d", new BsonDocument("z", 1) },
  69. { "e", new BsonDocument("z", 2) },
  70. { "f", new BsonDocument("z", 3) },
  71. }
  72. },
  73. { "c", new BsonDocument("x", 3) }
  74. };
  75. var bson = document.ToBson();
  76. var subject = new PartiallyRawBsonDocumentSerializer("b",
  77. new PartiallyRawBsonDocumentSerializer("e", RawBsonDocumentSerializer.Instance));
  78. var result = Deserialize(bson, subject);
  79. result["a"].Should().BeOfType<BsonDocument>();
  80. result["b"].Should().BeOfType<BsonDocument>();
  81. result["c"].Should().BeOfType<BsonDocument>();
  82. result["b"]["d"].Should().BeOfType<BsonDocument>();
  83. result["b"]["e"].Should().BeOfType<RawBsonDocument>();
  84. result["b"]["f"].Should().BeOfType<BsonDocument>();
  85. }
  86. // private methods
  87. private BsonDocument Deserialize(byte[] bson, PartiallyRawBsonDocumentSerializer serializer)
  88. {
  89. using (var stream = new MemoryStream(bson))
  90. using (var reader = new BsonBinaryReader(stream))
  91. {
  92. var context = BsonDeserializationContext.CreateRoot(reader);
  93. return serializer.Deserialize(context);
  94. }
  95. }
  96. }
  97. }