Skip to content

Commit

Permalink
fix: assertAlmostEqual with precision (#19794)
Browse files Browse the repository at this point in the history
(cherry picked from commit 4bcb126)

Co-authored-by: barredterra <14891507+barredterra@users.noreply.github.com>
  • Loading branch information
mergify[bot] and barredterra committed Jan 27, 2023
1 parent 5f57816 commit 9f7c4e0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion frappe/model/base_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,7 @@ def get_password(self, fieldname="password", raise_exception=True):
def is_dummy_password(self, pwd):
return "".join(set(pwd)) == "*"

def precision(self, fieldname, parentfield=None):
def precision(self, fieldname, parentfield=None) -> int | None:
"""Returns float precision for a particular field (or get global default).
:param fieldname: Fieldname for which precision is required.
Expand Down
6 changes: 4 additions & 2 deletions frappe/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ def assertDocumentEqual(self, expected, actual):
else:
self._compare_field(value, actual.get(field), actual, field)

def _compare_field(self, expected, actual, doc, field):
def _compare_field(self, expected, actual, doc: BaseDocument, field: str):
msg = f"{field} should be same."

if isinstance(expected, float):
precision = doc.precision(field)
self.assertAlmostEqual(expected, actual, f"{field} should be same to {precision} digits")
self.assertAlmostEqual(
expected, actual, places=precision, msg=f"{field} should be same to {precision} digits"
)
elif isinstance(expected, (bool, int)):
self.assertEqual(expected, cint(actual), msg=msg)
elif isinstance(expected, datetime_like_types):
Expand Down

0 comments on commit 9f7c4e0

Please sign in to comment.