Merge remote-tracking branch 'origin/metadata-version-avec-tests' into metadata-version

This commit is contained in:
Jack 2016-07-27 20:18:00 -04:00
commit 9674381fc2
3 changed files with 125 additions and 12 deletions

View file

@ -53,8 +53,9 @@ class LBRYFeeFormat(dict):
class LBRYFee(LBRYFeeFormat):
def __init__(self, fee_dict, rate_dict):
def __init__(self, fee_dict, rate_dict, bittrex_fee=None):
LBRYFeeFormat.__init__(self, fee_dict)
self.bittrex_fee = BITTREX_FEE if bittrex_fee is None else bittrex_fee
rates = deepcopy(rate_dict)
assert 'BTCLBC' in rates and 'USDBTC' in rates
@ -86,23 +87,15 @@ class LBRYFee(LBRYFeeFormat):
return r
def _usd_to_btc(self, usd):
# log.error("usd to btc: " + str(usd))
# log.error("%f * %f = %f" % (self._USDBTC['spot'], float(usd), self._USDBTC['spot'] * float(usd)))
return self._USDBTC['spot'] * float(usd)
def _btc_to_usd(self, btc):
# log.error("btc to usd: " + str(btc))
# log.error("%f / %f = %f" % (float(btc), self._USDBTC['spot'], float(btc) / self._USDBTC['spot']))
return float(btc) / self._USDBTC['spot']
def _btc_to_lbc(self, btc):
# log.error("btc to lbc: " + str(btc))
# log.error("%f * %f = %f" % (float(btc), self._BTCLBC['spot'], float(btc) * self._BTCLBC['spot'] / (1.0 - BITTREX_FEE)))
return float(btc) * self._BTCLBC['spot'] / (1.0 - BITTREX_FEE)
return float(btc) * self._BTCLBC['spot'] / (1.0 - self.bittrex_fee)
def _lbc_to_btc(self, lbc):
# log.error("lbc to btc: " + str(lbc))
# log.error("%f / %f = %f" % (self._BTCLBC['spot'], float(lbc), self._BTCLBC['spot'] / float(lbc)))
return self._BTCLBC['spot'] / float(lbc)

View file

@ -609,12 +609,30 @@ class LBRYDaemon(jsonrpc.JSONRPC):
d.addCallback(lambda _: self._get_lbry_file("name", name, return_json=False))
d.addCallback(lambda l: _start_file(l) if l.stopped else "LBRY file was already running")
def _process_lbry_file(name, lbry_file):
# lbry_file is an instance of ManagedLBRYFileDownloader or None
ready_to_start = (
lbry_file and
self.pending_claims[name] == lbry_file.txid and
not isinstance(lbry_file['metadata'], str)
)
if ready_to_start:
_start_file(name)
else:
re_add_to_pending_claims(name)
def re_add_to_pending_claims(name):
txid = self.pending_claims.pop(name)
self._add_to_pending_claims(name, txid)
for name in self.pending_claims:
log.info("Checking if new claim for lbry://%s is confirmed" % name)
d = self._resolve_name(name, force_refresh=True)
d.addCallback(lambda _: self._get_lbry_file_by_uri(name))
d.addCallbacks(lambda lbry_file: _start_file(name) if self.pending_claims[name] == lbry_file['txid'] and not isinstance(lbry_file['metadata'], str) else self._add_to_pending_claims(name, self.pending_claims.pop(name)),
self._add_to_pending_claims(name, self.pending_claims.pop(name)))
d.addCallbacks(
lambda lbry_file: _process_lbry_file(name, lbry_file),
lambda _: re_add_to_pending_claims(name)
)
def _start_server(self):
if self.peer_port is not None:

View file

@ -0,0 +1,102 @@
import mock
from lbrynet.core import LBRYMetadata
from twisted.trial import unittest
class LBRYFeeFormatTest(unittest.TestCase):
def test_fee_created_with_correct_inputs(self):
fee_dict = {
'USD': {
'amount': 10,
'address': None
}
}
fee = LBRYMetadata.LBRYFeeFormat(fee_dict)
self.assertEqual(10, fee['USD']['amount'])
class LBRYFeeTest(unittest.TestCase):
def setUp(self):
self.patcher = mock.patch('time.time')
self.time = self.patcher.start()
self.time.return_value = 0
def tearDown(self):
self.time.stop()
def test_fee_converts_to_lbc(self):
fee_dict = {
'USD': {
'amount': 10,
'address': None
}
}
rates = {'BTCLBC': {'spot': 3, 'ts': 2}, 'USDBTC': {'spot': 2, 'ts': 3}}
fee = LBRYMetadata.LBRYFee(fee_dict, rates, 0)
self.assertEqual(60, fee.to_lbc())
class MetadataTest(unittest.TestCase):
def test_assertion_if_source_is_missing(self):
metadata = {}
with self.assertRaises(AssertionError):
LBRYMetadata.Metadata(metadata)
def test_assertion_if_invalid_source(self):
metadata = {
'sources': {'garbage': None}
}
with self.assertRaises(AssertionError):
LBRYMetadata.Metadata(metadata)
def test_assertion_if_missing_v001_field(self):
metadata = {
'sources': [],
}
with self.assertRaises(AssertionError):
LBRYMetadata.Metadata(metadata)
def test_version_is_001_if_all_fields_are_present(self):
metadata = {
'sources': [],
'title': None,
'description': None,
'author': None,
'language': None,
'license': None,
'content-type': None,
}
m = LBRYMetadata.Metadata(metadata)
self.assertEquals('0.0.1', m.metaversion)
def test_assertion_if_there_is_an_extra_field(self):
metadata = {
'sources': [],
'title': None,
'description': None,
'author': None,
'language': None,
'license': None,
'content-type': None,
'extra': None
}
with self.assertRaises(AssertionError):
LBRYMetadata.Metadata(metadata)
def test_version_is_002_if_all_fields_are_present(self):
metadata = {
'sources': [],
'title': None,
'description': None,
'author': None,
'language': None,
'license': None,
'content-type': None,
'nsfw': None,
'ver': None
}
m = LBRYMetadata.Metadata(metadata)
self.assertEquals('0.0.2', m.metaversion)