Skip to content

Commit 94fc89f

Browse files
committed
CSHARP-1237: added ToString on FindFluent.
1 parent d440fca commit 94fc89f

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

src/MongoDB.Driver.Tests/FindFluentTests.cs

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

16+
using System;
1617
using System.Threading;
1718
using System.Threading.Tasks;
1819
using FluentAssertions;
1920
using MongoDB.Bson;
21+
using MongoDB.Bson.Serialization;
2022
using NSubstitute;
2123
using NUnit.Framework;
2224

@@ -40,10 +42,44 @@ public void CountAsync_should_not_throw_a_null_reference_exception()
4042
Arg.Any<CancellationToken>());
4143
}
4244

45+
[Test]
46+
public void ToString_should_return_the_correct_string()
47+
{
48+
var subject = CreateSubject();
49+
subject.Filter = new BsonDocument("Age", 20);
50+
subject.Options.Comment = "awesome";
51+
subject.Options.MaxTime = TimeSpan.FromSeconds(2);
52+
subject.Options.Modifiers = new BsonDocument
53+
{
54+
{ "$explain", true },
55+
{ "$hint", "ix_1" }
56+
};
57+
58+
var find = subject
59+
.SortBy(x => x.LastName)
60+
.ThenByDescending(x => x.FirstName)
61+
.Skip(2)
62+
.Limit(10)
63+
.Project(x => x.FirstName + " " + x.LastName);
64+
65+
var str = find.ToString();
66+
67+
str.Should().Be(
68+
"find({ \"Age\" : 20 }, { \"FirstName\" : 1, \"LastName\" : 1, \"_id\" : 0 })" +
69+
".sort({ \"LastName\" : 1, \"FirstName\" : -1 })" +
70+
".skip(2)" +
71+
".limit(10)" +
72+
".maxTime(2000)" +
73+
"._addSpecial(\"$comment\", \"awesome\")" +
74+
"._addSpecial(\"$explain\", true)" +
75+
"._addSpecial(\"$hint\", \"ix_1\")");
76+
}
77+
4378
private IFindFluent<Person, Person> CreateSubject()
4479
{
4580
var settings = new MongoCollectionSettings();
4681
_collection = Substitute.For<IMongoCollection<Person>>();
82+
_collection.DocumentSerializer.Returns(BsonSerializer.SerializerRegistry.GetSerializer<Person>());
4783
_collection.Settings.Returns(settings);
4884
var options = new FindOptions<Person, Person>();
4985
var subject = new FindFluent<Person, Person>(_collection, new BsonDocument(), options);

src/MongoDB.Driver/FindFluent.cs

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

16+
using System;
17+
using System.Text;
1618
using System.Threading;
1719
using System.Threading.Tasks;
1820
using MongoDB.Bson;
21+
using MongoDB.Bson.Serialization;
1922
using MongoDB.Driver.Core.Misc;
2023

2124
namespace MongoDB.Driver
@@ -107,5 +110,73 @@ public override IFindFluent<TDocument, TProjection> Sort(SortDefinition<TDocumen
107110
{
108111
return _collection.FindAsync(_filter, _options, cancellationToken);
109112
}
113+
114+
115+
public override string ToString()
116+
{
117+
var sb = new StringBuilder("find(");
118+
var renderedFilter = Render(_filter.Render);
119+
sb.Append(renderedFilter.ToString());
120+
121+
if (_options.Projection != null)
122+
{
123+
var renderedProjection = Render(_options.Projection.Render);
124+
if (renderedProjection.Document != null)
125+
{
126+
sb.Append(", " + renderedProjection.Document.ToString());
127+
}
128+
}
129+
sb.Append(")");
130+
131+
if (_options.Sort != null)
132+
{
133+
var renderedSort = Render(_options.Sort.Render);
134+
sb.Append(".sort(" + renderedSort.ToString() + ")");
135+
}
136+
137+
if (_options.Skip.HasValue)
138+
{
139+
sb.Append(".skip(" + _options.Skip.Value.ToString() + ")");
140+
}
141+
142+
if (_options.Limit.HasValue)
143+
{
144+
sb.Append(".limit(" + _options.Limit.Value.ToString() + ")");
145+
}
146+
147+
if (_options.MaxTime != null)
148+
{
149+
sb.Append(".maxTime(" + _options.MaxTime.Value.TotalMilliseconds + ")");
150+
}
151+
152+
if (_options.Comment != null)
153+
{
154+
sb.Append("._addSpecial(\"$comment\", \"" + _options.Comment + "\")");
155+
}
156+
157+
if (_options.Modifiers != null)
158+
{
159+
foreach (var modifier in _options.Modifiers)
160+
{
161+
sb.Append("._addSpecial(\"" + modifier.Name + "\", ");
162+
if (modifier.Value.BsonType == BsonType.String)
163+
{
164+
sb.Append("\"" + modifier.Value.ToString() + "\"");
165+
}
166+
else
167+
{
168+
sb.Append(modifier.Value.ToString());
169+
}
170+
sb.Append(")");
171+
}
172+
}
173+
174+
return sb.ToString();
175+
}
176+
177+
private TRendered Render<TRendered>(Func<IBsonSerializer<TDocument>, IBsonSerializerRegistry, TRendered> renderer)
178+
{
179+
return renderer(_collection.DocumentSerializer, _collection.Settings.SerializerRegistry);
180+
}
110181
}
111182
}

0 commit comments

Comments
 (0)