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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/MongoDB.Bson/Exceptions/BsonException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public BsonException(string format, params object[] args)
/// </summary>
/// <param name="info">The SerializationInfo.</param>
/// <param name="context">The StreamingContext.</param>
[Obsolete("Legacy serialization support APIs are obsolete: SYSLIB0051")]
public BsonException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
Expand Down
1 change: 1 addition & 0 deletions src/MongoDB.Bson/Exceptions/BsonInternalException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public BsonInternalException(string message, Exception innerException)
/// </summary>
/// <param name="info">The SerializationInfo.</param>
/// <param name="context">The StreamingContext.</param>
[Obsolete("Legacy serialization support APIs are obsolete: SYSLIB0051")]
public BsonInternalException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public BsonSerializationException(string message, Exception innerException)
/// </summary>
/// <param name="info">The SerializationInfo.</param>
/// <param name="context">The StreamingContext.</param>
[Obsolete("Legacy serialization support APIs are obsolete: SYSLIB0051")]
public BsonSerializationException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public DuplicateBsonMemberMapAttributeException(string message, Exception inner)
/// </summary>
/// <param name="info">The info.</param>
/// <param name="context">The context.</param>
[Obsolete("Legacy serialization support APIs are obsolete: SYSLIB0051")]
protected DuplicateBsonMemberMapAttributeException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
Expand Down
1 change: 1 addition & 0 deletions src/MongoDB.Bson/Exceptions/TruncationException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public TruncationException(string message, Exception innerException)
/// </summary>
/// <param name="info">The SerializationInfo.</param>
/// <param name="context">The StreamingContext.</param>
[Obsolete("Legacy serialization support APIs are obsolete: SYSLIB0051")]
public TruncationException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
Expand Down
21 changes: 1 addition & 20 deletions src/MongoDB.Bson/Serialization/BsonDeserializationArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/

using System;
using MongoDB.Bson.IO;

namespace MongoDB.Bson.Serialization
{
Expand All @@ -23,27 +22,9 @@ namespace MongoDB.Bson.Serialization
/// </summary>
public struct BsonDeserializationArgs
{
// private fields
private Type _nominalType;

// constructors
private BsonDeserializationArgs(
Type nominalType)
{
_nominalType = nominalType;
}

// public properties
/// <summary>
/// Gets or sets the nominal type.
/// </summary>
/// <value>
/// The nominal type.
/// </value>
public Type NominalType
{
get { return _nominalType; }
set { _nominalType = value; }
}
public Type NominalType { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/MongoDB.Bson/Serialization/BsonSerializationInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class BsonSerializationInfo
{
#region static
/// <summary>
/// Creates a new instance of the BsonSerializationinfo class with an element path instead of an element name.
/// Creates a new instance of the BsonSerializationInfo class with an element path instead of an element name.
/// </summary>
/// <param name="elementPath">The element path.</param>
/// <param name="serializer">The serializer.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public static class NullableSerializer
/// <returns>A NullableSerializer</returns>
public static IBsonSerializer Create(IBsonSerializer valueSerializer)
{
if (valueSerializer == null)
{
throw new ArgumentNullException(nameof(valueSerializer));
}

var valueType = valueSerializer.ValueType;
var nullableSerializerType = typeof(NullableSerializer<>).MakeGenericType(valueType);
return (IBsonSerializer)Activator.CreateInstance(nullableSerializerType, valueSerializer);
Expand Down
53 changes: 53 additions & 0 deletions tests/MongoDB.Bson.Tests/Exceptions/BsonExceptionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using FluentAssertions;
using Xunit;

namespace MongoDB.Bson.Tests.Exceptions
{
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);

{
// Act
var exception = new BsonException("Error code: {0}, message: {1}", 123, "Test error");

// Assert
exception.Message.Should().Be("Error code: 123, message: Test error");
}

[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.

{
// Act
var exception = new BsonException("Simple message");

// Assert
exception.Message.Should().Be("Simple message");
}

[Fact]
public void constructor_with_format_and_args_should_handle_null_args()
{
// Act
var exception = new BsonException("Message with {0}", (object)null);

// Assert
exception.Message.Should().Be("Message with ");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using FluentAssertions;
using Xunit;

namespace MongoDB.Bson.Tests.Exceptions
{
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.

{
// Act
var exception = new BsonInternalException();

// Assert
exception.Message.Should().Be("Exception of type 'MongoDB.Bson.BsonInternalException' was thrown.");
exception.InnerException.Should().BeNull();
}

[Fact]
public void constructor_should_initialize_instance_with_message()
{
// Arrange
var message = "Test internal exception message";

// Act
var exception = new BsonInternalException(message);

// Assert
exception.Message.Should().Be(message);
exception.InnerException.Should().BeNull();
}

[Fact]
public void constructor_should_initialize_instance_with_message_and_inner_exception()
{
// Arrange
var message = "Test internal exception message";
var innerException = new Exception("Inner exception message");

// Act
var exception = new BsonInternalException(message, innerException);

// Assert
exception.Message.Should().Be(message);
exception.InnerException.Should().BeSameAs(innerException);
}

[Fact]
public void should_inherit_from_bson_exception()
{
// Act
var exception = new BsonInternalException();

// Assert
exception.Should().BeAssignableTo<BsonException>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using FluentAssertions;
using Xunit;

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.

{
[Fact]
public void Constructor_with_message_and_inner_exception_should_initialize_properties()
{
// Arrange
var message = "Test error message";
var innerException = new ArgumentException("Inner exception message");

// Act
var exception = new BsonSerializationException(message, innerException);

// Assert
exception.Message.Should().Be(message);
exception.InnerException.Should().BeSameAs(innerException);
}
}
}
69 changes: 69 additions & 0 deletions tests/MongoDB.Bson.Tests/IO/DateTimeJsonTokenTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using FluentAssertions;
using MongoDB.Bson.IO;
using Xunit;

namespace MongoDB.Bson.Tests.IO
{
public class DateTimeJsonTokenTests
{
[Fact]
public void Constructor_should_initialize_token_with_provided_values()
{
// Arrange
var lexeme = "ISODate(\"2023-01-01T00:00:00Z\")";
var value = new BsonDateTime(new DateTime(2023, 1, 1, 0, 0, 0, DateTimeKind.Utc));

// Act
var token = new DateTimeJsonToken(lexeme, value);

// Assert
token.Lexeme.Should().Be(lexeme);
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.

public void Constructor_should_set_token_type_to_DateTime()
{
// Arrange
var lexeme = "ISODate(\"2023-01-01T00:00:00Z\")";
var value = new BsonDateTime(new DateTime(2023, 1, 1, 0, 0, 0, DateTimeKind.Utc));

// Act
var token = new DateTimeJsonToken(lexeme, value);

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

[Fact]
public void DateTimeValue_should_return_provided_value()
{
// Arrange
var lexeme = "ISODate(\"2023-01-01T00:00:00Z\")";
var value = new BsonDateTime(new DateTime(2023, 1, 1, 0, 0, 0, DateTimeKind.Utc));
var token = new DateTimeJsonToken(lexeme, value);

// Act
var result = token.DateTimeValue;

// Assert
result.Should().Be(value);
}
}
}
Loading