Skip to content

Commit dc02987

Browse files
committed
CSHARP-1716: Remove Equals from AggregateBucketResult.
1 parent d781a7b commit dc02987

File tree

5 files changed

+65
-26
lines changed

5 files changed

+65
-26
lines changed

src/MongoDB.Driver/AggregateBucketResult.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,5 @@ public AggregateBucketResult(TValue id, long count)
5858
/// </value>
5959
[BsonElement("count")]
6060
public long Count { get; private set; }
61-
62-
// public methods
63-
/// <inheritdoc/>
64-
public override bool Equals(object obj)
65-
{
66-
var other = obj as AggregateBucketResult<TValue>;
67-
if (object.ReferenceEquals(other, null))
68-
{
69-
return false;
70-
}
71-
72-
return
73-
Id.Equals(other.Id) &&
74-
Count == other.Count;
75-
}
76-
77-
/// <inheritdoc/>
78-
public override int GetHashCode()
79-
{
80-
return 0;
81-
}
8261
}
8362
}

tests/MongoDB.Bson.TestHelpers/CustomEquatable.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,21 @@ public CustomEquatable(T value, IEqualityComparer<T> comparer)
3333

3434
public override bool Equals(object obj)
3535
{
36-
var other = obj as CustomEquatable<T>;
37-
if (object.ReferenceEquals(other, null))
36+
T other;
37+
if (obj is T)
38+
{
39+
other = (T)obj;
40+
}
41+
else if (obj is CustomEquatable<T>)
42+
{
43+
other = ((CustomEquatable<T>)obj)._value;
44+
}
45+
else
3846
{
3947
return false;
4048
}
4149

42-
return _comparer.Equals(_value, other._value);
50+
return _comparer.Equals(_value, other);
4351
}
4452

4553
public override int GetHashCode()
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* Copyright 2016 MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System.Collections.Generic;
17+
18+
namespace MongoDB.Driver.Tests
19+
{
20+
public class AggregateBucketResultEqualityComparer<TValue> : IEqualityComparer<AggregateBucketResult<TValue>>
21+
{
22+
private static readonly AggregateBucketResultEqualityComparer<TValue> __instance = new AggregateBucketResultEqualityComparer<TValue>();
23+
24+
public static AggregateBucketResultEqualityComparer<TValue> Instance => __instance;
25+
26+
public bool Equals(AggregateBucketResult<TValue> x, AggregateBucketResult<TValue> y)
27+
{
28+
if (object.ReferenceEquals(x, y))
29+
{
30+
return true;
31+
}
32+
33+
if (object.ReferenceEquals(x, null) || object.ReferenceEquals(y, null))
34+
{
35+
return false;
36+
}
37+
38+
return
39+
x.Id.Equals(y.Id) &&
40+
x.Count == y.Count;
41+
}
42+
43+
public int GetHashCode(AggregateBucketResult<TValue> obj)
44+
{
45+
return 0;
46+
}
47+
}
48+
}

tests/MongoDB.Driver.Tests/AggregateFluentTests.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using MongoDB.Bson.Serialization;
2323
using MongoDB.Bson.Serialization.Attributes;
2424
using MongoDB.Bson.Serialization.Serializers;
25+
using MongoDB.Bson.TestHelpers;
2526
using MongoDB.Bson.TestHelpers.XunitExtensions;
2627
using MongoDB.Driver.Core.Misc;
2728
using MongoDB.Driver.Core.TestHelpers.XunitExtensions;
@@ -485,7 +486,8 @@ public void Bucket_should_return_expected_result()
485486
new AggregateBucketResult<BsonValue>(1920, 2),
486487
new AggregateBucketResult<BsonValue>("Unknown", 1),
487488
};
488-
buckets.Should().Equal(expectedBuckets);
489+
var comparer = AggregateBucketResultEqualityComparer<BsonValue>.Instance;
490+
buckets.WithComparer(comparer).Should().Equal(expectedBuckets);
489491
}
490492

491493
[Fact]
@@ -567,7 +569,8 @@ public void Bucket_typed_should_return_expected_result()
567569
new AggregateBucketResult<BsonValue>(1920, 2),
568570
new AggregateBucketResult<BsonValue>("Unknown", 1),
569571
};
570-
buckets.Should().Equal(expectedBuckets);
572+
var comparer = AggregateBucketResultEqualityComparer<BsonValue>.Instance;
573+
buckets.WithComparer(comparer).Should().Equal(expectedBuckets);
571574
}
572575

573576
[Fact]

tests/MongoDB.Driver.Tests/MongoDB.Driver.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
</Reference>
9898
</ItemGroup>
9999
<ItemGroup>
100+
<Compile Include="AggregateBucketResultEqualityComparer.cs" />
100101
<Compile Include="AggregateFluentTests.cs" />
101102
<Compile Include="AsyncCursorHelperTests.cs" />
102103
<Compile Include="BulkWriteErrorTests.cs" />

0 commit comments

Comments
 (0)