Skip to content

Commit 77cd3b4

Browse files
authored
Extended ObjectSerializer uses default GuidSerializer GuidRepresentation (#100)
1 parent 69e71db commit 77cd3b4

12 files changed

+193
-10
lines changed

src/Context.AllowedTypes.Tests/Helpers/TestHelpers.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Collections.Generic;
22
using System.Linq;
3-
using MongoDB.Extensions.Context.Internal;
43

54
namespace MongoDB.Extensions.Context.AllowedTypes.Tests.Helpers;
65

src/Context.AllowedTypes.Tests/MongoDatabaseBuilderTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using MongoDB.Bson.Serialization;
44
using MongoDB.Driver;
55
using MongoDB.Extensions.Context.AllowedTypes.Tests.Helpers;
6-
using MongoDB.Extensions.Context.Internal;
76
using Snapshooter.Xunit;
87
using Squadron;
98
using Xunit;

src/Context.AllowedTypes.Tests/TypeObjectSerializerTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using MongoDB.Extensions.Context.AllowedTypes.Tests.Helpers;
2-
using MongoDB.Extensions.Context.Internal;
32
using Snapshooter.Xunit;
43
using Xunit;
54

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<Import Project="$(CCTestProjectProps)" Condition="Exists('$(CCTestProjectProps)')" />
3+
4+
<PropertyGroup>
5+
<AssemblyName>MongoDB.Extensions.Context.GuidSerializers.Tests</AssemblyName>
6+
<RootNamespace>MongoDB.Extensions.Context.GuidSerializers.Tests</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\Context\Context.csproj" />
11+
<ProjectReference Include="..\Prime.Extensions\Prime.Extensions.csproj" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="Xunit.Priority" />
16+
</ItemGroup>
17+
18+
</Project>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using MongoDB.Driver;
2+
using Squadron;
3+
using Xunit;
4+
using System;
5+
using System.Threading.Tasks;
6+
using Snapshooter.Xunit;
7+
using MongoDB.Prime.Extensions;
8+
9+
namespace MongoDB.Extensions.Context.GuidSerializers.Tests;
10+
11+
public class GuidSerializerTests : IClassFixture<MongoResource>
12+
{
13+
private readonly MongoOptions _mongoOptions;
14+
private readonly IMongoDatabase _mongoDatabase;
15+
16+
public GuidSerializerTests(MongoResource mongoResource)
17+
{
18+
_mongoDatabase = mongoResource.CreateDatabase();
19+
_mongoOptions = new MongoOptions
20+
{
21+
ConnectionString = mongoResource.ConnectionString,
22+
DatabaseName = _mongoDatabase.DatabaseNamespace.DatabaseName
23+
};
24+
}
25+
26+
[Fact]
27+
public async Task Serialize_GuidPropertyGuidSerialized_Successfully()
28+
{
29+
// Arrange
30+
var foobarMongoDbContext = new FooBarMongoDbContext(_mongoOptions);
31+
32+
IMongoCollection<Foo> collection =
33+
_mongoDatabase.GetCollection<Foo>("foos");
34+
35+
Foo foo = new Foo
36+
(
37+
fooId: Guid.Parse("b1eba0d6-a1f9-4e31-bd70-0feed19f4492"),
38+
name: "test",
39+
additionalId: Guid.Parse("b58ec857-c874-457e-8662-133a055282f6")
40+
);
41+
42+
// Act
43+
await collection.InsertOneAsync(foo);
44+
45+
// Assert
46+
Snapshot.Match(collection.Dump());
47+
}
48+
49+
[Fact]
50+
public async Task Serialize_ObjectPropertyGuidSerialized_Successfully()
51+
{
52+
// Arrange
53+
var foobarMongoDbContext = new FooBarMongoDbContext(_mongoOptions);
54+
55+
IMongoCollection<Bar> collection =
56+
_mongoDatabase.GetCollection<Bar>("bars");
57+
58+
Bar bar = new Bar
59+
(
60+
fooId: Guid.Parse("b1eba0d6-a1f9-4e31-bd70-0feed19f4492"),
61+
name: "test",
62+
additionalId: Guid.Parse("b58ec857-c874-457e-8662-133a055282f6")
63+
);
64+
65+
// Act
66+
await collection.InsertOneAsync(bar);
67+
68+
// Assert
69+
Snapshot.Match(collection.Dump());
70+
}
71+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
3+
namespace MongoDB.Extensions.Context.GuidSerializers.Tests;
4+
5+
public class Bar
6+
{
7+
public Bar(Guid fooId, string name, Guid additionalId)
8+
{
9+
Id = fooId;
10+
Name = name;
11+
AdditionalId = additionalId;
12+
}
13+
14+
public Guid Id { get; private set; }
15+
16+
public string Name { get; private set;}
17+
18+
public object AdditionalId { get; private set;}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
3+
namespace MongoDB.Extensions.Context.GuidSerializers.Tests;
4+
5+
public class Foo
6+
{
7+
public Foo(Guid fooId, string name, Guid additionalId)
8+
{
9+
Id = fooId;
10+
Name = name;
11+
AdditionalId = additionalId;
12+
}
13+
14+
public Guid Id { get; private set; }
15+
16+
public string Name { get; private set;}
17+
18+
public Guid AdditionalId { get; private set;}
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using MongoDB.Bson;
2+
using MongoDB.Bson.Serialization.Serializers;
3+
4+
namespace MongoDB.Extensions.Context.GuidSerializers.Tests;
5+
6+
public class FooBarMongoDbContext : MongoDbContext
7+
{
8+
public FooBarMongoDbContext(MongoOptions mongoOptions)
9+
: base(mongoOptions)
10+
{
11+
}
12+
13+
protected override void OnConfiguring(
14+
IMongoDatabaseBuilder databaseBuilder)
15+
{
16+
databaseBuilder.RegisterSerializer(new GuidSerializer(GuidRepresentation.CSharpLegacy));
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"Id": "b1eba0d6-a1f9-4e31-bd70-0feed19f4492",
4+
"Name": "test",
5+
"AdditionalId": "b58ec857-c874-457e-8662-133a055282f6"
6+
}
7+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"Id": "b1eba0d6-a1f9-4e31-bd70-0feed19f4492",
4+
"Name": "test",
5+
"AdditionalId": "b58ec857-c874-457e-8662-133a055282f6"
6+
}
7+
]

0 commit comments

Comments
 (0)