Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: error in determining timestamp less than #979

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions prometheus_client/samples.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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()