Skip to content

Commit 4d353d4

Browse files
author
rstam
committed
CSHARP-614, CSHARP-615: better unit tests.
1 parent 566c6b5 commit 4d353d4

31 files changed

+4870
-2625
lines changed

Driver/Core/MongoClient.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,27 @@ private static MongoClientSettings ParseConnectionString(string connectionString
9090

9191
// public methods
9292
/// <summary>
93-
/// Gets a MongoServer object using this client's settings.
93+
/// Gets a MongoDatabase instance representing a database. See also GetServer.
94+
/// </summary>
95+
/// <param name="databaseSettings">The settings to use with this database.</param>
96+
/// <returns>An instance of MongoDatabase.</returns>
97+
public MongoDatabase GetDatabase(MongoDatabaseSettings databaseSettings)
98+
{
99+
return GetServer().GetDatabase(databaseSettings);
100+
}
101+
102+
/// <summary>
103+
/// Gets a MongoDatabase instance representing a database. See also GetServer.
104+
/// </summary>
105+
/// <param name="databaseName">The name of the database.</param>
106+
/// <returns>An instance of MongoDatabase.</returns>
107+
public virtual MongoDatabase GetDatabase(string databaseName)
108+
{
109+
return GetServer().GetDatabase(databaseName);
110+
}
111+
112+
/// <summary>
113+
/// Gets a MongoServer object using this client's settings. See also GetDatabase.
94114
/// </summary>
95115
/// <returns>A MongoServer.</returns>
96116
public MongoServer GetServer()

Driver/Core/MongoConnectionStringBuilder.cs

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,16 @@ public bool? FireAndForget
195195
get { return _fireAndForget; }
196196
set
197197
{
198-
if (_safe != null)
199-
{
200-
throw new InvalidOperationException("FireAndForget and Safe are mutually exclusive.");
201-
}
202-
if ((value != null && value.Value) && AnyWriteConcernSettingsAreSet())
198+
if (value != null)
203199
{
204-
throw new InvalidOperationException("FireAndForget cannot be set to true if any other write concern values have been set.");
200+
if (_safe != null)
201+
{
202+
throw new InvalidOperationException("FireAndForget and Safe are mutually exclusive.");
203+
}
204+
if (value.Value && AnyWriteConcernSettingsAreSet())
205+
{
206+
throw new InvalidOperationException("FireAndForget cannot be set to true if any other write concern values have been set.");
207+
}
205208
}
206209
_fireAndForget = value;
207210
base["fireAndForget"] = (value == null) ? null : XmlConvert.ToString(value.Value);
@@ -230,7 +233,8 @@ public GuidRepresentation GuidRepresentation
230233
get { return _guidRepresentation; }
231234
set
232235
{
233-
base["uuidRepresentation"] = _guidRepresentation = value;
236+
_guidRepresentation = value;
237+
base["uuidRepresentation"] = (value == GuidRepresentation.CSharpLegacy) ? "csharpLegacy" : MongoUtils.ToCamelCase(value.ToString());
234238
}
235239
}
236240

@@ -363,28 +367,28 @@ public ReadPreference ReadPreference
363367
}
364368
set
365369
{
366-
if (_slaveOk.HasValue)
370+
if (value != null && _slaveOk.HasValue)
367371
{
368372
throw new InvalidOperationException("ReadPreference cannot be set because SlaveOk already has a value.");
369373
}
370374
_readPreference = value;
371375

372-
base["readPreference"] = MongoUtils.ToCamelCase(_readPreference.ReadPreferenceMode.ToString());
373-
if (_readPreference.TagSets == null)
374-
{
375-
base["readPreferenceTags"] = null;
376-
}
377-
else
376+
base["readPreference"] = (value == null) ? null : MongoUtils.ToCamelCase(value.ReadPreferenceMode.ToString());
377+
if (value != null && value.TagSets != null)
378378
{
379379
var readPreferenceTagsString = string.Join(
380380
"|",
381-
_readPreference.TagSets.Select(ts => string.Join(
381+
value.TagSets.Select(ts => string.Join(
382382
",",
383383
ts.Tags.Select(t => string.Format("{0}:{1}", t.Name, t.Value)).ToArray()
384384
)).ToArray()
385385
);
386386
base["readPreferenceTags"] = readPreferenceTagsString;
387387
}
388+
else
389+
{
390+
base["readPreferenceTags"] = null;
391+
}
388392
}
389393
}
390394

@@ -409,13 +413,16 @@ public bool? Safe
409413
get { return _safe; }
410414
set
411415
{
412-
if (_fireAndForget != null)
413-
{
414-
throw new InvalidOperationException("FireAndForget and Safe are mutually exclusive.");
415-
}
416-
if ((value != null && !value.Value) && AnyWriteConcernSettingsAreSet())
416+
if (value != null)
417417
{
418-
throw new InvalidOperationException("Safe cannot be set to false if any other write concern values have been set.");
418+
if (_fireAndForget != null)
419+
{
420+
throw new InvalidOperationException("FireAndForget and Safe are mutually exclusive.");
421+
}
422+
if (!value.Value && AnyWriteConcernSettingsAreSet())
423+
{
424+
throw new InvalidOperationException("Safe cannot be set to false if any other write concern values have been set.");
425+
}
419426
}
420427
_safe = value;
421428
base["safe"] = (value == null) ? null : XmlConvert.ToString(value.Value);
@@ -489,7 +496,7 @@ public TimeSpan SecondaryAcceptableLatency
489496
public MongoServerAddress Server
490497
{
491498
get { return (_servers == null) ? null : _servers.Single(); }
492-
set { Servers = new[] { value }; }
499+
set { Servers = (value == null) ? null : new[] { value }; }
493500
}
494501

495502
/// <summary>
@@ -501,7 +508,7 @@ public IEnumerable<MongoServerAddress> Servers
501508
set
502509
{
503510
_servers = value;
504-
base["server"] = GetServersString();
511+
base["server"] = (value == null) ? null : GetServersString(value);
505512
}
506513
}
507514

@@ -912,10 +919,10 @@ private void EnsureFireAndForgetIsNotTrue(string propertyName)
912919
}
913920
}
914921

