PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoDB.Driver.Tests/IndexKeysDefinitionBuilderTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 253 lines | 184 code | 55 blank | 14 comment | 0 complexity | fa53d9a97cdab4b2d2f56b1600926a21 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 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 IndexKeysDefinitionBuilderTests
  24. {
  25. [Fact]
  26. public void Ascending()
  27. {
  28. var subject = CreateSubject<BsonDocument>();
  29. Assert(subject.Ascending("a"), "{a: 1}");
  30. }
  31. [Fact]
  32. public void Ascending_Typed()
  33. {
  34. var subject = CreateSubject<Person>();
  35. Assert(subject.Ascending(x => x.FirstName), "{fn: 1}");
  36. Assert(subject.Ascending("FirstName"), "{fn: 1}");
  37. }
  38. [Fact]
  39. public void Combine()
  40. {
  41. var subject = CreateSubject<BsonDocument>();
  42. var result = subject.Combine(
  43. "{a: 1, b: -1}",
  44. subject.Descending("c"));
  45. Assert(result, "{a: 1, b: -1, c: -1}");
  46. }
  47. [Fact]
  48. public void Combine_with_repeated_fields()
  49. {
  50. var subject = CreateSubject<BsonDocument>();
  51. var combined = subject.Combine(
  52. "{a: 1, b: -1}",
  53. subject.Descending("a"));
  54. Action act = () => Render(combined);
  55. act.ShouldThrow<MongoException>();
  56. }
  57. [Fact]
  58. public void Combine_many_texts()
  59. {
  60. var subject = CreateSubject<BsonDocument>();
  61. var result = subject.Combine(
  62. subject.Text("a"),
  63. subject.Text("b"),
  64. subject.Text("c"));
  65. Assert(result, "{a: 'text', b: 'text', c: 'text'}");
  66. }
  67. [Fact]
  68. public void Combine_with_repeated_fields_using_extension_methods()
  69. {
  70. var subject = CreateSubject<BsonDocument>();
  71. var result = subject.Ascending("a")
  72. .Ascending("b")
  73. .Descending("c")
  74. .Descending("a");
  75. Action act = () => Render(result);
  76. act.ShouldThrow<MongoException>();
  77. }
  78. [Fact]
  79. public void Descending()
  80. {
  81. var subject = CreateSubject<BsonDocument>();
  82. Assert(subject.Descending("a"), "{a: -1}");
  83. }
  84. [Fact]
  85. public void Descending_Typed()
  86. {
  87. var subject = CreateSubject<Person>();
  88. Assert(subject.Descending(x => x.FirstName), "{fn: -1}");
  89. Assert(subject.Descending("FirstName"), "{fn: -1}");
  90. }
  91. [Fact]
  92. public void Geo2D()
  93. {
  94. var subject = CreateSubject<BsonDocument>();
  95. Assert(subject.Geo2D("a"), "{a: '2d'}");
  96. }
  97. [Fact]
  98. public void Geo2D_Typed()
  99. {
  100. var subject = CreateSubject<Person>();
  101. Assert(subject.Geo2D(x => x.FirstName), "{fn: '2d'}");
  102. Assert(subject.Geo2D("FirstName"), "{fn: '2d'}");
  103. }
  104. [Fact]
  105. public void GeoHaystack()
  106. {
  107. var subject = CreateSubject<BsonDocument>();
  108. #pragma warning disable 618
  109. Assert(subject.GeoHaystack("a"), "{a: 'geoHaystack'}");
  110. Assert(subject.GeoHaystack("a", "b"), "{a: 'geoHaystack', b: 1 }");
  111. #pragma warning restore 618
  112. }
  113. [Fact]
  114. public void GeoHaystack_Typed()
  115. {
  116. var subject = CreateSubject<Person>();
  117. #pragma warning disable 618
  118. Assert(subject.GeoHaystack(x => x.FirstName), "{fn: 'geoHaystack'}");
  119. Assert(subject.GeoHaystack(x => x.FirstName, x => x.LastName), "{fn: 'geoHaystack', ln: 1}");
  120. Assert(subject.GeoHaystack("FirstName", "LastName"), "{fn: 'geoHaystack', ln: 1}");
  121. #pragma warning restore 618
  122. }
  123. [Fact]
  124. public void Geo2DSphere()
  125. {
  126. var subject = CreateSubject<BsonDocument>();
  127. Assert(subject.Geo2DSphere("a"), "{a: '2dsphere'}");
  128. }
  129. [Fact]
  130. public void Geo2DSphere_Typed()
  131. {
  132. var subject = CreateSubject<Person>();
  133. Assert(subject.Geo2DSphere(x => x.FirstName), "{fn: '2dsphere'}");
  134. Assert(subject.Geo2DSphere("FirstName"), "{fn: '2dsphere'}");
  135. }
  136. [Fact]
  137. public void Text()
  138. {
  139. var subject = CreateSubject<BsonDocument>();
  140. Assert(subject.Text("a"), "{a: 'text'}");
  141. Assert(subject.Text("$**"), "{'$**': 'text'}");
  142. }
  143. [Fact]
  144. public void Text_Typed()
  145. {
  146. var subject = CreateSubject<Person>();
  147. Assert(subject.Text(x => x.FirstName), "{fn: 'text'}");
  148. Assert(subject.Text("FirstName"), "{fn: 'text'}");
  149. Assert(subject.Text("$**"), "{'$**': 'text'}");
  150. }
  151. [Fact]
  152. public void Wildcard_should_return_expected_result()
  153. {
  154. var subject = CreateSubject<BsonDocument>();
  155. Assert(subject.Wildcard("a"), "{ 'a.$**' : 1 }");
  156. Assert(subject.Wildcard(), "{ '$**' : 1 }");
  157. }
  158. [Fact]
  159. public void Wildcard_typed_should_return_expected_result()
  160. {
  161. var subject = CreateSubject<Person>();
  162. Assert(subject.Wildcard(person => person.FirstName), "{ 'fn.$**' : 1 }"); // with BsonElement attribute
  163. Assert(subject.Wildcard(person => person.Info), "{ 'Info.$**' : 1 }");
  164. Assert(subject.Wildcard(person => person.Age), "{ 'Age.$**' : 1 }");
  165. Assert(subject.Wildcard(person => person.Job), "{ 'Job.$**' : 1 }");
  166. Assert(subject.Wildcard(person => person.Interviews), "{ 'Interviews.$**' : 1 }");
  167. }
  168. private void Assert<TDocument>(IndexKeysDefinition<TDocument> keys, string expectedJson)
  169. {
  170. var rendered = Render(keys);
  171. rendered.Should().Be(expectedJson);
  172. }
  173. private BsonDocument Render<TDocument>(IndexKeysDefinition<TDocument> keys)
  174. {
  175. var documentSerializer = BsonSerializer.SerializerRegistry.GetSerializer<TDocument>();
  176. return keys.Render(documentSerializer, BsonSerializer.SerializerRegistry);
  177. }
  178. private IndexKeysDefinitionBuilder<TDocument> CreateSubject<TDocument>()
  179. {
  180. return new IndexKeysDefinitionBuilder<TDocument>();
  181. }
  182. private class Person
  183. {
  184. [BsonElement("fn")]
  185. public string FirstName { get; set; }
  186. [BsonElement("ln")]
  187. public string LastName { get; set; }
  188. public string Info { get; set; }
  189. public int Age { get; set; }
  190. public Job Job { get; set; }
  191. public Job[] Interviews { get; set; }
  192. }
  193. public class Job
  194. {
  195. public string Title { get; set; }
  196. }
  197. }
  198. }