Implemented removal of float usage. Floats converted to int internally.

This commit is contained in:
Mark Beamer Jr 2018-09-12 17:59:51 -04:00 committed by Lex Berezhny
parent c753a5825a
commit 559afa465a
2 changed files with 32 additions and 10 deletions

View file

@ -5,6 +5,7 @@ import requests
import urllib
import json
import textwrap
import re
from operator import itemgetter
from binascii import hexlify, unhexlify
@ -3325,17 +3326,12 @@ class Daemon(AuthJSONRPCServer):
raise ValueError("Couldn't find account: {}.".format(account_id))
@staticmethod
def get_dewies_or_error(argument: str, amount: Union[str, int, float]):
def get_dewies_or_error(argument: str, amount: str):
if isinstance(amount, str):
if '.' in amount:
try:
return int(Decimal(amount) * COIN)
except InvalidOperation:
raise ValueError("Invalid decimal for '{}' argument: {}".format(argument, amount))
elif amount.isdigit():
amount = int(amount)
if isinstance(amount, (float, int)):
return int(amount * COIN)
result = re.search(r'^(\d{1,10})\.(\d{1,8})$', amount)
if result is not None:
whole, fractional = result.groups()
return int(whole+fractional.ljust(8,"0"))
raise ValueError("Invalid value for '{}' argument: {}".format(argument, amount))

View file

@ -313,3 +313,29 @@ class TestFileListSorting(unittest.TestCase):
setattr(lbry_file, key, faked_attributes[key])
return lbry_file
class TestDeweyInputOutput(unittest.TestCase):
def test_input(self):
self.assertEqual(LBRYDaemon.get_dewies_or_error("", "1.0"), 100000000)
self.assertEqual(LBRYDaemon.get_dewies_or_error("", "2.00000000"), 200000000)
self.assertEqual(LBRYDaemon.get_dewies_or_error("", "2000000000.0"), 200000000000000000)
def test_invalid_input(self):
with self.assertRaises(ValueError):
LBRYDaemon.get_dewies_or_error("", "1")
with self.assertRaises(ValueError):
LBRYDaemon.get_dewies_or_error("", "-1.0")
with self.assertRaises(ValueError):
LBRYDaemon.get_dewies_or_error("", "10000000000.0")
with self.assertRaises(ValueError):
LBRYDaemon.get_dewies_or_error("", "1.000000000")
with self.assertRaises(ValueError):
LBRYDaemon.get_dewies_or_error("", "-0")
with self.assertRaises(ValueError):
LBRYDaemon.get_dewies_or_error("", "1")
with self.assertRaises(ValueError):
LBRYDaemon.get_dewies_or_error("", ".1")
with self.assertRaises(ValueError):
LBRYDaemon.get_dewies_or_error("", "1e-7")