Skip to content

CSHARP-5589: Improve BSON project test coverage #1694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

BorisDog
Copy link
Contributor

No description provided.

@BorisDog BorisDog requested a review from a team as a code owner May 16, 2025 22:54
@BorisDog BorisDog requested review from papafe and removed request for a team May 16, 2025 22:54
@BorisDog BorisDog changed the title Csharp5589: Improve BSON project test coverage CSHARP5589: Improve BSON project test coverage May 16, 2025
@BorisDog BorisDog added the chore Label to hide PR from generated Release Notes label May 16, 2025
@BorisDog BorisDog changed the title CSHARP5589: Improve BSON project test coverage CSHARP-5589: Improve BSON project test coverage May 16, 2025
@BorisDog BorisDog requested review from sanych-sun and removed request for papafe May 16, 2025 23:04
public class BsonExceptionTests
{
[Fact]
public void constructor_with_format_and_args_should_format_message_correctly()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add test to check mismatching format string and arguments list?
Like:
var exception = new BsonException("Error code: {0}, message: {1}", 123);

}

[Fact]
public void constructor_with_format_and_args_should_handle_empty_args()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test method name is wrong, because it actually uses another constructor: the one accepting a single string as a parameter.


namespace MongoDB.Bson.Tests.Exceptions
{
public class BsonSerializationExceptionTests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BsonSerializationException class looks exactly like BsonInternalException. Should we make tests similar as well? Somehow BsonInternalExceptionTests has much more test cases.

public class BsonInternalExceptionTests
{
[Fact]
public void constructor_should_initialize_empty_instance()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose test method name should start with capital letter.

token.Type.Should().Be(JsonTokenType.DateTime);
}

[Fact]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like previous test validates token.Type as well. Either remove the check there or remove this entire test.

{
// We'll test a few representative BsonValue types

// Arrange - Int32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above (multiple arrange-act-assert section. Split into several tests?)

var subject = new DictionaryInterfaceImplementerSerializer<Dictionary<string, int>>(dictionaryRepresentation);

// Assert
subject.Should().NotBeNull();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and in other places: Does such check has any sense? Is there any way in C# to return null from the object constructor?


var bson = obj.ToBson();
var rehydrated = BsonSerializer.Deserialize<T>(bson);
Assert.IsType<Dictionary<object, object>>(rehydrated.D);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use fluent assertions?

var result = subject.Deserialize(context, args);

// Assert
result.Should().Be(expectedResult);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not compare results, we have to Verify if the method with proper arguments was called on the mock.

var mockArraySerializer = new Mock<IBsonSerializer<string>>();
mockArraySerializer.As<IBsonArraySerializer>()
.Setup(s => s.TryGetItemSerializationInfo(out It.Ref<BsonSerializationInfo>.IsAny))
.Returns((out BsonSerializationInfo info) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not really need any return value. Verifying if proper method was called should be enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chore Label to hide PR from generated Release Notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants