diff --git a/frappe/tests/test_utils.py b/frappe/tests/test_utils.py index d4f0acafcc..d31bbdb7b7 100644 --- a/frappe/tests/test_utils.py +++ b/frappe/tests/test_utils.py @@ -55,6 +55,7 @@ from frappe.utils.data import ( add_to_date, add_years, cast, + cint, cstr, duration_to_seconds, expand_relative_urls, @@ -1250,6 +1251,15 @@ class TestRounding(FrappeTestCase): def test_default_rounding(self): self.assertEqual(frappe.get_system_settings("rounding_method"), "Banker's Rounding") + @given( + st.floats(min_value=-(2**32) - 1, max_value=2**32 + 1), + st.integers(min_value=-(2**63) - 1, max_value=2**63 + 1), + ) + def test_cint(self, floating_point, integer): + self.assertEqual(cint(integer), integer) + self.assertEqual(cint(str(integer)), integer) + self.assertEqual(cint(str(floating_point)), int(floating_point)) + class TestArgumentTypingValidations(FrappeTestCase): def test_validate_argument_types(self): diff --git a/frappe/utils/data.py b/frappe/utils/data.py index a094157ac1..748e0c8954 100644 --- a/frappe/utils/data.py +++ b/frappe/utils/data.py @@ -1122,9 +1122,12 @@ def cint(s: NumericType | str, default: int = 0) -> int: """ try: - return int(float(s)) + return int(s) except Exception: - return default + try: + return int(float(s)) + except Exception: + return default def floor(s: NumericType | str) -> int: