PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoDB.Driver.Tests/Linq/Translators/ProjectedObjectDeserializerTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 77 lines | 55 code | 8 blank | 14 comment | 0 complexity | 74f665ea849b4dd23da8d083e401ff8e MD5 | raw file
Possible License(s): Apache-2.0
  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.Collections.Generic;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using Xunit;
  21. using FluentAssertions;
  22. using MongoDB.Driver.Linq.Translators;
  23. using MongoDB.Bson;
  24. using MongoDB.Bson.IO;
  25. using MongoDB.Bson.Serialization;
  26. using MongoDB.Bson.Serialization.Serializers;
  27. namespace MongoDB.Driver.Tests.Linq.Translators
  28. {
  29. public class ProjectedObjectDeserializerTests
  30. {
  31. [Fact]
  32. public void Should_deserialize_top_level_fields()
  33. {
  34. var result = Deserialize("{a: 1, b: 2}",
  35. new BsonSerializationInfo("a", new Int32Serializer(), typeof(int)),
  36. new BsonSerializationInfo("b", new Int32Serializer(), typeof(int)));
  37. result.GetValue<int>("a", null).Should().Be(1);
  38. result.GetValue<int>("b", null).Should().Be(2);
  39. }
  40. [Fact]
  41. public void Should_deserialize_unspecified_documents()
  42. {
  43. var result = Deserialize("{a: { b: 1, c: 2}}",
  44. new BsonSerializationInfo("a.b", new Int32Serializer(), typeof(int)),
  45. new BsonSerializationInfo("a.c", new Int32Serializer(), typeof(int)));
  46. result.GetValue<int>("a.b", null).Should().Be(1);
  47. result.GetValue<int>("a.c", null).Should().Be(2);
  48. }
  49. [Fact]
  50. public void Should_deserialize_unspecified_arrays()
  51. {
  52. var result = Deserialize("{a: [{b: 1}, {b: 2}]}",
  53. new BsonSerializationInfo("a.b", new Int32Serializer(), typeof(int)));
  54. var list = result.GetValue<IEnumerable<object>>("a", null)
  55. .Cast<ProjectedObject>()
  56. .Select(x => x.GetValue<int>("b", 10)).ToList();
  57. list.Should().BeEquivalentTo(1, 2);
  58. }
  59. private ProjectedObject Deserialize(string json, params BsonSerializationInfo[] serializationInfos)
  60. {
  61. using (var reader = new JsonReader(json))
  62. {
  63. var context = BsonDeserializationContext.CreateRoot(reader);
  64. var serializer = new ProjectedObjectDeserializer(serializationInfos);
  65. return serializer.Deserialize(context);
  66. }
  67. }
  68. }
  69. }