Skip to content

Commit

Permalink
task: updated tests for Bson Guid Serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
dtanglr committed Nov 17, 2023
1 parent cd4123c commit 5bef4a3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,26 @@ namespace Primitively.IntegrationTests.GuidTests.Default;

public class BsonDeserializerTests
{
[Fact]
public void Non_Nullable_Primitive_Deserialises_From_Bson_Correctly()
public static IEnumerable<object[]> GuidRepresentations()
{
yield return new object[] { GuidRepresentation.CSharpLegacy };
yield return new object[] { GuidRepresentation.JavaLegacy };
yield return new object[] { GuidRepresentation.PythonLegacy };
yield return new object[] { GuidRepresentation.Standard };
}

[Theory]
[MemberData(nameof(GuidRepresentations))]
public void Non_Nullable_Primitive_Deserialises_From_Bson_Correctly(GuidRepresentation representation)
{
// Assign
var example = DefaultThirtySixDigitsWithHyphens.Example;
var expected = (DefaultThirtySixDigitsWithHyphens)example;
var bsonReader = new Mock<IBsonReader>();
var context = BsonDeserializationContext.CreateRoot(bsonReader.Object);
var serializer = new GuidBsonSerializer<DefaultThirtySixDigitsWithHyphens>();
var bytes = GuidConverter.ToBytes(expected, GuidRepresentation.CSharpLegacy);
var subType = GuidConverter.GetSubType(GuidRepresentation.CSharpLegacy);
var serializer = new GuidBsonSerializer<DefaultThirtySixDigitsWithHyphens>(representation);
var bytes = GuidConverter.ToBytes(expected, representation);
var subType = GuidConverter.GetSubType(representation);
var binaryData = new BsonBinaryData(bytes, subType);
bsonReader.Setup(r => r.GetCurrentBsonType()).Returns(BsonType.Binary);
bsonReader.Setup(r => r.ReadBinaryData()).Returns(binaryData);
Expand All @@ -34,17 +43,18 @@ public void Non_Nullable_Primitive_Deserialises_From_Bson_Correctly()
bsonReader.Verify(r => r.ReadBinaryData(), Times.Once);
}

[Fact]
public void Nullable_Primitive_Deserialises_From_Bson_Correctly()
[Theory]
[MemberData(nameof(GuidRepresentations))]
public void Nullable_Primitive_Deserialises_From_Bson_Correctly(GuidRepresentation representation)
{
// Assign
var example = DefaultThirtySixDigitsWithHyphens.Example;
var expected = (DefaultThirtySixDigitsWithHyphens)example;
var bsonReader = new Mock<IBsonReader>();
var context = BsonDeserializationContext.CreateRoot(bsonReader.Object);
var serializer = NullableSerializer.Create(new GuidBsonSerializer<DefaultThirtySixDigitsWithHyphens>());
var bytes = GuidConverter.ToBytes(expected, GuidRepresentation.CSharpLegacy);
var subType = GuidConverter.GetSubType(GuidRepresentation.CSharpLegacy);
var serializer = NullableSerializer.Create(new GuidBsonSerializer<DefaultThirtySixDigitsWithHyphens>(representation));
var bytes = GuidConverter.ToBytes(expected, representation);
var subType = GuidConverter.GetSubType(representation);
var binaryData = new BsonBinaryData(bytes, subType);
bsonReader.Setup(r => r.GetCurrentBsonType()).Returns(BsonType.Binary);
bsonReader.Setup(r => r.ReadBinaryData()).Returns(binaryData);
Expand All @@ -57,14 +67,15 @@ public void Nullable_Primitive_Deserialises_From_Bson_Correctly()
bsonReader.Verify(r => r.ReadBinaryData(), Times.Once);
}

[Fact]
public void Nullable_Primitive_Deserialises_From_Bson_Correctly_When_Bson_Null()
[Theory]
[MemberData(nameof(GuidRepresentations))]
public void Nullable_Primitive_Deserialises_From_Bson_Correctly_When_Bson_Null(GuidRepresentation representation)
{
// Assign
var expected = (DefaultThirtySixDigitsWithHyphens?)null;
var bsonReader = new Mock<IBsonReader>();
var context = BsonDeserializationContext.CreateRoot(bsonReader.Object);
var serializer = NullableSerializer.Create(new GuidBsonSerializer<DefaultThirtySixDigitsWithHyphens>());
var serializer = NullableSerializer.Create(new GuidBsonSerializer<DefaultThirtySixDigitsWithHyphens>(representation));
bsonReader.Setup(r => r.GetCurrentBsonType()).Returns(BsonType.Null);

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,24 @@ namespace Primitively.IntegrationTests.GuidTests.Default;

public class BsonSerializerTests
{
[Fact]
public void Non_Nullable_Primitive_Serialises_To_Bson_Correctly()
public static IEnumerable<object[]> GuidRepresentations()
{
yield return new object[] { GuidRepresentation.CSharpLegacy };
yield return new object[] { GuidRepresentation.JavaLegacy };
yield return new object[] { GuidRepresentation.PythonLegacy };
yield return new object[] { GuidRepresentation.Standard };
}

[Theory]
[MemberData(nameof(GuidRepresentations))]
public void Non_Nullable_Primitive_Serialises_To_Bson_Correctly(GuidRepresentation representation)
{
// Assign
var example = DefaultThirtySixDigitsWithHyphens.Example;
var expected = (DefaultThirtySixDigitsWithHyphens)example;
var bsonWriter = new Mock<IBsonWriter>();
var context = BsonSerializationContext.CreateRoot(bsonWriter.Object);
var serializer = new GuidBsonSerializer<DefaultThirtySixDigitsWithHyphens>();
var serializer = new GuidBsonSerializer<DefaultThirtySixDigitsWithHyphens>(representation);
bsonWriter.Setup(r => r.WriteBinaryData(It.IsAny<BsonBinaryData>()));

// Act
Expand All @@ -28,15 +37,16 @@ public void Non_Nullable_Primitive_Serialises_To_Bson_Correctly()
bsonWriter.Verify(r => r.WriteBinaryData(It.IsAny<BsonBinaryData>()), Times.Once);
}

[Fact]
public void Nullable_Primitive_Serialises_To_Bson_Correctly()
[Theory]
[MemberData(nameof(GuidRepresentations))]
public void Nullable_Primitive_Serialises_To_Bson_Correctly(GuidRepresentation representation)
{
// Assign
var example = DefaultThirtySixDigitsWithHyphens.Example;
var expected = (DefaultThirtySixDigitsWithHyphens)example;
var bsonWriter = new Mock<IBsonWriter>();
var context = BsonSerializationContext.CreateRoot(bsonWriter.Object);
var serializer = NullableSerializer.Create(new GuidBsonSerializer<DefaultThirtySixDigitsWithHyphens>());
var serializer = NullableSerializer.Create(new GuidBsonSerializer<DefaultThirtySixDigitsWithHyphens>(representation));
bsonWriter.Setup(r => r.WriteBinaryData(It.IsAny<BsonBinaryData>()));

// Act
Expand All @@ -46,14 +56,15 @@ public void Nullable_Primitive_Serialises_To_Bson_Correctly()
bsonWriter.Verify(r => r.WriteBinaryData(It.IsAny<BsonBinaryData>()), Times.Once);
}

[Fact]
public void Nullable_Primitive_Serialises_To_Bson_Correctly_When_Null()
[Theory]
[MemberData(nameof(GuidRepresentations))]
public void Nullable_Primitive_Serialises_To_Bson_Correctly_When_Null(GuidRepresentation representation)
{
// Assign
var expected = (DefaultThirtySixDigitsWithHyphens?)null;
var bsonWriter = new Mock<IBsonWriter>();
var context = BsonSerializationContext.CreateRoot(bsonWriter.Object);
var serializer = NullableSerializer.Create(new GuidBsonSerializer<DefaultThirtySixDigitsWithHyphens>());
var serializer = NullableSerializer.Create(new GuidBsonSerializer<DefaultThirtySixDigitsWithHyphens>(representation));
bsonWriter.Setup(r => r.WriteNull());

// Act
Expand Down

0 comments on commit 5bef4a3

Please sign in to comment.