From f74f4a925a398c662d5ccad1217c2a6982097151 Mon Sep 17 00:00:00 2001 From: Vladimir Magamedov Date: Wed, 15 Nov 2017 23:41:53 +0200 Subject: [PATCH] Fixed Deadline comparison methods, improved tests --- grpclib/metadata.py | 10 ++++++++-- tests/test_metadata.py | 11 ++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/grpclib/metadata.py b/grpclib/metadata.py index a344314..7863b07 100644 --- a/grpclib/metadata.py +++ b/grpclib/metadata.py @@ -43,10 +43,16 @@ class Deadline: self._timestamp = _timestamp def __lt__(self, other): - return self._timestamp < other.timestamp + if not isinstance(other, Deadline): + raise TypeError('comparison is not supported between ' + 'instances of \'{}\' and \'{}\'' + .format(type(self).__name__, type(other).__name__)) + return self._timestamp < other._timestamp def __eq__(self, other): - return self._timestamp == other.timestamp + if not isinstance(other, Deadline): + return False + return self._timestamp == other._timestamp @classmethod def from_metadata(cls, metadata): diff --git a/tests/test_metadata.py b/tests/test_metadata.py index 3faf781..458afb9 100644 --- a/tests/test_metadata.py +++ b/tests/test_metadata.py @@ -33,7 +33,16 @@ def test_decode_timeout(value, expected): def test_deadline(): - assert min(Deadline(1), Deadline(2)) == Deadline(1) + assert Deadline.from_timeout(1) < Deadline.from_timeout(2) + + with pytest.raises(TypeError) as err: + Deadline.from_timeout(1) < 'invalid' + err.match('comparison is not supported between instances ' + 'of \'Deadline\' and \'str\'') + + assert Deadline(_timestamp=1) == Deadline(_timestamp=1) + + assert Deadline.from_timeout(1) != 'invalid' def test_headers_with_deadline():