From 4573107e21376ff83ef3123d52e61f7457dbe5f4 Mon Sep 17 00:00:00 2001
From: Stijn Moreels <9039753+stijnmoreels@users.noreply.github.com>
Date: Tue, 21 Jan 2025 07:42:36 +0100
Subject: [PATCH] =?UTF-8?q?chore:=20upgrade=20to=20mongodb=20v3=20(?=
=?UTF-8?q?=E2=9D=97)=20(#255)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* chore: upgrade to mongodb v3
* pr-fix: correct with new doc id serilization
* pr-fix: add space back
---
.../Arcus.Testing.Storage.Cosmos.csproj | 2 +-
.../TemporaryMongoDbCollection.cs | 12 ++++-----
.../Storage/Fixture/MongoDbTestContext.cs | 14 +++++------
.../Storage/TemporaryMongoDbDocumentTests.cs | 25 ++++++++++---------
4 files changed, 27 insertions(+), 26 deletions(-)
diff --git a/src/Arcus.Testing.Storage.Cosmos/Arcus.Testing.Storage.Cosmos.csproj b/src/Arcus.Testing.Storage.Cosmos/Arcus.Testing.Storage.Cosmos.csproj
index 5868a415..20c5ccb8 100644
--- a/src/Arcus.Testing.Storage.Cosmos/Arcus.Testing.Storage.Cosmos.csproj
+++ b/src/Arcus.Testing.Storage.Cosmos/Arcus.Testing.Storage.Cosmos.csproj
@@ -31,7 +31,7 @@
-
+
diff --git a/src/Arcus.Testing.Storage.Cosmos/TemporaryMongoDbCollection.cs b/src/Arcus.Testing.Storage.Cosmos/TemporaryMongoDbCollection.cs
index 55770126..af53195e 100644
--- a/src/Arcus.Testing.Storage.Cosmos/TemporaryMongoDbCollection.cs
+++ b/src/Arcus.Testing.Storage.Cosmos/TemporaryMongoDbCollection.cs
@@ -15,7 +15,7 @@ namespace Arcus.Testing
/// Represents the available options when the is created.
///
internal enum OnSetupMongoDbCollection { LeaveExisted = 0, CleanIfExisted, CleanIfMatched }
-
+
///
/// Represents the available options when the is deleted.
///
@@ -88,7 +88,7 @@ public OnSetupMongoDbCollectionOptions CleanMatchingDocuments(FilterD
IBsonSerializerRegistry serializerRegistry = BsonSerializer.SerializerRegistry;
IBsonSerializer documentSerializer = serializerRegistry.GetSerializer();
- BsonDocument doc = filter.Render(documentSerializer, serializerRegistry);
+ BsonDocument doc = filter.Render(new RenderArgs(documentSerializer, serializerRegistry));
IsMatched = Builders.Filter.Or(IsMatched, doc);
Documents = OnSetupMongoDbCollection.CleanIfMatched;
@@ -166,7 +166,7 @@ public OnTeardownMongoDbCollectionOptions CleanMatchingDocuments(Filt
IBsonSerializerRegistry serializerRegistry = BsonSerializer.SerializerRegistry;
IBsonSerializer documentSerializer = serializerRegistry.GetSerializer();
- BsonDocument doc = filter.Render(documentSerializer, serializerRegistry);
+ BsonDocument doc = filter.Render(new RenderArgs(documentSerializer, serializerRegistry));
IsMatched = Builders.Filter.Or(IsMatched, doc);
Documents = OnTeardownMongoDbCollection.CleanIfMatched;
@@ -217,7 +217,7 @@ private TemporaryMongoDbCollection(
_database = database;
_options = options;
_logger = logger ?? NullLogger.Instance;
-
+
Name = collectionName;
}
@@ -268,7 +268,7 @@ public static async Task CreateIfNotExistsAsync(
{
ArgumentNullException.ThrowIfNull(cosmosDbResourceId);
logger ??= NullLogger.Instance;
-
+
if (string.IsNullOrWhiteSpace(databaseName))
{
throw new ArgumentException(
@@ -409,7 +409,7 @@ public async ValueTask DisposeAsync()
disposables.Add(AsyncDisposable.Create(async () =>
{
_logger.LogDebug("[Test:Teardown] Delete Azure Cosmos MongoDb '{CollectionName}' collection in database '{DatabaseName}'", Name, _database.DatabaseNamespace.DatabaseName);
- await _database.DropCollectionAsync(Name);
+ await _database.DropCollectionAsync(Name);
}));
}
else
diff --git a/src/Arcus.Testing.Tests.Integration/Storage/Fixture/MongoDbTestContext.cs b/src/Arcus.Testing.Tests.Integration/Storage/Fixture/MongoDbTestContext.cs
index cca05b13..00a0cb21 100644
--- a/src/Arcus.Testing.Tests.Integration/Storage/Fixture/MongoDbTestContext.cs
+++ b/src/Arcus.Testing.Tests.Integration/Storage/Fixture/MongoDbTestContext.cs
@@ -67,14 +67,14 @@ private static async Task AuthenticateMongoDbClientAsync(TestConfig
string cosmosDbName = config["Arcus:Cosmos:MongoDb:Name"];
var listConnectionStringUrl = $"https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DocumentDB/databaseAccounts/{cosmosDbName}/listConnectionStrings?api-version=2021-04-15";
-
+
using var request = new HttpRequestMessage(HttpMethod.Post, listConnectionStringUrl);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.Token);
using HttpResponseMessage response = await HttpClient.SendAsync(request);
string responseBody = await response.Content.ReadAsStringAsync();
var connectionStringDic = JsonSerializer.Deserialize>>>(responseBody);
-
+
Assert.NotNull(connectionStringDic);
List> connectionStrings = connectionStringDic["connectionStrings"];
Assert.NotEmpty(connectionStrings);
@@ -127,9 +127,9 @@ public async Task WhenCollectionDeletedAsync(string collectionName)
public async Task WhenDocumentAvailableAsync(string collectionName, T document)
{
IMongoCollection collection = _database.GetCollection(collectionName);
-
+
var bson = document.ToBsonDocument();
-
+
BsonClassMap classMap = BsonClassMap.LookupClassMap(typeof(T));
string elementName = classMap.IdMemberMap.ElementName;
@@ -149,7 +149,7 @@ public async Task WhenDocumentAvailableAsync(string collectionName
public async Task WhenDocumentDeletedAsync(string collectionName, BsonValue id)
{
_logger.LogTrace("[Test] delete MongoDb document '{DocId}' in collection '{CollectionName}' outside test fixture's scope", id, collectionName);
-
+
IMongoCollection collection = _database.GetCollection(collectionName);
FilterDefinition filter = CreateIdFilter(id);
@@ -162,7 +162,7 @@ public async Task WhenDocumentDeletedAsync(string collectionName, BsonValue i
public async Task ShouldStoreCollectionAsync(string collectionName)
{
Assert.True(
- await StoresCollectionNameAsync(collectionName),
+ await StoresCollectionNameAsync(collectionName),
$"temporary mongo db collection '{collectionName}' should be available");
}
@@ -172,7 +172,7 @@ await StoresCollectionNameAsync(collectionName),
public async Task ShouldNotStoreCollectionAsync(string collectionName)
{
Assert.False(
- await StoresCollectionNameAsync(collectionName),
+ await StoresCollectionNameAsync(collectionName),
$"temporary mongo db collection '{collectionName}' should not be available");
}
diff --git a/src/Arcus.Testing.Tests.Integration/Storage/TemporaryMongoDbDocumentTests.cs b/src/Arcus.Testing.Tests.Integration/Storage/TemporaryMongoDbDocumentTests.cs
index c8ddd9e1..8e411ace 100644
--- a/src/Arcus.Testing.Tests.Integration/Storage/TemporaryMongoDbDocumentTests.cs
+++ b/src/Arcus.Testing.Tests.Integration/Storage/TemporaryMongoDbDocumentTests.cs
@@ -4,6 +4,7 @@
using Bogus;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
+using MongoDB.Bson.Serialization.IdGenerators;
using Xunit;
using Xunit.Abstractions;
@@ -52,7 +53,7 @@ public async Task CreateTempMongoDbDocument_OnExistingDocumentId_SucceedsByRever
BsonValue id = await context.WhenDocumentAvailableAsync(collectionName, original);
Product replacement = CreateProduct();
- replacement.Id = (ObjectId) id;
+ replacement.Id = (ObjectId)id;
TemporaryMongoDbDocument temp = await WhenTempDocumentCreatedAsync(collectionName, replacement);
await context.ShouldStoreDocumentAsync(collectionName, id, stored => AssertProduct(replacement, stored));
@@ -64,7 +65,7 @@ public async Task CreateTempMongoDbDocument_OnExistingDocumentId_SucceedsByRever
await context.ShouldStoreDocumentAsync(collectionName, id, stored => AssertProduct(original, stored));
}
- private async Task WhenTempDocumentCreatedAsync(string collectionName, TDoc doc)
+ private async Task WhenTempDocumentCreatedAsync(string collectionName, TDoc doc)
where TDoc : class
{
return await TemporaryMongoDbDocument.InsertIfNotExistsAsync(
@@ -108,32 +109,32 @@ public async Task CreateTempMongoDbDocument_WithOtherThanObjectIdPropertyType_Su
await using MongoDbTestContext context = await GivenCosmosMongoDbAsync();
string collectionName = await context.WhenCollectionNameAvailableAsync();
- var original = DocWithIntId.Generate();
+ var original = DocWithStringId.Generate();
BsonValue id = await context.WhenDocumentAvailableAsync(collectionName, original);
- var replacement = DocWithIntId.Generate();
- replacement.Id = (Guid) id;
+ var replacement = DocWithStringId.Generate();
+ replacement.Id = (string)id;
TemporaryMongoDbDocument temp = await WhenTempDocumentCreatedAsync(collectionName, replacement);
- await context.ShouldStoreDocumentAsync(collectionName, id, stored => Assert.Equal(replacement.Name, stored.Name));
+ await context.ShouldStoreDocumentAsync(collectionName, id, stored => Assert.Equal(replacement.Name, stored.Name));
// Act
await temp.DisposeAsync();
// Assert
- await context.ShouldStoreDocumentAsync(collectionName, id, stored => Assert.Equal(original.Name, stored.Name));
+ await context.ShouldStoreDocumentAsync(collectionName, id, stored => Assert.Equal(original.Name, stored.Name));
}
- public class DocWithIntId
+ public class DocWithStringId
{
- [BsonId]
- public Guid Id { get; set; }
+ [BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
+ public string Id { get; set; }
public string Name { get; set; }
- public static DocWithIntId Generate()
+ public static DocWithStringId Generate()
{
- return new DocWithIntId { Name = Bogus.Random.Word() };
+ return new DocWithStringId { Name = Bogus.Random.Word() };
}
}