Skip to content

Commit c535304

Browse files
committed
CSHARP-1683: Convert MongoDB.Driver.Examples from NUnit to xUnit.net.
1 parent 51a5ecc commit c535304

12 files changed

+125
-162
lines changed

src/MongoDB.Driver.Examples/AggregatePrimer.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,18 @@
1616
using System.Threading.Tasks;
1717
using FluentAssertions;
1818
using MongoDB.Bson;
19-
using NUnit.Framework;
19+
using Xunit;
2020

2121
namespace MongoDB.Driver.Examples
2222
{
23-
[TestFixture]
2423
public class AggregatePrimer : PrimerTestFixture
2524
{
26-
[Test]
25+
[Fact]
2726
public async Task GroupDocumentsByAFieldAndCalculateCount()
2827
{
2928
// @begin: group-documents-by-a-field-and-calculate-count
3029
// @code: start
31-
var collection = _database.GetCollection<BsonDocument>("restaurants");
30+
var collection = __database.GetCollection<BsonDocument>("restaurants");
3231
var aggregate = collection.Aggregate().Group(new BsonDocument { { "_id", "$borough" }, { "count", new BsonDocument("$sum", 1) } });
3332
var results = await aggregate.ToListAsync();
3433
// @code: end
@@ -49,12 +48,12 @@ public async Task GroupDocumentsByAFieldAndCalculateCount()
4948
// @end: group-documents-by-a-field-and-calculate-count
5049
}
5150

52-
[Test]
51+
[Fact]
5352
public async Task FilterAndGroupDocuments()
5453
{
5554
// @begin: filter-and-group-documents
5655
// @code: start
57-
var collection = _database.GetCollection<BsonDocument>("restaurants");
56+
var collection = __database.GetCollection<BsonDocument>("restaurants");
5857
var aggregate = collection.Aggregate()
5958
.Match(new BsonDocument { { "borough", "Queens" }, { "cuisine", "Brazilian" } })
6059
.Group(new BsonDocument { { "_id", "$address.zipcode" }, { "count", new BsonDocument("$sum", 1) } });

src/MongoDB.Driver.Examples/AltersCollectionAttribute.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/MongoDB.Driver.Examples/IndexesPrimer.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,20 @@
1616
using System.Threading.Tasks;
1717
using FluentAssertions;
1818
using MongoDB.Bson;
19-
using NUnit.Framework;
19+
using Xunit;
2020

2121
namespace MongoDB.Driver.Examples
2222
{
23-
[TestFixture]
2423
public class IndexesPrimer : PrimerTestFixture
2524
{
26-
[Test]
27-
[AltersCollection]
25+
[Fact]
2826
public async Task SingleFieldIndex()
2927
{
28+
AltersCollection();
3029

3130
// @begin: single-field-index
3231
// @code: start
33-
var collection = _database.GetCollection<BsonDocument>("restaurants");
32+
var collection = __database.GetCollection<BsonDocument>("restaurants");
3433
var keys = Builders<BsonDocument>.IndexKeys.Ascending("cuisine");
3534
await collection.Indexes.CreateOneAsync(keys);
3635
// @code: end
@@ -46,13 +45,14 @@ public async Task SingleFieldIndex()
4645
// @end: single-field-index
4746
}
4847

49-
[Test]
50-
[AltersCollection]
48+
[Fact]
5149
public async Task CreateCompoundIndex()
5250
{
51+
AltersCollection();
52+
5353
// @begin: create-compound-index
5454
// @code: start
55-
var collection = _database.GetCollection<BsonDocument>("restaurants");
55+
var collection = __database.GetCollection<BsonDocument>("restaurants");
5656
var keys = Builders<BsonDocument>.IndexKeys.Ascending("cuisine").Ascending("address.zipcode");
5757
await collection.Indexes.CreateOneAsync(keys);
5858
// @code: end

src/MongoDB.Driver.Examples/InsertPrimer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616
using System;
1717
using System.Threading.Tasks;
1818
using MongoDB.Bson;
19-
using NUnit.Framework;
19+
using Xunit;
2020

2121
namespace MongoDB.Driver.Examples
2222
{
23-
[TestFixture]
2423
public class InsertPrimer : PrimerTestFixture
2524
{
26-
[Test]
27-
[AltersCollection]
25+
[Fact]
2826
public async Task InsertADocument()
2927
{
28+
AltersCollection();
29+
3030
// @begin: insert-a-document
3131
// @code: start
3232
var document = new BsonDocument
@@ -61,7 +61,7 @@ public async Task InsertADocument()
6161
{ "restaurant_id", "41704620" }
6262
};
6363

64-
var collection = _database.GetCollection<BsonDocument>("restaurants");
64+
var collection = __database.GetCollection<BsonDocument>("restaurants");
6565
await collection.InsertOneAsync(document);
6666
// @code: end
6767

src/MongoDB.Driver.Examples/MongoDB.Driver.Examples.csproj

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,42 +38,48 @@
3838
<HintPath>..\packages\FluentAssertions.4.5.0\lib\net45\FluentAssertions.Core.dll</HintPath>
3939
<Private>True</Private>
4040
</Reference>
41-
<Reference Include="nunit.framework, Version=3.2.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
42-
<HintPath>..\packages\NUnit.3.2.1\lib\net45\nunit.framework.dll</HintPath>
43-
<Private>True</Private>
44-
</Reference>
4541
<Reference Include="System" />
4642
<Reference Include="System.Core" />
4743
<Reference Include="System.Xml.Linq" />
4844
<Reference Include="System.Data.DataSetExtensions" />
4945
<Reference Include="Microsoft.CSharp" />
5046
<Reference Include="System.Data" />
5147
<Reference Include="System.Xml" />
48+
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
49+
<HintPath>..\packages\xunit.abstractions.2.0.0\lib\net35\xunit.abstractions.dll</HintPath>
50+
<Private>True</Private>
51+
</Reference>
52+
<Reference Include="xunit.assert, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
53+
<HintPath>..\packages\xunit.assert.2.1.0\lib\dotnet\xunit.assert.dll</HintPath>
54+
<Private>True</Private>
55+
</Reference>
56+
<Reference Include="xunit.core, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
57+
<HintPath>..\packages\xunit.extensibility.core.2.1.0\lib\dotnet\xunit.core.dll</HintPath>
58+
<Private>True</Private>
59+
</Reference>
60+
<Reference Include="xunit.execution.desktop, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
61+
<HintPath>..\packages\xunit.extensibility.execution.2.1.0\lib\net45\xunit.execution.desktop.dll</HintPath>
62+
<Private>True</Private>
63+
</Reference>
5264
</ItemGroup>
5365
<ItemGroup>
5466
<Compile Include="AggregatePrimer.cs" />
55-
<Compile Include="AltersCollectionAttribute.cs" />
5667
<Compile Include="IndexesPrimer.cs" />
5768
<Compile Include="InsertPrimer.cs" />
5869
<Compile Include="PrimerTestFixture.cs" />
5970
<Compile Include="Properties\AssemblyInfo.cs" />
6071
<Compile Include="QueryPrimer.cs" />
6172
<Compile Include="RemovePrimer.cs" />
62-
<Compile Include="SetupFixture.cs" />
6373
<Compile Include="UpdatePrimer.cs" />
6474
</ItemGroup>
6575
<ItemGroup>
66-
<ProjectReference Include="..\MongoDB.Bson.TestHelpers.NUnit\MongoDB.Bson.TestHelpers.NUnit.csproj">
67-
<Project>{b2ed9e44-0e42-41ae-a018-122691f39ccc}</Project>
68-
<Name>MongoDB.Bson.TestHelpers.NUnit</Name>
69-
</ProjectReference>
7076
<ProjectReference Include="..\MongoDB.Bson\MongoDB.Bson.csproj">
7177
<Project>{0e9a3a2a-49cd-4f6c-847c-dc79b4b65ce6}</Project>
7278
<Name>MongoDB.Bson</Name>
7379
</ProjectReference>
74-
<ProjectReference Include="..\MongoDB.Driver.Core.TestHelpers.NUnit\MongoDB.Driver.Core.TestHelpers.NUnit.csproj">
75-
<Project>{26c8e1f2-d859-4ed0-a1a7-de0b32f81084}</Project>
76-
<Name>MongoDB.Driver.Core.TestHelpers.NUnit</Name>
80+
<ProjectReference Include="..\MongoDB.Driver.Core.TestHelpers\MongoDB.Driver.Core.TestHelpers.csproj">
81+
<Project>{F7B7D81A-CA16-4CD7-8B6C-444280EA37C1}</Project>
82+
<Name>MongoDB.Driver.Core.TestHelpers</Name>
7783
</ProjectReference>
7884
<ProjectReference Include="..\MongoDB.Driver.Core\MongoDB.Driver.Core.csproj">
7985
<Project>{da56482a-5d8f-41e0-85e6-1f22b310f91b}</Project>

src/MongoDB.Driver.Examples/PrimerTestFixture.cs

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,61 +13,59 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System;
1617
using System.Collections.Generic;
1718
using System.IO;
1819
using System.Reflection;
19-
using System.Threading.Tasks;
2020
using MongoDB.Bson;
21-
using NUnit.Framework;
2221

2322
namespace MongoDB.Driver.Examples
2423
{
25-
public class PrimerTestFixture
24+
public abstract class PrimerTestFixture
2625
{
27-
protected static IMongoClient _client;
28-
protected static IMongoDatabase _database;
29-
private static List<BsonDocument> _dataset;
26+
protected static IMongoClient __client;
27+
protected static IMongoDatabase __database;
28+
private static List<BsonDocument> __dataset;
29+
private static Lazy<bool> __lazyOneTimeSetup = new Lazy<bool>(OneTimeSetup);
30+
private static bool __reloadCollection = true;
3031

31-
[OneTimeSetUp]
32-
public void OneTimeSetUp()
32+
protected PrimerTestFixture()
3333
{
34-
var connectionString = CoreTestConfiguration.ConnectionString.ToString();
35-
_client = new MongoClient(connectionString);
36-
_database = _client.GetDatabase("test");
34+
var _ = __lazyOneTimeSetup.Value;
35+
if (__reloadCollection)
36+
{
37+
LoadCollection();
38+
__reloadCollection = false;
39+
}
40+
}
3741

42+
private static bool OneTimeSetup()
43+
{
44+
var connectionString = CoreTestConfiguration.ConnectionString.ToString();
45+
__client = new MongoClient(connectionString);
46+
__database = __client.GetDatabase("test");
3847
LoadDataSetFromResource();
39-
LoadCollection();
48+
return true;
4049
}
4150

42-
[TearDown]
43-
public void TearDown()
51+
// protected methods
52+
protected void AltersCollection()
4453
{
45-
var methodName = TestContext.CurrentContext.Test.Name;
46-
var methodInfo = GetType().GetMethod(methodName);
47-
var altersCollectionAttribute = methodInfo.GetCustomAttribute(typeof(AltersCollectionAttribute));
48-
if (altersCollectionAttribute != null)
49-
{
50-
LoadCollection();
51-
}
54+
__reloadCollection = true;
5255
}
5356

5457
// helper methods
5558
private void LoadCollection()
5659
{
57-
LoadCollectionAsync().GetAwaiter().GetResult();
58-
}
59-
60-
private async Task LoadCollectionAsync()
61-
{
62-
await _database.DropCollectionAsync("restaurants");
60+
__database.DropCollection("restaurants");
6361

64-
var collection = _database.GetCollection<BsonDocument>("restaurants");
65-
await collection.InsertManyAsync(_dataset);
62+
var collection = __database.GetCollection<BsonDocument>("restaurants");
63+
collection.InsertMany(__dataset);
6664
}
6765

68-
private void LoadDataSetFromResource()
66+
private static void LoadDataSetFromResource()
6967
{
70-
_dataset = new List<BsonDocument>();
68+
__dataset = new List<BsonDocument>();
7169

7270
var assembly = Assembly.GetExecutingAssembly();
7371
using (var stream = assembly.GetManifestResourceStream("MongoDB.Driver.Examples.dataset.json"))
@@ -77,7 +75,7 @@ private void LoadDataSetFromResource()
7775
while ((line = reader.ReadLine()) != null)
7876
{
7977
var document = BsonDocument.Parse(line);
80-
_dataset.Add(document);
78+
__dataset.Add(document);
8179
}
8280
}
8381
}

src/MongoDB.Driver.Examples/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using System.Reflection;
1717
using System.Runtime.InteropServices;
18+
using Xunit;
1819

1920
// General Information about an assembly is controlled through the following
2021
// set of attributes. Change these attribute values to modify the information
@@ -48,3 +49,5 @@
4849
// [assembly: AssemblyVersion("1.0.*")]
4950
[assembly: AssemblyVersion("1.0.0.0")]
5051
[assembly: AssemblyFileVersion("1.0.0.0")]
52+
53+
[assembly: CollectionBehavior(DisableTestParallelization = true)]

0 commit comments

Comments
 (0)