Skip to content

Commit 36a96cb

Browse files
committed
CSHARP-3894: Add Linq3Client property and Linq3TestHelpers class.
1 parent be9b610 commit 36a96cb

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

tests/MongoDB.Driver.TestHelpers/DriverTestConfiguration.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using MongoDB.Driver.Core;
2020
using MongoDB.Driver.Core.Clusters;
2121
using MongoDB.Driver.Core.Configuration;
22+
using MongoDB.Driver.Linq;
2223
using MongoDB.Driver.TestHelpers;
2324

2425
namespace MongoDB.Driver.Tests
@@ -34,6 +35,7 @@ public static class DriverTestConfiguration
3435
private static CollectionNamespace __collectionNamespace;
3536
private static DatabaseNamespace __databaseNamespace;
3637
private static Lazy<IReadOnlyList<IMongoClient>> __directClientsToShardRouters;
38+
private static Lazy<MongoClient> __linq3Client;
3739

3840
// static constructor
3941
static DriverTestConfiguration()
@@ -45,6 +47,7 @@ static DriverTestConfiguration()
4547
() => CreateDirectClientsToHostsInConnectionString(CoreTestConfiguration.ConnectionStringWithMultipleShardRouters).ToList().AsReadOnly(),
4648
isThreadSafe: true);
4749
__collectionNamespace = new CollectionNamespace(__databaseNamespace, "testcollection");
50+
__linq3Client = new Lazy<MongoClient>(CreateLinq3Client, isThreadSafe: true);
4851
}
4952

5053
// public static properties
@@ -94,6 +97,14 @@ public static DatabaseNamespace DatabaseNamespace
9497
get { return __databaseNamespace; }
9598
}
9699

100+
/// <summary>
101+
/// Gets the LINQ3 test client.
102+
/// </summary>
103+
public static MongoClient Linq3Client
104+
{
105+
get { return __linq3Client.Value; }
106+
}
107+
97108
// public static methods
98109
public static IEnumerable<IMongoClient> CreateDirectClientsToServersInClientSettings(MongoClientSettings settings)
99110
{
@@ -165,6 +176,13 @@ public static DisposableMongoClient CreateDisposableClient(MongoClientSettings s
165176
return new DisposableMongoClient(new MongoClient(settings));
166177
}
167178

179+
private static MongoClient CreateLinq3Client()
180+
{
181+
var linq3ClientSettings = Client.Settings.Clone();
182+
linq3ClientSettings.LinqProvider = LinqProvider.V3;
183+
return new MongoClient(linq3ClientSettings);
184+
}
185+
168186
public static MongoClientSettings GetClientSettings()
169187
{
170188
var connectionString = CoreTestConfiguration.ConnectionString.ToString();
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
16+
using System.Linq;
17+
using FluentAssertions;
18+
using MongoDB.Bson;
19+
using MongoDB.Driver.Linq.Linq3Implementation;
20+
using MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToExecutableQueryTranslators;
21+
22+
namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests
23+
{
24+
public static class Linq3TestHelpers
25+
{
26+
public static void AssertStages(BsonDocument[] stages, string[] expectedStages)
27+
{
28+
stages.Should().Equal(expectedStages.Select(json => BsonDocument.Parse(json)));
29+
}
30+
31+
// in this overload the collection argument is used only to infer the TDocument type
32+
public static BsonDocument[] Translate<TDocument, TResult>(IMongoCollection<TDocument> collection, IQueryable<TResult> queryable)
33+
{
34+
return Translate<TDocument, TResult>(queryable);
35+
}
36+
37+
public static BsonDocument[] Translate<TDocument, TResult>(IQueryable<TResult> queryable)
38+
{
39+
var provider = (MongoQueryProvider<TDocument>)queryable.Provider;
40+
var executableQuery = ExpressionToExecutableQueryTranslator.Translate<TDocument, TResult>(provider, queryable.Expression);
41+
var stages = executableQuery.Pipeline.Stages;
42+
return stages.Select(s => s.Render().AsBsonDocument).ToArray();
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)