Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ private TemporaryMongoDbCollection(
ILogger logger,
TemporaryMongoDbCollectionOptions options)
{
ArgumentNullException.ThrowIfNull(client);
ArgumentNullException.ThrowIfNull(database);
ArgumentNullException.ThrowIfNull(collectionName);
ArgumentNullException.ThrowIfNull(options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ namespace Arcus.Testing.Tests.Integration.Storage.Fixture
/// </summary>
public class MongoDbTestContext : IAsyncDisposable
{
private readonly IMongoDatabase _database;
private readonly Collection<string> _collectionNames = new();
private readonly ILogger _logger;

Expand All @@ -36,10 +35,15 @@ private MongoDbTestContext(
IMongoDatabase database,
ILogger logger)
{
_database = database;
_logger = logger;
Database = database;
}

/// <summary>
/// Gets the current database client to interact with the Mongo DB.
/// </summary>
public IMongoDatabase Database { get; }

/// <summary>
/// Creates an authenticated <see cref="MongoDbTestContext"/>.
/// </summary>
Expand Down Expand Up @@ -104,7 +108,7 @@ public async Task<string> WhenCollectionNameAvailableAsync()
_logger.LogTrace("[Test] create MongoDb collection '{CollectionName}' outside the fixture's scope", collectionName);

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

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

/// <summary>
/// Provides an existing document in a MongoDb collection.
/// </summary>
public async Task<BsonValue> WhenDocumentAvailableAsync<T>(string collectionName, T document)
{
IMongoCollection<BsonDocument> collection = _database.GetCollection<BsonDocument>(collectionName);
IMongoCollection<BsonDocument> collection = Database.GetCollection<BsonDocument>(collectionName);

var bson = document.ToBsonDocument();

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

IMongoCollection<T> collection = _database.GetCollection<T>(collectionName);
IMongoCollection<T> collection = Database.GetCollection<T>(collectionName);
FilterDefinition<T> filter = CreateIdFilter<T>(id);

await collection.DeleteOneAsync(filter);
Expand Down Expand Up @@ -208,7 +212,7 @@ private async Task<bool> StoresCollectionNameAsync(string collectionName)
{
Filter = Builders<BsonDocument>.Filter.Eq("name", collectionName)
};
using IAsyncCursor<string> collectionNames = await _database.ListCollectionNamesAsync(options);
using IAsyncCursor<string> collectionNames = await Database.ListCollectionNamesAsync(options);
return await collectionNames.AnyAsync();
}

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

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

List<T> matchingDocs = await collection.Find(filter).ToListAsync();
Expand Down Expand Up @@ -259,7 +263,7 @@ public async ValueTask DisposeAsync()
{
disposables.Add(AsyncDisposable.Create(async () =>
{
await _database.DropCollectionAsync(collectionName);
await Database.DropCollectionAsync(collectionName);
}));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task CreateTempMongoDbCollection_OnNonExistingCollection_SucceedsBy
await using MongoDbTestContext context = await GivenCosmosMongoDbAsync();

string collectionName = context.WhenCollectionNameUnavailable();
TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName);
TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName, context);

await context.ShouldStoreCollectionAsync(collectionName);

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

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

TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName, options =>
TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName, context, options =>
{
options.OnSetup.CleanAllDocuments();
});
Expand Down Expand Up @@ -105,7 +105,7 @@ public async Task CreateTempMongoDbCollectionWithCleanMatchingOnSetup_OnExisting
BsonValue unmatchedId = await context.WhenDocumentAvailableAsync(collectionName, unmatched);

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

string collectionName = await context.WhenCollectionNameAvailableAsync();
TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName, options =>
TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName, context, options =>
{
options.OnTeardown.CleanAllDocuments();
});
Expand All @@ -151,7 +151,7 @@ public async Task CreateTempMongoDbCollectionWithCleanMatchingOnTeardown_OnExist

Shipment matched = CreateShipment();
Shipment unmatched = CreateShipment();
TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName, options =>
TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName, context, options =>
{
options.OnTeardown.CleanMatchingDocuments((Shipment s) => s.BoatName != unmatched.BoatName)
.CleanMatchingDocuments((Shipment s) => s.BoatName == matched.BoatName);
Expand Down Expand Up @@ -182,7 +182,7 @@ public async Task CreateTempMongoDbCollectionWithSetupTeardown_OnExistingCollect
BsonValue matchedOnSetupId = await context.WhenDocumentAvailableAsync(collectionName, matchedOnSetup);
BsonValue unmatchedOnSetupId = await context.WhenDocumentAvailableAsync(collectionName, unmatchedOnSetup);

TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName, options =>
TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName, context, options =>
{
options.OnSetup.CleanMatchingDocuments((Shipment s) => s.BoatName == matchedOnSetup.BoatName);
});
Expand Down Expand Up @@ -225,7 +225,7 @@ public async Task CreateTempMongoDbCollection_WhenCollectionWasDeletedOutsideFix
await using MongoDbTestContext context = await GivenCosmosMongoDbAsync();

string collectionName = context.WhenCollectionNameUnavailable();
TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName);
TemporaryMongoDbCollection collection = await WhenTempCollectionCreatedAsync(collectionName, context);
await context.WhenCollectionDeletedAsync(collectionName);

// Act
Expand All @@ -235,13 +235,17 @@ public async Task CreateTempMongoDbCollection_WhenCollectionWasDeletedOutsideFix
await context.ShouldNotStoreCollectionAsync(collectionName);
}

private async Task<TemporaryMongoDbCollection> WhenTempCollectionCreatedAsync(string collectionName, Action<TemporaryMongoDbCollectionOptions> configureOptions = null)
private async Task<TemporaryMongoDbCollection> WhenTempCollectionCreatedAsync(string collectionName, MongoDbTestContext context, Action<TemporaryMongoDbCollectionOptions> configureOptions = null)
{
return await MongoDbTestContext.WhenMongoDbAvailableAsync(async () =>
{
var collection = configureOptions is null
? await TemporaryMongoDbCollection.CreateIfNotExistsAsync(MongoDb.AccountResourceId, MongoDb.DatabaseName, collectionName, Logger)
: await TemporaryMongoDbCollection.CreateIfNotExistsAsync(MongoDb.AccountResourceId, MongoDb.DatabaseName, collectionName, Logger, configureOptions);
var collection = (Bogus.Random.Bool(), configureOptions is null) switch
{
(false, false) => await TemporaryMongoDbCollection.CreateIfNotExistsAsync(MongoDb.AccountResourceId, MongoDb.DatabaseName, collectionName, Logger, configureOptions),
(false, true) => await TemporaryMongoDbCollection.CreateIfNotExistsAsync(MongoDb.AccountResourceId, MongoDb.DatabaseName, collectionName, Logger),
(true, false) => await TemporaryMongoDbCollection.CreateIfNotExistsAsync(context.Database, collectionName, Logger, configureOptions),
(true, true) => await TemporaryMongoDbCollection.CreateIfNotExistsAsync(context.Database, collectionName, Logger)
};

Assert.Equal(collectionName, collection.Name);
return collection;
Expand Down
Loading