-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChangeStreamExecute.cs
55 lines (44 loc) · 2.01 KB
/
ChangeStreamExecute.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
[Test]
public async Task ChangeStreamExecute_ProcessesChangeStreamCorrectly()
{
// Arrange
var changeStreamOptions = new ChangeStreamOptions
{
FullDocument = ChangeStreamFullDocumentOption.UpdateLookup
};
var mockCursor = new Mock<IAsyncCursor<ChangeStreamDocument<BsonDocument>>>();
// Simulate a ChangeStreamDocument with mocked properties
var bsonDocument = new BsonDocument
{
{ "timestamp", DateTime.UtcNow }
};
var changeStreamDocument = new ChangeStreamDocument<BsonDocument>(
fullDocument: bsonDocument,
namespaceDocument: null,
documentKey: null,
updateDescription: null,
clusterTime: null,
operationType: ChangeStreamOperationType.Insert,
resumeToken: BsonDocument.Parse("{}")
);
mockCursor
.SetupSequence(c => c.MoveNextAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(true)
.ReturnsAsync(false);
mockCursor.Setup(c => c.Current).Returns(new[] { changeStreamDocument });
_mockChangeRecordsRepo
.Setup(r => r.Watch(It.IsAny<PipelineDefinition<ChangeStreamDocument<BsonDocument>, ChangeStreamDocument<BsonDocument>>>(), changeStreamOptions, It.IsAny<CancellationToken>()))
.Returns(mockCursor.Object);
var pipelineStages = new Mock<PipelineStages>(_mockUnitOfWork.Object);
pipelineStages
.Setup(p => p.ChangeRecordToFlatRecord(It.IsAny<BsonDocument>()))
.ReturnsAsync(new List<BsonDocument> { bsonDocument });
_mockUnitOfWork.Setup(u => u.ChangeRecordsFlatRepo.CreateAsync(It.IsAny<ChangeRecordsFlat>()))
.Returns(Task.CompletedTask);
var service = new MyService(_mockLogger.Object, _mockUnitOfWork.Object, pipelineStages.Object);
// Act
await service.ChangeStreamExecute(_cancellationTokenSource.Token);
// Assert
_mockLogger.Verify(l => l.LogInformation("Subscribe to Change Stream"), Times.Once);
_mockUnitOfWork.Verify(u => u.ChangeRecordsFlatRepo.CreateAsync(It.IsAny<ChangeRecordsFlat>()), Times.Once);
}