From da8d29d02492fbb1ea39f67dcccc90175447282c Mon Sep 17 00:00:00 2001 From: weidongkl Date: Mon, 13 Nov 2023 13:54:20 +0800 Subject: [PATCH] fix: error in determining timestamp less than ``` __lt__ is the negation of __gt__ by default. so samples.Timestamp(1, 1) > samples.Timestamp(1, 1) is false and samples.Timestamp(1, 1) < samples.Timestamp(1, 1) is true. but samples.Timestamp(1, 1) < samples.Timestamp(1, 1) should be false too. add __lt__ func to fix the bug ``` Signed-off-by: weidongkl --- prometheus_client/samples.py | 3 +++ tests/test_samples.py | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 tests/test_samples.py diff --git a/prometheus_client/samples.py b/prometheus_client/samples.py index d3e351c6..8735fed9 100644 --- a/prometheus_client/samples.py +++ b/prometheus_client/samples.py @@ -30,6 +30,9 @@ def __ne__(self, other: object) -> bool: def __gt__(self, other: "Timestamp") -> bool: return self.sec > other.sec or self.nsec > other.nsec + def __lt__(self, other: "Timestamp") -> bool: + return self.sec < other.sec or self.nsec < other.nsec + # Timestamp and exemplar are optional. # Value can be an int or a float. diff --git a/tests/test_samples.py b/tests/test_samples.py new file mode 100644 index 00000000..796afe7e --- /dev/null +++ b/tests/test_samples.py @@ -0,0 +1,27 @@ +import unittest + +from prometheus_client import samples + + +class TestSamples(unittest.TestCase): + def test_gt(self): + self.assertEqual(samples.Timestamp(1, 1) > samples.Timestamp(1, 1), False) + self.assertEqual(samples.Timestamp(1, 1) > samples.Timestamp(1, 2), False) + self.assertEqual(samples.Timestamp(1, 1) > samples.Timestamp(2, 1), False) + self.assertEqual(samples.Timestamp(1, 1) > samples.Timestamp(2, 2), False) + self.assertEqual(samples.Timestamp(1, 2) > samples.Timestamp(1, 1), True) + self.assertEqual(samples.Timestamp(2, 1) > samples.Timestamp(1, 1), True) + self.assertEqual(samples.Timestamp(2, 2) > samples.Timestamp(1, 1), True) + + def test_lt(self): + self.assertEqual(samples.Timestamp(1, 1) < samples.Timestamp(1, 1), False) + self.assertEqual(samples.Timestamp(1, 1) < samples.Timestamp(1, 2), True) + self.assertEqual(samples.Timestamp(1, 1) < samples.Timestamp(2, 1), True) + self.assertEqual(samples.Timestamp(1, 1) < samples.Timestamp(2, 2), True) + self.assertEqual(samples.Timestamp(1, 2) < samples.Timestamp(1, 1), False) + self.assertEqual(samples.Timestamp(2, 1) < samples.Timestamp(1, 1), False) + self.assertEqual(samples.Timestamp(2, 2) < samples.Timestamp(1, 1), False) + + +if __name__ == '__main__': + unittest.main()