Skip to content

Commit 5ebe18f

Browse files
committed
CSHARP-4358: Update tests that only test against LINQ2 to test against LINQ3 instead of or in addition to LINQ2.
1 parent 32835bf commit 5ebe18f

12 files changed

+155
-97
lines changed

tests/MongoDB.Driver.TestHelpers/DriverTestConfiguration.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ public static MongoClientSettings GetClientSettings()
225225
return clientSettings;
226226
}
227227

228+
public static MongoClient GetLinqClient(LinqProvider linqProvider)
229+
{
230+
return linqProvider == LinqProvider.V2 ? Linq2Client : Linq3Client;
231+
}
232+
228233
public static bool IsReplicaSet(IMongoClient client)
229234
{
230235
var clusterTypeIsKnown = SpinWait.SpinUntil(() => client.Cluster.Description.Type != ClusterType.Unknown, TimeSpan.FromSeconds(10));

tests/MongoDB.Driver.Tests/AggregateFluentBucketTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class AggregateFluentBucketTests
4040
static AggregateFluentBucketTests()
4141
{
4242
var databaseNamespace = DriverTestConfiguration.DatabaseNamespace;
43-
__database = DriverTestConfiguration.Linq2Client.GetDatabase(databaseNamespace.DatabaseName);
43+
__database = DriverTestConfiguration.Client.GetDatabase(databaseNamespace.DatabaseName);
4444
__collectionNamespace = DriverTestConfiguration.CollectionNamespace;
4545
__ensureTestData = new Lazy<bool>(CreateTestData);
4646
}
@@ -156,7 +156,7 @@ public void Bucket_typed_should_add_expected_stage()
156156
var stage = result.Stages.Single();
157157
var serializerRegistry = BsonSerializer.SerializerRegistry;
158158
var exhibitSerializer = serializerRegistry.GetSerializer<Exhibit>();
159-
var renderedStage = stage.Render(exhibitSerializer, serializerRegistry, LinqProvider.V2);
159+
var renderedStage = stage.Render(exhibitSerializer, serializerRegistry);
160160
renderedStage.Document.Should().Be("{ $bucket : { groupBy : \"$year\", boundaries : [ 1900, 1920, 1950 ], default : \"Unknown\" } }");
161161
}
162162

@@ -196,7 +196,7 @@ public void Bucket_typed_with_output_should_add_expected_stage()
196196
var stage = result.Stages.Single();
197197
var serializerRegistry = BsonSerializer.SerializerRegistry;
198198
var exhibitSerializer = serializerRegistry.GetSerializer<Exhibit>();
199-
var renderedStage = stage.Render(exhibitSerializer, serializerRegistry, LinqProvider.V2);
199+
var renderedStage = stage.Render(exhibitSerializer, serializerRegistry);
200200
renderedStage.Document.Should().Be("{ $bucket : { groupBy : \"$year\", boundaries : [ 1900, 1920, 1950 ], default : \"Unknown\", output : { Years : { $push : \"$year\" }, Count : { $sum : 1 } } } }");
201201
}
202202

