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: def locations(self) -> LocationList:
return LocationList(self.claim.message.locations) return LocationList(self.claim.message.locations)
def clear_field_by_name(self, field_name: str):
self.message.ClearField(field_name)
class Stream(BaseClaim): class Stream(BaseClaim):
@ -217,12 +214,6 @@ class Stream(BaseClaim):
kwargs.pop('fee_amount', None) 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: if 'sd_hash' in kwargs:
self.source.sd_hash = kwargs.pop('sd_hash') self.source.sd_hash = kwargs.pop('sd_hash')
if 'file_name' in kwargs: if 'file_name' in kwargs:
@ -241,8 +232,8 @@ class Stream(BaseClaim):
if 'file_size' in kwargs: if 'file_size' in kwargs:
self.source.size = kwargs.pop('file_size') self.source.size = kwargs.pop('file_size')
if source_stream_type in ('image', 'video', 'audio') and stream_type != source_stream_type: if self.stream_type is not None and self.stream_type != stream_type:
self.clear_field_by_name(source_stream_type) self.message.ClearField(self.stream_type)
if stream_type in ('image', 'video', 'audio'): if stream_type in ('image', 'video', 'audio'):
media = getattr(self, stream_type) media = getattr(self, stream_type)

View file

@ -1,3 +1,4 @@
import os
import json import json
import shutil import shutil
import tempfile import tempfile
@ -12,6 +13,7 @@ from lbry.conf import Config
from lbry.extras.daemon.Daemon import Daemon, jsonrpc_dumps_pretty from lbry.extras.daemon.Daemon import Daemon, jsonrpc_dumps_pretty
from lbry.wallet import LbryWalletManager from lbry.wallet import LbryWalletManager
from lbry.wallet.account import Account 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 Component, WalletComponent
from lbry.extras.daemon.Components import ( from lbry.extras.daemon.Components import (
DHT_COMPONENT, HASH_ANNOUNCER_COMPONENT, PEER_PROTOCOL_SERVER_COMPONENT, 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) await wallet_node.start(self.conductor.spv_node, seed=seed)
self.extra_wallet_nodes.append(wallet_node) self.extra_wallet_nodes.append(wallet_node)
upload_dir = os.path.join(wallet_node.data_path, 'uploads')
os.mkdir(upload_dir)
conf = Config() conf = Config()
conf.data_dir = wallet_node.data_path conf.data_dir = wallet_node.data_path
conf.wallet_dir = wallet_node.data_path conf.wallet_dir = wallet_node.data_path
conf.download_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.share_usage_data = False
conf.use_upnp = False conf.use_upnp = False
conf.reflect_streams = True conf.reflect_streams = True
@ -197,103 +203,73 @@ class CommandTestCase(IntegrationTestCase):
""" Synchronous version of `out` method. """ """ Synchronous version of `out` method. """
return json.loads(jsonrpc_dumps_pretty(value, ledger=self.ledger))['result'] return json.loads(jsonrpc_dumps_pretty(value, ledger=self.ledger))['result']
def create_tempfile(self, data=None, prefix=None, suffix=None): async def confirm_and_render(self, awaitable, confirm) -> Transaction:
file = tempfile.NamedTemporaryFile(prefix=prefix, suffix=suffix) 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 create_upload_file(self, data, suffix=""):
def cleanup(): file_path = tempfile.mktemp(prefix="tmp", suffix=suffix or "", dir=self.daemon.conf.upload_dir)
try: with open(file_path, 'w+b') as file:
file.close() file.write(data)
except FileNotFoundError: file.flush()
pass return file.name
self.addCleanup(cleanup) async def stream_create(
file.write(data) self, name='hovercraft', bid='1.0', file_path=None,
file.flush() data=b'hi!', confirm=True, suffix=None, **kwargs):
return file.name if file_path is None:
file_path = self.create_upload_file(data=data, suffix=suffix)
async def stream_create(self, name='hovercraft', bid='1.0', data=b'hi!', confirm=True, return await self.confirm_and_render(
prefix=None, suffix=None, **kwargs): self.daemon.jsonrpc_stream_create(name, bid, file_path=file_path, **kwargs), confirm
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)
) )
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): async def stream_update(self, claim_id, data=None, suffix=None, confirm=True, **kwargs):
if data: if data is not None:
file_path = self.create_tempfile(data) file_path = self.create_upload_file(data=data, suffix=suffix)
claim = await self.out( return await self.confirm_and_render(
self.daemon.jsonrpc_stream_update(claim_id, file_path=file_path, **kwargs) self.daemon.jsonrpc_stream_update(claim_id, file_path=file_path, **kwargs), confirm
) )
else: return await self.confirm_and_render(
claim = await self.out(self.daemon.jsonrpc_stream_update(claim_id, **kwargs)) self.daemon.jsonrpc_stream_update(claim_id, **kwargs), confirm
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
async def stream_abandon(self, *args, confirm=True, **kwargs): async def stream_abandon(self, *args, confirm=True, **kwargs):
if 'blocking' not in kwargs: if 'blocking' not in kwargs:
kwargs['blocking'] = False kwargs['blocking'] = False
tx = await self.out(self.daemon.jsonrpc_stream_abandon(*args, **kwargs)) return await self.confirm_and_render(
if confirm: self.daemon.jsonrpc_stream_abandon(*args, **kwargs), confirm
await self.on_transaction_dict(tx) )
await self.generate(1)
await self.on_transaction_dict(tx)
return tx
async def publish(self, name, *args, confirm=True, **kwargs): async def publish(self, name, *args, confirm=True, **kwargs):
claim = await self.out(self.daemon.jsonrpc_publish(name, *args, **kwargs)) return await self.confirm_and_render(
self.assertEqual(claim['outputs'][0]['name'], name) self.daemon.jsonrpc_publish(name, *args, **kwargs), confirm
if confirm: )
await self.on_transaction_dict(claim)
await self.generate(1)
await self.on_transaction_dict(claim)
return claim
async def channel_create(self, name='@arena', bid='1.0', confirm=True, **kwargs): 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)) return await self.confirm_and_render(
self.assertEqual(channel['outputs'][0]['name'], name) self.daemon.jsonrpc_channel_create(name, bid, **kwargs), confirm
if confirm: )
await self.on_transaction_dict(channel)
await self.generate(1)
await self.on_transaction_dict(channel)
return channel
async def channel_update(self, claim_id, confirm=True, **kwargs): async def channel_update(self, claim_id, confirm=True, **kwargs):
channel = await self.out(self.daemon.jsonrpc_channel_update(claim_id, **kwargs)) return await self.confirm_and_render(
self.assertTrue(channel['outputs'][0]['name'].startswith('@')) self.daemon.jsonrpc_channel_update(claim_id, **kwargs), confirm
if confirm: )
await self.on_transaction_dict(channel)
await self.generate(1)
await self.on_transaction_dict(channel)
return channel
async def channel_abandon(self, *args, confirm=True, **kwargs): async def channel_abandon(self, *args, confirm=True, **kwargs):
if 'blocking' not in kwargs: if 'blocking' not in kwargs:
kwargs['blocking'] = False kwargs['blocking'] = False
tx = await self.out(self.daemon.jsonrpc_channel_abandon(*args, **kwargs)) return await self.confirm_and_render(
if confirm: self.daemon.jsonrpc_channel_abandon(*args, **kwargs), confirm
await self.on_transaction_dict(tx) )
await self.generate(1)
await self.on_transaction_dict(tx)
return tx
async def support_create(self, claim_id, bid='1.0', confirm=True, **kwargs): 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)) return await self.confirm_and_render(
if confirm: self.daemon.jsonrpc_support_create(claim_id, bid, **kwargs), confirm
await self.on_transaction_dict(tx) )
await self.generate(1)
await self.on_transaction_dict(tx)
return tx
async def resolve(self, uri): async def resolve(self, uri):
return await self.out(self.daemon.jsonrpc_resolve(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') 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_url = 'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerEscapes.mp4'
video_file_name = os.path.join(files_directory, '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): def setUp(self):
if not os.path.exists(self.video_file_name): 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: open(self.video_file_name, 'wb') as video_file:
video_file.write(response.read()) video_file.write(response.read())
async def _image_stream_operation(self, func, *args, **kwargs): async def image_stream_create(self, name='blank-image', bid='1.0', confirm=True, **kwargs):
with tempfile.NamedTemporaryFile(suffix='.png') as file: return await self.stream_create(name, bid, confirm=confirm, data=self.image_data, suffix='.png', **kwargs)
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 _confirm_tx(self, tx): async def image_stream_update(self, claim_id, confirm=True, **kwargs):
await self.on_transaction_dict(tx) return await self.stream_update(claim_id, confirm=confirm, data=self.image_data, suffix='.png', **kwargs)
await self.generate(1)
await self.on_transaction_dict(tx)
async def image_stream_create(self, name='blank-image', bid='1.0', confirm=True): async def video_stream_create(self, name='chrome', bid='1.0', confirm=True, **kwargs):
tx = await self._image_stream_operation(self.daemon.jsonrpc_stream_create, name, bid) return await self.stream_create(name, bid, confirm=confirm, file_path=self.video_file_name, **kwargs)
if confirm:
await self._confirm_tx(tx)
return tx
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): 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']['stream_type'], 'image')
self.assertEqual(image_txo['value']['source']['media_type'], 'image/png') self.assertEqual(image_txo['value']['source']['media_type'], 'image/png')
self.assertEqual( 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 # Update the stream and assert the file name is not sanitized, but the suggested file name is
prefix, suffix = 'derpyderp?', '.ext.' prefix, suffix = 'derpyderp?', '.ext.'
san_prefix, san_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, data=b'amazing content', prefix=prefix, suffix=suffix)
tx = await self.stream_update(claim_id, file_path=new_file_path)
full_path = (await self.daemon.jsonrpc_get('lbry://' + claim_name, save_file=True)).full_path full_path = (await self.daemon.jsonrpc_get('lbry://' + claim_name, save_file=True)).full_path
updated_stream = self.daemon.jsonrpc_file_list()[0] updated_stream = self.daemon.jsonrpc_file_list()[0]