14
14
*/
15
15
16
16
using System ;
17
- using System . Collections ;
18
17
using System . Collections . Generic ;
19
- using System . IO ;
20
18
using System . Linq ;
21
- using System . Reflection ;
22
19
using System . Threading ;
23
20
using FluentAssertions ;
24
21
using MongoDB . Bson ;
25
22
using MongoDB . Bson . TestHelpers ;
23
+ using MongoDB . Bson . TestHelpers . JsonDrivenTests ;
26
24
using MongoDB . Driver . Core . Clusters ;
27
25
using MongoDB . Driver . Core . Configuration ;
28
26
using MongoDB . Driver . Core . Connections ;
@@ -43,9 +41,11 @@ public class TestRunner
43
41
44
42
[ Theory ]
45
43
[ ClassData ( typeof ( TestCaseFactory ) ) ]
46
- public void RunTestDefinition ( BsonDocument definition )
44
+ public void RunTestDefinition ( JsonDrivenTestCase testCase )
47
45
{
48
- VerifyFields ( definition , "description" , "path" , "phases" , "uri" ) ;
46
+ var definition = testCase . Test ;
47
+
48
+ JsonDrivenHelper . EnsureAllFieldsAreValid ( definition , "description" , "_path" , "phases" , "uri" ) ;
49
49
50
50
_cluster = BuildCluster ( definition ) ;
51
51
_cluster . Initialize ( ) ;
@@ -59,7 +59,7 @@ public void RunTestDefinition(BsonDocument definition)
59
59
60
60
private void ApplyPhase ( BsonDocument phase )
61
61
{
62
- VerifyFields ( phase , "outcome" , "responses" ) ;
62
+ JsonDrivenHelper . EnsureAllFieldsAreValid ( phase , "outcome" , "responses" ) ;
63
63
64
64
var responses = phase [ "responses" ] . AsBsonArray ;
65
65
foreach ( BsonArray response in responses )
@@ -80,7 +80,7 @@ private void ApplyResponse(BsonArray response)
80
80
81
81
var address = response [ 0 ] . AsString ;
82
82
var isMasterDocument = response [ 1 ] . AsBsonDocument ;
83
- VerifyFields ( isMasterDocument , "arbiterOnly" , "arbiters" , "electionId" , "hidden" , "hosts" , "ismaster" , "isreplicaset" , "logicalSessionTimeoutMinutes" , "maxWireVersion" , "me" , "minWireVersion" , "msg" , "ok" , "passive" , "passives" , "primary" , "secondary" , "setName" , "setVersion" ) ;
83
+ JsonDrivenHelper . EnsureAllFieldsAreValid ( isMasterDocument , "arbiterOnly" , "arbiters" , "electionId" , "hidden" , "hosts" , "ismaster" , "isreplicaset" , "logicalSessionTimeoutMinutes" , "maxWireVersion" , "me" , "minWireVersion" , "msg" , "ok" , "passive" , "passives" , "primary" , "secondary" , "setName" , "setVersion" ) ;
84
84
85
85
var endPoint = EndPointHelper . Parse ( address ) ;
86
86
var isMasterResult = new IsMasterResult ( isMasterDocument ) ;
@@ -99,17 +99,6 @@ private void ApplyResponse(BsonArray response)
99
99
SpinWait . SpinUntil ( ( ) => ! object . ReferenceEquals ( _cluster . Description , currentClusterDescription ) , 100 ) ; // sometimes returns false and that's OK
100
100
}
101
101
102
- private void VerifyFields ( BsonDocument document , params string [ ] expectedNames )
103
- {
104
- foreach ( var name in document . Names )
105
- {
106
- if ( ! expectedNames . Contains ( name ) )
107
- {
108
- throw new FormatException ( $ "Invalid field: \" { name } \" .") ;
109
- }
110
- }
111
- }
112
-
113
102
private void VerifyTopology ( ICluster cluster , string expectedType )
114
103
{
115
104
switch ( expectedType )
@@ -141,7 +130,7 @@ private void VerifyTopology(ICluster cluster, string expectedType)
141
130
142
131
private void VerifyOutcome ( BsonDocument outcome )
143
132
{
144
- VerifyFields ( outcome , "compatible" , "logicalSessionTimeoutMinutes" , "servers" , "setName" , "topologyType" ) ;
133
+ JsonDrivenHelper . EnsureAllFieldsAreValid ( outcome , "compatible" , "logicalSessionTimeoutMinutes" , "servers" , "setName" , "topologyType" ) ;
145
134
146
135
var expectedTopologyType = ( string ) outcome [ "topologyType" ] ;
147
136
VerifyTopology ( _cluster , expectedTopologyType ) ;
@@ -194,7 +183,7 @@ private void VerifyOutcome(BsonDocument outcome)
194
183
195
184
private void VerifyServerDescription ( ServerDescription actualDescription , BsonDocument expectedDescription )
196
185
{
197
- VerifyFields ( expectedDescription , "electionId" , "setName" , "setVersion" , "type" ) ;
186
+ JsonDrivenHelper . EnsureAllFieldsAreValid ( expectedDescription , "electionId" , "setName" , "setVersion" , "type" ) ;
198
187
199
188
var expectedType = ( string ) expectedDescription [ "type" ] ;
200
189
switch ( expectedType )
@@ -282,37 +271,19 @@ private ICluster BuildCluster(BsonDocument definition)
282
271
. CreateCluster ( ) ;
283
272
}
284
273
285
- private class TestCaseFactory : IEnumerable < object [ ] >
274
+ private class TestCaseFactory : JsonDrivenTestCaseFactory
286
275
{
287
- public IEnumerator < object [ ] > GetEnumerator ( )
288
- {
289
- const string prefix = "MongoDB.Driver.Core.Tests.Specifications.server_discovery_and_monitoring.tests." ;
290
- const string monitoringPrefix = "MongoDB.Driver.Core.Tests.Specifications.server_discovery_and_monitoring.tests.monitoring." ;
291
- var executingAssembly = typeof ( TestCaseFactory ) . GetTypeInfo ( ) . Assembly ;
292
- var enumerable = executingAssembly
293
- . GetManifestResourceNames ( )
294
- . Where ( path => path . StartsWith ( prefix ) && path . EndsWith ( ".json" ) )
295
- . Where ( path => ! path . StartsWith ( monitoringPrefix ) )
296
- . Select ( path => ReadDefinition ( path ) )
297
- . Select ( definition => new object [ ] { definition } ) ;
298
- return enumerable . GetEnumerator ( ) ;
299
- }
276
+ private readonly string __ignoredTestFolder = "MongoDB.Driver.Core.Tests.Specifications.server_discovery_and_monitoring.tests.monitoring." ;
300
277
301
- IEnumerator IEnumerable . GetEnumerator ( )
302
- {
303
- return GetEnumerator ( ) ;
304
- }
278
+ protected override string PathPrefix => "MongoDB.Driver.Core.Tests.Specifications.server_discovery_and_monitoring.tests." ;
305
279
306
- private static BsonDocument ReadDefinition ( string path )
280
+ protected override IEnumerable < JsonDrivenTestCase > CreateTestCases ( BsonDocument document )
307
281
{
308
- var executingAssembly = typeof ( TestCaseFactory ) . GetTypeInfo ( ) . Assembly ;
309
- using ( var definitionStream = executingAssembly . GetManifestResourceStream ( path ) )
310
- using ( var definitionStringReader = new StreamReader ( definitionStream ) )
282
+ var path = document [ "_path" ] . ToString ( ) ;
283
+ if ( ! path . StartsWith ( __ignoredTestFolder ) )
311
284
{
312
- var definitionString = definitionStringReader . ReadToEnd ( ) ;
313
- var definition = BsonDocument . Parse ( definitionString ) ;
314
- definition . InsertAt ( 0 , new BsonElement ( "path" , path ) ) ;
315
- return definition ;
285
+ var name = GetTestCaseName ( document , document , 0 ) ;
286
+ yield return new JsonDrivenTestCase ( name , document , document ) ;
316
287
}
317
288
}
318
289
}
0 commit comments