PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/MongoDB.Driver.Tests/SortDefinitionBuilderTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 123 lines | 85 code | 24 blank | 14 comment | 0 complexity | 86f4e59135198a537eaa765d3131394c 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 FluentAssertions;
  16. using MongoDB.Bson;
  17. using MongoDB.Bson.Serialization;
  18. using MongoDB.Bson.Serialization.Attributes;
  19. using Xunit;
  20. namespace MongoDB.Driver.Tests
  21. {
  22. public class SortDefinitionBuilderTests
  23. {
  24. [Fact]
  25. public void Ascending()
  26. {
  27. var subject = CreateSubject<BsonDocument>();
  28. Assert(subject.Ascending("a"), "{a: 1}");
  29. }
  30. [Fact]
  31. public void Ascending_Typed()
  32. {
  33. var subject = CreateSubject<Person>();
  34. Assert(subject.Ascending(x => x.FirstName), "{fn: 1}");
  35. Assert(subject.Ascending("FirstName"), "{fn: 1}");
  36. }
  37. [Fact]
  38. public void Combine()
  39. {
  40. var subject = CreateSubject<BsonDocument>();
  41. var sort = subject.Combine(
  42. "{a: 1, b: -1}",
  43. subject.Descending("c"));
  44. Assert(sort, "{a: 1, b: -1, c: -1}");
  45. }
  46. [Fact]
  47. public void Combine_with_repeated_fields()
  48. {
  49. var subject = CreateSubject<BsonDocument>();
  50. var sort = subject.Combine(
  51. "{a: 1, b: -1}",
  52. subject.Descending("a"));
  53. Assert(sort, "{b: -1, a: -1}");
  54. }
  55. [Fact]
  56. public void Combine_with_repeated_fields_using_extension_methods()
  57. {
  58. var subject = CreateSubject<BsonDocument>();
  59. var sort = subject.Ascending("a")
  60. .Descending("b")
  61. .Descending("a");
  62. Assert(sort, "{b: -1, a: -1}");
  63. }
  64. [Fact]
  65. public void Descending()
  66. {
  67. var subject = CreateSubject<BsonDocument>();
  68. Assert(subject.Descending("a"), "{a: -1}");
  69. }
  70. [Fact]
  71. public void Descending_Typed()
  72. {
  73. var subject = CreateSubject<Person>();
  74. Assert(subject.Descending(x => x.FirstName), "{fn: -1}");
  75. Assert(subject.Descending("FirstName"), "{fn: -1}");
  76. }
  77. [Fact]
  78. public void MetaTextScore()
  79. {
  80. var subject = CreateSubject<BsonDocument>();
  81. Assert(subject.MetaTextScore("awesome"), "{awesome: {$meta: 'textScore'}}");
  82. }
  83. private void Assert<TDocument>(SortDefinition<TDocument> sort, string expectedJson)
  84. {
  85. var documentSerializer = BsonSerializer.SerializerRegistry.GetSerializer<TDocument>();
  86. var renderedSort = sort.Render(documentSerializer, BsonSerializer.SerializerRegistry);
  87. renderedSort.Should().Be(expectedJson);
  88. }
  89. private SortDefinitionBuilder<TDocument> CreateSubject<TDocument>()
  90. {
  91. return new SortDefinitionBuilder<TDocument>();
  92. }
  93. private class Person
  94. {
  95. [BsonElement("fn")]
  96. public string FirstName { get; set; }
  97. }
  98. }
  99. }