refactored tests and simplified clearing of metadata on file type change

This commit is contained in:
Lex Berezhny 2019-10-24 23:32:32 -04:00
parent 5fe536a058
commit 63b8a76bf6
4 changed files with 68 additions and 124 deletions

View file

@ -179,9 +179,6 @@ class BaseClaim:
def locations(self) -> LocationList:
return LocationList(self.claim.message.locations)
def clear_field_by_name(self, field_name: str):
self.message.ClearField(field_name)
class Stream(BaseClaim):
@ -217,12 +214,6 @@ class Stream(BaseClaim):
kwargs.pop('fee_amount', None)
)
source_stream_type = None
if self.source.name:
_, source_stream_type = guess_media_type(self.source.name)
elif self.source.media_type:
source_stream_type = guess_stream_type(self.source.media_type)
if 'sd_hash' in kwargs:
self.source.sd_hash = kwargs.pop('sd_hash')
if 'file_name' in kwargs:
@ -241,8 +232,8 @@ class Stream(BaseClaim):
if 'file_size' in kwargs:
self.source.size = kwargs.pop('file_size')
if source_stream_type in ('image', 'video', 'audio') and stream_type != source_stream_type:
self.clear_field_by_name(source_stream_type)
if self.stream_type is not None and self.stream_type != stream_type:
self.message.ClearField(self.stream_type)
if stream_type in ('image', 'video', 'audio'):
media = getattr(self, stream_type)

View file

