-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
[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); | ||
} | ||
} | ||
} |
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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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);