1
1
using EphemeralMongo ;
2
+ using MongoDB . Bson ;
3
+ using MongoDB . Bson . Serialization ;
4
+ using MongoDB . Bson . Serialization . Serializers ;
2
5
3
6
namespace TestBuildingBlocks ;
4
7
5
8
// Based on https://gist.github.com/asimmon/612b2d54f1a0d2b4e1115590d456e0be.
6
9
internal sealed class MongoRunnerProvider
7
10
{
8
11
public static readonly MongoRunnerProvider Instance = new ( ) ;
12
+ private static readonly GuidSerializer StandardGuidSerializer = new ( GuidRepresentation . Standard ) ;
9
13
10
14
#if NET8_0
11
15
private readonly object _lockObject = new ( ) ;
@@ -26,11 +30,13 @@ public IMongoRunner Get()
26
30
{
27
31
if ( _runner == null )
28
32
{
33
+ BsonSerializer . TryRegisterSerializer ( StandardGuidSerializer ) ;
34
+
29
35
var runnerOptions = new MongoRunnerOptions
30
36
{
31
37
// Single-node replica set mode is required for transaction support in MongoDB.
32
38
UseSingleNodeReplicaSet = true ,
33
- AdditionalArguments = "--quiet"
39
+ AdditionalArguments = [ "--quiet" ]
34
40
} ;
35
41
36
42
_runner = MongoRunner . Run ( runnerOptions ) ;
@@ -63,20 +69,45 @@ private sealed class MongoRunnerWrapper(MongoRunnerProvider owner, IMongoRunner
63
69
private readonly MongoRunnerProvider _owner = owner ;
64
70
private IMongoRunner ? _underlyingMongoRunner = underlyingMongoRunner ;
65
71
66
- public string ConnectionString => _underlyingMongoRunner ? . ConnectionString ?? throw new ObjectDisposedException ( nameof ( IMongoRunner ) ) ;
72
+ public string ConnectionString
73
+ {
74
+ get
75
+ {
76
+ ObjectDisposedException . ThrowIf ( _underlyingMongoRunner == null , typeof ( IMongoRunner ) ) ;
77
+ return _underlyingMongoRunner . ConnectionString ;
78
+ }
79
+ }
80
+
81
+ public void Import ( string database , string collection , string inputFilePath , string [ ] ? additionalArguments = null , bool drop = false ,
82
+ CancellationToken cancellationToken = default )
83
+ {
84
+ ObjectDisposedException . ThrowIf ( _underlyingMongoRunner == null , typeof ( IMongoRunner ) ) ;
85
+
86
+ _underlyingMongoRunner . Import ( database , collection , inputFilePath , additionalArguments , drop , cancellationToken ) ;
87
+ }
88
+
89
+ public async Task ImportAsync ( string database , string collection , string inputFilePath , string [ ] ? additionalArguments = null , bool drop = false ,
90
+ CancellationToken cancellationToken = default )
91
+ {
92
+ ObjectDisposedException . ThrowIf ( _underlyingMongoRunner == null , typeof ( IMongoRunner ) ) ;
93
+
94
+ await _underlyingMongoRunner . ImportAsync ( database , collection , inputFilePath , additionalArguments , drop , cancellationToken ) ;
95
+ }
67
96
68
- public void Import ( string database , string collection , string inputFilePath , string ? additionalArguments = null , bool drop = false )
97
+ public void Export ( string database , string collection , string outputFilePath , string [ ] ? additionalArguments = null ,
98
+ CancellationToken cancellationToken = default )
69
99
{
70
- ObjectDisposedException . ThrowIf ( _underlyingMongoRunner == null , this ) ;
100
+ ObjectDisposedException . ThrowIf ( _underlyingMongoRunner == null , typeof ( IMongoRunner ) ) ;
71
101
72
- _underlyingMongoRunner . Import ( database , collection , inputFilePath , additionalArguments , drop ) ;
102
+ _underlyingMongoRunner . Export ( database , collection , outputFilePath , additionalArguments , cancellationToken ) ;
73
103
}
74
104
75
- public void Export ( string database , string collection , string outputFilePath , string ? additionalArguments = null )
105
+ public async Task ExportAsync ( string database , string collection , string outputFilePath , string [ ] ? additionalArguments = null ,
106
+ CancellationToken cancellationToken = default )
76
107
{
77
- ObjectDisposedException . ThrowIf ( _underlyingMongoRunner == null , this ) ;
108
+ ObjectDisposedException . ThrowIf ( _underlyingMongoRunner == null , typeof ( IMongoRunner ) ) ;
78
109
79
- _underlyingMongoRunner . Export ( database , collection , outputFilePath , additionalArguments ) ;
110
+ await _underlyingMongoRunner . ExportAsync ( database , collection , outputFilePath , additionalArguments , cancellationToken ) ;
80
111
}
81
112
82
113
public void Dispose ( )
0 commit comments