test: add assert_scale assertion to test framework

This commit is contained in:
Jon Atack 2020-06-06 11:07:05 +02:00 committed by Luke Dashjr
parent cee5305a97
commit fb6ef09a1d

View File

@ -197,6 +197,18 @@ def assert_array_result(object_array, to_match, expected, should_not_find=False)
if num_matched > 0 and should_not_find:
raise AssertionError("Objects were found %s" % (str(to_match)))
def assert_scale(number, expected_scale=8):
"""Assert number has expected scale, e.g. fractional digits; number of
digits after the decimal. The default of 8 corresponds to a Bitcoin amount."""
number = str(number)
mantissa = number.split('.')[-1].upper()
if mantissa[:3] == '0E-':
assert_equal(mantissa, '0E-{}'.format(expected_scale)) # zeros in exponent notation
elif mantissa == number:
assert_equal(0, expected_scale) # no mantissa, ergo, expected scale must be 0
else:
assert_equal(len(mantissa), expected_scale)
# Utility functions
###################