Skip to content

Commit ea3784a

Browse files
committed
add simple tests
1 parent 9f29350 commit ea3784a

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/MongoDB.Driver/SortDefinitionBuilder.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,11 @@ internal sealed class CombinedSortDefinition<TDocument> : SortDefinition<TDocume
250250
public CombinedSortDefinition(IEnumerable<SortDefinition<TDocument>> sorts)
251251
{
252252
_sorts = Ensure.IsNotNull(sorts, nameof(sorts)).ToList();
253+
254+
if (_sorts.Any(sort => sort is NoFieldDirectionalSortDefinition<TDocument>))
255+
{
256+
throw new InvalidOperationException("Value-based sort cannot be combined with other sorts. When sorting by the entire element value, no other sorting criteria can be applied.");
257+
}
253258
}
254259

255260
public override BsonDocument Render(RenderArgs<TDocument> args)

tests/MongoDB.Driver.Tests/SortDefinitionBuilderTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System;
1617
using FluentAssertions;
1718
using MongoDB.Bson;
1819
using MongoDB.Bson.Serialization;
@@ -31,6 +32,14 @@ public void Ascending()
3132
Assert(subject.Ascending("a"), "{a: 1}");
3233
}
3334

35+
[Fact]
36+
public void Ascending_no_field()
37+
{
38+
var subject = CreateSubject<BsonDocument>();
39+
40+
Assert(subject.Ascending(), "{direction: 1}");
41+
}
42+
3443
[Fact]
3544
public void Ascending_Typed()
3645
{
@@ -76,6 +85,16 @@ public void Combine_with_repeated_fields_using_extension_methods()
7685
Assert(sort, "{b: -1, a: -1}");
7786
}
7887

88+
[Fact]
89+
public void Combine_with_value_based_sort_and_additional_sort_should_throw()
90+
{
91+
var subject = CreateSubject<BsonDocument>();
92+
93+
var exception = Record.Exception(() => subject.Ascending().Descending("b"));
94+
95+
exception.Should().BeOfType<InvalidOperationException>();
96+
}
97+
7998
[Fact]
8099
public void Descending()
81100
{
@@ -84,6 +103,14 @@ public void Descending()
84103
Assert(subject.Descending("a"), "{a: -1}");
85104
}
86105

106+
[Fact]
107+
public void Descending_no_field()
108+
{
109+
var subject = CreateSubject<BsonDocument>();
110+
111+
Assert(subject.Descending(), "{direction: -1}");
112+
}
113+
87114
[Fact]
88115
public void Descending_Typed()
89116
{

0 commit comments

Comments
 (0)