Enforces the default paginated output on file_list

This commit is contained in:
Oleg Silkin 2019-10-01 19:50:08 -04:00 committed by Lex Berezhny
parent 766dcff6c6
commit 4637559c6e
5 changed files with 98 additions and 101 deletions

View file

@ -1805,7 +1805,6 @@ class Daemon(metaclass=JSONRPCServerType):
sort, reverse, comparison, **kwargs sort, reverse, comparison, **kwargs
) )
if page is not None or page_size is not None:
page = page or 1 page = page or 1
page_size = page_size or 10 page_size = page_size or 10
total_items = len(file_list) total_items = len(file_list)
@ -1818,8 +1817,6 @@ class Daemon(metaclass=JSONRPCServerType):
'items': file_list[offset:offset+page_size] 'items': file_list[offset:offset+page_size]
} }
return file_list
@requires(STREAM_MANAGER_COMPONENT) @requires(STREAM_MANAGER_COMPONENT)
async def jsonrpc_file_set_status(self, status, **kwargs): async def jsonrpc_file_set_status(self, status, **kwargs):
""" """

View file

@ -672,14 +672,14 @@ class StreamCommands(ClaimTestCase):
tx = await self.out(self.stream_create(title='created')) tx = await self.out(self.stream_create(title='created'))
txo = tx['outputs'][0] txo = tx['outputs'][0]
claim_id, expected = txo['claim_id'], txo['value'] claim_id, expected = txo['claim_id'], txo['value']
files = self.sout(self.daemon.jsonrpc_file_list()) files = self.sout(self.daemon.jsonrpc_file_list()).get('items')
self.assertEqual(1, len(files)) self.assertEqual(1, len(files))
self.assertEqual(tx['txid'], files[0]['txid']) self.assertEqual(tx['txid'], files[0]['txid'])
self.assertEqual(expected, files[0]['metadata']) self.assertEqual(expected, files[0]['metadata'])
# update with metadata-only changes # update with metadata-only changes
tx = await self.out(self.stream_update(claim_id, title='update 1')) tx = await self.out(self.stream_update(claim_id, title='update 1'))
files = self.sout(self.daemon.jsonrpc_file_list()) files = self.sout(self.daemon.jsonrpc_file_list()).get('items')
expected['title'] = 'update 1' expected['title'] = 'update 1'
self.assertEqual(1, len(files)) self.assertEqual(1, len(files))
self.assertEqual(tx['txid'], files[0]['txid']) self.assertEqual(tx['txid'], files[0]['txid'])
@ -688,7 +688,7 @@ class StreamCommands(ClaimTestCase):
# update with new data # update with new data
tx = await self.out(self.stream_update(claim_id, title='update 2', data=b'updated data')) tx = await self.out(self.stream_update(claim_id, title='update 2', data=b'updated data'))
expected = tx['outputs'][0]['value'] expected = tx['outputs'][0]['value']
files = self.sout(self.daemon.jsonrpc_file_list()) files = self.sout(self.daemon.jsonrpc_file_list()).get('items')
self.assertEqual(1, len(files)) self.assertEqual(1, len(files))
self.assertEqual(tx['txid'], files[0]['txid']) self.assertEqual(tx['txid'], files[0]['txid'])
self.assertEqual(expected, files[0]['metadata']) self.assertEqual(expected, files[0]['metadata'])
@ -1055,10 +1055,10 @@ class StreamCommands(ClaimTestCase):
self.assertEqual(txs[0]['value'], '0.0') self.assertEqual(txs[0]['value'], '0.0')
self.assertEqual(txs[0]['fee'], '-0.020107') self.assertEqual(txs[0]['fee'], '-0.020107')
await self.assertBalance(self.account, '7.479893') await self.assertBalance(self.account, '7.479893')
self.assertEqual(1, len(self.daemon.jsonrpc_file_list())) self.assertEqual(1, self.daemon.jsonrpc_file_list().get('total_items'))
await self.daemon.jsonrpc_file_delete(delete_all=True) await self.daemon.jsonrpc_file_delete(delete_all=True)
self.assertEqual(0, len(self.daemon.jsonrpc_file_list())) self.assertEqual(0, self.daemon.jsonrpc_file_list().get('total_items'))
await self.stream_update(claim_id, bid='1.0') # updates previous claim await self.stream_update(claim_id, bid='1.0') # updates previous claim
txs = (await self.out(self.daemon.jsonrpc_transaction_list()))['items'] txs = (await self.out(self.daemon.jsonrpc_transaction_list()))['items']
@ -1100,12 +1100,12 @@ class StreamCommands(ClaimTestCase):
file.flush() file.flush()
tx1 = await self.publish('foo', bid='1.0', file_path=file.name) tx1 = await self.publish('foo', bid='1.0', file_path=file.name)
self.assertEqual(1, len(self.daemon.jsonrpc_file_list())) self.assertEqual(1, self.daemon.jsonrpc_file_list().get('total_items'))
# doesn't error on missing arguments when doing an update stream # doesn't error on missing arguments when doing an update stream
tx2 = await self.publish('foo', tags='updated') tx2 = await self.publish('foo', tags='updated')
self.assertEqual(1, len(self.daemon.jsonrpc_file_list())) self.assertEqual(1, self.daemon.jsonrpc_file_list().get('total_items'))
self.assertEqual(self.get_claim_id(tx1), self.get_claim_id(tx2)) self.assertEqual(self.get_claim_id(tx1), self.get_claim_id(tx2))
# update conflict with two claims of the same name # update conflict with two claims of the same name
@ -1113,14 +1113,14 @@ class StreamCommands(ClaimTestCase):
with self.assertRaisesRegex(Exception, "There are 2 claims for 'foo'"): with self.assertRaisesRegex(Exception, "There are 2 claims for 'foo'"):
await self.daemon.jsonrpc_publish('foo') await self.daemon.jsonrpc_publish('foo')
self.assertEqual(2, len(self.daemon.jsonrpc_file_list())) self.assertEqual(2, self.daemon.jsonrpc_file_list().get('total_items'))
# abandon duplicate stream # abandon duplicate stream
await self.stream_abandon(self.get_claim_id(tx3)) await self.stream_abandon(self.get_claim_id(tx3))
# publish to a channel # publish to a channel
await self.channel_create('@abc') await self.channel_create('@abc')
tx3 = await self.publish('foo', channel_name='@abc') tx3 = await self.publish('foo', channel_name='@abc')
self.assertEqual(2, len(self.daemon.jsonrpc_file_list())) self.assertEqual(2, self.daemon.jsonrpc_file_list().get('total_items'))
r = await self.resolve('lbry://@abc/foo') r = await self.resolve('lbry://@abc/foo')
self.assertEqual( self.assertEqual(
r['lbry://@abc/foo']['claim_id'], r['lbry://@abc/foo']['claim_id'],
@ -1129,7 +1129,7 @@ class StreamCommands(ClaimTestCase):
# publishing again clears channel # publishing again clears channel
tx4 = await self.publish('foo', languages='uk-UA', tags=['Anime', 'anime ']) tx4 = await self.publish('foo', languages='uk-UA', tags=['Anime', 'anime '])
self.assertEqual(2, len(self.daemon.jsonrpc_file_list())) self.assertEqual(2, self.daemon.jsonrpc_file_list().get('total_items'))
r = await self.resolve('lbry://foo') r = await self.resolve('lbry://foo')
claim = r['lbry://foo'] claim = r['lbry://foo']
self.assertEqual(claim['txid'], tx4['outputs'][0]['txid']) self.assertEqual(claim['txid'], tx4['outputs'][0]['txid'])

View file

@ -15,23 +15,23 @@ class FileCommands(CommandTestCase):
await self.stream_create('foo', '0.01') await self.stream_create('foo', '0.01')
await self.stream_create('foo2', '0.01') await self.stream_create('foo2', '0.01')
file1, file2 = self.sout(self.daemon.jsonrpc_file_list('claim_name')) file1, file2 = self.sout(self.daemon.jsonrpc_file_list('claim_name')).get('items')
self.assertEqual(file1['claim_name'], 'foo') self.assertEqual(file1['claim_name'], 'foo')
self.assertEqual(file2['claim_name'], 'foo2') self.assertEqual(file2['claim_name'], 'foo2')
await self.daemon.jsonrpc_file_delete(claim_name='foo') await self.daemon.jsonrpc_file_delete(claim_name='foo')
self.assertEqual(len(self.daemon.jsonrpc_file_list()), 1) self.assertEqual(self.daemon.jsonrpc_file_list().get('total_items'), 1)
await self.daemon.jsonrpc_file_delete(claim_name='foo2') await self.daemon.jsonrpc_file_delete(claim_name='foo2')
self.assertEqual(len(self.daemon.jsonrpc_file_list()), 0) self.assertEqual(self.daemon.jsonrpc_file_list().get('total_items'), 0)
await self.daemon.jsonrpc_get('lbry://foo') await self.daemon.jsonrpc_get('lbry://foo')
self.assertEqual(len(self.daemon.jsonrpc_file_list()), 1) self.assertEqual(self.daemon.jsonrpc_file_list().get('total_items'), 1)
async def test_announces(self): async def test_announces(self):
# announces on publish # announces on publish
self.assertEqual(await self.daemon.storage.get_blobs_to_announce(), []) self.assertEqual(await self.daemon.storage.get_blobs_to_announce(), [])
await self.stream_create('foo', '0.01') await self.stream_create('foo', '0.01')
stream = self.daemon.jsonrpc_file_list()[0] stream = self.daemon.jsonrpc_file_list().get('items')[0]
self.assertSetEqual( self.assertSetEqual(
set(await self.daemon.storage.get_blobs_to_announce()), set(await self.daemon.storage.get_blobs_to_announce()),
{stream.sd_hash, stream.descriptor.blobs[0].blob_hash} {stream.sd_hash, stream.descriptor.blobs[0].blob_hash}
@ -120,14 +120,14 @@ class FileCommands(CommandTestCase):
async def test_file_list_fields(self): async def test_file_list_fields(self):
await self.stream_create('foo', '0.01') await self.stream_create('foo', '0.01')
file_list = self.sout(self.daemon.jsonrpc_file_list()) file_list = self.sout(self.daemon.jsonrpc_file_list()).get('items')
self.assertEqual( self.assertEqual(
file_list[0]['timestamp'], file_list[0]['timestamp'],
None None
) )
self.assertEqual(file_list[0]['confirmations'], -1) self.assertEqual(file_list[0]['confirmations'], -1)
await self.daemon.jsonrpc_resolve('foo') await self.daemon.jsonrpc_resolve('foo')
file_list = self.sout(self.daemon.jsonrpc_file_list()) file_list = self.sout(self.daemon.jsonrpc_file_list()).get('items')
self.assertEqual( self.assertEqual(
file_list[0]['timestamp'], file_list[0]['timestamp'],
self.ledger.headers[file_list[0]['height']]['timestamp'] self.ledger.headers[file_list[0]['height']]['timestamp']
@ -157,12 +157,12 @@ class FileCommands(CommandTestCase):
claim.stream.description = "fix typos, fix the world" claim.stream.description = "fix typos, fix the world"
await self.blockchain_update_name(txid, hexlify(claim.to_bytes()).decode(), '0.01') await self.blockchain_update_name(txid, hexlify(claim.to_bytes()).decode(), '0.01')
await self.daemon.jsonrpc_resolve('lbry://bar') await self.daemon.jsonrpc_resolve('lbry://bar')
file_list = self.daemon.jsonrpc_file_list() file_list = self.daemon.jsonrpc_file_list().get('items')
self.assertEqual(file_list[0].stream_claim_info.claim.stream.description, claim.stream.description) self.assertEqual(file_list[0].stream_claim_info.claim.stream.description, claim.stream.description)
async def test_file_list_paginated_output(self): async def test_file_list_paginated_output(self):
# Make sure that without both page params, file_list returns a normal list # Make sure that without both page params, file_list returns a normal list
file_list = self.daemon.jsonrpc_file_list() file_list = self.daemon.jsonrpc_file_list().get('items')
self.assertIsInstance(file_list, list) self.assertIsInstance(file_list, list)
# Should paginate # Should paginate
@ -195,10 +195,10 @@ class FileCommands(CommandTestCase):
stream4 = await self.stream_create('file4', '0.01', b'witnessing the wild breeze') stream4 = await self.stream_create('file4', '0.01', b'witnessing the wild breeze')
stream5 = await self.stream_create('file5', '0.01', b'come on baby run with me') stream5 = await self.stream_create('file5', '0.01', b'come on baby run with me')
# Without page parameters, this should be a normal list of full length # Without page parameters, this should be a paginated list with default page & length
file_list = self.daemon.jsonrpc_file_list() file_list = self.daemon.jsonrpc_file_list()
self.assertIsInstance(file_list, list) self.assertIsInstance(file_list, dict)
self.assertIs(len(file_list), 5) self.assertIs(file_list.get('total_items'), 5)
# relist a subset of the file list # relist a subset of the file list
file_list_page = self.daemon.jsonrpc_file_list(page=1, page_size=3) file_list_page = self.daemon.jsonrpc_file_list(page=1, page_size=3)
@ -227,12 +227,12 @@ class FileCommands(CommandTestCase):
self.assertEqual('Failed to download sd blob %s within timeout' % sd_hash, resp['error']) self.assertEqual('Failed to download sd blob %s within timeout' % sd_hash, resp['error'])
async def wait_files_to_complete(self): async def wait_files_to_complete(self):
while self.sout(self.daemon.jsonrpc_file_list(status='running')): while self.sout(self.daemon.jsonrpc_file_list(status='running')).get('items'):
await asyncio.sleep(0.01) await asyncio.sleep(0.01)
async def test_filename_conflicts_management_on_resume_download(self): async def test_filename_conflicts_management_on_resume_download(self):
await self.stream_create('foo', '0.01', data=bytes([0] * (1 << 23))) await self.stream_create('foo', '0.01', data=bytes([0] * (1 << 23)))
file_info = self.sout(self.daemon.jsonrpc_file_list())[0] file_info = self.sout(self.daemon.jsonrpc_file_list()).get('items')[0]
original_path = os.path.join(self.daemon.conf.download_dir, file_info['file_name']) original_path = os.path.join(self.daemon.conf.download_dir, file_info['file_name'])
await self.daemon.jsonrpc_file_delete(claim_name='foo') await self.daemon.jsonrpc_file_delete(claim_name='foo')
await self.daemon.jsonrpc_get('lbry://foo') await self.daemon.jsonrpc_get('lbry://foo')
@ -243,7 +243,7 @@ class FileCommands(CommandTestCase):
await asyncio.wait_for(self.wait_files_to_complete(), timeout=5) # if this hangs, file didn't get set completed await asyncio.wait_for(self.wait_files_to_complete(), timeout=5) # if this hangs, file didn't get set completed
# check that internal state got through up to the file list API # check that internal state got through up to the file list API
stream = self.daemon.stream_manager.get_stream_by_stream_hash(file_info['stream_hash']) stream = self.daemon.stream_manager.get_stream_by_stream_hash(file_info['stream_hash'])
file_info = self.sout(self.daemon.jsonrpc_file_list()[0]) file_info = self.sout(self.daemon.jsonrpc_file_list().get('items')[0])
self.assertEqual(stream.file_name, file_info['file_name']) self.assertEqual(stream.file_name, file_info['file_name'])
# checks if what the API shows is what he have at the very internal level. # checks if what the API shows is what he have at the very internal level.
self.assertEqual(stream.full_path, file_info['download_path']) self.assertEqual(stream.full_path, file_info['download_path'])
@ -251,7 +251,7 @@ class FileCommands(CommandTestCase):
async def test_incomplete_downloads_erases_output_file_on_stop(self): async def test_incomplete_downloads_erases_output_file_on_stop(self):
tx = await self.stream_create('foo', '0.01', data=b'deadbeef' * 1000000) tx = await self.stream_create('foo', '0.01', data=b'deadbeef' * 1000000)
sd_hash = tx['outputs'][0]['value']['source']['sd_hash'] sd_hash = tx['outputs'][0]['value']['source']['sd_hash']
file_info = self.sout(self.daemon.jsonrpc_file_list())[0] file_info = self.sout(self.daemon.jsonrpc_file_list()).get('items')[0]
await self.daemon.jsonrpc_file_delete(claim_name='foo') await self.daemon.jsonrpc_file_delete(claim_name='foo')
blobs = await self.server_storage.get_blobs_for_stream( blobs = await self.server_storage.get_blobs_for_stream(
await self.server_storage.get_stream_hash_for_sd_hash(sd_hash) await self.server_storage.get_stream_hash_for_sd_hash(sd_hash)
@ -291,8 +291,8 @@ class FileCommands(CommandTestCase):
# start the download # start the download
resp = await self.out(self.daemon.jsonrpc_get('lbry://foo', timeout=2)) resp = await self.out(self.daemon.jsonrpc_get('lbry://foo', timeout=2))
self.assertNotIn('error', resp) self.assertNotIn('error', resp)
self.assertEqual(len(self.daemon.jsonrpc_file_list()), 1) self.assertEqual(self.daemon.jsonrpc_file_list().get('total_items'), 1)
self.assertEqual('running', self.sout(self.daemon.jsonrpc_file_list())[0]['status']) self.assertEqual('running', self.sout(self.daemon.jsonrpc_file_list()).get('items')[0]['status'])
await self.daemon.jsonrpc_file_set_status('stop', claim_name='foo') await self.daemon.jsonrpc_file_set_status('stop', claim_name='foo')
# recover blobs # recover blobs
@ -304,7 +304,7 @@ class FileCommands(CommandTestCase):
await self.daemon.jsonrpc_file_set_status('start', claim_name='foo') await self.daemon.jsonrpc_file_set_status('start', claim_name='foo')
await asyncio.wait_for(self.wait_files_to_complete(), timeout=5) await asyncio.wait_for(self.wait_files_to_complete(), timeout=5)
file_info = self.sout(self.daemon.jsonrpc_file_list())[0] file_info = self.sout(self.daemon.jsonrpc_file_list()).get('items')[0]
self.assertEqual(file_info['blobs_completed'], file_info['blobs_in_stream']) self.assertEqual(file_info['blobs_completed'], file_info['blobs_in_stream'])
self.assertEqual('finished', file_info['status']) self.assertEqual('finished', file_info['status'])
@ -339,7 +339,7 @@ class FileCommands(CommandTestCase):
await self.daemon.jsonrpc_file_delete(claim_name='expensive') await self.daemon.jsonrpc_file_delete(claim_name='expensive')
response = await self.out(self.daemon.jsonrpc_get('lbry://expensive')) response = await self.out(self.daemon.jsonrpc_get('lbry://expensive'))
self.assertEqual(response['error'], 'fee of 11.00000 exceeds max available balance') self.assertEqual(response['error'], 'fee of 11.00000 exceeds max available balance')
self.assertEqual(len(self.daemon.jsonrpc_file_list()), 0) self.assertEqual(self.daemon.jsonrpc_file_list().get('total_items'), 0)
# FAIL: beyond maximum key fee # FAIL: beyond maximum key fee
await self.stream_create( await self.stream_create(
@ -348,7 +348,7 @@ class FileCommands(CommandTestCase):
) )
await self.daemon.jsonrpc_file_delete(claim_name='maxkey') await self.daemon.jsonrpc_file_delete(claim_name='maxkey')
response = await self.out(self.daemon.jsonrpc_get('lbry://maxkey')) response = await self.out(self.daemon.jsonrpc_get('lbry://maxkey'))
self.assertEqual(len(self.daemon.jsonrpc_file_list()), 0) self.assertEqual(self.daemon.jsonrpc_file_list().get('total_items'), 0)
self.assertEqual(response['error'], 'fee of 111.00000 exceeds max configured to allow of 50.00000') self.assertEqual(response['error'], 'fee of 111.00000 exceeds max configured to allow of 50.00000')
# PASS: purchase is successful # PASS: purchase is successful
@ -362,7 +362,7 @@ class FileCommands(CommandTestCase):
raw_content_fee = response.content_fee.raw raw_content_fee = response.content_fee.raw
await self.ledger.wait(response.content_fee) await self.ledger.wait(response.content_fee)
await self.assertBalance(self.account, '8.925555') await self.assertBalance(self.account, '8.925555')
self.assertEqual(len(self.daemon.jsonrpc_file_list()), 1) self.assertEqual(self.daemon.jsonrpc_file_list().get('total_items'), 1)
await asyncio.wait_for(self.wait_files_to_complete(), timeout=1) await asyncio.wait_for(self.wait_files_to_complete(), timeout=1)
@ -378,8 +378,8 @@ class FileCommands(CommandTestCase):
self.daemon.stream_manager.stop() self.daemon.stream_manager.stop()
await self.daemon.stream_manager.start() await self.daemon.stream_manager.start()
self.assertEqual(len(self.daemon.jsonrpc_file_list()), 1) self.assertEqual(self.daemon.jsonrpc_file_list().get('total_items'), 1)
self.assertEqual(self.daemon.jsonrpc_file_list()[0].content_fee.raw, raw_content_fee) self.assertEqual(self.daemon.jsonrpc_file_list().get('items')[0].content_fee.raw, raw_content_fee)
await self.daemon.jsonrpc_file_delete(claim_name='icanpay') await self.daemon.jsonrpc_file_delete(claim_name='icanpay')
# PASS: no fee address --> use the claim address to pay # PASS: no fee address --> use the claim address to pay
@ -390,12 +390,12 @@ class FileCommands(CommandTestCase):
tx, fee_amount='2.0', fee_currency='LBC', claim_address=target_address tx, fee_amount='2.0', fee_currency='LBC', claim_address=target_address
) )
await self.daemon.jsonrpc_file_delete(claim_name='nofeeaddress') await self.daemon.jsonrpc_file_delete(claim_name='nofeeaddress')
self.assertEqual(len(self.daemon.jsonrpc_file_list()), 0) self.assertEqual(self.daemon.jsonrpc_file_list().get('total_items'), 0)
response = await self.out(self.daemon.jsonrpc_get('lbry://nofeeaddress')) response = await self.out(self.daemon.jsonrpc_get('lbry://nofeeaddress'))
self.assertIsNone(self.daemon.jsonrpc_file_list()[0].stream_claim_info.claim.stream.fee.address) self.assertIsNone(self.daemon.jsonrpc_file_list().get('items')[0].stream_claim_info.claim.stream.fee.address)
self.assertIsNotNone(response['content_fee']) self.assertIsNotNone(response['content_fee'])
self.assertEqual(len(self.daemon.jsonrpc_file_list()), 1) self.assertEqual(self.daemon.jsonrpc_file_list().get('total_items'), 1)
self.assertEqual(response['content_fee']['outputs'][0]['amount'], '2.0') self.assertEqual(response['content_fee']['outputs'][0]['amount'], '2.0')
self.assertEqual(response['content_fee']['outputs'][0]['address'], target_address) self.assertEqual(response['content_fee']['outputs'][0]['address'], target_address)
@ -416,7 +416,7 @@ class FileCommands(CommandTestCase):
# Assert the file downloads # Assert the file downloads
await asyncio.wait_for(self.wait_files_to_complete(), timeout=1) await asyncio.wait_for(self.wait_files_to_complete(), timeout=1)
self.assertEqual(len(self.daemon.jsonrpc_file_list()), 1) self.assertEqual(self.daemon.jsonrpc_file_list().get('total_items'), 1)
# Assert the transaction is recorded to the blockchain # Assert the transaction is recorded to the blockchain
starting_balance = await self.blockchain.get_balance() starting_balance = await self.blockchain.get_balance()
@ -435,7 +435,7 @@ class FileCommands(CommandTestCase):
await self.__raw_value_update_no_fee_amount(tx, target_address) await self.__raw_value_update_no_fee_amount(tx, target_address)
await self.daemon.jsonrpc_file_delete(claim_name='nullfee') await self.daemon.jsonrpc_file_delete(claim_name='nullfee')
response = await self.daemon.jsonrpc_get('lbry://nullfee') response = await self.daemon.jsonrpc_get('lbry://nullfee')
self.assertEqual(len(self.daemon.jsonrpc_file_list()), 1) self.assertEqual(self.daemon.jsonrpc_file_list().get('total_items'), 1)
self.assertIsNone(response.content_fee) self.assertIsNone(response.content_fee)
self.assertTrue(response.stream_claim_info.claim.stream.has_fee) self.assertTrue(response.stream_claim_info.claim.stream.has_fee)
self.assertDictEqual( self.assertDictEqual(

View file

@ -32,7 +32,7 @@ class RangeRequests(CommandTestCase):
await self.stream_create('foo', '0.01', data=self.data, file_size=file_size) await self.stream_create('foo', '0.01', data=self.data, file_size=file_size)
if save_blobs: if save_blobs:
self.assertTrue(len(os.listdir(self.daemon.blob_manager.blob_dir)) > 1) self.assertTrue(len(os.listdir(self.daemon.blob_manager.blob_dir)) > 1)
await self.daemon.jsonrpc_file_list()[0].fully_reflected.wait() await self.daemon.jsonrpc_file_list().get('items')[0].fully_reflected.wait()
await self.daemon.jsonrpc_file_delete(delete_from_download_dir=True, claim_name='foo') await self.daemon.jsonrpc_file_delete(delete_from_download_dir=True, claim_name='foo')
self.assertEqual(0, len(os.listdir(self.daemon.blob_manager.blob_dir))) self.assertEqual(0, len(os.listdir(self.daemon.blob_manager.blob_dir)))
# await self._restart_stream_manager() # await self._restart_stream_manager()
@ -40,7 +40,7 @@ class RangeRequests(CommandTestCase):
site = aiohttp.web.TCPSite(self.daemon.streaming_runner, self.daemon.conf.streaming_host, site = aiohttp.web.TCPSite(self.daemon.streaming_runner, self.daemon.conf.streaming_host,
self.daemon.conf.streaming_port) self.daemon.conf.streaming_port)
await site.start() await site.start()
self.assertListEqual(self.daemon.jsonrpc_file_list(), []) self.assertListEqual(self.daemon.jsonrpc_file_list().get('items'), [])
async def _test_range_requests(self): async def _test_range_requests(self):
name = 'foo' name = 'foo'
@ -127,7 +127,7 @@ class RangeRequests(CommandTestCase):
await self._setup_stream(self.data) await self._setup_stream(self.data)
await self._test_range_requests() await self._test_range_requests()
stream = self.daemon.jsonrpc_file_list()[0] stream = self.daemon.jsonrpc_file_list().get('items')[0]
self.assertTrue(os.path.isfile(self.daemon.blob_manager.get_blob(stream.sd_hash).file_path)) self.assertTrue(os.path.isfile(self.daemon.blob_manager.get_blob(stream.sd_hash).file_path))
self.assertIsNone(stream.download_directory) self.assertIsNone(stream.download_directory)
self.assertIsNone(stream.full_path) self.assertIsNone(stream.full_path)
@ -136,7 +136,7 @@ class RangeRequests(CommandTestCase):
# test that repeated range requests do not create duplicate files # test that repeated range requests do not create duplicate files
for _ in range(3): for _ in range(3):
await self._test_range_requests() await self._test_range_requests()
stream = self.daemon.jsonrpc_file_list()[0] stream = self.daemon.jsonrpc_file_list().get('items')[0]
self.assertTrue(os.path.isfile(self.daemon.blob_manager.get_blob(stream.sd_hash).file_path)) self.assertTrue(os.path.isfile(self.daemon.blob_manager.get_blob(stream.sd_hash).file_path))
self.assertIsNone(stream.download_directory) self.assertIsNone(stream.download_directory)
self.assertIsNone(stream.full_path) self.assertIsNone(stream.full_path)
@ -152,13 +152,13 @@ class RangeRequests(CommandTestCase):
self.assertEqual( self.assertEqual(
len(files_in_download_dir), len(current_files_in_download_dir) len(files_in_download_dir), len(current_files_in_download_dir)
) )
stream = self.daemon.jsonrpc_file_list()[0] stream = self.daemon.jsonrpc_file_list().get('items')[0]
self.assertTrue(os.path.isfile(self.daemon.blob_manager.get_blob(stream.sd_hash).file_path)) self.assertTrue(os.path.isfile(self.daemon.blob_manager.get_blob(stream.sd_hash).file_path))
self.assertIsNone(stream.download_directory) self.assertIsNone(stream.download_directory)
self.assertIsNone(stream.full_path) self.assertIsNone(stream.full_path)
await self._test_range_requests() await self._test_range_requests()
stream = self.daemon.jsonrpc_file_list()[0] stream = self.daemon.jsonrpc_file_list().get('items')[0]
self.assertTrue(os.path.isfile(self.daemon.blob_manager.get_blob(stream.sd_hash).file_path)) self.assertTrue(os.path.isfile(self.daemon.blob_manager.get_blob(stream.sd_hash).file_path))
self.assertIsNone(stream.download_directory) self.assertIsNone(stream.download_directory)
self.assertIsNone(stream.full_path) self.assertIsNone(stream.full_path)
@ -171,7 +171,7 @@ class RangeRequests(CommandTestCase):
self.data = get_random_bytes((MAX_BLOB_SIZE - 1) * 4) self.data = get_random_bytes((MAX_BLOB_SIZE - 1) * 4)
await self._setup_stream(self.data, save_blobs=False) await self._setup_stream(self.data, save_blobs=False)
await self._test_range_requests() await self._test_range_requests()
stream = self.daemon.jsonrpc_file_list()[0] stream = self.daemon.jsonrpc_file_list().get('items')[0]
self.assertIsNone(stream.download_directory) self.assertIsNone(stream.download_directory)
self.assertIsNone(stream.full_path) self.assertIsNone(stream.full_path)
files_in_download_dir = list(os.scandir(os.path.dirname(self.daemon.conf.data_dir))) files_in_download_dir = list(os.scandir(os.path.dirname(self.daemon.conf.data_dir)))
@ -179,7 +179,7 @@ class RangeRequests(CommandTestCase):
# test that repeated range requests do not create duplicate files # test that repeated range requests do not create duplicate files
for _ in range(3): for _ in range(3):
await self._test_range_requests() await self._test_range_requests()
stream = self.daemon.jsonrpc_file_list()[0] stream = self.daemon.jsonrpc_file_list().get('items')[0]
self.assertIsNone(stream.download_directory) self.assertIsNone(stream.download_directory)
self.assertIsNone(stream.full_path) self.assertIsNone(stream.full_path)
current_files_in_download_dir = list(os.scandir(os.path.dirname(self.daemon.conf.data_dir))) current_files_in_download_dir = list(os.scandir(os.path.dirname(self.daemon.conf.data_dir)))
@ -194,12 +194,12 @@ class RangeRequests(CommandTestCase):
self.assertEqual( self.assertEqual(
len(files_in_download_dir), len(current_files_in_download_dir) len(files_in_download_dir), len(current_files_in_download_dir)
) )
stream = self.daemon.jsonrpc_file_list()[0] stream = self.daemon.jsonrpc_file_list().get('items')[0]
self.assertIsNone(stream.download_directory) self.assertIsNone(stream.download_directory)
self.assertIsNone(stream.full_path) self.assertIsNone(stream.full_path)
await self._test_range_requests() await self._test_range_requests()
stream = self.daemon.jsonrpc_file_list()[0] stream = self.daemon.jsonrpc_file_list().get('items')[0]
self.assertIsNone(stream.download_directory) self.assertIsNone(stream.download_directory)
self.assertIsNone(stream.full_path) self.assertIsNone(stream.full_path)
current_files_in_download_dir = list(os.scandir(os.path.dirname(self.daemon.conf.data_dir))) current_files_in_download_dir = list(os.scandir(os.path.dirname(self.daemon.conf.data_dir)))
@ -212,7 +212,7 @@ class RangeRequests(CommandTestCase):
await self._setup_stream(self.data, save_files=True) await self._setup_stream(self.data, save_files=True)
await self._test_range_requests() await self._test_range_requests()
streams = self.daemon.jsonrpc_file_list() streams = self.daemon.jsonrpc_file_list().get('items')
self.assertEqual(1, len(streams)) self.assertEqual(1, len(streams))
stream = streams[0] stream = streams[0]
self.assertTrue(os.path.isfile(self.daemon.blob_manager.get_blob(stream.sd_hash).file_path)) self.assertTrue(os.path.isfile(self.daemon.blob_manager.get_blob(stream.sd_hash).file_path))
@ -223,7 +223,7 @@ class RangeRequests(CommandTestCase):
for _ in range(3): for _ in range(3):
await self._test_range_requests() await self._test_range_requests()
streams = self.daemon.jsonrpc_file_list() streams = self.daemon.jsonrpc_file_list().get('items')
self.assertEqual(1, len(streams)) self.assertEqual(1, len(streams))
stream = streams[0] stream = streams[0]
self.assertTrue(os.path.isfile(self.daemon.blob_manager.get_blob(stream.sd_hash).file_path)) self.assertTrue(os.path.isfile(self.daemon.blob_manager.get_blob(stream.sd_hash).file_path))
@ -240,7 +240,7 @@ class RangeRequests(CommandTestCase):
self.assertEqual( self.assertEqual(
len(files_in_download_dir), len(current_files_in_download_dir) len(files_in_download_dir), len(current_files_in_download_dir)
) )
streams = self.daemon.jsonrpc_file_list() streams = self.daemon.jsonrpc_file_list().get('items')
self.assertEqual(1, len(streams)) self.assertEqual(1, len(streams))
stream = streams[0] stream = streams[0]
self.assertTrue(os.path.isfile(self.daemon.blob_manager.get_blob(stream.sd_hash).file_path)) self.assertTrue(os.path.isfile(self.daemon.blob_manager.get_blob(stream.sd_hash).file_path))
@ -248,7 +248,7 @@ class RangeRequests(CommandTestCase):
self.assertTrue(os.path.isfile(stream.full_path)) self.assertTrue(os.path.isfile(stream.full_path))
await self._test_range_requests() await self._test_range_requests()
streams = self.daemon.jsonrpc_file_list() streams = self.daemon.jsonrpc_file_list().get('items')
self.assertEqual(1, len(streams)) self.assertEqual(1, len(streams))
stream = streams[0] stream = streams[0]
self.assertTrue(os.path.isfile(self.daemon.blob_manager.get_blob(stream.sd_hash).file_path)) self.assertTrue(os.path.isfile(self.daemon.blob_manager.get_blob(stream.sd_hash).file_path))
@ -267,7 +267,7 @@ class RangeRequests(CommandTestCase):
self.daemon.conf.save_blobs = False self.daemon.conf.save_blobs = False
await self._test_range_requests() await self._test_range_requests()
stream = self.daemon.jsonrpc_file_list()[0] stream = self.daemon.jsonrpc_file_list().get('items')[0]
self.assertTrue(os.path.isdir(stream.download_directory)) self.assertTrue(os.path.isdir(stream.download_directory))
self.assertTrue(os.path.isfile(stream.full_path)) self.assertTrue(os.path.isfile(stream.full_path))
full_path = stream.full_path full_path = stream.full_path
@ -275,7 +275,7 @@ class RangeRequests(CommandTestCase):
for _ in range(3): for _ in range(3):
await self._test_range_requests() await self._test_range_requests()
stream = self.daemon.jsonrpc_file_list()[0] stream = self.daemon.jsonrpc_file_list().get('items')[0]
self.assertTrue(os.path.isdir(stream.download_directory)) self.assertTrue(os.path.isdir(stream.download_directory))
self.assertTrue(os.path.isfile(stream.full_path)) self.assertTrue(os.path.isfile(stream.full_path))
current_files_in_download_dir = list(os.scandir(os.path.dirname(full_path))) current_files_in_download_dir = list(os.scandir(os.path.dirname(full_path)))
@ -288,14 +288,14 @@ class RangeRequests(CommandTestCase):
self.assertEqual( self.assertEqual(
len(files_in_download_dir), len(current_files_in_download_dir) len(files_in_download_dir), len(current_files_in_download_dir)
) )
streams = self.daemon.jsonrpc_file_list() streams = self.daemon.jsonrpc_file_list().get('items')
self.assertEqual(1, len(streams)) self.assertEqual(1, len(streams))
stream = streams[0] stream = streams[0]
self.assertTrue(os.path.isdir(stream.download_directory)) self.assertTrue(os.path.isdir(stream.download_directory))
self.assertTrue(os.path.isfile(stream.full_path)) self.assertTrue(os.path.isfile(stream.full_path))
await self._test_range_requests() await self._test_range_requests()
streams = self.daemon.jsonrpc_file_list() streams = self.daemon.jsonrpc_file_list().get('items')
self.assertEqual(1, len(streams)) self.assertEqual(1, len(streams))
stream = streams[0] stream = streams[0]
self.assertTrue(os.path.isdir(stream.download_directory)) self.assertTrue(os.path.isdir(stream.download_directory))
@ -311,31 +311,31 @@ class RangeRequests(CommandTestCase):
async def test_switch_save_blobs_while_running(self): async def test_switch_save_blobs_while_running(self):
await self.test_streaming_only_without_blobs() await self.test_streaming_only_without_blobs()
self.daemon.conf.save_blobs = True self.daemon.conf.save_blobs = True
blobs_in_stream = self.daemon.jsonrpc_file_list()[0].blobs_in_stream blobs_in_stream = self.daemon.jsonrpc_file_list().get('items')[0].blobs_in_stream
sd_hash = self.daemon.jsonrpc_file_list()[0].sd_hash sd_hash = self.daemon.jsonrpc_file_list().get('items')[0].sd_hash
start_file_count = len(os.listdir(self.daemon.blob_manager.blob_dir)) start_file_count = len(os.listdir(self.daemon.blob_manager.blob_dir))
await self._test_range_requests() await self._test_range_requests()
self.assertEqual(start_file_count + blobs_in_stream, len(os.listdir(self.daemon.blob_manager.blob_dir))) self.assertEqual(start_file_count + blobs_in_stream, len(os.listdir(self.daemon.blob_manager.blob_dir)))
self.assertEqual(0, self.daemon.jsonrpc_file_list()[0].blobs_remaining) self.assertEqual(0, self.daemon.jsonrpc_file_list().get('items')[0].blobs_remaining)
# switch back # switch back
self.daemon.conf.save_blobs = False self.daemon.conf.save_blobs = False
await self._test_range_requests() await self._test_range_requests()
self.assertEqual(start_file_count + blobs_in_stream, len(os.listdir(self.daemon.blob_manager.blob_dir))) self.assertEqual(start_file_count + blobs_in_stream, len(os.listdir(self.daemon.blob_manager.blob_dir)))
self.assertEqual(0, self.daemon.jsonrpc_file_list()[0].blobs_remaining) self.assertEqual(0, self.daemon.jsonrpc_file_list().get('items')[0].blobs_remaining)
await self.daemon.jsonrpc_file_delete(delete_from_download_dir=True, sd_hash=sd_hash) await self.daemon.jsonrpc_file_delete(delete_from_download_dir=True, sd_hash=sd_hash)
self.assertEqual(start_file_count, len(os.listdir(self.daemon.blob_manager.blob_dir))) self.assertEqual(start_file_count, len(os.listdir(self.daemon.blob_manager.blob_dir)))
await self._test_range_requests() await self._test_range_requests()
self.assertEqual(start_file_count, len(os.listdir(self.daemon.blob_manager.blob_dir))) self.assertEqual(start_file_count, len(os.listdir(self.daemon.blob_manager.blob_dir)))
self.assertEqual(blobs_in_stream, self.daemon.jsonrpc_file_list()[0].blobs_remaining) self.assertEqual(blobs_in_stream, self.daemon.jsonrpc_file_list().get('items')[0].blobs_remaining)
async def test_file_save_streaming_only_save_blobs(self): async def test_file_save_streaming_only_save_blobs(self):
await self.test_streaming_only_with_blobs() await self.test_streaming_only_with_blobs()
stream = self.daemon.jsonrpc_file_list()[0] stream = self.daemon.jsonrpc_file_list().get('items')[0]
self.assertIsNone(stream.full_path) self.assertIsNone(stream.full_path)
self.server.stop_server() self.server.stop_server()
await self.daemon.jsonrpc_file_save('test', self.daemon.conf.data_dir) await self.daemon.jsonrpc_file_save('test', self.daemon.conf.data_dir)
stream = self.daemon.jsonrpc_file_list()[0] stream = self.daemon.jsonrpc_file_list().get('items')[0]
self.assertIsNotNone(stream.full_path) self.assertIsNotNone(stream.full_path)
await stream.finished_writing.wait() await stream.finished_writing.wait()
with open(stream.full_path, 'rb') as f: with open(stream.full_path, 'rb') as f:
@ -344,18 +344,18 @@ class RangeRequests(CommandTestCase):
async def test_file_save_stop_before_finished_streaming_only(self, wait_for_start_writing=False): async def test_file_save_stop_before_finished_streaming_only(self, wait_for_start_writing=False):
await self.test_streaming_only_with_blobs() await self.test_streaming_only_with_blobs()
stream = self.daemon.jsonrpc_file_list()[0] stream = self.daemon.jsonrpc_file_list().get('items')[0]
self.assertIsNone(stream.full_path) self.assertIsNone(stream.full_path)
self.server.stop_server() self.server.stop_server()
await self.daemon.jsonrpc_file_save('test', self.daemon.conf.data_dir) await self.daemon.jsonrpc_file_save('test', self.daemon.conf.data_dir)
stream = self.daemon.jsonrpc_file_list()[0] stream = self.daemon.jsonrpc_file_list().get('items')[0]
path = stream.full_path path = stream.full_path
self.assertIsNotNone(path) self.assertIsNotNone(path)
if wait_for_start_writing: if wait_for_start_writing:
await stream.started_writing.wait() await stream.started_writing.wait()
self.assertTrue(os.path.isfile(path)) self.assertTrue(os.path.isfile(path))
await self._restart_stream_manager() await self._restart_stream_manager()
stream = self.daemon.jsonrpc_file_list()[0] stream = self.daemon.jsonrpc_file_list().get('items')[0]
self.assertIsNotNone(stream.full_path) self.assertIsNotNone(stream.full_path)
self.assertFalse(os.path.isfile(path)) self.assertFalse(os.path.isfile(path))
if wait_for_start_writing: if wait_for_start_writing:
@ -367,10 +367,10 @@ class RangeRequests(CommandTestCase):
async def test_file_save_streaming_only_dont_save_blobs(self): async def test_file_save_streaming_only_dont_save_blobs(self):
await self.test_streaming_only_without_blobs() await self.test_streaming_only_without_blobs()
stream = self.daemon.jsonrpc_file_list()[0] stream = self.daemon.jsonrpc_file_list().get('items')[0]
self.assertIsNone(stream.full_path) self.assertIsNone(stream.full_path)
await self.daemon.jsonrpc_file_save('test', self.daemon.conf.data_dir) await self.daemon.jsonrpc_file_save('test', self.daemon.conf.data_dir)
stream = self.daemon.jsonrpc_file_list()[0] stream = self.daemon.jsonrpc_file_list().get('items')[0]
await stream.finished_writing.wait() await stream.finished_writing.wait()
with open(stream.full_path, 'rb') as f: with open(stream.full_path, 'rb') as f:
self.assertEqual(self.data, f.read()) self.assertEqual(self.data, f.read())
@ -398,7 +398,7 @@ class RangeRequestsLRUCache(CommandTestCase):
self.daemon.conf.save_blobs = False self.daemon.conf.save_blobs = False
self.daemon.conf.save_files = False self.daemon.conf.save_files = False
await self.stream_create('foo', '0.01', data=self.data, file_size=0) await self.stream_create('foo', '0.01', data=self.data, file_size=0)
await self.daemon.jsonrpc_file_list()[0].fully_reflected.wait() await self.daemon.jsonrpc_file_list().get('items')[0].fully_reflected.wait()
await self.daemon.jsonrpc_file_delete(delete_from_download_dir=True, claim_name='foo') await self.daemon.jsonrpc_file_delete(delete_from_download_dir=True, claim_name='foo')
self.assertEqual(0, len(os.listdir(self.daemon.blob_manager.blob_dir))) self.assertEqual(0, len(os.listdir(self.daemon.blob_manager.blob_dir)))
@ -406,10 +406,10 @@ class RangeRequestsLRUCache(CommandTestCase):
site = aiohttp.web.TCPSite(self.daemon.streaming_runner, self.daemon.conf.streaming_host, site = aiohttp.web.TCPSite(self.daemon.streaming_runner, self.daemon.conf.streaming_host,
self.daemon.conf.streaming_port) self.daemon.conf.streaming_port)
await site.start() await site.start()
self.assertListEqual(self.daemon.jsonrpc_file_list(), []) self.assertListEqual(self.daemon.jsonrpc_file_list().get('items'), [])
await self._request_stream() await self._request_stream()
self.assertEqual(1, len(self.daemon.jsonrpc_file_list())) self.assertEqual(1, self.daemon.jsonrpc_file_list().get('total_items'))
self.server.stop_server() self.server.stop_server()
# running with cache size 0 gets through without errors without # running with cache size 0 gets through without errors without

View file

@ -135,32 +135,32 @@ class TestFileListSorting(unittest.TestCase):
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).get('items'))
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])
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).get('items'))
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])
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).get('items'))
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])
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).get('items'))
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])
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).get('items'))
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])
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).get('items'))
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])
def test_sort_by_multiple_criteria(self): def test_sort_by_multiple_criteria(self):
@ -179,48 +179,48 @@ class TestFileListSorting(unittest.TestCase):
format_result = lambda f: f"file_name={f['file_name']}, points_paid={f['points_paid']}" format_result = lambda f: f"file_name={f['file_name']}, points_paid={f['points_paid']}"
sort_options = ['file_name,asc', 'points_paid,desc'] sort_options = ['file_name,asc', '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).get('items'))
self.assertEqual(expected, [format_result(r) for r in file_list]) self.assertEqual(expected, [format_result(r) for r in file_list])
# Check that the list is not sorted as expected when sorted only by file_name. # Check that the list is not sorted as expected when sorted only by file_name.
sort_options = ['file_name,asc'] sort_options = ['file_name,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).get('items'))
self.assertNotEqual(expected, [format_result(r) for r in file_list]) self.assertNotEqual(expected, [format_result(r) for r in file_list])
# Check that the list is not sorted as expected when sorted only by points_paid. # Check that the list is not sorted as expected when sorted only by points_paid.
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).get('items'))
self.assertNotEqual(expected, [format_result(r) for r in file_list]) self.assertNotEqual(expected, [format_result(r) for r in file_list])
# Check that the list is not sorted as expected when not sorted at all. # Check that the list is not sorted as expected when not sorted at all.
file_list = yield f2d(self.test_daemon.jsonrpc_file_list()) file_list = yield f2d(self.test_daemon.jsonrpc_file_list().get('items'))
self.assertNotEqual(expected, [format_result(r) for r in file_list]) self.assertNotEqual(expected, [format_result(r) for r in file_list])
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]
sort_options = ['metadata.author'] sort_options = ['metadata.author']
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).get('items'))
self.assertEqual(self.test_authors, extract_authors(file_list)) self.assertEqual(self.test_authors, extract_authors(file_list))
# Check that the list matches the expected in reverse when sorting in descending order. # Check that the list matches the expected in reverse when sorting in descending order.
sort_options = ['metadata.author,desc'] sort_options = ['metadata.author,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).get('items'))
self.assertEqual(list(reversed(self.test_authors)), extract_authors(file_list)) self.assertEqual(list(reversed(self.test_authors)), extract_authors(file_list))
# Check that the list is not sorted as expected when not sorted at all. # Check that the list is not sorted as expected when not sorted at all.
file_list = yield f2d(self.test_daemon.jsonrpc_file_list()) file_list = yield f2d(self.test_daemon.jsonrpc_file_list().get('items'))
self.assertNotEqual(self.test_authors, extract_authors(file_list)) self.assertNotEqual(self.test_authors, extract_authors(file_list))
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."
with self.assertRaisesRegex(Exception, expected_message): with self.assertRaisesRegex(Exception, expected_message):
yield f2d(self.test_daemon.jsonrpc_file_list(sort=sort_options)) yield f2d(self.test_daemon.jsonrpc_file_list(sort=sort_options).get('items'))
sort_options = ['metadata.foo.bar'] sort_options = ['metadata.foo.bar']
expected_message = "Failed to get 'metadata.foo.bar', key 'foo' was not found." expected_message = "Failed to get 'metadata.foo.bar', key 'foo' was not found."
with self.assertRaisesRegex(Exception, expected_message): with self.assertRaisesRegex(Exception, expected_message):
yield f2d(self.test_daemon.jsonrpc_file_list(sort=sort_options)) yield f2d(self.test_daemon.jsonrpc_file_list(sort=sort_options).get('items'))
@staticmethod @staticmethod
def _get_fake_lbry_files(): def _get_fake_lbry_files():