11using EphemeralMongo ;
2+ using MongoDB . Bson ;
3+ using MongoDB . Bson . Serialization ;
4+ using MongoDB . Bson . Serialization . Serializers ;
25
36namespace TestBuildingBlocks ;
47
58// Based on https://gist.github.com/asimmon/612b2d54f1a0d2b4e1115590d456e0be.
69internal sealed class MongoRunnerProvider
710{
811 public static readonly MongoRunnerProvider Instance = new ( ) ;
12+ private static readonly GuidSerializer StandardGuidSerializer = new ( GuidRepresentation . Standard ) ;
913
1014#if NET8_0
1115 private readonly object _lockObject = new ( ) ;
@@ -26,11 +30,13 @@ public IMongoRunner Get()
2630 {
2731 if ( _runner == null )
2832 {
33+ BsonSerializer . TryRegisterSerializer ( StandardGuidSerializer ) ;
34+
2935 var runnerOptions = new MongoRunnerOptions
3036 {
3137 // Single-node replica set mode is required for transaction support in MongoDB.
3238 UseSingleNodeReplicaSet = true ,
33- AdditionalArguments = "--quiet"
39+ AdditionalArguments = [ "--quiet" ]
3440 } ;
3541
3642 _runner = MongoRunner . Run ( runnerOptions ) ;
@@ -63,20 +69,45 @@ private sealed class MongoRunnerWrapper(MongoRunnerProvider owner, IMongoRunner
6369 private readonly MongoRunnerProvider _owner = owner ;
6470 private IMongoRunner ? _underlyingMongoRunner = underlyingMongoRunner ;
6571
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+ }
6796
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 )
6999 {
70- ObjectDisposedException . ThrowIf ( _underlyingMongoRunner == null , this ) ;
100+ ObjectDisposedException . ThrowIf ( _underlyingMongoRunner == null , typeof ( IMongoRunner ) ) ;
71101
72- _underlyingMongoRunner . Import ( database , collection , inputFilePath , additionalArguments , drop ) ;
102+ _underlyingMongoRunner . Export ( database , collection , outputFilePath , additionalArguments , cancellationToken ) ;
73103 }
74104
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 )
76107 {
77- ObjectDisposedException . ThrowIf ( _underlyingMongoRunner == null , this ) ;
108+ ObjectDisposedException . ThrowIf ( _underlyingMongoRunner == null , typeof ( IMongoRunner ) ) ;
78109
79- _underlyingMongoRunner . Export ( database , collection , outputFilePath , additionalArguments ) ;
110+ await _underlyingMongoRunner . ExportAsync ( database , collection , outputFilePath , additionalArguments , cancellationToken ) ;
80111 }
81112
82113 public void Dispose ( )
0 commit comments