update tests

This commit is contained in:
Jack Robison 2019-01-22 17:54:25 -05:00 committed by Lex Berezhny
parent 65ae1b3f08
commit 59f5381b0a
9 changed files with 157 additions and 250 deletions

View file

@ -1,8 +1,9 @@
from lbrynet.extras.daemon import analytics from lbrynet.extras.daemon import analytics
from twisted.trial import unittest import unittest
@unittest.SkipTest
class TrackTest(unittest.TestCase): class TrackTest(unittest.TestCase):
def test_empty_summarize_is_none(self): def test_empty_summarize_is_none(self):
track = analytics.Manager(None, 'x', 'y', 'z') track = analytics.Manager(None, 'x', 'y', 'z')

View file

@ -1,18 +1,18 @@
import asyncio import asyncio
from unittest import TestCase import unittest
from torba.testcase import AdvanceTimeTestCase from torba.testcase import AdvanceTimeTestCase
from tests import mocks
from lbrynet.conf import Config from lbrynet.conf import Config
from lbrynet.extras.daemon.ComponentManager import ComponentManager from lbrynet.extras.daemon.ComponentManager import ComponentManager
from lbrynet.extras.daemon.Components import DATABASE_COMPONENT, DHT_COMPONENT from lbrynet.extras.daemon.Components import DATABASE_COMPONENT, DHT_COMPONENT
from lbrynet.extras.daemon.Components import HASH_ANNOUNCER_COMPONENT, REFLECTOR_COMPONENT, UPNP_COMPONENT from lbrynet.extras.daemon.Components import HASH_ANNOUNCER_COMPONENT, UPNP_COMPONENT
from lbrynet.extras.daemon.Components import PEER_PROTOCOL_SERVER_COMPONENT, EXCHANGE_RATE_MANAGER_COMPONENT from lbrynet.extras.daemon.Components import PEER_PROTOCOL_SERVER_COMPONENT, EXCHANGE_RATE_MANAGER_COMPONENT
from lbrynet.extras.daemon.Components import RATE_LIMITER_COMPONENT, HEADERS_COMPONENT, PAYMENT_RATE_COMPONENT from lbrynet.extras.daemon.Components import HEADERS_COMPONENT
from lbrynet.extras.daemon import Components from lbrynet.extras.daemon import Components
class TestComponentManager(TestCase): @unittest.SkipTest
class TestComponentManager(unittest.TestCase):
def setUp(self): def setUp(self):
self.default_components_sort = [ self.default_components_sort = [
[ [
@ -60,7 +60,8 @@ class TestComponentManager(TestCase):
self.component_manager.get_component("random_component") self.component_manager.get_component("random_component")
class TestComponentManagerOverrides(TestCase): @unittest.SkipTest
class TestComponentManagerOverrides(unittest.TestCase):
def test_init_with_overrides(self): def test_init_with_overrides(self):
class FakeWallet: class FakeWallet:
component_name = "wallet" component_name = "wallet"
@ -88,8 +89,8 @@ class TestComponentManagerOverrides(TestCase):
ComponentManager(Config(), randomComponent=FakeRandomComponent) ComponentManager(Config(), randomComponent=FakeRandomComponent)
@unittest.SkipTest
class TestComponentManagerProperStart(AdvanceTimeTestCase): class TestComponentManagerProperStart(AdvanceTimeTestCase):
def setUp(self): def setUp(self):
self.component_manager = ComponentManager( self.component_manager = ComponentManager(
Config(), Config(),

View file

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from lbrynet import utils from lbrynet import utils
from twisted.trial import unittest import unittest
class CompareVersionTest(unittest.TestCase): class CompareVersionTest(unittest.TestCase):

View file

@ -1,14 +1,13 @@
import os
import shutil import shutil
import tempfile import tempfile
import unittest
import asyncio
import logging import logging
from copy import deepcopy from torba.testcase import AsyncioTestCase
from twisted.internet import defer from lbrynet.blob.blob_info import BlobInfo
from twisted.trial import unittest from lbrynet.blob.blob_manager import BlobFileManager
from lbrynet.conf import Config from lbrynet.stream.descriptor import StreamDescriptor
from lbrynet.extras.compat import f2d from lbrynet.extras.daemon.storage import SQLiteStorage
from lbrynet.extras.daemon.storage import SQLiteStorage, open_file_for_writing
from lbrynet.blob.EncryptedFileDownloader import ManagedEncryptedFileDownloader
from tests.test_utils import random_lbry_hash from tests.test_utils import random_lbry_hash
log = logging.getLogger() log = logging.getLogger()
@ -66,103 +65,62 @@ fake_claim_info = {
} }
class FakeAnnouncer: class StorageTest(AsyncioTestCase):
def __init__(self): async def asyncSetUp(self):
self._queue_size = 0 self.storage = SQLiteStorage(':memory:')
self.blob_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, self.blob_dir)
self.blob_manager = BlobFileManager(asyncio.get_event_loop(), self.blob_dir, self.storage)
await self.storage.open()
def hash_queue_size(self): async def asyncTearDown(self):
return self._queue_size await self.storage.close()
async def store_fake_blob(self, blob_hash, length=100):
await self.storage.add_known_blob(blob_hash, length)
await self.storage.add_completed_blob(blob_hash)
class MocSession: async def store_fake_stream(self, stream_hash, blobs=None, file_name="fake_file", key="DEADBEEF"):
def __init__(self, storage): blobs = blobs or [BlobInfo(1, 100, "DEADBEEF", random_lbry_hash())]
self.storage = storage descriptor = StreamDescriptor(
asyncio.get_event_loop(), self.blob_dir, file_name, key, file_name, blobs, stream_hash
)
sd_blob = await descriptor.make_sd_blob()
await self.storage.store_stream(sd_blob, descriptor)
return descriptor
async def make_and_store_fake_stream(self, blob_count=2, stream_hash=None):
class StorageTest(unittest.TestCase):
maxDiff = 5000
@defer.inlineCallbacks
def setUp(self):
self.db_dir = tempfile.mkdtemp()
self.storage = SQLiteStorage(Config(data_dir=self.db_dir), ':memory:')
yield f2d(self.storage.open())
@defer.inlineCallbacks
def tearDown(self):
yield f2d(self.storage.close())
shutil.rmtree(self.db_dir)
@defer.inlineCallbacks
def store_fake_blob(self, blob_hash, blob_length=100, next_announce=0, should_announce=0):
yield f2d(self.storage.add_completed_blob(blob_hash, blob_length, next_announce,
should_announce, "finished"))
@defer.inlineCallbacks
def store_fake_stream_blob(self, stream_hash, blob_hash, blob_num, length=100, iv="DEADBEEF"):
blob_info = {
'blob_hash': blob_hash, 'blob_num': blob_num, 'iv': iv
}
if length:
blob_info['length'] = length
yield f2d(self.storage.add_blobs_to_stream(stream_hash, [blob_info]))
@defer.inlineCallbacks
def store_fake_stream(self, stream_hash, sd_hash, file_name="fake_file", key="DEADBEEF",
blobs=[]):
yield f2d(self.storage.store_stream(stream_hash, sd_hash, file_name, key,
file_name, blobs))
@defer.inlineCallbacks
def make_and_store_fake_stream(self, blob_count=2, stream_hash=None, sd_hash=None):
stream_hash = stream_hash or random_lbry_hash() stream_hash = stream_hash or random_lbry_hash()
sd_hash = sd_hash or random_lbry_hash() blobs = [
blobs = { BlobInfo(i + 1, 100, "DEADBEEF", random_lbry_hash())
i + 1: random_lbry_hash() for i in range(blob_count) for i in range(blob_count)
} ]
await self.store_fake_stream(stream_hash, blobs)
yield self.store_fake_blob(sd_hash)
for blob in blobs.values():
yield self.store_fake_blob(blob)
yield self.store_fake_stream(stream_hash, sd_hash)
for pos, blob in sorted(blobs.items(), key=lambda x: x[0]):
yield self.store_fake_stream_blob(stream_hash, blob, pos)
class TestSetup(StorageTest): class TestSQLiteStorage(StorageTest):
@defer.inlineCallbacks async def test_setup(self):
def test_setup(self): files = await self.storage.get_all_lbry_files()
files = yield f2d(self.storage.get_all_lbry_files())
self.assertEqual(len(files), 0) self.assertEqual(len(files), 0)
blobs = yield f2d(self.storage.get_all_blob_hashes()) blobs = await self.storage.get_all_blob_hashes()
self.assertEqual(len(blobs), 0) self.assertEqual(len(blobs), 0)
async def test_store_blob(self):
class BlobStorageTests(StorageTest):
@defer.inlineCallbacks
def test_store_blob(self):
blob_hash = random_lbry_hash() blob_hash = random_lbry_hash()
yield self.store_fake_blob(blob_hash) await self.store_fake_blob(blob_hash)
blob_hashes = yield f2d(self.storage.get_all_blob_hashes()) blob_hashes = await self.storage.get_all_blob_hashes()
self.assertEqual(blob_hashes, [blob_hash]) self.assertEqual(blob_hashes, [blob_hash])
@defer.inlineCallbacks async def test_delete_blob(self):
def test_delete_blob(self):
blob_hash = random_lbry_hash() blob_hash = random_lbry_hash()
yield self.store_fake_blob(blob_hash) await self.store_fake_blob(blob_hash)
blob_hashes = yield f2d(self.storage.get_all_blob_hashes()) blob_hashes = await self.storage.get_all_blob_hashes()
self.assertEqual(blob_hashes, [blob_hash]) self.assertEqual(blob_hashes, [blob_hash])
yield f2d(self.storage.delete_blobs_from_db(blob_hashes)) await self.storage.delete_blobs_from_db(blob_hashes)
blob_hashes = yield f2d(self.storage.get_all_blob_hashes()) blob_hashes = await self.storage.get_all_blob_hashes()
self.assertEqual(blob_hashes, []) self.assertEqual(blob_hashes, [])
async def test_supports_storage(self):
class SupportsStorageTests(StorageTest):
@defer.inlineCallbacks
def test_supports_storage(self):
claim_ids = [random_lbry_hash() for _ in range(10)] claim_ids = [random_lbry_hash() for _ in range(10)]
random_supports = [{ random_supports = [{
"txid": random_lbry_hash(), "txid": random_lbry_hash(),
@ -172,83 +130,38 @@ class SupportsStorageTests(StorageTest):
} for i in range(20)] } for i in range(20)]
expected_supports = {} expected_supports = {}
for idx, claim_id in enumerate(claim_ids): for idx, claim_id in enumerate(claim_ids):
yield f2d(self.storage.save_supports(claim_id, random_supports[idx*2:idx*2+2])) await self.storage.save_supports(claim_id, random_supports[idx*2:idx*2+2])
for random_support in random_supports[idx*2:idx*2+2]: for random_support in random_supports[idx*2:idx*2+2]:
random_support['claim_id'] = claim_id random_support['claim_id'] = claim_id
expected_supports.setdefault(claim_id, []).append(random_support) expected_supports.setdefault(claim_id, []).append(random_support)
supports = yield f2d(self.storage.get_supports(claim_ids[0]))
supports = await self.storage.get_supports(claim_ids[0])
self.assertEqual(supports, expected_supports[claim_ids[0]]) self.assertEqual(supports, expected_supports[claim_ids[0]])
all_supports = yield f2d(self.storage.get_supports(*claim_ids)) all_supports = await self.storage.get_supports(*claim_ids)
for support in all_supports: for support in all_supports:
self.assertIn(support, expected_supports[support['claim_id']]) self.assertIn(support, expected_supports[support['claim_id']])
class StreamStorageTests(StorageTest): class StreamStorageTests(StorageTest):
@defer.inlineCallbacks async def test_store_and_delete_stream(self):
def test_store_stream(self, stream_hash=None):
stream_hash = stream_hash or random_lbry_hash()
sd_hash = random_lbry_hash()
blob1 = random_lbry_hash()
blob2 = random_lbry_hash()
yield self.store_fake_blob(sd_hash)
yield self.store_fake_blob(blob1)
yield self.store_fake_blob(blob2)
yield self.store_fake_stream(stream_hash, sd_hash)
yield self.store_fake_stream_blob(stream_hash, blob1, 1)
yield self.store_fake_stream_blob(stream_hash, blob2, 2)
stream_blobs = yield f2d(self.storage.get_blobs_for_stream(stream_hash))
stream_blob_hashes = [b.blob_hash for b in stream_blobs]
self.assertListEqual(stream_blob_hashes, [blob1, blob2])
blob_hashes = yield f2d(self.storage.get_all_blob_hashes())
self.assertSetEqual(set(blob_hashes), {sd_hash, blob1, blob2})
stream_blobs = yield f2d(self.storage.get_blobs_for_stream(stream_hash))
stream_blob_hashes = [b.blob_hash for b in stream_blobs]
self.assertListEqual(stream_blob_hashes, [blob1, blob2])
yield f2d(self.storage.set_should_announce(sd_hash, 1, 1))
yield f2d(self.storage.set_should_announce(blob1, 1, 1))
should_announce_count = yield f2d(self.storage.count_should_announce_blobs())
self.assertEqual(should_announce_count, 2)
should_announce_hashes = yield f2d(self.storage.get_blobs_to_announce())
self.assertSetEqual(set(should_announce_hashes), {sd_hash, blob1})
stream_hashes = yield f2d(self.storage.get_all_streams())
self.assertListEqual(stream_hashes, [stream_hash])
@defer.inlineCallbacks
def test_delete_stream(self):
stream_hash = random_lbry_hash() stream_hash = random_lbry_hash()
yield self.test_store_stream(stream_hash) descriptor = await self.store_fake_stream(stream_hash)
yield f2d(self.storage.delete_stream(stream_hash)) files = await self.storage.get_all_lbry_files()
stream_hashes = yield f2d(self.storage.get_all_streams()) self.assertListEqual(files, [])
stream_hashes = await self.storage.get_all_stream_hashes()
self.assertListEqual(stream_hashes, [stream_hash])
await self.storage.delete_stream(descriptor)
files = await self.storage.get_all_lbry_files()
self.assertListEqual(files, [])
stream_hashes = await self.storage.get_all_stream_hashes()
self.assertListEqual(stream_hashes, []) self.assertListEqual(stream_hashes, [])
stream_blobs = yield f2d(self.storage.get_blobs_for_stream(stream_hash))
self.assertListEqual(stream_blobs, [])
blob_hashes = yield f2d(self.storage.get_all_blob_hashes())
self.assertListEqual(blob_hashes, [])
@unittest.SkipTest
class FileStorageTests(StorageTest): class FileStorageTests(StorageTest):
async def test_store_file(self):
@defer.inlineCallbacks
def test_setup_output(self):
file_name = 'encrypted_file_saver_test.tmp'
self.assertFalse(os.path.isfile(file_name))
written_to = yield f2d(open_file_for_writing(self.db_dir, file_name))
self.assertEqual(written_to, file_name)
self.assertTrue(os.path.isfile(os.path.join(self.db_dir, file_name)))
@defer.inlineCallbacks
def test_store_file(self):
download_directory = self.db_dir download_directory = self.db_dir
out = yield f2d(self.storage.get_all_lbry_files()) out = await self.storage.get_all_lbry_files()
self.assertEqual(len(out), 0) self.assertEqual(len(out), 0)
stream_hash = random_lbry_hash() stream_hash = random_lbry_hash()
@ -256,40 +169,29 @@ class FileStorageTests(StorageTest):
blob1 = random_lbry_hash() blob1 = random_lbry_hash()
blob2 = random_lbry_hash() blob2 = random_lbry_hash()
yield self.store_fake_blob(sd_hash) await self.store_fake_blob(sd_hash)
yield self.store_fake_blob(blob1) await self.store_fake_blob(blob1)
yield self.store_fake_blob(blob2) await self.store_fake_blob(blob2)
yield self.store_fake_stream(stream_hash, sd_hash) await self.store_fake_stream(stream_hash, sd_hash)
yield self.store_fake_stream_blob(stream_hash, blob1, 1) await self.store_fake_stream_blob(stream_hash, blob1, 1)
yield self.store_fake_stream_blob(stream_hash, blob2, 2) await self.store_fake_stream_blob(stream_hash, blob2, 2)
blob_data_rate = 0 blob_data_rate = 0
file_name = "test file" file_name = "test file"
out = yield f2d(self.storage.save_published_file( await self.storage.save_published_file(
stream_hash, file_name, download_directory, blob_data_rate stream_hash, file_name, download_directory, blob_data_rate
)) )
rowid = yield f2d(self.storage.get_rowid_for_stream_hash(stream_hash))
self.assertEqual(out, rowid)
files = yield f2d(self.storage.get_all_lbry_files()) files = await self.storage.get_all_lbry_files()
self.assertEqual(1, len(files)) self.assertEqual(1, len(files))
status = yield f2d(self.storage.get_lbry_file_status(rowid))
self.assertEqual(status, ManagedEncryptedFileDownloader.STATUS_STOPPED)
running = ManagedEncryptedFileDownloader.STATUS_RUNNING
yield f2d(self.storage.change_file_status(rowid, running))
status = yield f2d(self.storage.get_lbry_file_status(rowid))
self.assertEqual(status, ManagedEncryptedFileDownloader.STATUS_RUNNING)
@unittest.SkipTest
class ContentClaimStorageTests(StorageTest): class ContentClaimStorageTests(StorageTest):
async def test_store_content_claim(self):
@defer.inlineCallbacks
def test_store_content_claim(self):
download_directory = self.db_dir download_directory = self.db_dir
out = yield f2d(self.storage.get_all_lbry_files()) out = await self.storage.get_all_lbry_files()
self.assertEqual(len(out), 0) self.assertEqual(len(out), 0)
stream_hash = random_lbry_hash() stream_hash = random_lbry_hash()
@ -299,35 +201,35 @@ class ContentClaimStorageTests(StorageTest):
# use the generated sd hash in the fake claim # use the generated sd hash in the fake claim
fake_outpoint = "%s:%i" % (fake_claim_info['txid'], fake_claim_info['nout']) fake_outpoint = "%s:%i" % (fake_claim_info['txid'], fake_claim_info['nout'])
yield self.make_and_store_fake_stream(blob_count=2, stream_hash=stream_hash, sd_hash=sd_hash) await self.make_and_store_fake_stream(blob_count=2, stream_hash=stream_hash, sd_hash=sd_hash)
blob_data_rate = 0 blob_data_rate = 0
file_name = "test file" file_name = "test file"
yield f2d(self.storage.save_published_file( await self.storage.save_published_file(
stream_hash, file_name, download_directory, blob_data_rate stream_hash, file_name, download_directory, blob_data_rate
)) )
yield f2d(self.storage.save_claims([fake_claim_info])) await self.storage.save_claims([fake_claim_info])
yield f2d(self.storage.save_content_claim(stream_hash, fake_outpoint)) await self.storage.save_content_claim(stream_hash, fake_outpoint)
stored_content_claim = yield f2d(self.storage.get_content_claim(stream_hash)) stored_content_claim = await self.storage.get_content_claim(stream_hash)
self.assertDictEqual(stored_content_claim, fake_claim_info) self.assertDictEqual(stored_content_claim, fake_claim_info)
stream_hashes = yield f2d(self.storage.get_old_stream_hashes_for_claim_id(fake_claim_info['claim_id'], stream_hashes = await self.storage.get_old_stream_hashes_for_claim_id(fake_claim_info['claim_id'],
stream_hash)) stream_hash)
self.assertListEqual(stream_hashes, []) self.assertListEqual(stream_hashes, [])
# test that we can't associate a claim update with a new stream to the file # test that we can't associate a claim update with a new stream to the file
second_stream_hash, second_sd_hash = random_lbry_hash(), random_lbry_hash() second_stream_hash, second_sd_hash = random_lbry_hash(), random_lbry_hash()
yield self.make_and_store_fake_stream(blob_count=2, stream_hash=second_stream_hash, sd_hash=second_sd_hash) await self.make_and_store_fake_stream(blob_count=2, stream_hash=second_stream_hash, sd_hash=second_sd_hash)
with self.assertRaisesRegex(Exception, "stream mismatch"): with self.assertRaisesRegex(Exception, "stream mismatch"):
yield f2d(self.storage.save_content_claim(second_stream_hash, fake_outpoint)) await self.storage.save_content_claim(second_stream_hash, fake_outpoint)
# test that we can associate a new claim update containing the same stream to the file # test that we can associate a new claim update containing the same stream to the file
update_info = deepcopy(fake_claim_info) update_info = deepcopy(fake_claim_info)
update_info['txid'] = "beef0000" * 12 update_info['txid'] = "beef0000" * 12
update_info['nout'] = 0 update_info['nout'] = 0
second_outpoint = "%s:%i" % (update_info['txid'], update_info['nout']) second_outpoint = "%s:%i" % (update_info['txid'], update_info['nout'])
yield f2d(self.storage.save_claims([update_info])) await self.storage.save_claims([update_info])
yield f2d(self.storage.save_content_claim(stream_hash, second_outpoint)) await self.storage.save_content_claim(stream_hash, second_outpoint)
update_info_result = yield f2d(self.storage.get_content_claim(stream_hash)) update_info_result = await self.storage.get_content_claim(stream_hash)
self.assertDictEqual(update_info_result, update_info) self.assertDictEqual(update_info_result, update_info)
# test that we can't associate an update with a mismatching claim id # test that we can't associate an update with a mismatching claim id
@ -339,8 +241,8 @@ class ContentClaimStorageTests(StorageTest):
with self.assertRaisesRegex(Exception, "mismatching claim ids when updating stream " with self.assertRaisesRegex(Exception, "mismatching claim ids when updating stream "
"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef " "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef "
"vs beef0002beef0002beef0002beef0002beef0002"): "vs beef0002beef0002beef0002beef0002beef0002"):
yield f2d(self.storage.save_claims([invalid_update_info])) await self.storage.save_claims([invalid_update_info])
yield f2d(self.storage.save_content_claim(stream_hash, invalid_update_outpoint)) await self.storage.save_content_claim(stream_hash, invalid_update_outpoint)
current_claim_info = yield f2d(self.storage.get_content_claim(stream_hash)) current_claim_info = await self.storage.get_content_claim(stream_hash)
# this should still be the previous update # this should still be the previous update
self.assertDictEqual(current_claim_info, update_info) self.assertDictEqual(current_claim_info, update_info)

View file

@ -1,30 +1,24 @@
from unittest import mock import unittest
import mock
import json import json
from twisted.internet import defer from lbrynet.conf import Config
from twisted.trial import unittest
from lbrynet.extras.compat import f2d
from lbrynet.schema.decode import smart_decode from lbrynet.schema.decode import smart_decode
from lbrynet.extras.daemon.storage import SQLiteStorage from lbrynet.extras.daemon.storage import SQLiteStorage
from lbrynet.extras.daemon.ComponentManager import ComponentManager from lbrynet.extras.daemon.ComponentManager import ComponentManager
from lbrynet.extras.daemon.Components import DATABASE_COMPONENT, DHT_COMPONENT, WALLET_COMPONENT from lbrynet.extras.daemon.Components import DATABASE_COMPONENT, DHT_COMPONENT, WALLET_COMPONENT
from lbrynet.extras.daemon.Components import HASH_ANNOUNCER_COMPONENT, REFLECTOR_COMPONENT from lbrynet.extras.daemon.Components import HASH_ANNOUNCER_COMPONENT
from lbrynet.extras.daemon.Components import UPNP_COMPONENT, BLOB_COMPONENT from lbrynet.extras.daemon.Components import UPNP_COMPONENT, BLOB_COMPONENT
from lbrynet.extras.daemon.Components import PEER_PROTOCOL_SERVER_COMPONENT, EXCHANGE_RATE_MANAGER_COMPONENT from lbrynet.extras.daemon.Components import PEER_PROTOCOL_SERVER_COMPONENT, EXCHANGE_RATE_MANAGER_COMPONENT
from lbrynet.extras.daemon.Components import RATE_LIMITER_COMPONENT, HEADERS_COMPONENT, FILE_MANAGER_COMPONENT from lbrynet.extras.daemon.Components import HEADERS_COMPONENT, STREAM_MANAGER_COMPONENT
from lbrynet.extras.daemon.Daemon import Daemon as LBRYDaemon from lbrynet.extras.daemon.Daemon import Daemon as LBRYDaemon
from lbrynet.blob.EncryptedFileDownloader import ManagedEncryptedFileDownloader
from lbrynet.blob.EncryptedFileStatusReport import EncryptedFileStatusReport
from lbrynet.extras.wallet import LbryWalletManager from lbrynet.extras.wallet import LbryWalletManager
from torba.client.wallet import Wallet from torba.client.wallet import Wallet
from lbrynet.conf import Config
from lbrynet.p2p.PaymentRateManager import OnlyFreePaymentsManager
from tests import test_utils from tests import test_utils
from tests.mocks import FakeNetwork, FakeFileManager # from tests.mocks import mock_conf_settings, FakeNetwork, FakeFileManager
from tests.mocks import ExchangeRateManager as DummyExchangeRateManager # from tests.mocks import ExchangeRateManager as DummyExchangeRateManager
from tests.mocks import BTCLBCFeed, USDBTCFeed # from tests.mocks import BTCLBCFeed, USDBTCFeed
from tests.test_utils import is_android from tests.test_utils import is_android
@ -51,7 +45,7 @@ def get_test_daemon(conf: Config, with_fee=False):
daemon.storage = mock.Mock(spec=SQLiteStorage) daemon.storage = mock.Mock(spec=SQLiteStorage)
market_feeds = [BTCLBCFeed(), USDBTCFeed()] market_feeds = [BTCLBCFeed(), USDBTCFeed()]
daemon.exchange_rate_manager = DummyExchangeRateManager(market_feeds, rates) daemon.exchange_rate_manager = DummyExchangeRateManager(market_feeds, rates)
daemon.file_manager = component_manager.get_component(FILE_MANAGER_COMPONENT) daemon.stream_manager = component_manager.get_component(FILE_MANAGER_COMPONENT)
metadata = { metadata = {
"author": "fake author", "author": "fake author",
@ -78,12 +72,11 @@ def get_test_daemon(conf: Config, with_fee=False):
return daemon return daemon
@unittest.SkipTest
class TestCostEst(unittest.TestCase): class TestCostEst(unittest.TestCase):
def setUp(self): def setUp(self):
test_utils.reset_time(self) test_utils.reset_time(self)
@defer.inlineCallbacks
def test_fee_and_generous_data(self): def test_fee_and_generous_data(self):
size = 10000000 size = 10000000
correct_result = 4.5 correct_result = 4.5
@ -91,7 +84,6 @@ class TestCostEst(unittest.TestCase):
result = yield f2d(daemon.get_est_cost("test", size)) result = yield f2d(daemon.get_est_cost("test", size))
self.assertEqual(result, correct_result) self.assertEqual(result, correct_result)
@defer.inlineCallbacks
def test_fee_and_ungenerous_data(self): def test_fee_and_ungenerous_data(self):
conf = Config(is_generous_host=False) conf = Config(is_generous_host=False)
size = 10000000 size = 10000000
@ -101,7 +93,6 @@ class TestCostEst(unittest.TestCase):
result = yield f2d(daemon.get_est_cost("test", size)) result = yield f2d(daemon.get_est_cost("test", size))
self.assertEqual(result, round(correct_result, 1)) self.assertEqual(result, round(correct_result, 1))
@defer.inlineCallbacks
def test_generous_data_and_no_fee(self): def test_generous_data_and_no_fee(self):
size = 10000000 size = 10000000
correct_result = 0.0 correct_result = 0.0
@ -109,7 +100,6 @@ class TestCostEst(unittest.TestCase):
result = yield f2d(daemon.get_est_cost("test", size)) result = yield f2d(daemon.get_est_cost("test", size))
self.assertEqual(result, correct_result) self.assertEqual(result, correct_result)
@defer.inlineCallbacks
def test_ungenerous_data_and_no_fee(self): def test_ungenerous_data_and_no_fee(self):
conf = Config(is_generous_host=False) conf = Config(is_generous_host=False)
size = 10000000 size = 10000000
@ -119,8 +109,8 @@ class TestCostEst(unittest.TestCase):
self.assertEqual(result, round(correct_result, 1)) self.assertEqual(result, round(correct_result, 1))
@unittest.SkipTest
class TestJsonRpc(unittest.TestCase): class TestJsonRpc(unittest.TestCase):
def setUp(self): def setUp(self):
def noop(): def noop():
return None return None
@ -129,7 +119,6 @@ class TestJsonRpc(unittest.TestCase):
self.test_daemon = get_test_daemon(Config()) self.test_daemon = get_test_daemon(Config())
self.test_daemon.wallet_manager.get_best_blockhash = noop self.test_daemon.wallet_manager.get_best_blockhash = noop
@defer.inlineCallbacks
def test_status(self): def test_status(self):
status = yield f2d(self.test_daemon.jsonrpc_status()) status = yield f2d(self.test_daemon.jsonrpc_status())
self.assertDictContainsSubset({'is_running': False}, status) self.assertDictContainsSubset({'is_running': False}, status)
@ -142,8 +131,8 @@ class TestJsonRpc(unittest.TestCase):
test_help.skip = "Test cannot pass on Android because PYTHONOPTIMIZE removes the docstrings." test_help.skip = "Test cannot pass on Android because PYTHONOPTIMIZE removes the docstrings."
@unittest.SkipTest
class TestFileListSorting(unittest.TestCase): class TestFileListSorting(unittest.TestCase):
def setUp(self): def setUp(self):
test_utils.reset_time(self) test_utils.reset_time(self)
self.test_daemon = get_test_daemon(Config()) self.test_daemon = get_test_daemon(Config())
@ -162,43 +151,36 @@ class TestFileListSorting(unittest.TestCase):
] ]
return f2d(self.test_daemon.component_manager.setup()) return f2d(self.test_daemon.component_manager.setup())
@defer.inlineCallbacks
def test_sort_by_points_paid_no_direction_specified(self): def test_sort_by_points_paid_no_direction_specified(self):
sort_options = ['points_paid'] sort_options = ['points_paid']
file_list = yield f2d(self.test_daemon.jsonrpc_file_list(sort=sort_options)) file_list = yield f2d(self.test_daemon.jsonrpc_file_list(sort=sort_options))
self.assertEqual(self.test_points_paid, [f['points_paid'] for f in file_list]) self.assertEqual(self.test_points_paid, [f['points_paid'] for f in file_list])
@defer.inlineCallbacks
def test_sort_by_points_paid_ascending(self): def test_sort_by_points_paid_ascending(self):
sort_options = ['points_paid,asc'] sort_options = ['points_paid,asc']
file_list = yield f2d(self.test_daemon.jsonrpc_file_list(sort=sort_options)) file_list = yield f2d(self.test_daemon.jsonrpc_file_list(sort=sort_options))
self.assertEqual(self.test_points_paid, [f['points_paid'] for f in file_list]) self.assertEqual(self.test_points_paid, [f['points_paid'] for f in file_list])
@defer.inlineCallbacks
def test_sort_by_points_paid_descending(self): def test_sort_by_points_paid_descending(self):
sort_options = ['points_paid, desc'] sort_options = ['points_paid, desc']
file_list = yield f2d(self.test_daemon.jsonrpc_file_list(sort=sort_options)) file_list = yield f2d(self.test_daemon.jsonrpc_file_list(sort=sort_options))
self.assertEqual(list(reversed(self.test_points_paid)), [f['points_paid'] for f in file_list]) self.assertEqual(list(reversed(self.test_points_paid)), [f['points_paid'] for f in file_list])
@defer.inlineCallbacks
def test_sort_by_file_name_no_direction_specified(self): def test_sort_by_file_name_no_direction_specified(self):
sort_options = ['file_name'] sort_options = ['file_name']
file_list = yield f2d(self.test_daemon.jsonrpc_file_list(sort=sort_options)) file_list = yield f2d(self.test_daemon.jsonrpc_file_list(sort=sort_options))
self.assertEqual(self.test_file_names, [f['file_name'] for f in file_list]) self.assertEqual(self.test_file_names, [f['file_name'] for f in file_list])
@defer.inlineCallbacks
def test_sort_by_file_name_ascending(self): def test_sort_by_file_name_ascending(self):
sort_options = ['file_name,\nasc'] sort_options = ['file_name,\nasc']
file_list = yield f2d(self.test_daemon.jsonrpc_file_list(sort=sort_options)) file_list = yield f2d(self.test_daemon.jsonrpc_file_list(sort=sort_options))
self.assertEqual(self.test_file_names, [f['file_name'] for f in file_list]) self.assertEqual(self.test_file_names, [f['file_name'] for f in file_list])
@defer.inlineCallbacks
def test_sort_by_file_name_descending(self): def test_sort_by_file_name_descending(self):
sort_options = ['\tfile_name,\n\tdesc'] sort_options = ['\tfile_name,\n\tdesc']
file_list = yield f2d(self.test_daemon.jsonrpc_file_list(sort=sort_options)) file_list = yield f2d(self.test_daemon.jsonrpc_file_list(sort=sort_options))
self.assertEqual(list(reversed(self.test_file_names)), [f['file_name'] for f in file_list]) self.assertEqual(list(reversed(self.test_file_names)), [f['file_name'] for f in file_list])
@defer.inlineCallbacks
def test_sort_by_multiple_criteria(self): def test_sort_by_multiple_criteria(self):
expected = [ expected = [
'file_name=different.json, points_paid=9.1', 'file_name=different.json, points_paid=9.1',
@ -232,7 +214,6 @@ class TestFileListSorting(unittest.TestCase):
file_list = yield f2d(self.test_daemon.jsonrpc_file_list()) file_list = yield f2d(self.test_daemon.jsonrpc_file_list())
self.assertNotEqual(expected, [format_result(r) for r in file_list]) self.assertNotEqual(expected, [format_result(r) for r in file_list])
@defer.inlineCallbacks
def test_sort_by_nested_field(self): def test_sort_by_nested_field(self):
extract_authors = lambda file_list: [f['metadata']['author'] for f in file_list] extract_authors = lambda file_list: [f['metadata']['author'] for f in file_list]
@ -249,7 +230,6 @@ class TestFileListSorting(unittest.TestCase):
file_list = yield f2d(self.test_daemon.jsonrpc_file_list()) file_list = yield f2d(self.test_daemon.jsonrpc_file_list())
self.assertNotEqual(self.test_authors, extract_authors(file_list)) self.assertNotEqual(self.test_authors, extract_authors(file_list))
@defer.inlineCallbacks
def test_invalid_sort_produces_meaningful_errors(self): def test_invalid_sort_produces_meaningful_errors(self):
sort_options = ['meta.author'] sort_options = ['meta.author']
expected_message = "Failed to get 'meta.author', key 'meta' was not found." expected_message = "Failed to get 'meta.author', key 'meta' was not found."

View file

@ -1,10 +1,38 @@
import unittest import unittest
from lbrynet.schema.fee import Fee from lbrynet.schema.fee import Fee
from lbrynet.extras.daemon import exchange_rate_manager from lbrynet.extras.daemon import exchange_rate_manager
from lbrynet.p2p.Error import InvalidExchangeRateResponse from lbrynet.error import InvalidExchangeRateResponse
from tests import test_utils from tests import test_utils
from tests.mocks import ExchangeRateManager as DummyExchangeRateManager
from tests.mocks import BTCLBCFeed, USDBTCFeed
class BTCLBCFeed(exchange_rate_manager.MarketFeed):
def __init__(self):
super().__init__(
"BTCLBC",
"market name",
"derp.com",
None,
0.0
)
class USDBTCFeed(exchange_rate_manager.MarketFeed):
def __init__(self):
super().__init__(
"USDBTC",
"market name",
"derp.com",
None,
0.0
)
class DummyExchangeRateManager(exchange_rate_manager.ExchangeRateManager):
def __init__(self, market_feeds, rates):
self.market_feeds = market_feeds
for feed in self.market_feeds:
feed.rate = exchange_rate_manager.ExchangeRate(
feed.market, rates[feed.market]['spot'], rates[feed.market]['ts'])
class FeeFormatTest(unittest.TestCase): class FeeFormatTest(unittest.TestCase):
@ -91,11 +119,11 @@ class LBRYioFeedTest(unittest.TestCase):
response = '{}' response = '{}'
with self.assertRaises(InvalidExchangeRateResponse): with self.assertRaises(InvalidExchangeRateResponse):
out = feed._handle_response(response) feed._handle_response(response)
response = '{"success":true,"result":[]}' response = '{"success":true,"result":[]}'
with self.assertRaises(InvalidExchangeRateResponse): with self.assertRaises(InvalidExchangeRateResponse):
out = feed._handle_response(response) feed._handle_response(response)
class TestExchangeRateFeeds(unittest.TestCase): class TestExchangeRateFeeds(unittest.TestCase):
@ -129,11 +157,11 @@ class TestExchangeRateFeeds(unittest.TestCase):
response = '{}' response = '{}'
with self.assertRaises(InvalidExchangeRateResponse): with self.assertRaises(InvalidExchangeRateResponse):
out = feed._handle_response(response) feed._handle_response(response)
response = '{"success":true,"ticker":{}}' response = '{"success":true,"ticker":{}}'
with self.assertRaises(InvalidExchangeRateResponse): with self.assertRaises(InvalidExchangeRateResponse):
out = feed._handle_response(response) feed._handle_response(response)
def test_handle_cryptonator_btc_response(self): def test_handle_cryptonator_btc_response(self):
feed = exchange_rate_manager.CryptonatorBTCFeed() feed = exchange_rate_manager.CryptonatorBTCFeed()
@ -147,11 +175,11 @@ class TestExchangeRateFeeds(unittest.TestCase):
response = '{}' response = '{}'
with self.assertRaises(InvalidExchangeRateResponse): with self.assertRaises(InvalidExchangeRateResponse):
out = feed._handle_response(response) feed._handle_response(response)
response = '{"success":true,"ticker":{}}' response = '{"success":true,"ticker":{}}'
with self.assertRaises(InvalidExchangeRateResponse): with self.assertRaises(InvalidExchangeRateResponse):
out = feed._handle_response(response) feed._handle_response(response)
def test_handle_bittrex_response(self): def test_handle_bittrex_response(self):
feed = exchange_rate_manager.BittrexFeed() feed = exchange_rate_manager.BittrexFeed()
@ -167,8 +195,8 @@ class TestExchangeRateFeeds(unittest.TestCase):
response = '{}' response = '{}'
with self.assertRaises(InvalidExchangeRateResponse): with self.assertRaises(InvalidExchangeRateResponse):
out = feed._handle_response(response) feed._handle_response(response)
response = '{"success":true,"result":[]}' response = '{"success":true,"result":[]}'
with self.assertRaises(InvalidExchangeRateResponse): with self.assertRaises(InvalidExchangeRateResponse):
out = feed._handle_response(response) feed._handle_response(response)

View file

@ -1,4 +1,4 @@
from twisted.trial import unittest import unittest
from lbrynet.extras.daemon.Daemon import sort_claim_results from lbrynet.extras.daemon.Daemon import sort_claim_results
@ -35,9 +35,5 @@ class ClaimsComparatorTest(unittest.TestCase):
self.run_test(results, 'nout', [1, 2, 3]) self.run_test(results, 'nout', [1, 2, 3])
def run_test(self, results, field, expected): def run_test(self, results, field, expected):
actual = sort_claim_results(results) sort_claim_results(results)
self.assertEqual(expected, [r[field] for r in actual]) self.assertListEqual(expected, [r[field] for r in results])
if __name__ == '__main__':
unittest.main()

View file

@ -1,5 +1,5 @@
import docopt import docopt
from twisted.trial import unittest import unittest
from lbrynet.extras.daemon.Daemon import Daemon from lbrynet.extras.daemon.Daemon import Daemon

View file

@ -2,7 +2,6 @@ import contextlib
from io import StringIO from io import StringIO
import unittest import unittest
from docopt import DocoptExit
from lbrynet.extras.cli import normalize_value, main from lbrynet.extras.cli import normalize_value, main
from lbrynet.extras.system_info import get_platform from lbrynet.extras.system_info import get_platform