1
+ using MongoDB . Bson ;
2
+ using MongoDB . Driver ;
3
+ using MongoDbTest . Driver ;
4
+ using MongoDbTest . Dtos ;
5
+
6
+ namespace MongoDbTest ;
7
+
8
+ public class FixtureObjectUpdateDefinition : IClassFixture < MongoDbConnection >
9
+ {
10
+ private readonly MongoDbConnection _dbConnection ;
11
+
12
+ public FixtureObjectUpdateDefinition ( MongoDbConnection dbConnection )
13
+ {
14
+ _dbConnection = dbConnection ;
15
+ }
16
+
17
+ [ Fact ]
18
+ public async Task UpdateUsingObjectDefinition ( )
19
+ {
20
+ var ( collection , cat ) = await SetupDb ( nameof ( UpdateUsingObjectDefinition ) ) ;
21
+
22
+ cat . Name = "UpdatedCat" ;
23
+ var update = new ObjectUpdateDefinition < Animal > ( cat ) ;
24
+ await ExecuteUpdate ( collection , cat . Id , update , cat . Name ) ;
25
+ }
26
+
27
+ [ Fact ]
28
+ public async Task UpdateUsingFixedObjectDefinition ( )
29
+ {
30
+ var ( collection , cat ) = await SetupDb ( nameof ( UpdateUsingFixedObjectDefinition ) ) ;
31
+
32
+ cat . Name = "UpdatedCat" ;
33
+ var update = new FixedObjectUpdateDefinition < Animal > ( cat ) ;
34
+ await ExecuteUpdate ( collection , cat . Id , update , cat . Name ) ;
35
+ }
36
+
37
+ private async Task < ( IMongoCollection < Animal > collection , Cat cat ) > SetupDb ( string testName )
38
+ {
39
+ var collection = _dbConnection . Database . GetCollection < Animal > ( testName ) ;
40
+
41
+ var cat = new Cat
42
+ {
43
+ Name = "TheCat"
44
+ } ;
45
+ await collection . InsertOneAsync ( cat ) ;
46
+
47
+ return ( collection , cat ) ;
48
+ }
49
+
50
+ private async Task ExecuteUpdate ( IMongoCollection < Animal > collection , ObjectId id , UpdateDefinition < Animal > update , string expectedName )
51
+ {
52
+ var updatedCat = await collection . FindOneAndUpdateAsync ( new ExpressionFilterDefinition < Animal > ( animal => animal . Id == id ) , update ,
53
+ new FindOneAndUpdateOptions < Animal > ( ) { ReturnDocument = ReturnDocument . After } ) ;
54
+
55
+ Assert . Equal ( expectedName , updatedCat . Name ) ;
56
+ }
57
+ }
0 commit comments