915-
private string GetServersString()
922+
private string GetServersString(IEnumerable<MongoServerAddress> servers)
916923
{
917924
var sb = new StringBuilder();
918-
foreach (var server in _servers)
925+
foreach (var server in servers)
919926
{
920927
if (sb.Length > 0) { sb.Append(","); }
921928
if (server.Port == 27017)

Driver/Core/MongoCredentialsStore.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ public MongoCredentialsStore()
3939
{
4040
}
4141

42+
// public properties
43+
/// <summary>
44+
/// Gets whether the credentials store has been frozen to prevent further changes.
45+
/// </summary>
46+
public bool IsFrozen
47+
{
48+
get { return _isFrozen; }
49+
}
50+
4251
// public methods
4352
/// <summary>
4453
/// Adds the credentials for a database to the store.

Driver/Core/MongoUrlBuilder.cs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,16 @@ public bool? FireAndForget
150150
get { return _fireAndForget; }
151151
set
152152
{
153-
if (_safe != null)
154-
{
155-
throw new InvalidOperationException("FireAndForget and Safe are mutually exclusive.");
156-
}
157-
if ((value != null && value.Value) && AnyWriteConcernSettingsAreSet())
153+
if (value != null)
158154
{
159-
throw new InvalidOperationException("FireAndForget cannot be set to true if any other write concern values have been set.");
155+
if (_safe != null)
156+
{
157+
throw new InvalidOperationException("FireAndForget and Safe are mutually exclusive.");
158+
}
159+
if (value.Value && AnyWriteConcernSettingsAreSet())
160+
{
161+
throw new InvalidOperationException("FireAndForget cannot be set to true if any other write concern values have been set.");
162+
}
160163
}
161164
_fireAndForget = value;
162165
}
@@ -292,7 +295,7 @@ public ReadPreference ReadPreference
292295
}
293296
set
294297
{
295-
if (_slaveOk.HasValue)
298+
if (value != null && _slaveOk.HasValue)
296299
{
297300
throw new InvalidOperationException("ReadPreference cannot be set because SlaveOk already has a value.");
298301
}
@@ -318,13 +321,16 @@ public bool? Safe
318321
get { return _safe; }
319322
set
320323
{
321-
if (_fireAndForget != null)
322-
{
323-
throw new InvalidOperationException("FireAndForget and Safe are mutually exclusive.");
324-
}
325-
if ((value != null && !value.Value) && AnyWriteConcernSettingsAreSet())
324+
if (value != null)
326325
{
327-
throw new InvalidOperationException("Safe cannot be set to false if any other write concern values have been set.");
326+
if (_fireAndForget != null)
327+
{
328+
throw new InvalidOperationException("FireAndForget and Safe are mutually exclusive.");
329+
}
330+
if (!value.Value && AnyWriteConcernSettingsAreSet())
331+
{
332+
throw new InvalidOperationException("Safe cannot be set to false if any other write concern values have been set.");
333+
}
328334
}
329335
_safe = value;
330336
}
@@ -396,7 +402,7 @@ public TimeSpan SecondaryAcceptableLatency
396402
public MongoServerAddress Server
397403
{
398404
get { return (_servers == null) ? null : _servers.Single(); }
399-
set { _servers = new [] { value }; }
405+
set { _servers = (value == null) ? null : new [] { value }; }
400406
}
401407

402408
/// <summary>
@@ -765,7 +771,7 @@ public void Parse(string url)
765771
const string pattern =
766772
@"^mongodb://" +
767773
@"((?<username>[^:]+):(?<password>[^@]+)@)?" +
768-
@"(?<servers>" + serverPattern + "(," + serverPattern + ")*)" +
774+
@"(?<servers>" + serverPattern + "(," + serverPattern + ")*)?" +
769775
@"(/(?<database>[^?]+)?(\?(?<query>.*))?)?$";
770776
Match match = Regex.Match(url, pattern);
771777
if (match.Success)
@@ -795,10 +801,6 @@ public void Parse(string url)
795801
}
796802
_servers = addresses;
797803
}
798-
else
799-
{
800-
throw new FormatException("Invalid connection string. Server missing.");
801-
}
802804

