PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoDB.Driver.Tests/ProjectionDefinitionBuilderTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 218 lines | 160 code | 44 blank | 14 comment | 1 complexity | 3ffba600e80919ef7eb4027b0c7e4989 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.Linq;
  16. using FluentAssertions;
  17. using MongoDB.Bson;
  18. using MongoDB.Bson.Serialization;
  19. using MongoDB.Bson.Serialization.Attributes;
  20. using Xunit;
  21. namespace MongoDB.Driver.Tests
  22. {
  23. public class ProjectionDefinitionBuilderTests
  24. {
  25. [Fact]
  26. public void Combine()
  27. {
  28. var subject = CreateSubject<Person>();
  29. var projection = subject.Combine(
  30. subject.Include(x => x.FirstName),
  31. subject.Exclude("LastName"));
  32. Assert(projection, "{fn: 1, LastName: 0}");
  33. }
  34. [Fact]
  35. public void Combine_with_redundant_fields()
  36. {
  37. var subject = CreateSubject<Person>();
  38. var projection = subject.Combine(
  39. subject.Include(x => x.FirstName),
  40. subject.Exclude("LastName"),
  41. subject.Include("fn"));
  42. Assert(projection, "{LastName: 0, fn: 1}");
  43. }
  44. [Fact]
  45. public void Combine_with_redundant_fields_using_extension_method()
  46. {
  47. var subject = CreateSubject<Person>();
  48. var projection = subject.Include(x => x.FirstName).Exclude("LastName").Include("fn");
  49. Assert(projection, "{LastName: 0, fn: 1}");
  50. }
  51. [Fact]
  52. public void ElemMatch()
  53. {
  54. var subject = CreateSubject<BsonDocument>();
  55. Assert(subject.ElemMatch<BsonDocument>("a", "{b: 1}"), "{a: {$elemMatch: {b: 1}}}");
  56. }
  57. [Fact]
  58. public void ElemMatch_Typed()
  59. {
  60. var subject = CreateSubject<Person>();
  61. Assert(subject.ElemMatch<Pet>("Pets", "{Name: 'Fluffy'}"), "{pets: {$elemMatch: {Name: 'Fluffy'}}}");
  62. Assert(subject.ElemMatch(x => x.Pets, "{Name: 'Fluffy'}"), "{pets: {$elemMatch: {Name: 'Fluffy'}}}");
  63. Assert(subject.ElemMatch(x => x.Pets, x => x.Name == "Fluffy"), "{pets: {$elemMatch: {name: 'Fluffy'}}}");
  64. }
  65. [Fact]
  66. public void ElemMatch_from_filter()
  67. {
  68. var subject = CreateSubject<BsonDocument>();
  69. Assert(subject.Include("a.$"), "{'a.$': 1}");
  70. }
  71. [Fact]
  72. public void ElemMatch_from_filter_Typed()
  73. {
  74. var subject = CreateSubject<Person>();
  75. Assert(subject.Include(x => x.Pets.ElementAt(-1)), "{'pets.$': 1}");
  76. Assert(subject.Include("Pets.$"), "{'pets.$': 1}");
  77. }
  78. [Fact]
  79. public void Exclude()
  80. {
  81. var subject = CreateSubject<BsonDocument>();
  82. Assert(subject.Exclude("a"), "{a: 0}");
  83. }
  84. [Fact]
  85. public void Exclude_Typed()
  86. {
  87. var subject = CreateSubject<Person>();
  88. Assert(subject.Exclude(x => x.FirstName), "{fn: 0}");
  89. Assert(subject.Exclude("FirstName"), "{fn: 0}");
  90. }
  91. [Fact]
  92. public void Include()
  93. {
  94. var subject = CreateSubject<BsonDocument>();
  95. Assert(subject.Include("a"), "{a: 1}");
  96. }
  97. [Fact]
  98. public void Include_Typed()
  99. {
  100. var subject = CreateSubject<Person>();
  101. Assert(subject.Include(x => x.FirstName), "{fn: 1}");
  102. Assert(subject.Include("FirstName"), "{fn: 1}");
  103. }
  104. [Theory]
  105. [InlineData("textScore")]
  106. [InlineData("randVal")]
  107. [InlineData("searchScore")]
  108. [InlineData("searchHighlights")]
  109. [InlineData("geoNearDistance")]
  110. [InlineData("geoNearPoint")]
  111. [InlineData("recordId")]
  112. [InlineData("indexKey")]
  113. [InlineData("sortKey")]
  114. public void Meta(string metaFieldName)
  115. {
  116. var subject = CreateSubject<BsonDocument>();
  117. Assert(subject.Meta("a", metaFieldName), $"{{ a : {{ $meta : '{metaFieldName}' }} }}");
  118. }
  119. [Fact]
  120. public void MetaTextScore()
  121. {
  122. var subject = CreateSubject<BsonDocument>();
  123. Assert(subject.MetaTextScore("a"), "{a: {$meta: 'textScore'}}");
  124. }
  125. [Fact]
  126. public void Slice()
  127. {
  128. var subject = CreateSubject<BsonDocument>();
  129. Assert(subject.Slice("a", 10), "{a: {$slice: 10}}");
  130. }
  131. [Fact]
  132. public void Slice_Typed()
  133. {
  134. var subject = CreateSubject<Person>();
  135. Assert(subject.Slice(x => x.Pets, 10), "{pets: {$slice: 10}}");
  136. Assert(subject.Slice("Pets", 10), "{pets: {$slice: 10}}");
  137. }
  138. [Fact]
  139. public void Slice_with_limit()
  140. {
  141. var subject = CreateSubject<BsonDocument>();
  142. Assert(subject.Slice("a", 10, 20), "{a: {$slice: [10, 20]}}");
  143. }
  144. [Fact]
  145. public void Slice_Typed_with_limit()
  146. {
  147. var subject = CreateSubject<Person>();
  148. Assert(subject.Slice(x => x.Pets, 10, 20), "{pets: {$slice: [10, 20]}}");
  149. Assert(subject.Slice("Pets", 10, 20), "{pets: {$slice: [10, 20]}}");
  150. }
  151. private void Assert<TDocument>(ProjectionDefinition<TDocument> projection, string expectedJson)
  152. {
  153. var documentSerializer = BsonSerializer.SerializerRegistry.GetSerializer<TDocument>();
  154. var renderedProjection = projection.Render(documentSerializer, BsonSerializer.SerializerRegistry);
  155. renderedProjection.Should().Be(expectedJson);
  156. }
  157. private ProjectionDefinitionBuilder<TDocument> CreateSubject<TDocument>()
  158. {
  159. return new ProjectionDefinitionBuilder<TDocument>();
  160. }
  161. private class Person
  162. {
  163. [BsonElement("fn")]
  164. public string FirstName { get; set; }
  165. [BsonElement("pets")]
  166. public Pet[] Pets { get; set; }
  167. }
  168. private class Pet
  169. {
  170. [BsonElement("name")]
  171. public string Name { get; set; }
  172. }
  173. }
  174. }