lbry-sdk/tests/unit/lbrynet_daemon/test_ExchangeRateManager.py

118 lines
4.1 KiB
Python
Raw Normal View History

2017-05-29 22:38:08 +02:00
from lbryschema.fee import Fee
from lbrynet.daemon import ExchangeRateManager
2017-02-28 02:18:57 +01:00
from lbrynet.core.Error import InvalidExchangeRateResponse
from twisted.trial import unittest
from twisted.internet import defer
from tests import util
2017-05-31 20:15:15 +02:00
from tests.mocks import ExchangeRateManager as DummyExchangeRateManager
from tests.mocks import BTCLBCFeed, USDBTCFeed
2017-02-28 02:18:57 +01:00
class FeeFormatTest(unittest.TestCase):
def test_fee_created_with_correct_inputs(self):
fee_dict = {
2017-04-05 01:42:35 +02:00
'currency':'USD',
'amount': 10.0,
'address': "bRcHraa8bYJZL7vkh5sNmGwPDERFUjGPP9"
2017-02-28 02:18:57 +01:00
}
2017-05-29 22:38:08 +02:00
fee = Fee(fee_dict)
2017-04-05 01:42:35 +02:00
self.assertEqual(10.0, fee['amount'])
self.assertEqual('USD', fee['currency'])
2017-02-28 02:18:57 +01:00
2017-05-31 20:15:15 +02:00
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)
2017-02-28 02:18:57 +01:00
class FeeTest(unittest.TestCase):
def setUp(self):
util.resetTime(self)
def test_fee_converts_to_lbc(self):
2017-05-30 23:07:23 +02:00
fee = Fee({
2017-04-05 01:42:35 +02:00
'currency':'USD',
'amount': 10.0,
'address': "bRcHraa8bYJZL7vkh5sNmGwPDERFUjGPP9"
2017-05-30 23:07:23 +02:00
})
2017-04-05 01:42:35 +02:00
2017-02-28 02:18:57 +01:00
rates = {
'BTCLBC': {'spot': 3.0, 'ts': util.DEFAULT_ISO_TIME + 1},
'USDBTC': {'spot': 2.0, 'ts': util.DEFAULT_ISO_TIME + 2}
}
2017-05-31 20:15:15 +02:00
market_feeds = [BTCLBCFeed(), USDBTCFeed()]
manager = DummyExchangeRateManager(market_feeds,rates)
2017-05-30 23:07:23 +02:00
result = manager.convert_currency(fee.currency, "LBC", fee.amount)
2017-02-28 02:18:57 +01:00
self.assertEqual(60.0, result)
2017-05-31 20:15:15 +02:00
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):
2017-09-07 20:01:06 +02:00
manager.convert_currency(fee.currency, "LBC", fee.amount)
2017-05-31 20:15:15 +02:00
2017-09-07 20:01:06 +02:00
class LBRYioFeedTest(unittest.TestCase):
2017-02-28 02:18:57 +01:00
@defer.inlineCallbacks
def test_handle_response(self):
2017-09-07 20:01:06 +02:00
feed = ExchangeRateManager.LBRYioFeed()
2017-02-28 02:18:57 +01:00
2017-09-07 20:01:06 +02:00
response ='{\"data\": {\"fresh\": 0, \"lbc_usd\": 0.05863062523378918, \"lbc_btc\": 5.065289549855739e-05, \"btc_usd\": 1157.498}, \"success\": true, \"error\": null}'
2017-02-28 02:18:57 +01:00
out = yield feed._handle_response(response)
2017-09-07 20:01:06 +02:00
expected = 1.0 / 5.065289549855739e-05
self.assertEqual(expected, out)
response='{}'
with self.assertRaises(InvalidExchangeRateResponse):
out = yield feed._handle_response(response)
2017-02-28 02:18:57 +01:00
2017-09-07 20:01:06 +02:00
response='{"success":true,"result":[]}'
2017-02-28 02:18:57 +01:00
with self.assertRaises(InvalidExchangeRateResponse):
out = yield feed._handle_response(response)
2017-09-07 20:01:06 +02:00
class LBRYioBTCFeedTest(unittest.TestCase):
2017-02-28 02:18:57 +01:00
@defer.inlineCallbacks
def test_handle_response(self):
2017-09-07 20:01:06 +02:00
feed = ExchangeRateManager.LBRYioBTCFeed()
2017-02-28 02:18:57 +01:00
response ='{\"data\": {\"fresh\": 0, \"lbc_usd\": 0.05863062523378918, \"lbc_btc\": 5.065289549855739e-05, \"btc_usd\": 1157.498}, \"success\": true, \"error\": null}'
2017-02-28 02:18:57 +01:00
out = yield feed._handle_response(response)
2017-09-07 20:01:06 +02:00
expected = 1.0 / 1157.498
2017-02-28 02:18:57 +01:00
self.assertEqual(expected, out)
response='{}'
with self.assertRaises(InvalidExchangeRateResponse):
out = yield feed._handle_response(response)
response='{"success":true,"result":[]}'
with self.assertRaises(InvalidExchangeRateResponse):
out = yield feed._handle_response(response)