fixup fake exchange rate manager for integration tests

This commit is contained in:
Lex Berezhny 2019-12-15 01:37:55 -05:00
parent e36c672c9a
commit 46c6588aa1
3 changed files with 29 additions and 30 deletions

View file

@ -3,7 +3,9 @@ import json
import shutil import shutil
import tempfile import tempfile
import logging import logging
from time import time
from binascii import unhexlify from binascii import unhexlify
from functools import partial
from torba.testcase import IntegrationTestCase, WalletNode from torba.testcase import IntegrationTestCase, WalletNode
@ -20,19 +22,22 @@ from lbry.extras.daemon.Components import (
UPNP_COMPONENT, EXCHANGE_RATE_MANAGER_COMPONENT UPNP_COMPONENT, EXCHANGE_RATE_MANAGER_COMPONENT
) )
from lbry.extras.daemon.ComponentManager import ComponentManager from lbry.extras.daemon.ComponentManager import ComponentManager
from lbry.extras.daemon.exchange_rate_manager import ExchangeRateManager as BaseExchangeRateManager from lbry.extras.daemon.exchange_rate_manager import (
ExchangeRateManager, ExchangeRate, LBRYFeed, LBRYBTCFeed
)
from lbry.extras.daemon.storage import SQLiteStorage from lbry.extras.daemon.storage import SQLiteStorage
from lbry.blob.blob_manager import BlobManager from lbry.blob.blob_manager import BlobManager
from lbry.stream.reflector.server import ReflectorServer from lbry.stream.reflector.server import ReflectorServer
from lbry.blob_exchange.server import BlobServer from lbry.blob_exchange.server import BlobServer
class FakeExchangeRateManager(BaseExchangeRateManager): class FakeExchangeRateManager(ExchangeRateManager):
def __init__(self): def __init__(self, market_feeds, rates):
super().__init__() self.market_feeds = market_feeds
for i, feed in enumerate(self.market_feeds): for feed in self.market_feeds:
feed._save_price(i+1) feed.last_check = time()
feed.rate = ExchangeRate(feed.market, rates[feed.market], time())
def start(self): def start(self):
pass pass
@ -41,15 +46,22 @@ class FakeExchangeRateManager(BaseExchangeRateManager):
pass pass
def get_fake_exchange_rate_manager(rates=None):
return FakeExchangeRateManager(
[LBRYFeed(), LBRYBTCFeed()],
rates or {'BTCLBC': 3.0, 'USDBTC': 2.0}
)
class ExchangeRateManagerComponent(Component): class ExchangeRateManagerComponent(Component):
component_name = EXCHANGE_RATE_MANAGER_COMPONENT component_name = EXCHANGE_RATE_MANAGER_COMPONENT
def __init__(self, component_manager): def __init__(self, component_manager, rates=None):
super().__init__(component_manager) super().__init__(component_manager)
self.exchange_rate_manager = FakeExchangeRateManager() self.exchange_rate_manager = get_fake_exchange_rate_manager(rates)
@property @property
def component(self) -> BaseExchangeRateManager: def component(self) -> ExchangeRateManager:
return self.exchange_rate_manager return self.exchange_rate_manager
async def start(self): async def start(self):
@ -151,7 +163,9 @@ class CommandTestCase(IntegrationTestCase):
daemon = Daemon(conf, ComponentManager( daemon = Daemon(conf, ComponentManager(
conf, skip_components=conf.components_to_skip, wallet=wallet_maker, conf, skip_components=conf.components_to_skip, wallet=wallet_maker,
exchange_rate_manager=ExchangeRateManagerComponent exchange_rate_manager=partial(ExchangeRateManagerComponent, rates={
'BTCLBC': 1.0, 'USDBTC': 2.0
})
)) ))
await daemon.initialize() await daemon.initialize()
self.daemons.append(daemon) self.daemons.append(daemon)

View file

