Implement rich comparison for Timestamps, PYTHON-609.
This commit is contained in:
parent
3ffeced08e
commit
489f941d44
@ -86,6 +86,26 @@ class Timestamp(object):
|
||||
def __ne__(self, other):
|
||||
return not self == other
|
||||
|
||||
def __lt__(self, other):
|
||||
if isinstance(other, Timestamp):
|
||||
return (self.time, self.inc) < (other.time, other.inc)
|
||||
return NotImplemented
|
||||
|
||||
def __le__(self, other):
|
||||
if isinstance(other, Timestamp):
|
||||
return (self.time, self.inc) <= (other.time, other.inc)
|
||||
return NotImplemented
|
||||
|
||||
def __gt__(self, other):
|
||||
if isinstance(other, Timestamp):
|
||||
return (self.time, self.inc) > (other.time, other.inc)
|
||||
return NotImplemented
|
||||
|
||||
def __ge__(self, other):
|
||||
if isinstance(other, Timestamp):
|
||||
return (self.time, self.inc) >= (other.time, other.inc)
|
||||
return NotImplemented
|
||||
|
||||
def __repr__(self):
|
||||
return "Timestamp(%s, %s)" % (self.__time, self.__inc)
|
||||
|
||||
|
||||
@ -641,6 +641,29 @@ class TestBSON(unittest.TestCase):
|
||||
self.assertTrue(MaxKey() != MinKey())
|
||||
self.assertFalse(MaxKey() == MinKey())
|
||||
|
||||
def test_timestamp_comparison(self):
|
||||
# Timestamp is initialized with time, inc. Time is the more
|
||||
# significant comparand.
|
||||
self.assertTrue(Timestamp(1, 0) < Timestamp(2, 17))
|
||||
self.assertTrue(Timestamp(2, 0) > Timestamp(1, 0))
|
||||
self.assertTrue(Timestamp(1, 7) <= Timestamp(2, 0))
|
||||
self.assertTrue(Timestamp(2, 0) >= Timestamp(1, 1))
|
||||
self.assertTrue(Timestamp(2, 0) <= Timestamp(2, 0))
|
||||
self.assertTrue(Timestamp(2, 0) >= Timestamp(2, 0))
|
||||
self.assertFalse(Timestamp(1, 0) > Timestamp(2, 0))
|
||||
|
||||
# Comparison by inc.
|
||||
self.assertTrue(Timestamp(1, 0) < Timestamp(1, 1))
|
||||
self.assertTrue(Timestamp(1, 1) > Timestamp(1, 0))
|
||||
self.assertTrue(Timestamp(1, 0) <= Timestamp(1, 0))
|
||||
self.assertTrue(Timestamp(1, 0) <= Timestamp(1, 1))
|
||||
self.assertFalse(Timestamp(1, 0) >= Timestamp(1, 1))
|
||||
self.assertTrue(Timestamp(1, 0) >= Timestamp(1, 0))
|
||||
self.assertTrue(Timestamp(1, 1) >= Timestamp(1, 0))
|
||||
self.assertFalse(Timestamp(1, 1) <= Timestamp(1, 0))
|
||||
self.assertTrue(Timestamp(1, 0) <= Timestamp(1, 0))
|
||||
self.assertFalse(Timestamp(1, 0) > Timestamp(1, 0))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user