def test_LimitedFileTransferSpeed(self): """tests that speed transfers limitation works""" FILE_SIZE = 1024 ** 2 #1Mb file MAX_RATE = 750 #in kpbs EXPECTED_TIME = FILE_SIZE / float(MAX_RATE * 1024) #create a dummy file with open('dummy', 'wb') as dummy: dummy.seek(FILE_SIZE - 1) dummy.write('\x00') #copy and time it widgets = [dwl.LimitedFileTransferSpeed(max_rate=MAX_RATE)] with open('dummy', 'r') as origin: with open('dummy2', 'wb') as dest: pbar = ProgressBar(widgets=widgets, maxval=FILE_SIZE).start() start = time.time() dwl.copy_callback(origin, dest, callback_update= lambda pos: pbar.update(pos), callback_end= lambda : pbar.finish()) CONSUMED_TIME = time.time() - start #remove temp files os.remove('dummy') os.remove('dummy2') self.assertAlmostEqual (CONSUMED_TIME, CONSUMED_TIME)
def test_callbacks(self): """ tests a the given function is called on every chunk and the on_finish function just once """ FILE_SIZE = 1024 #1kb file CHUNK = 16 expected_on_chunk = FILE_SIZE / CHUNK on_chunk = Mock() on_finish = Mock() with open('dummy', 'wb') as dummy: dummy.seek(FILE_SIZE - 1) dummy.write('\x00') with open('dummy', 'r') as origin: with open('dummy2', 'wb') as dest: dwl.copy_callback(origin, dest, chunk=CHUNK , callback_update=on_chunk, callback_end=on_finish) os.remove('dummy') os.remove('dummy2') self.assertEqual (on_chunk.call_count, expected_on_chunk) self.assertEqual (on_finish.call_count, 1)