Skip to content

Commit f37ce85

Browse files
committed
feat(mongodb): Set Id and Verify storage
With MongoDB we must set the Id manually. A single item with a null Id can be successfully inserted, as null is a valid Id value. But trying in insert it a second time results in a duplicate key error.
1 parent 0b60dd5 commit f37ce85

File tree

6 files changed

+31
-7
lines changed

6 files changed

+31
-7
lines changed

.vscode/settings.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
22
"cSpell.words": [
3+
"Bson",
34
"cref",
45
"inheritdoc",
5-
"paramref"
6+
"paramref",
7+
"Xunit"
68
]
79
}

IntBasis.DocumentOriented.MongoDB.Tests/IntBasis.DocumentOriented.MongoDB.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12+
<PackageReference Include="FluentAssertions" Version="6.7.0" />
1213
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
1314
<PackageReference Include="xunit" Version="2.4.1" />
1415
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">

IntBasis.DocumentOriented.MongoDB.Tests/MongoDbDocumentStorageTest.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using MongoDB.Driver;
2+
13
namespace IntBasis.DocumentOriented.MongoDB.Tests;
24

35
class Category : IDocumentEntity
@@ -19,6 +21,13 @@ public async Task Storage()
1921
var subject = new MongoDbDocumentStorage();
2022
var entity = new Category("test category");
2123
await subject.Store(entity);
22-
// entity.Id.Should().NotBeNull();
24+
entity.Id.Should().NotBeNull();
25+
26+
var mongoDatabase = MongoDbDocumentStorage.OpenTestDatabase();
27+
var collectionName = "entities";
28+
var collection = mongoDatabase.GetCollection<Category>(collectionName);
29+
var found = collection.Find<Category>(doc => doc.Id == entity.Id).FirstOrDefault();
30+
found.Id.Should().Be(entity.Id);
31+
found.Name.Should().Be("test category");
2332
}
2433
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
global using Xunit;
1+
global using Xunit;
2+
global using FluentAssertions;

IntBasis.DocumentOriented.MongoDB/IntBasis.DocumentOriented.MongoDB.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
<PackageReference Include="MongoDB.Driver" Version="2.16.1" />
1818
</ItemGroup>
1919

20+
<ItemGroup>
21+
<InternalsVisibleTo Include="$(AssemblyName).Tests" />
22+
</ItemGroup>
2023
</Project>

IntBasis.DocumentOriented.MongoDB/MongoDbDocumentStorage.cs

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using MongoDB.Driver;
1+
using MongoDB.Bson;
2+
using MongoDB.Driver;
23

34
namespace IntBasis.DocumentOriented.MongoDB;
45

@@ -12,13 +13,20 @@ public Task<T> Retrieve<T>(string id) where T : IDocumentEntity
1213

1314
/// <inheritdoc/>
1415
public async Task Store<T>(T entity) where T : IDocumentEntity
16+
{
17+
IMongoDatabase database = OpenTestDatabase();
18+
var collectionName = "entities";
19+
var collection = database.GetCollection<T>(collectionName);
20+
entity.Id = ObjectId.GenerateNewId().ToString();
21+
await collection.InsertOneAsync(entity);
22+
}
23+
24+
internal static IMongoDatabase OpenTestDatabase()
1525
{
1626
var connectionString = "mongodb://localhost:27017";
1727
var client = new MongoClient(connectionString);
1828
var databaseName = "test";
1929
var database = client.GetDatabase(databaseName);
20-
var collectionName = "entities";
21-
var collection = database.GetCollection<T>(collectionName);
22-
await collection.InsertOneAsync(entity);
30+
return database;
2331
}
2432
}

0 commit comments

Comments
 (0)