Skip to content

Commit

Permalink
CSHARP-4447: Standardize on how tests are run against both LINQ provi…
Browse files Browse the repository at this point in the history
…ders.
  • Loading branch information
rstam committed Jan 5, 2023
1 parent 1330dc5 commit 985e39d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 27 deletions.
17 changes: 5 additions & 12 deletions tests/MongoDB.Driver.Tests/IMongoCollectionExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public void Aggregate_should_return_expected_result(
[ParameterAttributeData]
public void AsQueryable_should_return_expected_result(
[Values(false, true)] bool withSession,
[Values(2, 3)] int linqVersion)
[Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider)
{
var collection = CreateMockCollection(linqVersion).Object;
var collection = CreateMockCollection(linqProvider).Object;
var session = withSession ? Mock.Of<IClientSessionHandle>() : null;
var options = new AggregateOptions();

Expand All @@ -75,16 +75,15 @@ public void AsQueryable_should_return_expected_result(
result = collection.AsQueryable(options);
}

if (linqVersion == 2)
if (linqProvider == LinqProvider.V2)
{
var queryable = result.Should().BeOfType<Driver.Linq.Linq2Implementation.MongoQueryableImpl<Person, Person>>().Subject;
var provider = queryable.Provider.Should().BeOfType<Driver.Linq.Linq2Implementation.MongoQueryProviderImpl<Person>>().Subject;
provider._collection().Should().BeSameAs(collection);
provider._options().Should().BeSameAs(options);
provider._session().Should().BeSameAs(session);
}

if (linqVersion == 3)
else
{
var queryable = result.Should().BeOfType<Driver.Linq.Linq3Implementation.MongoQuery<Person, Person>>().Subject;
var provider = queryable.Provider.Should().BeOfType<Driver.Linq.Linq3Implementation.MongoQueryProvider<Person>>().Subject;
Expand Down Expand Up @@ -1234,15 +1233,9 @@ public void Watch_should_call_collection_with_expected_arguments(
}
}

private Mock<IMongoCollection<Person>> CreateMockCollection(int linqVersion = 2)
private Mock<IMongoCollection<Person>> CreateMockCollection(LinqProvider linqProvider = LinqProvider.V2)
{
var mockClient = new Mock<IMongoClient>();
var linqProvider = linqVersion switch
{
2 => LinqProvider.V2,
3 => LinqProvider.V3,
_ => throw new ArgumentException($"Invalid linqVersion: {linqVersion}.", nameof(linqVersion))
};
var clientSettings = new MongoClientSettings { LinqProvider = linqProvider };
mockClient.SetupGet(c => c.Settings).Returns(clientSettings);

Expand Down
13 changes: 7 additions & 6 deletions tests/MongoDB.Driver.Tests/Jira/CSharp4172Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver.Linq;
using MongoDB.Driver.Tests.Linq.Linq3ImplementationTests;
using MongoDB.TestHelpers.XunitExtensions;
using Xunit;

namespace MongoDB.Driver.Tests.Jira
{
public class CSharp4172Tests : Linq3IntegrationTest
{
[Theory]
[InlineData(LinqProvider.V2)]
[InlineData(LinqProvider.V3)]
public void Find_uses_the_expected_serializer(LinqProvider linqProvider)
[ParameterAttributeData]
public void Find_uses_the_expected_serializer(
[Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider)
{
var collection = GetCollection<Order>(null, null, linqProvider);

Expand All @@ -43,9 +44,9 @@ public void Find_uses_the_expected_serializer(LinqProvider linqProvider)
}

[Theory]
[InlineData(LinqProvider.V2)]
[InlineData(LinqProvider.V3)]
public void Aggregate_uses_the_expected_serializer(LinqProvider linqProvider)
[ParameterAttributeData]
public void Aggregate_uses_the_expected_serializer(
[Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider)
{
var collection = GetCollection<Order>(null, null, linqProvider);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ public class CSharp4079Tests : Linq3IntegrationTest
[Theory]
[ParameterAttributeData]
public void Positional_operator_with_negative_one_array_index_should_work_or_throw_depending_on_Linq_provider(
[Values(false, true)] bool useLinq2)
[Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider)
{
var linqProvider = useLinq2 ? LinqProvider.V2 : LinqProvider.V3;
var collection = GetCollection<C>();

var negativeOne = -1;
var update = Builders<C>.Update.Set(x => x.A[negativeOne], 0); // using -1 constant is a compile time error

if (useLinq2)
if (linqProvider == LinqProvider.V2)
{
var rendered = Render(update, linqProvider);
rendered.Should().Be("{ $set : { 'A.$' : 0 } }");
Expand All @@ -51,14 +50,13 @@ public void Positional_operator_with_negative_one_array_index_should_work_or_thr
[Theory]
[ParameterAttributeData]
public void Positional_operator_with_negative_one_ElementAt_should_work_or_throw_depending_on_Linq_provider(
[Values(false, true)] bool useLinq2)
[Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider)
{
var linqProvider = useLinq2 ? LinqProvider.V2 : LinqProvider.V3;
var collection = GetCollection<C>();

var update = Builders<C>.Update.Set(x => x.A.ElementAt(-1), 0);

if (useLinq2)
if (linqProvider == LinqProvider.V2)
{
var rendered = Render(update, linqProvider);
rendered.Should().Be("{ $set : { 'A.$' : 0 } }");
Expand Down
6 changes: 3 additions & 3 deletions tests/MongoDB.Driver.Tests/Samples/AggregationSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ public async Task States_with_pops_over_20000_queryable_method(

#if !MONO
[Theory]
[InlineData(LinqProvider.V2)]
[InlineData(LinqProvider.V3)]
public void States_with_pops_over_20000_queryable_syntax(LinqProvider linqProvider)
[ParameterAttributeData]
public void States_with_pops_over_20000_queryable_syntax(
[Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider)
{
var client = DriverTestConfiguration.GetLinqClient(linqProvider);
var database = client.GetDatabase(__collection.CollectionNamespace.DatabaseNamespace.DatabaseName);
Expand Down

0 comments on commit 985e39d

Please sign in to comment.