tests/MongoDB.Driver.Tests/FilterDefinitionBuilderEnumComparedToNullableEnumTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public void Where_operator_equals_should_render_correctly(E? value, string expec
160160
{
161161
var filter = __subject.Where(x => x.E == value);
162162

163-
filter.Render(__serializer, __registry, LinqProvider.V2).Should().Be(expectedFilter);
163+
filter.Render(__serializer, __registry).Should().Be(expectedFilter);
164164
}
165165

166166
[Theory]
@@ -170,7 +170,7 @@ public void Where_operator_greater_than_should_render_correctly(E? value, string
170170
{
171171
var filter = __subject.Where(x => x.E > value);
172172

173-
filter.Render(__serializer, __registry, LinqProvider.V2).Should().Be(expectedFilter);
173+
filter.Render(__serializer, __registry).Should().Be(expectedFilter);
174174
}
175175

176176
[Theory]
@@ -180,7 +180,7 @@ public void Where_operator_greater_than_or_equal_should_render_correctly(E? valu
180180
{
181181
var filter = __subject.Where(x => x.E >= value);
182182

183-
filter.Render(__serializer, __registry, LinqProvider.V2).Should().Be(expectedFilter);
183+
filter.Render(__serializer, __registry).Should().Be(expectedFilter);
184184
}
185185

186186
[Theory]
@@ -190,7 +190,7 @@ public void Where_operator_less_than_should_render_correctly(E? value, string ex
190190
{
191191
var filter = __subject.Where(x => x.E < value);
192192

193-
filter.Render(__serializer, __registry, LinqProvider.V2).Should().Be(expectedFilter);
193+
filter.Render(__serializer, __registry).Should().Be(expectedFilter);
194194
}
195195

196196
[Theory]
@@ -200,7 +200,7 @@ public void Where_operator_less_than_or_equal_should_render_correctly(E? value,
200200
{
201201
var filter = __subject.Where(x => x.E <= value);
202202

203-
filter.Render(__serializer, __registry, LinqProvider.V2).Should().Be(expectedFilter);
203+
filter.Render(__serializer, __registry).Should().Be(expectedFilter);
204204
}
205205

206206
[Theory]
@@ -210,7 +210,7 @@ public void Where_operator_not_equal_should_render_correctly(E? value, string ex
210210
{
211211
var filter = __subject.Where(x => x.E != value);
212212

213-
filter.Render(__serializer, __registry, LinqProvider.V2).Should().Be(expectedFilter);
213+
filter.Render(__serializer, __registry).Should().Be(expectedFilter);
214214
}
215215
}
216216
}

tests/MongoDB.Driver.Tests/FilterDefinitionBuilderEnumComparedToNullableEnumWithStringRepresentationTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public void Where_operator_equals_should_render_correctly(E? value, string expec
163163
{
164164
var filter = __subject.Where(x => x.E == value);
165165

166-
filter.Render(__serializer, __registry, LinqProvider.V2).Should().Be(expectedFilter);
166+
filter.Render(__serializer, __registry).Should().Be(expectedFilter);
167167
}
168168

169169
[Theory]
@@ -173,7 +173,7 @@ public void Where_operator_greater_than_should_render_correctly(E? value, string
173173
{
174174
var filter = __subject.Where(x => x.E > value);
175175

176-
filter.Render(__serializer, __registry, LinqProvider.V2).Should().Be(expectedFilter);
176+
filter.Render(__serializer, __registry).Should().Be(expectedFilter);
177177
}
178178

179179
[Theory]
@@ -183,7 +183,7 @@ public void Where_operator_greater_than_or_equal_should_render_correctly(E? valu
183183
{
184184
var filter = __subject.Where(x => x.E >= value);
185185

186-
filter.Render(__serializer, __registry, LinqProvider.V2).Should().Be(expectedFilter);
186+
filter.Render(__serializer, __registry).Should().Be(expectedFilter);
187187
}
188188

189189
[Theory]
@@ -193,7 +193,7 @@ public void Where_operator_less_than_should_render_correctly(E? value, string ex
193193
{
194194
var filter = __subject.Where(x => x.E < value);
195195

196-
filter.Render(__serializer, __registry, LinqProvider.V2).Should().Be(expectedFilter);
196+
filter.Render(__serializer, __registry).Should().Be(expectedFilter);
197197
}
198198

199199
[Theory]
@@ -203,7 +203,7 @@ public void Where_operator_less_than_or_equal_should_render_correctly(E? value,
203203
{
204204
var filter = __subject.Where(x => x.E <= value);
205205

206-
filter.Render(__serializer, __registry, LinqProvider.V2).Should().Be(expectedFilter);
206+
filter.Render(__serializer, __registry).Should().Be(expectedFilter);
207207
}
208208

209209
[Theory]
@@ -213,7 +213,7 @@ public void Where_operator_not_equal_should_render_correctly(E? value, string ex
213213
{
214214
var filter = __subject.Where(x => x.E != value);
215215

216-
filter.Render(__serializer, __registry, LinqProvider.V2).Should().Be(expectedFilter);
216+
filter.Render(__serializer, __registry).Should().Be(expectedFilter);
217217
}
218218
}
219219
}

tests/MongoDB.Driver.Tests/IAggregateFluentExtensionsTests.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using MongoDB.TestHelpers.XunitExtensions;
2121
using MongoDB.Driver.Core.TestHelpers.XunitExtensions;
2222
using MongoDB.Driver.Linq;
23+
using MongoDB.Driver.Tests.Linq.Linq3ImplementationTests;
2324
using Moq;
2425
using System;
2526
using System.Collections.Generic;
@@ -30,7 +31,7 @@
3031

3132
namespace MongoDB.Driver.Tests
3233
{
33-
public class IAggregateFluentExtensionsTests
34+
public class IAggregateFluentExtensionsTests : Linq3IntegrationTest
3435
{
3536
// public methods
3637
#if WINDOWS
@@ -166,15 +167,29 @@ public void Group_should_generate_the_correct_group_when_a_result_type_is_not_sp
166167
AssertLast(subject, expectedGroup);
167168
}
168169

169-
[Fact]
170-
public void Group_should_generate_the_correct_document_using_expressions()
170+
[Theory]
171+
[ParameterAttributeData]
172+
public void Group_should_generate_the_correct_document_using_expressions(
173+
[Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider)
171174
{
172-
var subject = CreateSubject()
175+
var collection = GetCollection<Person>(linqProvider: linqProvider);
176+
var subject = collection.Aggregate()
173177
.Group(x => x.Age, g => new { Name = g.Select(x => x.FirstName + " " + x.LastName).First() });
174178

175-
var expectedGroup = BsonDocument.Parse("{$group: {_id: '$Age', Name: {'$first': { '$concat': ['$FirstName', ' ', '$LastName']}}}}");
179+
var stages = Translate(collection, subject);
180+
var expectedStages = linqProvider == LinqProvider.V2 ?
181+
new[]
182+
{
183+
"{ $group : { _id : '$Age', Name : { $first : { $concat : ['$FirstName', ' ', '$LastName'] } } } }"
184+
}
185+
:
186+
new[]
187+
{
188+
"{ $group : { _id : '$Age', __agg0 : { $first : { $concat : ['$FirstName', ' ', '$LastName'] } } } }",
189+
"{ $project : { Name : '$__agg0', _id : 0 } }"
190+
};
176191

177-
AssertLast(subject, expectedGroup);
192+
AssertStages(stages, expectedStages);
178193
}
179194

180195
[Fact]
@@ -622,7 +637,7 @@ public void Unwind_with_options_with_includeArrayIndex_set_and_preserveNullAndEm
622637
private void AssertLast<TDocument>(IAggregateFluent<TDocument> fluent, BsonDocument expectedLast)
623638
{
624639
var pipeline = new PipelineStagePipelineDefinition<Person, TDocument>(fluent.Stages);
625-
var renderedPipeline = pipeline.Render(BsonSerializer.SerializerRegistry.GetSerializer<Person>(), BsonSerializer.SerializerRegistry, LinqProvider.V2);
640+
var renderedPipeline = pipeline.Render(BsonSerializer.SerializerRegistry.GetSerializer<Person>(), BsonSerializer.SerializerRegistry);
626641

627642
var last = renderedPipeline.Documents.Last();
628643
Assert.Equal(expectedLast, last);
@@ -638,7 +653,6 @@ private IMongoClient CreateClient()
638653
{
639654
var mockClient = new Mock<IMongoClient>();
640655
var settings = new MongoClientSettings();
641-
settings.LinqProvider = LinqProvider.V2;
642656
mockClient.SetupGet(x => x.Settings).Returns(settings);
643657
return mockClient.Object;
644658
}

tests/MongoDB.Driver.Tests/Linq/Linq2ImplementationTestsOnLinq3/MongoQueryableTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1827,7 +1827,7 @@ public void AsQueryable_in_transaction()
18271827
RequireServer.Check().Supports(Feature.ShardedTransactions);
18281828
}
18291829

1830-
using (var session = DriverTestConfiguration.Client.StartSession())
1830+
using (var session = DriverTestConfiguration.Linq3Client.StartSession())
18311831
{
18321832
session.StartTransaction();
18331833
try

tests/MongoDB.Driver.Tests/Linq/Linq2ImplementationTestsOnLinq3/Translators/LegacyPredicateTranslatorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public LegacyPredicateTranslatorTests()
5353

5454
public bool OneTimeSetup()
5555
{
56-
__database = DriverTestConfiguration.Client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName);
56+
__database = DriverTestConfiguration.Linq3Client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName);
5757
__collection = __database.GetCollection<C>(DriverTestConfiguration.CollectionNamespace.CollectionName);
5858

5959
// documents inserted deliberately out of order to test sorting

tests/MongoDB.Driver.Tests/Linq/Linq3ImplementationTests/Jira/CSharp3933Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public void PipelineStageDefinitionBuilderGroupForLinq3_with_expressions_should_
258258

259259
private IMongoCollection<TDocument> GetCollection<TDocument>(LinqProvider linqProvider)
260260
{
261-
var client = linqProvider == LinqProvider.V2 ? DriverTestConfiguration.Linq2Client : DriverTestConfiguration.Linq3Client;
261+
var client = DriverTestConfiguration.GetLinqClient(linqProvider);
262262
var database = client.GetDatabase("test");
263263
return database.GetCollection<TDocument>("test");
264264
}

tests/MongoDB.Driver.Tests/Linq/Linq3ImplementationTests/Linq3IntegrationTest.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,6 @@ protected void CreateCollection<TDocument>(IMongoCollection<TDocument> collectio
5858
CreateCollection(collection, (IEnumerable<TDocument>)documents); ;
5959
}
6060

61-
protected IMongoClient GetClient(LinqProvider linqProvider = LinqProvider.V3)
62-
{
63-
return linqProvider switch
64-
{
65-
LinqProvider.V2 => DriverTestConfiguration.Linq2Client,
66-
LinqProvider.V3 => DriverTestConfiguration.Linq3Client,
67-
_ => throw new ArgumentException($"Invalid linqProvider: {linqProvider}.", nameof(linqProvider))
68-
};
69-
}
70-
7161
protected IMongoCollection<TDocument> GetCollection<TDocument>(string collectionName = null, LinqProvider linqProvider = LinqProvider.V3)
7262
{
7363
return GetCollection<TDocument>(databaseName: null, collectionName, linqProvider);
@@ -81,7 +71,7 @@ protected IMongoCollection<TDocument> GetCollection<TDocument>(string databaseNa
8171

8272
protected IMongoDatabase GetDatabase(string databaseName = null, LinqProvider linqProvider = LinqProvider.V3)
8373
{
84-
var client = GetClient(linqProvider);
74+
var client = DriverTestConfiguration.GetLinqClient(linqProvider);
8575
return client.GetDatabase(databaseName ?? DriverTestConfiguration.DatabaseNamespace.DatabaseName);
8676
}
8777

tests/MongoDB.Driver.Tests/ProjectionDefinitionBuilderTests.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using MongoDB.Bson;
1919
using MongoDB.Bson.Serialization;
2020
using MongoDB.Bson.Serialization.Attributes;
21+
using MongoDB.Bson.TestHelpers.XunitExtensions;
2122
using MongoDB.Driver.Linq;
2223
using Xunit;
2324

@@ -86,13 +87,20 @@ public void ElemMatch_from_filter()
8687
Assert(subject.Include("a.$"), "{'a.$': 1}");
8788
}
8889

89-
[Fact]
90-
public void ElemMatch_from_filter_Typed()
90+
[Theory]
91+
[ParameterAttributeData]
92+
public void ElemMatch_from_filter_Typed(
93+
[Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider)
9194
{
9295
var subject = CreateSubject<Person>();
9396

94-
Assert(subject.Include(x => x.Pets.ElementAt(-1)), "{'pets.$': 1}");
95-
Assert(subject.Include("Pets.$"), "{'pets.$': 1}");
97+
var projection = linqProvider == LinqProvider.V2 ?
98+
subject.Include(x => x.Pets.ElementAt(-1)) :
99+
subject.Include(x => x.Pets.FirstMatchingElement());
100+
Assert(projection, "{ 'pets.$' : 1 }", linqProvider);
101+
102+
projection = subject.Include("Pets.$");
103+
Assert(projection, "{ 'pets.$' : 1 }", linqProvider);
96104
}
97105

98106
[Fact]
@@ -188,10 +196,10 @@ public void Slice_Typed_with_limit()
188196
Assert(subject.Slice("Pets", 10, 20), "{pets: {$slice: [10, 20]}}");
189197
}
190198

191-
private void Assert<TDocument>(ProjectionDefinition<TDocument> projection, string expectedJson)
199+
private void Assert<TDocument>(ProjectionDefinition<TDocument> projection, string expectedJson, LinqProvider linqProvider = LinqProvider.V3)
192200
{
193201
var documentSerializer = BsonSerializer.SerializerRegistry.GetSerializer<TDocument>();
194-
var renderedProjection = projection.Render(documentSerializer, BsonSerializer.SerializerRegistry, LinqProvider.V2);
202+
var renderedProjection = projection.Render(documentSerializer, BsonSerializer.SerializerRegistry, linqProvider);
195203

196204
renderedProjection.Should().Be(expectedJson);
197205
}

0 commit comments

Comments
 (0)