Skip to content

Commit 9f52b9c

Browse files
authored
CSHARP-5512: Convert LINQ-related tests to use fixtures. (#1627)
1 parent a5f4b50 commit 9f52b9c

File tree

89 files changed

+2087
-1667
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+2087
-1667
lines changed

Diff for: CSharpDriver.sln.DotSettings

+6-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ namespace $NAMESPACE$
9191
<s:String x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=58D5EB70DF984241A0863E67D7E33F3D/Field/=CLASS/Expression/@EntryValue">getAlphaNumericFileNameWithoutExtension()</s:String>
9292
<s:Int64 x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=58D5EB70DF984241A0863E67D7E33F3D/Field/=CLASS/InitialRange/@EntryValue">-1</s:Int64>
9393
<s:Int64 x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=58D5EB70DF984241A0863E67D7E33F3D/Field/=CLASS/Order/@EntryValue">0</s:Int64>
94+
<s:Boolean x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=58D5EB70DF984241A0863E67D7E33F3D/Field/=CLASS2/@KeyIndexDefined">True</s:Boolean>
95+
<s:String x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=58D5EB70DF984241A0863E67D7E33F3D/Field/=CLASS2/Expression/@EntryValue">typeName()</s:String>
96+
<s:Int64 x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=58D5EB70DF984241A0863E67D7E33F3D/Field/=CLASS2/InitialRange/@EntryValue">-1</s:Int64>
97+
<s:Int64 x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=58D5EB70DF984241A0863E67D7E33F3D/Field/=CLASS2/Order/@EntryValue">2</s:Int64>
9498
<s:Boolean x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=58D5EB70DF984241A0863E67D7E33F3D/Field/=NAMESPACE/@KeyIndexDefined">True</s:Boolean>
9599
<s:String x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=58D5EB70DF984241A0863E67D7E33F3D/Field/=NAMESPACE/Expression/@EntryValue">fileDefaultNamespace()</s:String>
96100
<s:Int64 x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=58D5EB70DF984241A0863E67D7E33F3D/Field/=NAMESPACE/InitialRange/@EntryValue">-1</s:Int64>
@@ -117,11 +121,12 @@ namespace $NAMESPACE$
117121

118122
using System.Collections.Generic;
119123
using MongoDB.Driver.TestHelpers;
124+
using FluentAssertions;
120125
using Xunit;
121126

122127
namespace $NAMESPACE$
123128
{
124-
public class $CLASS$ : LinqIntegrationTest&lt;$CLASS$.ClassFixture&gt;
129+
public class $CLASS$ : LinqIntegrationTest&lt;$CLASS2$.ClassFixture&gt;
125130
{
126131
public $CLASS$(ClassFixture fixture)
127132
: base(fixture)

Diff for: tests/MongoDB.Driver.TestHelpers/MongoCollectionFixture.cs

+8-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919

2020
namespace MongoDB.Driver.TestHelpers
2121
{
22-
public abstract class MongoCollectionFixture<TDocument> : MongoDatabaseFixture
22+
public abstract class MongoCollectionFixture<TDocument> : MongoCollectionFixture<TDocument, TDocument>
23+
{
24+
}
25+
26+
public abstract class MongoCollectionFixture<TDocument, TInitial> : MongoDatabaseFixture
2327
{
2428
private readonly Lazy<IMongoCollection<TDocument>> _collection;
2529
private bool _dataInitialized;
@@ -31,7 +35,7 @@ protected MongoCollectionFixture()
3135

3236
public IMongoCollection<TDocument> Collection => _collection.Value;
3337

34-
protected abstract IEnumerable<TDocument> InitialData { get; }
38+
protected abstract IEnumerable<TInitial> InitialData { get; }
3539

3640
public virtual bool InitializeDataBeforeEachTestCase => false;
3741

@@ -47,7 +51,8 @@ protected override void InitializeTestCase()
4751
}
4852
else
4953
{
50-
Collection.InsertMany(InitialData);
54+
var collection = Database.GetCollection<TInitial>(Collection.CollectionNamespace.CollectionName);
55+
collection.InsertMany(InitialData);
5156
}
5257

5358
_dataInitialized = true;

Diff for: tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp4118Tests.cs

+16-4
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,26 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System.Collections.Generic;
1617
using System.Linq;
1718
using MongoDB.Bson;
1819
using MongoDB.Bson.Serialization.Attributes;
20+
using MongoDB.Driver.TestHelpers;
1921
using Xunit;
2022

2123
namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira
2224
{
23-
public class CSharp4118Tests : Linq3IntegrationTest
25+
public class CSharp4118Tests : LinqIntegrationTest<CSharp4118Tests.ClassFixture>
2426
{
27+
public CSharp4118Tests(ClassFixture fixture)
28+
: base(fixture)
29+
{
30+
}
31+
2532
[Fact]
2633
public void Known_serializers_should_not_propagate_past_anonymous_class()
2734
{
28-
var collection = GetCollection<C>();
35+
var collection = Fixture.Collection;
2936
var queryable = collection.AsQueryable()
3037
.Select(x => new { S = "abc", HasId = x.Id != "000000000000000000000000" });
3138

@@ -37,7 +44,7 @@ public void Known_serializers_should_not_propagate_past_anonymous_class()
3744
[Fact]
3845
public void Known_serializers_should_not_propagate_past_class_with_member_initializers()
3946
{
40-
var collection = GetCollection<C>();
47+
var collection = Fixture.Collection;
4148
var queryable = collection.AsQueryable()
4249
.Select(x => new R { S = "abc", HasId = x.Id != "000000000000000000000000" });
4350

@@ -46,7 +53,7 @@ public void Known_serializers_should_not_propagate_past_class_with_member_initia
4653
AssertStages(stages, "{ $project : { S : 'abc', HasId : { $ne : ['$_id', ObjectId('000000000000000000000000')] }, _id : 0 } }");
4754
}
4855

49-
private class C
56+
public class C
5057
{
5158
[BsonRepresentation(BsonType.ObjectId)] public string Id { get; set; }
5259
}
@@ -56,5 +63,10 @@ private class R
5663
public string S { get; set; }
5764
public bool HasId { get; set; }
5865
}
66+
67+
public sealed class ClassFixture : MongoCollectionFixture<C>
68+
{
69+
protected override IEnumerable<C> InitialData => null;
70+
}
5971
}
6072
}

Diff for: tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp4126Tests.cs

+18-6
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,26 @@
1414
*/
1515

1616
using System;
17+
using System.Collections.Generic;
1718
using System.Linq;
1819
using FluentAssertions;
20+
using MongoDB.Driver.Core.TestHelpers.XunitExtensions;
21+
using MongoDB.Driver.TestHelpers;
1922
using Xunit;
2023

2124
namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira
2225
{
23-
public class CSharp4126Tests : Linq3IntegrationTest
26+
public class CSharp4126Tests : LinqIntegrationTest<CSharp4126Tests.ClassFixture>
2427
{
28+
public CSharp4126Tests(ClassFixture fixture)
29+
: base(fixture)
30+
{
31+
}
32+
2533
[Fact]
2634
public void Test()
2735
{
28-
var collection = GetCollection<C>();
29-
CreateCollection(
30-
collection,
31-
new C { Id = 1, A = new[] { 1, -2, -3 } });
32-
36+
var collection = Fixture.Collection;
3337
var queryable = collection.AsQueryable()
3438
.Select(_p0 => new { Id = _p0.Id, A = _p0.A.Select(_p1 => Math.Abs(_p1)) });
3539

@@ -47,5 +51,13 @@ public class C
4751
public int Id { get; set; }
4852
public int[] A { get; set; }
4953
}
54+
55+
public sealed class ClassFixture : MongoCollectionFixture<C>
56+
{
57+
protected override IEnumerable<C> InitialData =>
58+
[
59+
new C { Id = 1, A = new[] { 1, -2, -3 } }
60+
];
61+
}
5062
}
5163
}

Diff for: tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp4244Tests.cs

+19-16
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,28 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System;
17+
using System.Collections.Generic;
1618
using System.Linq;
1719
using FluentAssertions;
1820
using MongoDB.Driver.Core.TestHelpers.XunitExtensions;
1921
using MongoDB.Driver.Linq;
22+
using MongoDB.Driver.TestHelpers;
2023
using Xunit;
2124

2225
namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira
2326
{
24-
public class CSharp4244Tests : Linq3IntegrationTest
27+
public class CSharp4244Tests : LinqIntegrationTest<CSharp4244Tests.ClassFixture>
2528
{
29+
public CSharp4244Tests(ClassFixture fixture)
30+
: base(fixture, server => server.VersionGreaterThanOrEqualTo("6.0"))
31+
{
32+
}
33+
2634
[Fact]
2735
public void Where_with_root_should_work()
2836
{
29-
RequireServer.Check().VersionGreaterThanOrEqualTo("6.0");
30-
31-
var collection = CreateCollection();
37+
var collection = Fixture.Collection;
3238
var person = new Person { Id = 2, Name = "Jane Doe" };
3339

3440
var queryable = collection
@@ -42,22 +48,19 @@ public void Where_with_root_should_work()
4248
results.Single().ShouldBeEquivalentTo(person);
4349
}
4450

45-
private IMongoCollection<Person> CreateCollection()
51+
public class Person
4652
{
47-
var collection = GetCollection<Person>("C");
48-
49-
CreateCollection(
50-
collection,
51-
new Person { Id = 1, Name = "John Doe" },
52-
new Person { Id = 2, Name = "Jane Doe" });
53-
54-
return collection;
53+
public int Id { get; set; }
54+
public string Name { get; set; }
5555
}
5656

57-
private class Person
57+
public sealed class ClassFixture : MongoCollectionFixture<Person>
5858
{
59-
public int Id { get; set; }
60-
public string Name { get; set; }
59+
protected override IEnumerable<Person> InitialData =>
60+
[
61+
new Person { Id = 1, Name = "John Doe" },
62+
new Person { Id = 2, Name = "Jane Doe" }
63+
];
6164
}
6265
}
6366
}

Diff for: tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp4289Tests.cs

+20-14
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,30 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System;
17+
using System.Collections.Generic;
1618
using System.Linq;
1719
using FluentAssertions;
1820
using MongoDB.Bson;
1921
using MongoDB.Bson.Serialization.Attributes;
22+
using MongoDB.Driver.Core.TestHelpers.XunitExtensions;
2023
using MongoDB.Driver.Linq;
24+
using MongoDB.Driver.TestHelpers;
2125
using Xunit;
2226

2327
namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira
2428
{
25-
public class CSharp4289Tests : Linq3IntegrationTest
29+
public class CSharp4289Tests : LinqIntegrationTest<CSharp4289Tests.ClassFixture>
2630
{
31+
public CSharp4289Tests(ClassFixture fixture)
32+
: base(fixture)
33+
{
34+
}
35+
2736
[Fact]
2837
public void Select_using_anonymous_class_should_work()
2938
{
30-
var collection = CreateCollection();
39+
var collection = Fixture.Collection;
3140

3241
var queryable = collection.AsQueryable()
3342
.Select(x => new { V = x.Id, W = x.Id });
@@ -43,7 +52,7 @@ public void Select_using_anonymous_class_should_work()
4352
[Fact]
4453
public void Select_using_named_class_should_work()
4554
{
46-
var collection = CreateCollection();
55+
var collection = Fixture.Collection;
4756

4857
var queryable = collection.AsQueryable()
4958
.Select(x => new R(x.Id) { W = x.Id });
@@ -56,17 +65,6 @@ public void Select_using_named_class_should_work()
5665
results.Select(r => r.W).Should().Equal("111111111111111111111111");
5766
}
5867

59-
private IMongoCollection<C> CreateCollection()
60-
{
61-
var collection = GetCollection<C>();
62-
63-
CreateCollection(
64-
collection,
65-
new C { Id = "111111111111111111111111" });
66-
67-
return collection;
68-
}
69-
7068
public class C
7169
{
7270
[BsonRepresentation(BsonType.ObjectId)]
@@ -83,5 +81,13 @@ public R(string v)
8381
[BsonElement("v")] public string V { get; set; }
8482
[BsonElement("w")] public string W { get; set; }
8583
}
84+
85+
public sealed class ClassFixture : MongoCollectionFixture<C>
86+
{
87+
protected override IEnumerable<C> InitialData =>
88+
[
89+
new C { Id = "111111111111111111111111" }
90+
];
91+
}
8692
}
8793
}

Diff for: tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp4316Tests.cs

+20-18
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,24 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System.Collections.Generic;
1617
using System.Linq;
1718
using FluentAssertions;
18-
using MongoDB.Driver.Linq;
19+
using MongoDB.Driver.TestHelpers;
1920
using Xunit;
2021

2122
namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira
2223
{
23-
public class CSharp4316Tests : Linq3IntegrationTest
24+
public class CSharp4316Tests : LinqIntegrationTest<CSharp4316Tests.ClassFixture>
2425
{
26+
public CSharp4316Tests(ClassFixture fixture) : base(fixture)
27+
{
28+
}
29+
2530
[Fact]
2631
public void Value_and_HasValue_should_work_when_properties_on_Nullable_type()
2732
{
28-
var collection = CreateCollection();
33+
var collection = Fixture.Collection;
2934
var matchStage = "{ $match : { 'ActualNullable' : { $ne : null } } }";
3035
var projectStage = "{ $project : { _id : '$_id', Value : '$ActualNullable', HasValue : { $ne : ['$ActualNullable', null] } } }";
3136

@@ -43,7 +48,7 @@ public void Value_and_HasValue_should_work_when_properties_on_Nullable_type()
4348
[Fact]
4449
public void Value_and_HasValue_should_work_when_properties_not_on_Nullable_type()
4550
{
46-
var collection = CreateCollection();
51+
var collection = Fixture.Collection;
4752
var matchStage = "{ $match : { 'OnlyLooksLikeNullable.HasValue' : true } }";
4853
var projectStage = "{ $project : { _id : '$_id', Value : '$OnlyLooksLikeNullable.Value', HasValue : '$OnlyLooksLikeNullable.HasValue' } }";
4954

@@ -59,29 +64,26 @@ public void Value_and_HasValue_should_work_when_properties_not_on_Nullable_type(
5964
results.Select(r => r.Id).Should().Equal(1);
6065
}
6166

62-
private IMongoCollection<C> CreateCollection()
63-
{
64-
var collection = GetCollection<C>("C");
65-
66-
CreateCollection(
67-
collection,
68-
new C { Id = 1, OnlyLooksLikeNullable = new OnlyLooksLikeNullable { Value = "SomeValue", HasValue = true }, ActualNullable = null },
69-
new C { Id = 2, OnlyLooksLikeNullable = new OnlyLooksLikeNullable { Value = null, HasValue = false }, ActualNullable = true });
70-
71-
return collection;
72-
}
73-
74-
private class C
67+
public class C
7568
{
7669
public int Id { get; set; }
7770
public OnlyLooksLikeNullable OnlyLooksLikeNullable { get; set; }
7871
public bool? ActualNullable { get; set; }
7972
}
8073

81-
private class OnlyLooksLikeNullable
74+
public class OnlyLooksLikeNullable
8275
{
8376
public string Value { get; set; }
8477
public bool HasValue { get; set; }
8578
}
79+
80+
public sealed class ClassFixture : MongoCollectionFixture<C>
81+
{
82+
protected override IEnumerable<C> InitialData =>
83+
[
84+
new C { Id = 1, OnlyLooksLikeNullable = new OnlyLooksLikeNullable { Value = "SomeValue", HasValue = true }, ActualNullable = null },
85+
new C { Id = 2, OnlyLooksLikeNullable = new OnlyLooksLikeNullable { Value = null, HasValue = false }, ActualNullable = true }
86+
];
87+
}
8688
}
8789
}

0 commit comments

Comments
 (0)