-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongoDbPipelineTests.cs
76 lines (66 loc) · 2.31 KB
/
MongoDbPipelineTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using NUnit.Framework;
using Moq;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using MongoDB.Bson;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
[TestFixture]
public class MongoDbPipelineTests
{
private Mock<IMongoCollection<Product>> _mockCollection;
private Mock<IAsyncCursor<CategoryTotal>> _mockCursor;
[SetUp]
public void SetUp()
{
_mockCollection = new Mock<IMongoCollection<Product>>();
_mockCursor = new Mock<IAsyncCursor<CategoryTotal>>();
}
[Test]
public async Task Test_AggregateAsync_GroupByCategory()
{
// Arrange
var sampleData = new List<CategoryTotal>
{
new CategoryTotal { Category = "Books", TotalPrice = 25 },
new CategoryTotal { Category = "Electronics", TotalPrice = 250 }
};
// Mock the cursor's behavior
_mockCursor
.SetupSequence(cursor => cursor.MoveNext(It.IsAny<CancellationToken>()))
.Returns(true) // First call: has data
.Returns(false); // Second call: no data
_mockCursor
.SetupGet(cursor => cursor.Current)
.Returns(sampleData);
// Mock AggregateAsync behavior
_mockCollection
.Setup(collection => collection.AggregateAsync(
It.IsAny<PipelineDefinition<Product, CategoryTotal>>(),
It.IsAny<AggregateOptions>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(_mockCursor.Object);
// Act
var pipeline = new BsonDocument[]
{
new BsonDocument("$group", new BsonDocument
{
{ "_id", "$Category" },
{ "TotalPrice", new BsonDocument("$sum", "$Price") }
})
};
var result = await _mockCollection.Object.AggregateAsync(
pipeline: pipeline,
options: null,
cancellationToken: CancellationToken.None);
var resultList = result.ToList();
// Assert
Assert.AreEqual(2, resultList.Count);
Assert.AreEqual("Books", resultList[0].Category);
Assert.AreEqual(25, resultList[0].TotalPrice);
Assert.AreEqual("Electronics", resultList[1].Category);
Assert.AreEqual(250, resultList[1].TotalPrice);
}
}