Skip to content

Commit 9dae3d0

Browse files
pcorpetprashantmital
authored andcommitted
Fix equality comparison of WriteConcern objects. (mongodb#378)
1 parent d571ac0 commit 9dae3d0

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

pymongo/write_concern.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,14 @@ def __repr__(self):
113113
", ".join("%s=%s" % kvt for kvt in self.__document.items()),))
114114

115115
def __eq__(self, other):
116-
return self.__document == other.document
116+
if isinstance(other, WriteConcern):
117+
return self.__document == other.document
118+
return NotImplemented
117119

118120
def __ne__(self, other):
119-
return self.__document != other.document
121+
if isinstance(other, WriteConcern):
122+
return self.__document != other.document
123+
return NotImplemented
120124

121125

122126
DEFAULT_WRITE_CONCERN = WriteConcern()

test/test_write_concern.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Copyright 2018-present 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+
"""Run the unit tests for WriteConcern."""
16+
17+
import collections
18+
import unittest
19+
20+
from pymongo.write_concern import WriteConcern
21+
22+
23+
class TestWriteConcern(unittest.TestCase):
24+
25+
def test_equality(self):
26+
concern = WriteConcern(j=True, wtimeout=3000)
27+
self.assertEqual(concern, WriteConcern(j=True, wtimeout=3000))
28+
self.assertNotEqual(concern, WriteConcern())
29+
30+
def test_equality_to_none(self):
31+
concern = WriteConcern()
32+
self.assertNotEqual(concern, None)
33+
# Explicitly use the != operator.
34+
self.assertTrue(concern != None) # noqa
35+
36+
def test_equality_compatible_type(self):
37+
38+
class _FakeWriteConcern(object):
39+
40+
def __init__(self, **document):
41+
self.document = document
42+
43+
def __eq__(self, other):
44+
try:
45+
return self.document == other.document
46+
except AttributeError:
47+
return NotImplemented
48+
49+
def __ne__(self, other):
50+
try:
51+
return self.document != other.document
52+
except AttributeError:
53+
return NotImplemented
54+
55+
self.assertEqual(WriteConcern(j=True), _FakeWriteConcern(j=True))
56+
self.assertEqual(_FakeWriteConcern(j=True), WriteConcern(j=True))
57+
self.assertEqual(WriteConcern(j=True), _FakeWriteConcern(j=True))
58+
self.assertEqual(WriteConcern(wtimeout=42), _FakeWriteConcern(wtimeout=42))
59+
self.assertNotEqual(WriteConcern(wtimeout=42), _FakeWriteConcern(wtimeout=2000))
60+
61+
def test_equality_incompatible_type(self):
62+
_fake_type = collections.namedtuple('NotAWriteConcern', ['document'])
63+
self.assertNotEqual(WriteConcern(j=True), _fake_type({'j': True}))
64+
65+
66+
if __name__ == '__main__':
67+
unittest.main()

0 commit comments

Comments
 (0)