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

114 lines
4.5 KiB
Python
Raw Normal View History

import mock
2017-04-11 04:47:54 +02:00
import json
2017-04-26 19:27:24 +02:00
2016-12-02 20:39:01 +01:00
from twisted.internet import defer
from twisted.trial import unittest
2017-04-26 19:27:24 +02:00
2017-04-11 04:47:54 +02:00
from lbryschema.decode import smart_decode
2017-04-26 19:27:24 +02:00
from lbrynet import conf
from lbrynet.core import Session, PaymentRateManager, Wallet
2016-12-02 20:39:01 +01:00
from lbrynet.lbrynet_daemon.Daemon import Daemon as LBRYDaemon
from lbrynet.lbrynet_daemon import ExchangeRateManager
2017-04-26 19:27:24 +02:00
from tests import util
from tests.mocks import mock_conf_settings, FakeNetwork
from tests.mocks import BlobAvailabilityTracker as DummyBlobAvailabilityTracker
2017-05-31 20:15:15 +02:00
from tests.mocks import ExchangeRateManager as DummyExchangeRateManager
from tests.mocks import BTCLBCFeed, USDBTCFeed
2016-12-02 20:39:01 +01:00
2016-12-04 22:18:13 +01:00
def get_test_daemon(data_rate=None, generous=True, with_fee=False):
if data_rate is None:
2017-01-17 04:23:20 +01:00
data_rate = conf.ADJUSTABLE_SETTINGS['data_rate'][1]
2016-12-04 22:18:13 +01:00
2016-12-02 20:39:01 +01:00
rates = {
'BTCLBC': {'spot': 3.0, 'ts': util.DEFAULT_ISO_TIME + 1},
'USDBTC': {'spot': 2.0, 'ts': util.DEFAULT_ISO_TIME + 2}
}
2017-03-08 16:21:12 +01:00
daemon = LBRYDaemon(None, None)
2016-12-02 20:39:01 +01:00
daemon.session = mock.Mock(spec=Session.Session)
2017-04-11 04:47:54 +02:00
daemon.session.wallet = mock.Mock(spec=Wallet.LBRYumWallet)
2017-05-31 20:15:15 +02:00
market_feeds = [BTCLBCFeed(), USDBTCFeed()]
daemon.exchange_rate_manager = DummyExchangeRateManager(market_feeds, rates)
2016-12-02 20:39:01 +01:00
base_prm = PaymentRateManager.BasePaymentRateManager(rate=data_rate)
prm = PaymentRateManager.NegotiatedPaymentRateManager(base_prm, DummyBlobAvailabilityTracker(),
generous=generous)
2016-12-02 20:39:01 +01:00
daemon.session.payment_rate_manager = prm
metadata = {
2016-12-04 22:18:13 +01:00
"author": "fake author",
2017-04-11 04:47:54 +02:00
"language": "en",
2016-12-04 22:18:13 +01:00
"content_type": "fake/format",
"description": "fake description",
"license": "fake license",
"license_url": "fake license url",
2016-12-02 20:39:01 +01:00
"nsfw": False,
"sources": {
2016-12-04 22:18:13 +01:00
"lbry_sd_hash": "d2b8b6e907dde95245fe6d144d16c2fdd60c4e0c6463ec98b85642d06d8e9414e8fcfdcb7cb13532ec5454fb8fe7f280"
},
"thumbnail": "fake thumbnail",
"title": "fake title",
2016-12-02 20:39:01 +01:00
"ver": "0.0.3"
}
if with_fee:
metadata.update(
2017-04-11 04:47:54 +02:00
{"fee": {"USD": {"address": "bQ6BGboPV2SpTMEP7wLNiAcnsZiH8ye6eA", "amount": 0.75}}})
2016-12-04 22:18:13 +01:00
daemon._resolve_name = lambda _: defer.succeed(metadata)
2017-04-12 23:42:55 +02:00
migrated = smart_decode(json.dumps(metadata))
daemon.session.wallet.resolve_uri = lambda _: defer.succeed({'claim': {'value': migrated.claim_dict}})
2016-12-02 20:39:01 +01:00
return daemon
class TestCostEst(unittest.TestCase):
def setUp(self):
2017-01-17 04:23:20 +01:00
mock_conf_settings(self)
2016-12-02 20:39:01 +01:00
util.resetTime(self)
2016-12-04 22:18:13 +01:00
def test_fee_and_generous_data(self):
2016-12-02 20:39:01 +01:00
size = 10000000
2016-12-04 22:18:13 +01:00
correct_result = 4.5
2016-12-02 20:39:01 +01:00
daemon = get_test_daemon(generous=True, with_fee=True)
2017-04-11 04:47:54 +02:00
print daemon.get_est_cost("test", size)
2016-12-04 22:18:13 +01:00
self.assertEquals(daemon.get_est_cost("test", size).result, correct_result)
2016-12-02 20:39:01 +01:00
2016-12-04 22:18:13 +01:00
def test_fee_and_ungenerous_data(self):
2016-12-02 20:39:01 +01:00
size = 10000000
fake_fee_amount = 4.5
2017-01-17 04:23:20 +01:00
data_rate = conf.ADJUSTABLE_SETTINGS['data_rate'][1]
correct_result = size / 10 ** 6 * data_rate + fake_fee_amount
2016-12-02 20:39:01 +01:00
daemon = get_test_daemon(generous=False, with_fee=True)
2016-12-04 22:18:13 +01:00
self.assertEquals(daemon.get_est_cost("test", size).result, correct_result)
2016-12-02 20:39:01 +01:00
2016-12-04 22:18:13 +01:00
def test_generous_data_and_no_fee(self):
2016-12-02 20:39:01 +01:00
size = 10000000
2016-12-04 22:18:13 +01:00
correct_result = 0.0
2016-12-02 20:39:01 +01:00
daemon = get_test_daemon(generous=True)
2016-12-04 22:18:13 +01:00
self.assertEquals(daemon.get_est_cost("test", size).result, correct_result)
2016-12-02 20:39:01 +01:00
2016-12-04 22:18:13 +01:00
def test_ungenerous_data_and_no_fee(self):
2016-12-02 20:39:01 +01:00
size = 10000000
2017-01-17 04:23:20 +01:00
data_rate = conf.ADJUSTABLE_SETTINGS['data_rate'][1]
correct_result = size / 10 ** 6 * data_rate
2016-12-02 20:39:01 +01:00
daemon = get_test_daemon(generous=False)
2016-12-04 22:18:13 +01:00
self.assertEquals(daemon.get_est_cost("test", size).result, correct_result)
class TestJsonRpc(unittest.TestCase):
def setUp(self):
2017-02-21 19:47:47 +01:00
def noop():
return None
mock_conf_settings(self)
util.resetTime(self)
self.test_daemon = get_test_daemon()
self.test_daemon.session.wallet = Wallet.LBRYumWallet(storage=Wallet.InMemoryStorage())
2017-02-21 19:47:47 +01:00
self.test_daemon.session.wallet.network = FakeNetwork()
self.test_daemon.session.wallet.get_best_blockhash = noop
def test_status(self):
d = defer.maybeDeferred(self.test_daemon.jsonrpc_status)
d.addCallback(lambda status: self.assertDictContainsSubset({'is_running': False}, status))
def test_help(self):
d = defer.maybeDeferred(self.test_daemon.jsonrpc_help, command='status')
2017-03-09 13:58:36 +01:00
d.addCallback(lambda result: self.assertSubstring('daemon status', result['help']))
# self.assertSubstring('daemon status', d.result)