@ -1,3 +1,4 @@
import os
import json
import shutil
import tempfile
@ -12,6 +13,7 @@ from lbry.conf import Config
from lbry.extras.daemon.Daemon import Daemon, jsonrpc_dumps_pretty
from lbry.wallet import LbryWalletManager
from lbry.wallet.account import Account
from lbry.wallet.transaction import Transaction
from lbry.extras.daemon.Components import Component, WalletComponent
from lbry.extras.daemon.Components import (
DHT_COMPONENT, HASH_ANNOUNCER_COMPONENT, PEER_PROTOCOL_SERVER_COMPONENT,
@ -119,10 +121,14 @@ class CommandTestCase(IntegrationTestCase):
await wallet_node.start(self.conductor.spv_node, seed=seed)
self.extra_wallet_nodes.append(wallet_node)
upload_dir = os.path.join(wallet_node.data_path, 'uploads')
os.mkdir(upload_dir)
conf = Config()
conf.data_dir = wallet_node.data_path
conf.wallet_dir = wallet_node.data_path
conf.download_dir = wallet_node.data_path
conf.upload_dir = upload_dir # not a real conf setting
conf.share_usage_data = False
conf.use_upnp = False
conf.reflect_streams = True
@ -197,103 +203,73 @@ class CommandTestCase(IntegrationTestCase):
""" Synchronous version of `out` method. """
return json.loads(jsonrpc_dumps_pretty(value, ledger=self.ledger))['result']
def create_tempfile(self, data=None, prefix=None, suffix=None):
file = tempfile.NamedTemporaryFile(prefix=prefix, suffix=suffix)
async def confirm_and_render(self, awaitable, confirm) -> Transaction:
tx = await awaitable
if confirm:
await self.ledger.wait(tx)
await self.generate(1)
await self.ledger.wait(tx)
return self.sout(tx)
# tempfile throws FileNotFoundError when file is deleted before it's closed
def cleanup():
try:
file.close()
except FileNotFoundError:
pass
def create_upload_file(self, data, suffix=""):
file_path = tempfile.mktemp(prefix="tmp", suffix=suffix or "", dir=self.daemon.conf.upload_dir)
with open(file_path, 'w+b') as file:
file.write(data)
file.flush()
return file.name
self.addCleanup(cleanup)
file.write(data)
file.flush()
return file.name
async def stream_create(self, name='hovercraft', bid='1.0', data=b'hi!', confirm=True,
prefix=None, suffix=None, **kwargs):
file_path = self.create_tempfile(data=data, prefix=prefix, suffix=suffix)
claim = await self.out(
self.daemon.jsonrpc_stream_create(name, bid, file_path=file_path, **kwargs)
async def stream_create(
self, name='hovercraft', bid='1.0', file_path=None,
data=b'hi!', confirm=True, suffix=None, **kwargs):
if file_path is None:
file_path = self.create_upload_file(data=data, suffix=suffix)
return await self.confirm_and_render(
self.daemon.jsonrpc_stream_create(name, bid, file_path=file_path, **kwargs), confirm
)
self.assertEqual(claim['outputs'][0]['name'], name)
if confirm:
await self.on_transaction_dict(claim)
await self.generate(1)
await self.on_transaction_dict(claim)
return claim
async def stream_update(self, claim_id, data=None, confirm=True, **kwargs):
if data:
file_path = self.create_tempfile(data)
claim = await self.out(
self.daemon.jsonrpc_stream_update(claim_id, file_path=file_path, **kwargs)
async def stream_update(self, claim_id, data=None, suffix=None, confirm=True, **kwargs):
if data is not None:
file_path = self.create_upload_file(data=data, suffix=suffix)
return await self.confirm_and_render(
self.daemon.jsonrpc_stream_update(claim_id, file_path=file_path, **kwargs), confirm
)
else:
claim = await self.out(self.daemon.jsonrpc_stream_update(claim_id, **kwargs))
self.assertIsNotNone(claim['outputs'][0]['name'])
if confirm:
await self.on_transaction_dict(claim)
await self.generate(1)
await self.on_transaction_dict(claim)
return claim
return await self.confirm_and_render(
self.daemon.jsonrpc_stream_update(claim_id, **kwargs), confirm
)
async def stream_abandon(self, *args, confirm=True, **kwargs):
if 'blocking' not in kwargs:
kwargs['blocking'] = False
tx = await self.out(self.daemon.jsonrpc_stream_abandon(*args, **kwargs))
if confirm:
await self.on_transaction_dict(tx)
await self.generate(1)
await self.on_transaction_dict(tx)
return tx
return await self.confirm_and_render(
self.daemon.jsonrpc_stream_abandon(*args, **kwargs), confirm
)
async def publish(self, name, *args, confirm=True, **kwargs):
claim = await self.out(self.daemon.jsonrpc_publish(name, *args, **kwargs))
self.assertEqual(claim['outputs'][0]['name'], name)
if confirm:
await self.on_transaction_dict(claim)
await self.generate(1)
await self.on_transaction_dict(claim)
return claim
return await self.confirm_and_render(
self.daemon.jsonrpc_publish(name, *args, **kwargs), confirm
)
async def channel_create(self, name='@arena', bid='1.0', confirm=True, **kwargs):
channel = await self.out(self.daemon.jsonrpc_channel_create(name, bid, **kwargs))
self.assertEqual(channel['outputs'][0]['name'], name)
if confirm:
await self.on_transaction_dict(channel)
await self.generate(1)
await self.on_transaction_dict(channel)
return channel
return await self.confirm_and_render(
self.daemon.jsonrpc_channel_create(name, bid, **kwargs), confirm
)
async def channel_update(self, claim_id, confirm=True, **kwargs):
channel = await self.out(self.daemon.jsonrpc_channel_update(claim_id, **kwargs))
self.assertTrue(channel['outputs'][0]['name'].startswith('@'))
if confirm:
await self.on_transaction_dict(channel)
await self.generate(1)
await self.on_transaction_dict(channel)
return channel
return await self.confirm_and_render(
self.daemon.jsonrpc_channel_update(claim_id, **kwargs), confirm
)
async def channel_abandon(self, *args, confirm=True, **kwargs):
if 'blocking' not in kwargs:
kwargs['blocking'] = False
tx = await self.out(self.daemon.jsonrpc_channel_abandon(*args, **kwargs))
if confirm:
await self.on_transaction_dict(tx)
await self.generate(1)
await self.on_transaction_dict(tx)
return tx
return await self.confirm_and_render(
self.daemon.jsonrpc_channel_abandon(*args, **kwargs), confirm
)
async def support_create(self, claim_id, bid='1.0', confirm=True, **kwargs):
tx = await self.out(self.daemon.jsonrpc_support_create(claim_id, bid, **kwargs))
if confirm:
await self.on_transaction_dict(tx)
await self.generate(1)
await self.on_transaction_dict(tx)
return tx
return await self.confirm_and_render(
self.daemon.jsonrpc_support_create(claim_id, bid, **kwargs), confirm
)
async def resolve(self, uri):
return await self.out(self.daemon.jsonrpc_resolve(uri))

View file

@ -19,6 +19,12 @@ class ClaimTestCase(CommandTestCase):
files_directory = os.path.join(os.path.dirname(__file__), 'files')
video_file_url = 'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerEscapes.mp4'
video_file_name = os.path.join(files_directory, 'ForBiggerEscapes.mp4')
image_data = unhexlify(
b'89504e470d0a1a0a0000000d49484452000000050000000708020000004fc'
b'510b9000000097048597300000b1300000b1301009a9c1800000015494441'
b'5408d763fcffff3f031260624005d4e603004c45030b5286e9ea000000004'
b'9454e44ae426082'
)
def setUp(self):
if not os.path.exists(self.video_file_name):
@ -29,43 +35,15 @@ class ClaimTestCase(CommandTestCase):
open(self.video_file_name, 'wb') as video_file:
video_file.write(response.read())
async def _image_stream_operation(self, func, *args, **kwargs):
with tempfile.NamedTemporaryFile(suffix='.png') as file:
file.write(unhexlify(
b'89504e470d0a1a0a0000000d49484452000000050000000708020000004fc'
b'510b9000000097048597300000b1300000b1301009a9c1800000015494441'
b'5408d763fcffff3f031260624005d4e603004c45030b5286e9ea000000004'
b'9454e44ae426082'
))
file.flush()
return await self.out(func(*args, file_path=file.name, **kwargs))
async def image_stream_create(self, name='blank-image', bid='1.0', confirm=True, **kwargs):
return await self.stream_create(name, bid, confirm=confirm, data=self.image_data, suffix='.png', **kwargs)
async def _confirm_tx(self, tx):
await self.on_transaction_dict(tx)
await self.generate(1)
await self.on_transaction_dict(tx)
async def image_stream_update(self, claim_id, confirm=True, **kwargs):
return await self.stream_update(claim_id, confirm=confirm, data=self.image_data, suffix='.png', **kwargs)
async def image_stream_create(self, name='blank-image', bid='1.0', confirm=True):
tx = await self._image_stream_operation(self.daemon.jsonrpc_stream_create, name, bid)
if confirm:
await self._confirm_tx(tx)
return tx
async def video_stream_create(self, name='chrome', bid='1.0', confirm=True, **kwargs):
return await self.stream_create(name, bid, confirm=confirm, file_path=self.video_file_name, **kwargs)
async def update_stream_to_image_type(self, claim_id, confirm=True):
tx = await self._image_stream_operation(self.daemon.jsonrpc_stream_update, claim_id)
if confirm:
await self._confirm_tx(tx)
return tx
async def video_stream_create(self, name='chrome', bid='1.0', confirm=True):
tx = await self.out(
self.daemon.jsonrpc_stream_create(
name, bid, file_path=self.video_file_name
)
)
if confirm:
await self._confirm_tx(tx)
return tx
class ClaimSearchCommand(ClaimTestCase):
@ -1007,7 +985,7 @@ class StreamCommands(ClaimTestCase):
}
)
image_txo = (await self.update_stream_to_image_type(claim_id))['outputs'][0]
image_txo = (await self.image_stream_update(claim_id))['outputs'][0]
self.assertEqual(image_txo['value']['stream_type'], 'image')
self.assertEqual(image_txo['value']['source']['media_type'], 'image/png')
self.assertEqual(

View file

@ -103,8 +103,7 @@ class FileCommands(CommandTestCase):
# Update the stream and assert the file name is not sanitized, but the suggested file name is
prefix, suffix = 'derpyderp?', '.ext.'
san_prefix, san_suffix = 'derpyderp', '.ext'
new_file_path = self.create_tempfile(data=b'amazing content', prefix=prefix, suffix=suffix)
tx = await self.stream_update(claim_id, file_path=new_file_path)
tx = await self.stream_update(claim_id, data=b'amazing content', prefix=prefix, suffix=suffix)
full_path = (await self.daemon.jsonrpc_get('lbry://' + claim_name, save_file=True)).full_path
updated_stream = self.daemon.jsonrpc_file_list()[0]