From 772500d1c6103a4608f27dc90bc000194bb85ce7 Mon Sep 17 00:00:00 2001 From: Vishnu KS Date: Wed, 14 Apr 2021 19:57:56 +0530 Subject: [PATCH] validators: Make to_positive_or_allowed_int an optional argument. --- zerver/lib/validator.py | 4 ++-- zerver/tests/test_decorators.py | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/zerver/lib/validator.py b/zerver/lib/validator.py index 969eb0c0f9..9c84d41b58 100644 --- a/zerver/lib/validator.py +++ b/zerver/lib/validator.py @@ -465,10 +465,10 @@ def to_non_negative_int(s: str, max_int_size: int = 2 ** 32 - 1) -> int: return x -def to_positive_or_allowed_int(allowed_integer: int) -> Callable[[str], int]: +def to_positive_or_allowed_int(allowed_integer: Optional[int] = None) -> Callable[[str], int]: def converter(s: str) -> int: x = int(s) - if x == allowed_integer: + if allowed_integer is not None and x == allowed_integer: return x if x == 0: raise ValueError("argument is 0") diff --git a/zerver/tests/test_decorators.py b/zerver/tests/test_decorators.py index 45b6708e2e..0f6be0d8c5 100644 --- a/zerver/tests/test_decorators.py +++ b/zerver/tests/test_decorators.py @@ -787,12 +787,20 @@ class ValidatorTestCase(ZulipTestCase): self.assertEqual(to_non_negative_int(str(2 ** 32))) def test_to_positive_or_allowed_int(self) -> None: + self.assertEqual(to_positive_or_allowed_int()("5"), 5) self.assertEqual(to_positive_or_allowed_int(-1)("5"), 5) + self.assertEqual(to_positive_or_allowed_int(-1)("-1"), -1) with self.assertRaisesRegex(ValueError, "argument is negative"): to_positive_or_allowed_int(-1)("-5") + with self.assertRaisesRegex(ValueError, "argument is negative"): + to_positive_or_allowed_int()("-5") + with self.assertRaises(ValueError): to_positive_or_allowed_int(-1)("0") + with self.assertRaises(ValueError): + to_positive_or_allowed_int()("0") + self.assertEqual(to_positive_or_allowed_int(0)("0"), 0) def test_check_float(self) -> None: x: Any = 5.5