Skip to content

Commit 374a26f

Browse files
committed
fix(mongodb): remove incorrect client arg-null check in mongo db collection ctor
1 parent 6b0c91d commit 374a26f

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

src/Arcus.Testing.Storage.Cosmos/TemporaryMongoDbCollection.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ private TemporaryMongoDbCollection(
238238
ILogger logger,
239239
TemporaryMongoDbCollectionOptions options)
240240
{
241-
ArgumentNullException.ThrowIfNull(client);
242241
ArgumentNullException.ThrowIfNull(database);
243242
ArgumentNullException.ThrowIfNull(collectionName);
244243
ArgumentNullException.ThrowIfNull(options);

src/Arcus.Testing.Tests.Integration/Storage/Fixture/MongoDbTestContext.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ namespace Arcus.Testing.Tests.Integration.Storage.Fixture
2222
/// </summary>
2323
public class MongoDbTestContext : IAsyncDisposable
2424
{
25-
private readonly IMongoDatabase _database;
2625
private readonly Collection<string> _collectionNames = new();
2726
private readonly ILogger _logger;
2827

@@ -36,10 +35,15 @@ private MongoDbTestContext(
3635
IMongoDatabase database,
3736
ILogger logger)
3837
{
39-
_database = database;
4038
_logger = logger;
39+
Database = database;
4140
}
4241

42+
/// <summary>
43+
/// Gets the current database client to interact with the Mongo DB.
44+
/// </summary>
45+
public IMongoDatabase Database { get; }
46+
4347
/// <summary>
4448
/// Creates an authenticated <see cref="MongoDbTestContext"/>.
4549
/// </summary>
@@ -104,7 +108,7 @@ public async Task<string> WhenCollectionNameAvailableAsync()
104108
_logger.LogTrace("[Test] create MongoDb collection '{CollectionName}' outside the fixture's scope", collectionName);
105109

106110
await WhenMongoDbAvailableAsync(
107-
() => _database.CreateCollectionAsync(collectionName),
111+
() => Database.CreateCollectionAsync(collectionName),
108112
$"[Test] cannot create MongoDb collection '{collectionName}' outside the fixture's scope, due to a high-rate failure");
109113

110114
return collectionName;
@@ -144,15 +148,15 @@ internal static async Task<TResult> WhenMongoDbAvailableAsync<TResult>(Func<Task
144148
public async Task WhenCollectionDeletedAsync(string collectionName)
145149
{
146150
_logger.LogTrace("[Test] delete MongoDb collection '{CollectionName}' outside test fixture's scope", collectionName);
147-
await _database.DropCollectionAsync(collectionName);
151+
await Database.DropCollectionAsync(collectionName);
148152
}
149153

150154
/// <summary>
151155
/// Provides an existing document in a MongoDb collection.
152156
/// </summary>
153157
public async Task<BsonValue> WhenDocumentAvailableAsync<T>(string collectionName, T document)
154158
{
155-
IMongoCollection<BsonDocument> collection = _database.GetCollection<BsonDocument>(collectionName);
159+
IMongoCollection<BsonDocument> collection = Database.GetCollection<BsonDocument>(collectionName);
156160

157161
var bson = document.ToBsonDocument();
158162

@@ -176,7 +180,7 @@ public async Task WhenDocumentDeletedAsync<T>(string collectionName, BsonValue i
176180
{
177181
_logger.LogTrace("[Test] delete MongoDb document '{DocId}' in collection '{CollectionName}' outside test fixture's scope", id, collectionName);
178182

179-
IMongoCollection<T> collection = _database.GetCollection<T>(collectionName);
183+
IMongoCollection<T> collection = Database.GetCollection<T>(collectionName);
180184
FilterDefinition<T> filter = CreateIdFilter<T>(id);
181185

182186
await collection.DeleteOneAsync(filter);
@@ -208,7 +212,7 @@ private async Task<bool> StoresCollectionNameAsync(string collectionName)
208212
{
209213
Filter = Builders<BsonDocument>.Filter.Eq("name", collectionName)
210214
};
211-
using IAsyncCursor<string> collectionNames = await _database.ListCollectionNamesAsync(options);
215+
using IAsyncCursor<string> collectionNames = await Database.ListCollectionNamesAsync(options);
212216
return await collectionNames.AnyAsync();
213217
}
214218

@@ -217,7 +221,7 @@ private async Task<bool> StoresCollectionNameAsync(string collectionName)
217221
/// </summary>
218222
public async Task ShouldStoreDocumentAsync<T>(string collectionName, BsonValue id, Action<T> assertion = null)
219223
{
220-
IMongoCollection<T> collection = _database.GetCollection<T>(collectionName);
224+
IMongoCollection<T> collection = Database.GetCollection<T>(collectionName);
221225
FilterDefinition<T> filter = CreateIdFilter<T>(id);
222226

223227
List<T> matchingDocs = await collection.Find(filter).ToListAsync();
@@ -231,7 +235,7 @@ public async Task ShouldStoreDocumentAsync<T>(string collectionName, BsonValue i
231235
/// </summary>
232236
public async Task ShouldNotStoreDocumentAsync<T>(string collectionName, BsonValue id)
233237
{
234-
IMongoCollection<T> collection = _database.GetCollection<T>(collectionName);
238+
IMongoCollection<T> collection = Database.GetCollection<T>(collectionName);
235239
FilterDefinition<T> filter = CreateIdFilter<T>(id);
236240

237241
List<T> matchingDocs = await collection.Find(filter).ToListAsync();
@@ -259,7 +263,7 @@ public async ValueTask DisposeAsync()
259263
{
260264
disposables.Add(AsyncDisposable.Create(async () =>
261265
{
262-
await _database.DropCollectionAsync(collectionName);
266+
await Database.DropCollectionAsync(collectionName);
263267
}));
264268
}
265269
}

src/Arcus.Testing.Tests.Integration/Storage/TemporaryMongoDbCollectionTests.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public async Task CreateTempMongoDbCollection_OnNonExistingCollection_SucceedsBy
2626
await using MongoDbTestContext context = await GivenCosmosMongoDbAsync();
2727

2828
string collectionName = context.WhenCollectionNameUnavailable();
29-
TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName);
29+
TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName, context);
3030

3131
await context.ShouldStoreCollectionAsync(collectionName);
3232

@@ -47,7 +47,7 @@ public async Task CreateTempMongoDbCollection_OnExistingCollection_SucceedsByLea
4747
Shipment shipment = CreateShipment();
4848
BsonValue existingId = await context.WhenDocumentAvailableAsync(collectionName, shipment);
4949

50-
TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName);
50+
TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName, context);
5151
Shipment createdByUs = CreateShipment();
5252
#pragma warning disable CS0618 // Type or member is obsolete: currently still testing deprecated functionality.
5353
await collection.AddDocumentAsync(createdByUs);
@@ -77,7 +77,7 @@ public async Task CreateTempMongoDbCollectionWithCleanAllOnSetup_OnExistingColle
7777
Shipment shipment = CreateShipment();
7878
BsonValue existingId = await context.WhenDocumentAvailableAsync(collectionName, shipment);
7979

80-
TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName, options =>
80+
TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName, context, options =>
8181
{
8282
options.OnSetup.CleanAllDocuments();
8383
});
@@ -105,7 +105,7 @@ public async Task CreateTempMongoDbCollectionWithCleanMatchingOnSetup_OnExisting
105105
BsonValue unmatchedId = await context.WhenDocumentAvailableAsync(collectionName, unmatched);
106106

107107
// Act
108-
TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName, options =>
108+
TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName, context, options =>
109109
{
110110
options.OnSetup.CleanMatchingDocuments((Shipment s) => s.BoatId == (ObjectId) matchedId)
111111
.CleanMatchingDocuments((Shipment s) => s.BoatName == matched.BoatName);
@@ -126,7 +126,7 @@ public async Task CreateTempMongoDbCollectionWithCleanAllOnTeardown_OnExistingCo
126126
await using MongoDbTestContext context = await GivenCosmosMongoDbAsync();
127127

128128
string collectionName = await context.WhenCollectionNameAvailableAsync();
129-
TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName, options =>
129+
TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName, context, options =>
130130
{
131131
options.OnTeardown.CleanAllDocuments();
132132
});
@@ -151,7 +151,7 @@ public async Task CreateTempMongoDbCollectionWithCleanMatchingOnTeardown_OnExist
151151

152152
Shipment matched = CreateShipment();
153153
Shipment unmatched = CreateShipment();
154-
TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName, options =>
154+
TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName, context, options =>
155155
{
156156
options.OnTeardown.CleanMatchingDocuments((Shipment s) => s.BoatName != unmatched.BoatName)
157157
.CleanMatchingDocuments((Shipment s) => s.BoatName == matched.BoatName);
@@ -182,7 +182,7 @@ public async Task CreateTempMongoDbCollectionWithSetupTeardown_OnExistingCollect
182182
BsonValue matchedOnSetupId = await context.WhenDocumentAvailableAsync(collectionName, matchedOnSetup);
183183
BsonValue unmatchedOnSetupId = await context.WhenDocumentAvailableAsync(collectionName, unmatchedOnSetup);
184184

185-
TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName, options =>
185+
TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName, context, options =>
186186
{
187187
options.OnSetup.CleanMatchingDocuments((Shipment s) => s.BoatName == matchedOnSetup.BoatName);
188188
});
@@ -225,7 +225,7 @@ public async Task CreateTempMongoDbCollection_WhenCollectionWasDeletedOutsideFix
225225
await using MongoDbTestContext context = await GivenCosmosMongoDbAsync();
226226

227227
string collectionName = context.WhenCollectionNameUnavailable();
228-
TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName);
228+
TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName, context);
229229
await context.WhenCollectionDeletedAsync(collectionName);
230230

