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 5 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()
{
// 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()
{
// 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()
{
// 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);
}
}
}
39 changes: 27 additions & 12 deletions tests/MongoDB.Bson.Tests/IO/BsonChunkPoolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;
using MongoDB.Bson.IO;
using MongoDB.TestHelpers.XunitExtensions;
Expand All @@ -39,7 +36,7 @@ public void ChunkSize_get_should_return_expected_result()
}

[Fact]
public void constructor_should_initialize_subject()
public void Constructor_should_initialize_subject()
{
var maxChunkCount = 1;
var chunkSize = 16;
Expand All @@ -54,21 +51,27 @@ public void constructor_should_initialize_subject()

[Theory]
[ParameterAttributeData]
public void constructor_should_throw_chunkSize_is_less_than_zero(
public void Constructor_should_throw_chunkSize_is_less_than_zero(
[Values(-1, 0)]
int chunkSize)
{
Action action = () => new BsonChunkPool(1, chunkSize);

action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("chunkSize");
var exception = Record.Exception(action);

exception.Should().BeOfType<ArgumentOutOfRangeException>();
((ArgumentOutOfRangeException)exception).ParamName.Should().Be("chunkSize");
}

[Fact]
public void constructor_should_throw_when_MaxChunkCount_is_less_than_zero()
public void Constructor_should_throw_when_MaxChunkCount_is_less_than_zero()
{
Action action = () => new BsonChunkPool(-1, 16);

action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("maxChunkCount");
var exception = Record.Exception(action);

exception.Should().BeOfType<ArgumentOutOfRangeException>();
((ArgumentOutOfRangeException)exception).ParamName.Should().Be("maxChunkCount");
}

[Fact]
Expand Down Expand Up @@ -103,7 +106,10 @@ public void Default_set_should_throw_when_value_is_null()
{
Action action = () => BsonChunkPool.Default = null;

action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("value");
var exception = Record.Exception(action);

exception.Should().BeOfType<ArgumentNullException>();
((ArgumentNullException)exception).ParamName.Should().Be("value");
}

[Fact]
Expand Down Expand Up @@ -163,7 +169,10 @@ public void GetChunk_should_throw_when_subject_is_disposed()

Action action = () => subject.GetChunk(1);

action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("BsonChunkPool");
var exception = Record.Exception(action);

exception.Should().BeOfType<ObjectDisposedException>();
((ObjectDisposedException)exception).ObjectName.Should().Be("BsonChunkPool");
}

[Fact]
Expand Down Expand Up @@ -221,7 +230,10 @@ public void Bytes_get_should_throw_when_subject_is_disposed()

Action action = () => { var _ = subject.Bytes; };

action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("DisposableChunk");
var exception = Record.Exception(action);

exception.Should().BeOfType<ObjectDisposedException>();
((ObjectDisposedException)exception).ObjectName.Should().Be("DisposableChunk");
}

[Fact]
Expand Down Expand Up @@ -314,7 +326,10 @@ public void Fork_should_throw_when_subject_is_disposed()

Action action = () => subject.Fork();

action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("DisposableChunk");
var exception = Record.Exception(action);

exception.Should().BeOfType<ObjectDisposedException>();
((ObjectDisposedException)exception).ObjectName.Should().Be("DisposableChunk");
}

// nested types
Expand Down
6 changes: 4 additions & 2 deletions tests/MongoDB.Bson.Tests/IO/ByteArrayBufferTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ public void Clear_should_throw_when_count_is_invalid(int position, int count)

Action action = () => subject.Clear(position, count);

action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("count");
var exception = Record.Exception(action).Should().BeOfType<ArgumentOutOfRangeException>().Subject;
exception.ParamName.Should().Be("count");
}

[Theory]
Expand Down Expand Up @@ -200,7 +201,8 @@ public void constructor_with_bytes_and_length_should_throw_when_bytes_is_null()
{
Action action = () => new ByteArrayBuffer(null, 0);

action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("bytes");
var exception = Record.Exception(action).Should().BeOfType<ArgumentNullException>().Subject;
exception.ParamName.Should().Be("bytes");
}

[Theory]
Expand Down
Loading