@ -9,24 +9,10 @@ from lbry.extras.daemon.exchange_rate_manager import (
BittrexFeed, BittrexFeed,
) )
from torba.testcase import AsyncioTestCase from torba.testcase import AsyncioTestCase
from lbry.testcase import FakeExchangeRateManager, get_fake_exchange_rate_manager
from lbry.error import InvalidExchangeRateResponseError from lbry.error import InvalidExchangeRateResponseError
class DummyExchangeRateManager(ExchangeRateManager):
def __init__(self, market_feeds, rates):
self.market_feeds = market_feeds
for feed in self.market_feeds:
feed.last_check = time()
feed.rate = ExchangeRate(feed.market, rates[feed.market], time())
def get_dummy_exchange_rate_manager():
return DummyExchangeRateManager(
[LBRYFeed(), LBRYBTCFeed()],
{'BTCLBC': 3.0, 'USDBTC': 2.0}
)
class ExchangeRateTests(AsyncioTestCase): class ExchangeRateTests(AsyncioTestCase):
def test_invalid_rates(self): def test_invalid_rates(self):
@ -39,7 +25,7 @@ class ExchangeRateTests(AsyncioTestCase):
fee = Claim().stream.fee fee = Claim().stream.fee
fee.usd = Decimal(10.0) fee.usd = Decimal(10.0)
fee.address = "bRcHraa8bYJZL7vkh5sNmGwPDERFUjGPP9" fee.address = "bRcHraa8bYJZL7vkh5sNmGwPDERFUjGPP9"
manager = get_dummy_exchange_rate_manager() manager = get_fake_exchange_rate_manager()
result = manager.convert_currency(fee.currency, "LBC", fee.amount) result = manager.convert_currency(fee.currency, "LBC", fee.amount)
self.assertEqual(60.0, result) self.assertEqual(60.0, result)
@ -47,7 +33,7 @@ class ExchangeRateTests(AsyncioTestCase):
fee = Claim().stream.fee fee = Claim().stream.fee
fee.usd = Decimal(1.0) fee.usd = Decimal(1.0)
fee.address = "bRcHraa8bYJZL7vkh5sNmGwPDERFUjGPP9" fee.address = "bRcHraa8bYJZL7vkh5sNmGwPDERFUjGPP9"
manager = DummyExchangeRateManager([LBRYFeed()], {'BTCLBC': 1.0}) manager = FakeExchangeRateManager([LBRYFeed()], {'BTCLBC': 1.0})
with self.assertRaises(CurrencyConversionError): with self.assertRaises(CurrencyConversionError):
manager.convert_currency(fee.currency, "LBC", fee.amount) manager.convert_currency(fee.currency, "LBC", fee.amount)

View file

@ -3,11 +3,10 @@ import shutil
import binascii import binascii
from unittest import mock from unittest import mock
import asyncio import asyncio
import time
import json import json
from decimal import Decimal from decimal import Decimal
from tests.unit.blob_exchange.test_transfer_blob import BlobExchangeTestBase from tests.unit.blob_exchange.test_transfer_blob import BlobExchangeTestBase
from tests.unit.lbrynet_daemon.test_exchange_rate_manager import get_dummy_exchange_rate_manager from lbry.testcase import get_fake_exchange_rate_manager
from lbry.utils import generate_id from lbry.utils import generate_id
from torba.client.errors import InsufficientFundsError from torba.client.errors import InsufficientFundsError
from lbry.error import KeyFeeAboveMaxAllowedError, ResolveError, DownloadSDTimeoutError, DownloadDataTimeoutError from lbry.error import KeyFeeAboveMaxAllowedError, ResolveError, DownloadSDTimeoutError, DownloadDataTimeoutError
@ -140,7 +139,7 @@ class TestStreamManager(BlobExchangeTestBase):
AnalyticsManager(self.client_config, AnalyticsManager(self.client_config,
binascii.hexlify(generate_id()).decode(), binascii.hexlify(generate_id()).decode(),
binascii.hexlify(generate_id()).decode())) binascii.hexlify(generate_id()).decode()))
self.exchange_rate_manager = get_dummy_exchange_rate_manager() self.exchange_rate_manager = get_fake_exchange_rate_manager()
async def _test_time_to_first_bytes(self, check_post, error=None, after_setup=None): async def _test_time_to_first_bytes(self, check_post, error=None, after_setup=None):
await self.setup_stream_manager() await self.setup_stream_manager()