Skip to content

Commit

Permalink
CSHARP-3315: Add tests for serializer Equals methods (work in progress).
Browse files Browse the repository at this point in the history
  • Loading branch information
rstam committed Mar 13, 2024
1 parent 63cbb0e commit fa7b421
Show file tree
Hide file tree
Showing 35 changed files with 5,712 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public override bool Equals(object obj)
if (object.ReferenceEquals(this, obj)) { return true; }
return
GetType().Equals(obj.GetType()) &&
obj is ScalarDiscriminatorConvention other &&
obj is StandardDiscriminatorConvention other &&
object.Equals(_elementName, other._elementName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,10 @@ public override DateTime Deserialize(BsonDeserializationContext context, BsonDes
/// <inheritdoc/>
public override bool Equals(object obj)
{
if (object.ReferenceEquals(obj, null)) { return false; }
if (object.ReferenceEquals(this, obj)) { return true; }
return
base.Equals(obj) &&
obj is DateTimeSerializer other &&
_dateOnly.Equals(other._dateOnly) &&
_kind.Equals(other._kind) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ public override bool Equals(object obj)
base.Equals(obj) &&
obj is DiscriminatedInterfaceSerializer<TInterface> other &&
object.Equals(_discriminatorConvention, other._discriminatorConvention) &&
object.Equals(_interfaceSerializer, other._interfaceSerializer) &&
object.Equals(_interfaceType, other._interfaceType) &&
object.Equals(_objectSerializer, other._objectSerializer);
object.Equals(_interfaceSerializer, other._interfaceSerializer);
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public override bool Equals(object obj)
if (object.ReferenceEquals(obj, null)) { return false; }
if (object.ReferenceEquals(this, obj)) { return true; }
return
base.Equals(obj) &&
GetType().Equals(obj.GetType()) &&
obj is ElementAppendingSerializer<TDocument> other &&
object.Equals(_documentSerializer, other._documentSerializer) &&
SequenceComparer.Equals(_elements, other._elements) &&
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* 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.ObjectModel
{
public class MaterializedOnDemandBsonArraySerializerTests
{
[Fact]
public void Equals_derived_should_return_false()
{
var x = new MaterializedOnDemandBsonArray.MaterializedOnDemandBsonArraySerializer();
var y = new DerivedFromMaterializedOnDemandBsonArraySerializer();

var result = x.Equals(y);

result.Should().Be(false);
}

[Fact]
public void Equals_null_should_return_false()
{
var x = new MaterializedOnDemandBsonArray.MaterializedOnDemandBsonArraySerializer();

var result = x.Equals(null);

result.Should().Be(false);
}

[Fact]
public void Equals_object_should_return_false()
{
var x = new MaterializedOnDemandBsonArray.MaterializedOnDemandBsonArraySerializer();
var y = new object();

var result = x.Equals(y);

result.Should().Be(false);
}

[Fact]
public void Equals_self_should_return_true()
{
var x = new MaterializedOnDemandBsonArray.MaterializedOnDemandBsonArraySerializer();

var result = x.Equals(x);

result.Should().Be(true);
}

[Fact]
public void Equals_with_equal_fields_should_return_true()
{
var x = new MaterializedOnDemandBsonArray.MaterializedOnDemandBsonArraySerializer();
var y = new MaterializedOnDemandBsonArray.MaterializedOnDemandBsonArraySerializer();

var result = x.Equals(y);

result.Should().Be(true);
}

[Fact]
public void GetHashCode_should_return_zero()
{
var x = new MaterializedOnDemandBsonArray.MaterializedOnDemandBsonArraySerializer();

var result = x.GetHashCode();

result.Should().Be(0);
}

private class DerivedFromMaterializedOnDemandBsonArraySerializer : MaterializedOnDemandBsonArray.MaterializedOnDemandBsonArraySerializer
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* 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.ObjectModel
{
public class MaterializedOnDemandBsonDocumentSerializerTests
{
[Fact]
public void Equals_derived_should_return_false()
{
var x = new MaterializedOnDemandBsonDocumentSerializer();
var y = new DerivedFromMaterializedOnDemandBsonDocumentSerializer();

var result = x.Equals(y);

result.Should().Be(false);
}

[Fact]
public void Equals_null_should_return_false()
{
var x = new MaterializedOnDemandBsonDocumentSerializer();

var result = x.Equals(null);

result.Should().Be(false);
}

[Fact]
public void Equals_object_should_return_false()
{
var x = new MaterializedOnDemandBsonDocumentSerializer();
var y = new object();

var result = x.Equals(y);

result.Should().Be(false);
}

[Fact]
public void Equals_self_should_return_true()
{
var x = new MaterializedOnDemandBsonDocumentSerializer();

var result = x.Equals(x);

result.Should().Be(true);
}

[Fact]
public void Equals_with_equal_fields_should_return_true()
{
var x = new MaterializedOnDemandBsonDocumentSerializer();
var y = new MaterializedOnDemandBsonDocumentSerializer();

var result = x.Equals(y);

result.Should().Be(true);
}

[Fact]
public void GetHashCode_should_return_zero()
{
var x = new MaterializedOnDemandBsonDocumentSerializer();

var result = x.GetHashCode();

result.Should().Be(0);
}

private class DerivedFromMaterializedOnDemandBsonDocumentSerializer : MaterializedOnDemandBsonDocumentSerializer
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,28 @@
using FluentAssertions;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
using Xunit;

namespace MongoDB.Bson.Tests.Serialization
{
public class BsonClassMapSerializerTests
{
private static readonly BsonClassMap __classMap1;
private static readonly BsonClassMap __classMap2;

static BsonClassMapSerializerTests()
{
__classMap1 = new BsonClassMap(typeof(MyModel));
__classMap1.AutoMap();
__classMap1.Freeze();

__classMap2 = new BsonClassMap(typeof(MyModel));
__classMap2.AutoMap();
__classMap2.MapProperty("Id").SetSerializer(new StringSerializer(BsonType.ObjectId));
__classMap2.Freeze();
}

// public methods
[Fact]
public void Deserialize_should_throw_invalidOperationException_when_creator_returns_null()
Expand All @@ -39,6 +55,88 @@ public void Deserialize_should_throw_invalidOperationException_when_creator_retu
exception.Should().BeOfType<BsonSerializationException>();
}

[Fact]
public void Equals_derived_should_return_false()
{
var x = new BsonClassMapSerializer<MyModel>(__classMap1);
var y = new DerivedFromBsonClassMapSerializer<MyModel>(__classMap1);

var result = x.Equals(y);

result.Should().Be(false);
}

[Fact]
public void Equals_null_should_return_false()
{
var x = new BsonClassMapSerializer<MyModel>(__classMap1);

var result = x.Equals(null);

result.Should().Be(false);
}

[Fact]
public void Equals_object_should_return_false()
{
var x = new BsonClassMapSerializer<MyModel>(__classMap1);
var y = new object();

var result = x.Equals(y);

result.Should().Be(false);
}

[Fact]
public void Equals_self_should_return_true()
{
var x = new BsonClassMapSerializer<MyModel>(__classMap1);

var result = x.Equals(x);

result.Should().Be(true);
}

[Fact]
public void Equals_with_equal_fields_should_return_true()
{
var x = new BsonClassMapSerializer<MyModel>(__classMap1);
var y = new BsonClassMapSerializer<MyModel>(__classMap1);

var result = x.Equals(y);

result.Should().Be(true);
}

[Fact]
public void Equals_with_not_equal_field_should_return_false()
{
var x = new BsonClassMapSerializer<MyModel>(__classMap1);
var y = new BsonClassMapSerializer<MyModel>(__classMap2);

var result = x.Equals(y);

result.Should().Be(false);
}

[Fact]
public void GetHashCode_should_return_zero()
{
var x = new BsonClassMapSerializer<MyModel>(__classMap1);

var result = x.GetHashCode();

result.Should().Be(0);
}

private class DerivedFromBsonClassMapSerializer<TClass> : BsonClassMapSerializer<TClass>
{
public DerivedFromBsonClassMapSerializer(BsonClassMap classMap)
: base(classMap)
{
}
}

// nested classes
private class MyModel
{
Expand Down

0 comments on commit fa7b421

Please sign in to comment.