Skip to content

Commit beb8e7d

Browse files
author
Mike Dirolf
committed
test equality
1 parent 5c65676 commit beb8e7d

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

pymongo/binary.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,19 @@ def __new__(cls, data, subtype=2):
4545
raise TypeError("subtype must be an instance of int")
4646
if subtype >= 256 or subtype < 0:
4747
raise ValueError("subtype must be contained in [0, 256)")
48-
binary = str.__new__(cls, data)
49-
binary.__subtype = subtype
50-
return binary
48+
self = str.__new__(cls, data)
49+
self.__subtype = subtype
50+
return self
5151

5252
def subtype(self):
53+
"""Get the subtype of this binary data.
54+
"""
5355
return self.__subtype
5456

57+
def __eq__(self, other):
58+
if isinstance(other, Binary):
59+
return (self.__subtype, str(self)) == (other.__subtype, str(other))
60+
return NotImplemented
61+
5562
def __repr__(self):
5663
return "Binary(%s, %s)" % (str.__repr__(self), self.__subtype)

test/test_binary.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ def test_subtype(self):
5050
c = Binary("hello", 100)
5151
self.assertEqual(c.subtype(), 100)
5252

53+
def test_equality(self):
54+
b = Binary("hello")
55+
c = Binary("hello", 100)
56+
self.assertNotEqual(b, c)
57+
self.assertEqual(c, Binary("hello", 100))
58+
self.assertEqual(b, Binary("hello"))
59+
self.assertNotEqual(b, Binary("hello "))
60+
5361
def test_repr(self):
5462
b = Binary("hello world")
5563
self.assertEqual(repr(b), "Binary('hello world', 2)")

0 commit comments

Comments
 (0)