231231
// Act
@@ -235,13 +235,17 @@ public async Task CreateTempMongoDbCollection_WhenCollectionWasDeletedOutsideFix
235235
await context.ShouldNotStoreCollectionAsync(collectionName);
236236
}
237237

238-
private async Task<TemporaryMongoDbCollection> WhenTempCollectionCreatedAsync(string collectionName, Action<TemporaryMongoDbCollectionOptions> configureOptions = null)
238+
private async Task<TemporaryMongoDbCollection> WhenTempCollectionCreatedAsync(string collectionName, MongoDbTestContext context, Action<TemporaryMongoDbCollectionOptions> configureOptions = null)
239239
{
240240
return await MongoDbTestContext.WhenMongoDbAvailableAsync(async () =>
241241
{
242-
var collection = configureOptions is null
243-
? await TemporaryMongoDbCollection.CreateIfNotExistsAsync(MongoDb.AccountResourceId, MongoDb.DatabaseName, collectionName, Logger)
244-
: await TemporaryMongoDbCollection.CreateIfNotExistsAsync(MongoDb.AccountResourceId, MongoDb.DatabaseName, collectionName, Logger, configureOptions);
242+
var collection = (Bogus.Random.Bool(), configureOptions is null) switch
243+
{
244+
(false, false) => await TemporaryMongoDbCollection.CreateIfNotExistsAsync(MongoDb.AccountResourceId, MongoDb.DatabaseName, collectionName, Logger, configureOptions),
245+
(false, true) => await TemporaryMongoDbCollection.CreateIfNotExistsAsync(MongoDb.AccountResourceId, MongoDb.DatabaseName, collectionName, Logger),
246+
(true, false) => await TemporaryMongoDbCollection.CreateIfNotExistsAsync(context.Database, collectionName, Logger, configureOptions),
247+
(true, true) => await TemporaryMongoDbCollection.CreateIfNotExistsAsync(context.Database, collectionName, Logger)
248+
};
245249

246250
Assert.Equal(collectionName, collection.Name);
247251
return collection;

0 commit comments

Comments
 (0)