Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_read_past_end_of_file(self):
filename = os.path.join(self.tempdir, 'foo')
with open(filename, 'wb') as f:
f.write(b'onetwothreefourfivesixseveneightnineten')
chunk = ReadFileChunk.from_filename(
filename, start_byte=36, chunk_size=100000)
self.assertEqual(chunk.read(), b'ten')
self.assertEqual(chunk.read(), b'')
self.assertEqual(len(chunk), 3)
def test_read_with_amount_size(self):
filename = os.path.join(self.tempdir, 'foo')
with open(filename, 'wb') as f:
f.write(b'onetwothreefourfivesixseveneightnineten')
chunk = ReadFileChunk.from_filename(
filename, start_byte=11, chunk_size=4)
self.assertEqual(chunk.read(1), b'f')
self.assertEqual(chunk.read(1), b'o')
self.assertEqual(chunk.read(1), b'u')
self.assertEqual(chunk.read(1), b'r')
self.assertEqual(chunk.read(1), b'')
def test_callback_will_also_be_triggered_by_seek(self):
chunk = ReadFileChunk.from_filename(
self.filename, start_byte=0, chunk_size=3,
callbacks=[self.callback])
chunk.read(2)
chunk.seek(0)
chunk.read(2)
chunk.seek(1)
chunk.read(2)
self.assertEqual(self.amounts_seen, [2, -2, 2, -1, 2])
def test_reset_stream_emulation(self):
filename = os.path.join(self.tempdir, 'foo')
with open(filename, 'wb') as f:
f.write(b'onetwothreefourfivesixseveneightnineten')
chunk = ReadFileChunk.from_filename(
filename, start_byte=11, chunk_size=4)
self.assertEqual(chunk.read(), b'four')
chunk.seek(0)
self.assertEqual(chunk.read(), b'four')
def test_callback_can_be_disabled(self):
chunk = ReadFileChunk.from_filename(
self.filename, start_byte=0, chunk_size=3,
callbacks=[self.callback])
chunk.disable_callback()
# Now reading from the ReadFileChunk should not invoke
# the callback.
chunk.read()
self.assertEqual(self.amounts_seen, [])
def test_tell_and_seek(self):
filename = os.path.join(self.tempdir, 'foo')
with open(filename, 'wb') as f:
f.write(b'onetwothreefourfivesixseveneightnineten')
chunk = ReadFileChunk.from_filename(
filename, start_byte=36, chunk_size=100000)
self.assertEqual(chunk.tell(), 0)
self.assertEqual(chunk.read(), b'ten')
self.assertEqual(chunk.tell(), 3)
chunk.seek(0)
self.assertEqual(chunk.tell(), 0)
def test_iter_is_always_empty(self):
# This tests the workaround for the httplib bug (see
# the source for more info).
filename = os.path.join(self.tempdir, 'foo')
open(filename, 'wb').close()
chunk = ReadFileChunk.from_filename(
filename, start_byte=0, chunk_size=10)
self.assertEqual(list(chunk), [])
def test_signal_transferring(self):
chunk = ReadFileChunk.from_filename(
self.filename, start_byte=0, chunk_size=3,
callbacks=[self.callback])
chunk.signal_not_transferring()
chunk.read(1)
self.assertEqual(self.amounts_seen, [])
chunk.signal_transferring()
chunk.read(1)
self.assertEqual(self.amounts_seen, [1])
def open_file_chunk_reader(self, filename, start_byte, size, callbacks):
return ReadFileChunk.from_filename(filename, start_byte,
size, callbacks,
enable_callbacks=False)