qt history view custom fiat input fixes
previously, when you submitted a fiat value with thousands separator, it would be discarded.
This commit is contained in:
@@ -3,9 +3,16 @@ import tempfile
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
from decimal import Decimal
|
||||
from unittest import TestCase
|
||||
import time
|
||||
|
||||
from io import StringIO
|
||||
from electrum.storage import WalletStorage, FINAL_SEED_VERSION
|
||||
from electrum.wallet import Abstract_Wallet
|
||||
from electrum.exchange_rate import ExchangeBase, FxThread
|
||||
from electrum.util import TxMinedStatus
|
||||
from electrum.bitcoin import COIN
|
||||
|
||||
from . import SequentialTestCase
|
||||
|
||||
@@ -68,3 +75,67 @@ class TestWalletStorage(WalletTestCase):
|
||||
with open(self.wallet_path, "r") as f:
|
||||
contents = f.read()
|
||||
self.assertEqual(some_dict, json.loads(contents))
|
||||
|
||||
class FakeExchange(ExchangeBase):
|
||||
def __init__(self, rate):
|
||||
super().__init__(lambda self: None, lambda self: None)
|
||||
self.quotes = {'TEST': rate}
|
||||
|
||||
class FakeFxThread:
|
||||
def __init__(self, exchange):
|
||||
self.exchange = exchange
|
||||
self.ccy = 'TEST'
|
||||
|
||||
remove_thousands_separator = staticmethod(FxThread.remove_thousands_separator)
|
||||
timestamp_rate = FxThread.timestamp_rate
|
||||
ccy_amount_str = FxThread.ccy_amount_str
|
||||
history_rate = FxThread.history_rate
|
||||
|
||||
class FakeWallet:
|
||||
def __init__(self, fiat_value):
|
||||
super().__init__()
|
||||
self.fiat_value = fiat_value
|
||||
self.transactions = self.verified_tx = {'abc': 'Tx'}
|
||||
|
||||
def get_tx_height(self, txid):
|
||||
# because we use a current timestamp, and history is empty,
|
||||
# FxThread.history_rate will use spot prices
|
||||
return TxMinedStatus(height=10, conf=10, timestamp=time.time(), header_hash='def')
|
||||
|
||||
default_fiat_value = Abstract_Wallet.default_fiat_value
|
||||
price_at_timestamp = Abstract_Wallet.price_at_timestamp
|
||||
class storage:
|
||||
put = lambda self, x: None
|
||||
|
||||
txid = 'abc'
|
||||
ccy = 'TEST'
|
||||
|
||||
class TestFiat(TestCase):
|
||||
def setUp(self):
|
||||
self.value_sat = COIN
|
||||
self.fiat_value = {}
|
||||
self.wallet = FakeWallet(fiat_value=self.fiat_value)
|
||||
self.fx = FakeFxThread(FakeExchange(Decimal('1000.001')))
|
||||
default_fiat = Abstract_Wallet.default_fiat_value(self.wallet, txid, self.fx, self.value_sat)
|
||||
self.assertEqual(Decimal('1000.001'), default_fiat)
|
||||
self.assertEqual('1,000.00', self.fx.ccy_amount_str(default_fiat, commas=True))
|
||||
|
||||
def test_save_fiat_and_reset(self):
|
||||
self.assertEqual(False, Abstract_Wallet.set_fiat_value(self.wallet, txid, ccy, '1000.01', self.fx, self.value_sat))
|
||||
saved = self.fiat_value[ccy][txid]
|
||||
self.assertEqual('1,000.01', self.fx.ccy_amount_str(Decimal(saved), commas=True))
|
||||
self.assertEqual(True, Abstract_Wallet.set_fiat_value(self.wallet, txid, ccy, '', self.fx, self.value_sat))
|
||||
self.assertNotIn(txid, self.fiat_value[ccy])
|
||||
# even though we are not setting it to the exact fiat value according to the exchange rate, precision is truncated away
|
||||
self.assertEqual(True, Abstract_Wallet.set_fiat_value(self.wallet, txid, ccy, '1,000.002', self.fx, self.value_sat))
|
||||
|
||||
def test_too_high_precision_value_resets_with_no_saved_value(self):
|
||||
self.assertEqual(True, Abstract_Wallet.set_fiat_value(self.wallet, txid, ccy, '1,000.001', self.fx, self.value_sat))
|
||||
|
||||
def test_empty_resets(self):
|
||||
self.assertEqual(True, Abstract_Wallet.set_fiat_value(self.wallet, txid, ccy, '', self.fx, self.value_sat))
|
||||
self.assertNotIn(ccy, self.fiat_value)
|
||||
|
||||
def test_save_garbage(self):
|
||||
self.assertEqual(False, Abstract_Wallet.set_fiat_value(self.wallet, txid, ccy, 'garbage', self.fx, self.value_sat))
|
||||
self.assertNotIn(ccy, self.fiat_value)
|
||||
|
||||
Reference in New Issue
Block a user