Skip to content

Commit e06b5dc

Browse files
committed
Add PoC C# test suite
1 parent 1fe29cc commit e06b5dc

File tree

10 files changed

+246
-0
lines changed

10 files changed

+246
-0
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,12 @@ node_modules
2828

2929
# Mongosh temporary files
3030
temp
31+
32+
# C# Debug files
33+
Debug/
34+
35+
# NuGet generated artifact files
36+
obj/
37+
38+
# User-specific settings files from JetBrains Rider and ReSharper
39+
*.DotSettings.user
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
namespace Examples.Aggregation;
2+
3+
using MongoDB.Driver;
4+
using MongoDB.Bson;
5+
6+
public class GroupTotal
7+
{
8+
private IMongoDatabase _aggDB;
9+
private IMongoCollection<Order> _orders;
10+
11+
public void SeedData()
12+
{
13+
var uri = "mongodb://localhost:27017";
14+
var client = new MongoClient(uri);
15+
_aggDB = client.GetDatabase("agg_tutorials_db");
16+
_orders = _aggDB.GetCollection<Order>("orders");
17+
_orders.DeleteMany(Builders<Order>.Filter.Empty);
18+
19+
_orders.InsertMany(new List<Order>
20+
{
21+
new Order
22+
{
23+
CustomerId = "elise_smith@myemail.com",
24+
OrderDate = DateTime.Parse("2020-05-30T08:35:52Z"),
25+
Value = 231
26+
},
27+
new Order
28+
{
29+
CustomerId = "elise_smith@myemail.com",
30+
OrderDate = DateTime.Parse("2020-01-13T09:32:07Z"),
31+
Value = 99
32+
},
33+
new Order
34+
{
35+
CustomerId = "oranieri@warmmail.com",
36+
OrderDate = DateTime.Parse("2020-01-01T08:25:37Z"),
37+
Value = 63
38+
},
39+
new Order
40+
{
41+
CustomerId = "tj@wheresmyemail.com",
42+
OrderDate = DateTime.Parse("2019-05-28T19:13:32Z"),
43+
Value = 2
44+
},
45+
new Order
46+
{
47+
CustomerId = "tj@wheresmyemail.com",
48+
OrderDate = DateTime.Parse("2020-11-23T22:56:53Z"),
49+
Value = 187
50+
},
51+
new Order
52+
{
53+
CustomerId = "tj@wheresmyemail.com",
54+
OrderDate = DateTime.Parse("2020-08-18T23:04:48Z"),
55+
Value = 4
56+
},
57+
new Order
58+
{
59+
CustomerId = "elise_smith@myemail.com",
60+
OrderDate = DateTime.Parse("2020-12-26T08:55:46Z"),
61+
Value = 4
62+
},
63+
new Order
64+
{
65+
CustomerId = "tj@wheresmyemail.com",
66+
OrderDate = DateTime.Parse("2021-02-28T07:49:32Z"),
67+
Value = 1024
68+
},
69+
new Order
70+
{
71+
CustomerId = "elise_smith@myemail.com",
72+
OrderDate = DateTime.Parse("2020-10-03T13:49:44Z"),
73+
Value = 102
74+
}
75+
});
76+
}
77+
78+
public List<BsonDocument> PerformAggregation()
79+
{
80+
var uri = "mongodb://localhost:27017";
81+
var client = new MongoClient(uri);
82+
_aggDB = client.GetDatabase("agg_tutorials_db");
83+
_orders = _aggDB.GetCollection<Order>("orders");
84+
85+
var results = _orders.Aggregate()
86+
.Match(o => o.OrderDate >= DateTime.Parse("2020-01-01T00:00:00Z") && o.OrderDate < DateTime.Parse("2021-01-01T00:00:00Z"))
87+
.SortBy(o => o.OrderDate)
88+
.Group(
89+
o => o.CustomerId,
90+
g => new
91+
{
92+
CustomerId = g.Key,
93+
FirstPurchaseDate = g.First().OrderDate,
94+
TotalValue = g.Sum(i => i.Value),
95+
TotalOrders = g.Count(),
96+
Orders = g.Select(i => new { i.OrderDate, i.Value }).ToList()
97+
}
98+
)
99+
.SortBy(c => c.FirstPurchaseDate)
100+
.As<BsonDocument>();
101+
102+
foreach (var result in results.ToList())
103+
{
104+
Console.WriteLine(result);
105+
}
106+
107+
return results.ToList();
108+
}
109+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{ "CustomerId" : "oranieri@warmmail.com", "FirstPurchaseDate" : { "$date" : "2020-01-01T08:25:37Z" }, "TotalValue" : 63, "TotalOrders" : 1, "Orders" : [{ "OrderDate" : { "$date" : "2020-01-01T08:25:37Z" }, "Value" : 63 }] }
2+
{ "CustomerId" : "elise_smith@myemail.com", "FirstPurchaseDate" : { "$date" : "2020-01-13T09:32:07Z" }, "TotalValue" : 436, "TotalOrders" : 4, "Orders" : [{ "OrderDate" : { "$date" : "2020-01-13T09:32:07Z" }, "Value" : 99 }, { "OrderDate" : { "$date" : "2020-05-30T08:35:52Z" }, "Value" : 231 }, { "OrderDate" : { "$date" : "2020-10-03T13:49:44Z" }, "Value" : 102 }, { "OrderDate" : { "$date" : "2020-12-26T08:55:46Z" }, "Value" : 4 }] }
3+
{ "CustomerId" : "tj@wheresmyemail.com", "FirstPurchaseDate" : { "$date" : "2020-08-18T23:04:48Z" }, "TotalValue" : 191, "TotalOrders" : 2, "Orders" : [{ "OrderDate" : { "$date" : "2020-08-18T23:04:48Z" }, "Value" : 4 }, { "OrderDate" : { "$date" : "2020-11-23T22:56:53Z" }, "Value" : 187 }] }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using MongoDB.Bson;
2+
using MongoDB.Bson.Serialization.Attributes;
3+
4+
namespace Examples.Aggregation;
5+
6+
public class Order
7+
{
8+
[BsonId]
9+
public ObjectId Id { get; set; }
10+
public string CustomerId { get; set; }
11+
public DateTime OrderDate { get; set; }
12+
public int Value { get; set; }
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="MongoDB.Driver" Version="3.4.0" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Console.WriteLine("This project is not intended to be run as a console application. Run the tests using the 'Tests' solution.");
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Examples.Aggregation;
2+
3+
namespace Tests;
4+
5+
public class GroupTotalTest
6+
{
7+
[SetUp]
8+
public void Setup()
9+
{
10+
var obj = new GroupTotal();
11+
obj.SeedData();
12+
}
13+
14+
[Test]
15+
public void Test1()
16+
{
17+
var obj = new GroupTotal();
18+
var results = obj.PerformAggregation();
19+
20+
string outputfilePath = "/Users/dachary.carey/workspace/docs-code-examples/usage-examples/csharp/driver/Examples/Aggregation/GroupTotalOutput.txt";
21+
var fileData = TestUtils.ReadBsonDocumentsFromFile(outputfilePath);
22+
Assert.That(results.Count, Is.EqualTo(fileData.Length), "Result count does not match output example length.");
23+
for (int i = 0; i < fileData.Length; i++)
24+
{
25+
Assert.That(fileData[i], Is.EqualTo(results[i]), $"Mismatch at index {i}: expected {fileData[i]}, got {results[i]}.");
26+
}
27+
}
28+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using MongoDB.Bson;
2+
3+
public static class TestUtils
4+
{
5+
public static BsonDocument[] ReadBsonDocumentsFromFile(string filePath)
6+
{
7+
var bsonDocuments = new List<BsonDocument>();
8+
9+
foreach (var line in File.ReadLines(filePath))
10+
{
11+
if (!string.IsNullOrWhiteSpace(line))
12+
{
13+
var bsonDoc = BsonDocument.Parse(line);
14+
bsonDocuments.Add(bsonDoc);
15+
}
16+
}
17+
18+
return bsonDocuments.ToArray();
19+
}
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<LangVersion>latest</LangVersion>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<IsPackable>false</IsPackable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="coverlet.collector" Version="6.0.2"/>
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1"/>
14+
<PackageReference Include="NUnit" Version="4.2.2"/>
15+
<PackageReference Include="NUnit.Analyzers" Version="4.3.0"/>
16+
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0"/>
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<Using Include="NUnit.Framework"/>
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<ProjectReference Include="..\Examples\Examples.csproj" />
25+
</ItemGroup>
26+
27+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Examples", "Examples\Examples.csproj", "{D1D4ACA8-520F-4B09-983D-132861CC8AEB}"
4+
EndProject
5+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{E1F6E308-2D95-4FBB-8F39-214C70889A3D}"
6+
EndProject
7+
Global
8+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9+
Debug|Any CPU = Debug|Any CPU
10+
Release|Any CPU = Release|Any CPU
11+
EndGlobalSection
12+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
13+
{D1D4ACA8-520F-4B09-983D-132861CC8AEB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
14+
{D1D4ACA8-520F-4B09-983D-132861CC8AEB}.Debug|Any CPU.Build.0 = Debug|Any CPU
15+
{D1D4ACA8-520F-4B09-983D-132861CC8AEB}.Release|Any CPU.ActiveCfg = Release|Any CPU
16+
{D1D4ACA8-520F-4B09-983D-132861CC8AEB}.Release|Any CPU.Build.0 = Release|Any CPU
17+
{E1F6E308-2D95-4FBB-8F39-214C70889A3D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18+
{E1F6E308-2D95-4FBB-8F39-214C70889A3D}.Debug|Any CPU.Build.0 = Debug|Any CPU
19+
{E1F6E308-2D95-4FBB-8F39-214C70889A3D}.Release|Any CPU.ActiveCfg = Release|Any CPU
20+
{E1F6E308-2D95-4FBB-8F39-214C70889A3D}.Release|Any CPU.Build.0 = Release|Any CPU
21+
EndGlobalSection
22+
EndGlobal

0 commit comments

Comments
 (0)