Skip to content

Commit 4c1419e

Browse files
authored
CSHARP-4094: Add 'comment' option to EstimatedDocumentCountOptions. (#792)
1 parent 681f15d commit 4c1419e

File tree

8 files changed

+86
-1
lines changed

8 files changed

+86
-1
lines changed

src/MongoDB.Driver.Core/Core/Operations/EstimatedDocumentCountOperation.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class EstimatedDocumentCountOperation : IReadOperation<long>
3232
{
3333
// private fields
3434
private readonly CollectionNamespace _collectionNamespace;
35+
private BsonValue _comment;
3536
private TimeSpan? _maxTime;
3637
private readonly MessageEncoderSettings _messageEncoderSettings;
3738
private ReadConcern _readConcern = ReadConcern.Default;
@@ -55,6 +56,15 @@ public EstimatedDocumentCountOperation(CollectionNamespace collectionNamespace,
5556
/// </summary>
5657
public CollectionNamespace CollectionNamespace => _collectionNamespace;
5758

59+
/// <summary>
60+
/// Gets or sets the comment.
61+
/// </summary>
62+
public BsonValue Comment
63+
{
64+
get => _comment;
65+
set => _comment = value;
66+
}
67+
5868
/// <summary>
5969
/// Gets or sets the maximum time the server should spend on this operation.
6070
/// </summary>
@@ -183,6 +193,7 @@ private IExecutableInRetryableReadContext<long> CreateCountOperation()
183193
{
184194
var countOperation = new CountOperation(_collectionNamespace, _messageEncoderSettings)
185195
{
196+
Comment = _comment,
186197
MaxTime = _maxTime,
187198
ReadConcern = _readConcern,
188199
RetryRequested = _retryRequested

src/MongoDB.Driver/EstimatedDocumentCountOptions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
using System;
17+
using MongoDB.Bson;
1718
using MongoDB.Driver.Core.Misc;
1819

1920
namespace MongoDB.Driver
@@ -24,9 +25,19 @@ namespace MongoDB.Driver
2425
public sealed class EstimatedDocumentCountOptions
2526
{
2627
// private fields
28+
private BsonValue _comment;
2729
private TimeSpan? _maxTime;
2830

2931
// public properties
32+
/// <summary>
33+
/// Gets or sets the comment.
34+
/// </summary>
35+
public BsonValue Comment
36+
{
37+
get { return _comment; }
38+
set { _comment = value; }
39+
}
40+
3041
/// <summary>
3142
/// Gets or sets the maximum time.
3243
/// </summary>

src/MongoDB.Driver/MongoCollectionImpl.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,7 @@ private EstimatedDocumentCountOperation CreateEstimatedDocumentCountOperation(Es
972972
{
973973
return new EstimatedDocumentCountOperation(_collectionNamespace, _messageEncoderSettings)
974974
{
975+
Comment = options?.Comment,
975976
MaxTime = options?.MaxTime,
976977
RetryRequested = _database.Client.Settings.RetryReads
977978
};

tests/MongoDB.Driver.Core.Tests/Core/Operations/CountOperationTests.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
using MongoDB.Bson;
1919
using MongoDB.Bson.TestHelpers.XunitExtensions;
2020
using MongoDB.Driver.Core.Clusters;
21-
using MongoDB.Driver.Core.Misc;
2221
using MongoDB.Driver.Core.TestHelpers;
2322
using MongoDB.Driver.Core.TestHelpers.XunitExtensions;
2423
using Xunit;
@@ -36,6 +35,7 @@ public void constructor_should_initialize_instance()
3635
subject.MessageEncoderSettings.Should().BeSameAs(_messageEncoderSettings);
3736

3837
subject.Collation.Should().BeNull();
38+
subject.Comment.Should().BeNull();
3939
subject.Filter.Should().BeNull();
4040
subject.Hint.Should().BeNull();
4141
subject.Limit.Should().NotHaveValue();
@@ -210,6 +210,30 @@ public void CreateCommand_should_return_expected_result()
210210
result.Should().Be(expectedResult);
211211
}
212212

213+
[Theory]
214+
[ParameterAttributeData]
215+
public void CreateCommand_should_return_expected_result_when_Comment_is_set(
216+
[Values(null, "comment")]
217+
string comment)
218+
{
219+
var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings)
220+
{
221+
Comment = comment
222+
};
223+
224+
var connectionDescription = OperationTestHelper.CreateConnectionDescription();
225+
var session = OperationTestHelper.CreateSession();
226+
227+
var result = subject.CreateCommand(connectionDescription, session);
228+
229+
var expectedResult = new BsonDocument
230+
{
231+
{ "count", _collectionNamespace.CollectionName },
232+
{ "comment", comment, comment != null }
233+
};
234+
result.Should().Be(expectedResult);
235+
}
236+
213237
[Theory]
214238
[ParameterAttributeData]
215239
public void CreateCommand_should_return_expected_result_when_Collation_is_set(

tests/MongoDB.Driver.Core.Tests/Core/Operations/EstimatedDocumentCountOperationTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public void constructor_should_initialize_instance()
3737

3838
subject.CollectionNamespace.Should().BeSameAs(_collectionNamespace);
3939
subject.MessageEncoderSettings.Should().BeSameAs(_messageEncoderSettings);
40+
subject.Comment.Should().BeNull();
4041
subject.MaxTime.Should().NotHaveValue();
4142
subject.ReadConcern.IsServerDefault.Should().BeTrue();
4243
subject.RetryRequested.Should().BeFalse();
@@ -60,6 +61,35 @@ public void constructor_should_throw_when_messageEncoderSettings_is_null()
6061
argumentNullException.ParamName.Should().Be("messageEncoderSettings");
6162
}
6263

64+
[Fact]
65+
public void Comment_get_and_set_should_work()
66+
{
67+
var subject = new EstimatedDocumentCountOperation(_collectionNamespace, _messageEncoderSettings);
68+
var value = new BsonString("comment");
69+
70+
subject.Comment = value;
71+
var result = subject.Comment;
72+
73+
result.Should().Be(value);
74+
}
75+
76+
[Theory]
77+
[ParameterAttributeData]
78+
public void CreateCountOperation_should_return_expected_result_when_Comment_is_set(
79+
[Values(null, "test")] string comment)
80+
{
81+
var value = (BsonValue)comment;
82+
var subject = new EstimatedDocumentCountOperation(_collectionNamespace, _messageEncoderSettings)
83+
{
84+
Comment = value
85+
};
86+
87+
var result = subject.CreateCountOperation();
88+
89+
result.Should().BeOfType<CountOperation>()
90+
.Subject.Comment.Should().BeSameAs(value);
91+
}
92+
6393
[Theory]
6494
[ParameterAttributeData]
6595
public void MaxTime_get_and_set_should_work(

tests/MongoDB.Driver.Tests/EstimatedDocumentCountOptionsTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public void constructor_should_initialize_instance()
2727
{
2828
var result = new EstimatedDocumentCountOptions();
2929

30+
result.Comment.Should().BeNull();
3031
result.MaxTime.Should().NotHaveValue();
3132
}
3233

tests/MongoDB.Driver.Tests/UnifiedTestOperations/UnifiedCountDocumentsOperation.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ public UnifiedCountDocumentsOperation Build(string targetCollectionId, BsonDocum
9393
{
9494
switch (argument.Name)
9595
{
96+
case "comment":
97+
options ??= new CountOptions();
98+
options.Comment = argument.Value;
99+
break;
96100
case "filter":
97101
filter = new BsonDocumentFilterDefinition<BsonDocument>(argument.Value.AsBsonDocument);
98102
break;

tests/MongoDB.Driver.Tests/UnifiedTestOperations/UnifiedEstimatedDocumentCountOperation.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ public UnifiedEstimatedDocumentCountOperation Build(string targetCollectionId, B
8181
{
8282
switch (argument.Name)
8383
{
84+
case "comment":
85+
options.Comment = argument.Value.AsBsonValue;
86+
break;
8487
case "maxTimeMS":
8588
options.MaxTime = TimeSpan.FromMilliseconds(argument.Value.AsInt32);
8689
break;

0 commit comments

Comments
 (0)