forked from LBRYCommunity/lbry-sdk
improve tests for ExchangeRateManager
This commit is contained in:
parent
9f87d502c3
commit
c3f61b3e69
4 changed files with 84 additions and 44 deletions
|
@ -18,6 +18,7 @@ COINBASE_FEE = 0.0 #add fee
|
|||
class ExchangeRate(object):
|
||||
def __init__(self, market, spot, ts):
|
||||
assert int(time.time()) - ts < 600
|
||||
assert spot > 0
|
||||
self.currency_pair = (market[0:3], market[3:6])
|
||||
self.spot = spot
|
||||
self.ts = ts
|
||||
|
@ -200,45 +201,4 @@ class ExchangeRateManager(object):
|
|||
return {market: market.rate.as_dict() for market in self.market_feeds}
|
||||
|
||||
|
||||
class DummyBTCLBCFeed(MarketFeed):
|
||||
def __init__(self):
|
||||
MarketFeed.__init__(
|
||||
self,
|
||||
"BTCLBC",
|
||||
"market name",
|
||||
"derp.com",
|
||||
None,
|
||||
0.0
|
||||
)
|
||||
|
||||
|
||||
class DummyUSDBTCFeed(MarketFeed):
|
||||
def __init__(self):
|
||||
MarketFeed.__init__(
|
||||
self,
|
||||
"USDBTC",
|
||||
"market name",
|
||||
"derp.com",
|
||||
None,
|
||||
0.0
|
||||
)
|
||||
|
||||
|
||||
class DummyExchangeRateManager(object):
|
||||
def __init__(self, rates):
|
||||
self.market_feeds = [DummyBTCLBCFeed(), DummyUSDBTCFeed()]
|
||||
for feed in self.market_feeds:
|
||||
feed.rate = ExchangeRate(
|
||||
feed.market, rates[feed.market]['spot'], rates[feed.market]['ts'])
|
||||
|
||||
def convert_currency(self, from_currency, to_currency, amount):
|
||||
log.debug("Converting %f %s to %s" % (amount, from_currency, to_currency))
|
||||
for market in self.market_feeds:
|
||||
if (market.rate_is_initialized and
|
||||
market.rate.currency_pair == (from_currency, to_currency)):
|
||||
return amount * market.rate.spot
|
||||
for market in self.market_feeds:
|
||||
if (market.rate_is_initialized and
|
||||
market.rate.currency_pair[0] == from_currency):
|
||||
return self.convert_currency(
|
||||
market.rate.currency_pair[1], to_currency, amount * market.rate.spot)
|
||||
|
|
|
@ -6,6 +6,7 @@ from twisted.internet import defer
|
|||
|
||||
from lbrynet.core import PTCWallet
|
||||
from lbrynet.core import BlobAvailability
|
||||
from lbrynet.lbrynet_daemon import ExchangeRateManager as ERM
|
||||
from lbrynet import conf
|
||||
|
||||
KB = 2**10
|
||||
|
@ -40,6 +41,37 @@ class FakeNetwork(object):
|
|||
return 1
|
||||
|
||||
|
||||
class BTCLBCFeed(ERM.MarketFeed):
|
||||
def __init__(self):
|
||||
ERM.MarketFeed.__init__(
|
||||
self,
|
||||
"BTCLBC",
|
||||
"market name",
|
||||
"derp.com",
|
||||
None,
|
||||
0.0
|
||||
)
|
||||
|
||||
class USDBTCFeed(ERM.MarketFeed):
|
||||
def __init__(self):
|
||||
ERM.MarketFeed.__init__(
|
||||
self,
|
||||
"USDBTC",
|
||||
"market name",
|
||||
"derp.com",
|
||||
None,
|
||||
0.0
|
||||
)
|
||||
|
||||
class ExchangeRateManager(ERM.ExchangeRateManager):
|
||||
def __init__(self, market_feeds, rates):
|
||||
self.market_feeds = market_feeds
|
||||
for feed in self.market_feeds:
|
||||
feed.rate = ERM.ExchangeRate(
|
||||
feed.market, rates[feed.market]['spot'], rates[feed.market]['ts'])
|
||||
|
||||
|
||||
|
||||
class Wallet(object):
|
||||
def __init__(self):
|
||||
self.private_key = RSA.generate(1024)
|
||||
|
@ -254,3 +286,6 @@ def mock_conf_settings(obj, settings={}):
|
|||
conf.settings = original_settings
|
||||
|
||||
obj.addCleanup(_reset_settings)
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -13,7 +13,8 @@ from lbrynet.lbrynet_daemon import ExchangeRateManager
|
|||
from tests import util
|
||||
from tests.mocks import mock_conf_settings, FakeNetwork
|
||||
from tests.mocks import BlobAvailabilityTracker as DummyBlobAvailabilityTracker
|
||||
|
||||
from tests.mocks import ExchangeRateManager as DummyExchangeRateManager
|
||||
from tests.mocks import BTCLBCFeed, USDBTCFeed
|
||||
|
||||
def get_test_daemon(data_rate=None, generous=True, with_fee=False):
|
||||
if data_rate is None:
|
||||
|
@ -26,7 +27,8 @@ def get_test_daemon(data_rate=None, generous=True, with_fee=False):
|
|||
daemon = LBRYDaemon(None, None)
|
||||
daemon.session = mock.Mock(spec=Session.Session)
|
||||
daemon.session.wallet = mock.Mock(spec=Wallet.LBRYumWallet)
|
||||
daemon.exchange_rate_manager = ExchangeRateManager.DummyExchangeRateManager(rates)
|
||||
market_feeds = [BTCLBCFeed(), USDBTCFeed()]
|
||||
daemon.exchange_rate_manager = DummyExchangeRateManager(market_feeds, rates)
|
||||
base_prm = PaymentRateManager.BasePaymentRateManager(rate=data_rate)
|
||||
prm = PaymentRateManager.NegotiatedPaymentRateManager(base_prm, DummyBlobAvailabilityTracker(),
|
||||
generous=generous)
|
||||
|
|
|
@ -5,6 +5,8 @@ from lbrynet.core.Error import InvalidExchangeRateResponse
|
|||
from twisted.trial import unittest
|
||||
from twisted.internet import defer
|
||||
from tests import util
|
||||
from tests.mocks import ExchangeRateManager as DummyExchangeRateManager
|
||||
from tests.mocks import BTCLBCFeed, USDBTCFeed
|
||||
|
||||
|
||||
class FeeFormatTest(unittest.TestCase):
|
||||
|
@ -18,6 +20,28 @@ class FeeFormatTest(unittest.TestCase):
|
|||
self.assertEqual(10.0, fee['amount'])
|
||||
self.assertEqual('USD', fee['currency'])
|
||||
|
||||
def test_fee_zero(self):
|
||||
fee_dict = {
|
||||
'currency':'LBC',
|
||||
'amount': 0.0,
|
||||
'address': "bRcHraa8bYJZL7vkh5sNmGwPDERFUjGPP9"
|
||||
}
|
||||
fee = Fee(fee_dict)
|
||||
self.assertEqual(0.0, fee['amount'])
|
||||
self.assertEqual('LBC', fee['currency'])
|
||||
|
||||
|
||||
class ExchangeRateTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
util.resetTime(self)
|
||||
|
||||
def test_invalid_rates(self):
|
||||
with self.assertRaises(AssertionError):
|
||||
ExchangeRateManager.ExchangeRate('USDBTC', 0, util.DEFAULT_ISO_TIME)
|
||||
with self.assertRaises(AssertionError):
|
||||
ExchangeRateManager.ExchangeRate('USDBTC', -1, util.DEFAULT_ISO_TIME)
|
||||
|
||||
|
||||
class FeeTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
util.resetTime(self)
|
||||
|
@ -33,10 +57,29 @@ class FeeTest(unittest.TestCase):
|
|||
'BTCLBC': {'spot': 3.0, 'ts': util.DEFAULT_ISO_TIME + 1},
|
||||
'USDBTC': {'spot': 2.0, 'ts': util.DEFAULT_ISO_TIME + 2}
|
||||
}
|
||||
manager = ExchangeRateManager.DummyExchangeRateManager(rates)
|
||||
|
||||
market_feeds = [BTCLBCFeed(), USDBTCFeed()]
|
||||
manager = DummyExchangeRateManager(market_feeds,rates)
|
||||
result = manager.convert_currency(fee.currency, "LBC", fee.amount)
|
||||
self.assertEqual(60.0, result)
|
||||
|
||||
def test_missing_feed(self):
|
||||
# test when a feed is missing for conversion
|
||||
fee = Fee({
|
||||
'currency':'USD',
|
||||
'amount': 1.0,
|
||||
'address': "bRcHraa8bYJZL7vkh5sNmGwPDERFUjGPP9"
|
||||
})
|
||||
|
||||
rates = {
|
||||
'BTCLBC': {'spot': 1.0, 'ts': util.DEFAULT_ISO_TIME + 1},
|
||||
}
|
||||
market_feeds = [BTCLBCFeed()]
|
||||
manager = DummyExchangeRateManager(market_feeds,rates)
|
||||
with self.assertRaises(Exception):
|
||||
result = manager.convert_currency(fee.currency, "LBC", fee.amount)
|
||||
|
||||
|
||||
class GoogleBTCFeedTest(unittest.TestCase):
|
||||
|
||||
@defer.inlineCallbacks
|
||||
|
|
Loading…
Reference in a new issue