Skip to content

Commit

Permalink
Merge pull request #979 from weidongkl/master
Browse files Browse the repository at this point in the history
fix: error in determining timestamp less than
  • Loading branch information
csmarchbanks committed Nov 14, 2023
2 parents 3d78630 + da8d29d commit fae24b7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions prometheus_client/samples.py
Expand Up @@ -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.
Expand Down
27 changes: 27 additions & 0 deletions 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()

0 comments on commit fae24b7

Please sign in to comment.