803805
_databaseName = (databaseName != "") ? databaseName : null;
804806

@@ -980,13 +982,12 @@ public override string ToString()
980982
if (_useSsl)
981983
{
982984
query.AppendFormat("ssl=true;");
983-
if (!_verifySslCertificate)
984-
{
985-
query.AppendFormat("sslVerifyCertificate=false;");
986-
}
987985
}
988-
if (_connectionMode == ConnectionMode.Direct && _servers != null && _servers.Count() != 1 ||
989-
_connectionMode == ConnectionMode.ReplicaSet && (_servers == null || _servers.Count() == 1))
986+
if (!_verifySslCertificate)
987+
{
988+
query.AppendFormat("sslVerifyCertificate=false;");
989+
}
990+
if (_connectionMode != ConnectionMode.Automatic)
990991
{
991992
query.AppendFormat("connect={0};", MongoUtils.ToCamelCase(_connectionMode.ToString()));
992993
}
@@ -1075,7 +1076,7 @@ public override string ToString()
10751076
}
10761077
if (_guidRepresentation != MongoDefaults.GuidRepresentation)
10771078
{
1078-
query.AppendFormat("uuidRepresentation={0};", _guidRepresentation);
1079+
query.AppendFormat("uuidRepresentation={0};", (_guidRepresentation == GuidRepresentation.CSharpLegacy) ? "csharpLegacy" : MongoUtils.ToCamelCase(_guidRepresentation.ToString()));
10791080
}
10801081
if (query.Length != 0)
10811082
{

Driver/Core/ReadPreference.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ public override string ToString()
394394
// internal methods
395395
internal bool ToSlaveOk()
396396
{
397-
return _readPreferenceMode != ReadPreferenceMode.Primary;
397+
return _readPreferenceMode != ReadPreferenceMode.Primary && _readPreferenceMode != ReadPreferenceMode.PrimaryPreferred;
398398
}
399399

400400
// private methods

DriverUnitTests/Configuration.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace MongoDB.DriverUnitTests
3131
public static class Configuration
3232
{
3333
// private static fields
34+
private static MongoClient __testClient;
3435
private static MongoServer __testServer;
3536
private static MongoDatabase __testDatabase;
3637
private static MongoCollection<BsonDocument> __testCollection;
@@ -40,19 +41,28 @@ static Configuration()
4041
{
4142
var connectionString = "mongodb://localhost/?safe=true"; // TODO: make this configurable
4243

43-
var mongoUrlBuilder = new MongoUrlBuilder(connectionString);
44-
var serverSettings = mongoUrlBuilder.ToServerSettings();
45-
if (!serverSettings.SafeMode.Enabled)
44+
var mongoUrl = new MongoUrl(connectionString);
45+
var clientSettings = MongoClientSettings.FromUrl(mongoUrl);
46+
if (clientSettings.WriteConcern.FireAndForget)
4647
{
47-
serverSettings.SafeMode = SafeMode.True;
48+
clientSettings.WriteConcern.FireAndForget = false;
4849
}
4950

50-
__testServer = MongoServer.Create(serverSettings);
51-
__testDatabase = __testServer["csharpdriverunittests"];
52-
__testCollection = __testDatabase["testcollection"];
51+
__testClient = new MongoClient(clientSettings);
52+
__testServer = __testClient.GetServer();
53+
__testDatabase = __testServer.GetDatabase("csharpdriverunittests");
54+
__testCollection = __testDatabase.GetCollection("testcollection");
55+
}
56+
57+
// public static properties
58+
/// <summary>
59+
/// Gets the test client.
60+
/// </summary>
61+
public static MongoClient TestClient
62+
{
63+
get { return __testClient; }
5364
}
5465

55-
// public static methods
5666
/// <summary>
5767
/// Gets the test collection.
5868
/// </summary>

0 commit comments

Comments
 (0)