diff --git a/bson/timestamp.py b/bson/timestamp.py index 6eb173fb4..195314780 100644 --- a/bson/timestamp.py +++ b/bson/timestamp.py @@ -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) diff --git a/test/test_bson.py b/test/test_bson.py index 151fe4496..cebd675e2 100644 --- a/test/test_bson.py +++ b/test/test_bson.py @@ -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()